LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

conversion to integer

Hello,

 

I attached a Vi whose behavior is strange. I would like to have a more predictable conversion from double to integer.

 

Thank for your help

 

 

0 Kudos
Message 1 of 18
(5,525 Views)

From the help for the typecast:

This function rounds all floating-point and fixed-point numeric values to the nearest integer. If the fractional part of the value is .5, the function rounds the value to the nearest even integer. For example, the function rounds 13.5 to 14 and rounds 14.5 to 14.

hope this helps,

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 2 of 18
(5,523 Views)

I cqnnot guess whqt is te nearest integer, for me it looks like to an erractic behavior.

How can it fix it ?

In my example, it should not be an ambiguity.

 

Regards 

0 Kudos
Message 3 of 18
(5,501 Views)

Labview rounds to the nearest integer in the following manner.  (From the help file)

 

"Rounds the input to the nearest integer. If the value of the input is midway between two integers (for example, 1.5 or 2.5), the function returns the nearest even integer (2). The connector pane displays the default data types for this polymorphic function."

 

However this comes from a IEEE spec for microprocessors (Cant remember exactly off the top of my head) and i am still unsure why a high level programming language would choose to do it this way instead of following conventional math.

 

Hey ho, thats the way the cookie bounces!

 

Craig

LabVIEW 2012
0 Kudos
Message 4 of 18
(5,497 Views)
You can do this like it's shown in the example I posted here, but note that this will not necessarily produce the results you want always (e.g. negative numbers).

___________________
Try to take over the world!
Message 5 of 18
(5,496 Views)
Craih, this type of rounding (known as banker's rounding) is useful when processing large amounts of data, as it tends to cancel rounding errors. I'm sure you can find the math behind it with a simple search.

___________________
Try to take over the world!
0 Kudos
Message 6 of 18
(5,494 Views)

pdupre wrote:

I cqnnot guess whqt is te nearest integer, for me it looks like to an erractic behavior.

How can it fix it ?

In my example, it should not be an ambiguity.

 

Regards 


First of all, it only occurs if you have exactly x.5 in as number. It does not occur if you have x.51 or x.49.

That being said, it is very unlikely that you will have any unexpected issues with this rounding.

 

But if you have a problem with it: It was always like this in LabVIEW and therefore is not going to chance because this could mess up many existing applications.....

If you want to have a "fix"for this, you currently have to implement it on your own.... i am sorry to say that.

 

hope this helps,

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 7 of 18
(5,493 Views)

Hey guys,

 

Just to back up the exsisting posts, here is a KB which describes the rounding standards in LabVIEW. As previously mentioned, if dealing with large amounts of data, this rounding standard will statistically remove rounding errors. Clever stuff!

 

http://digital.ni.com/public.nsf/allkb/7ED5A95B08D7DF37862565A800819D2D?OpenDocument

 

However, if you want to consistantly roundup midpoint decimal numberics (i.e. 0.5 -> 1 / 5.5 ->6 / 6.5 -> 7), then perhaps the simple attached code may help you.

Best wishes to you all,

Rich Roberts
Senior Marketing Engineer, National Instruments
Connect on LinkedIn: https://www.linkedin.com/in/richard-roberts-4176a27b/
Message 8 of 18
(5,459 Views)

Added "Round towards +Infinity"

 

Message Edited by tartan5 on 10-08-2008 07:17 AM
Message Edited by tartan5 on 10-08-2008 07:19 AM
Message 9 of 18
(5,448 Views)

Sorry Tartan5, I dont think that plan will work. Your code will round up all values... for example, the value 4.4 will round up to 5. While only numbers "equal to or over 4.5" need to be rounded up. All else should round down.

 

That is my understanding, anyway Robot Happy

Rich Roberts
Senior Marketing Engineer, National Instruments
Connect on LinkedIn: https://www.linkedin.com/in/richard-roberts-4176a27b/
0 Kudos
Message 10 of 18
(5,434 Views)