From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

odd Behavior of I32 data type

I have done a simple division where all the data types are set to I32. the divider is 2. I have observed that for numerator 1 result is 0, 3 results in 2, for 5 again 2, for 7 as well as 9 4 only i.e. no change. Why is it like that? For an odd number it is assigning the upper one but for the next odd one it goes for lower one? Is it random or there is some logic behind it?

 

0 Kudos
Message 1 of 4
(2,870 Views)

How do you do the division? When using the divide primitive the output will be DBL. The DBL has limited precision, so converting the DBL back to I32 might lead to rounding "problems".

For integer divisions I would recommend using the "Quotient and Remainder" primitive. This will return the same data type as wired to the inputs and you won't face the problems you mentioned.

 

Message 2 of 4
(2,868 Views)
This is known as "banker's rounding" and it's the specified behavior in the IEEE 754 standard which LV implements. The logic is that when a number which lies half-way between two integers needs to be rounded, it is rounded to the nearest even number (so, both 3.5 and 4.5 are rounded to 4). I believe the documentation for the divide primitive even mentions this.

___________________
Try to take over the world!
Message 3 of 4
(2,858 Views)

Some more info on bankers rouding is found on wikipedia (http://en.wikipedia.org/wiki/Bankers_rounding#Round_half_to_even)

Round half to even

A tie-breaking rule that is even less biased is round half to even, namely

  • If the fraction of y is 0.5, then q is the even integer nearest to y.

Thus, for example, +23.5 becomes +24, +22.5 becomes +22, -22.5 becomes -22, and -23.5 becomes -24. This variant of the round-to-nearest method is also called unbiased rounding, convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, or bankers' rounding. For most reasonable distributions of y values, the expected (average) value of the rounded numbers is essentially the same as that of the original numbers, even if the latter are all positive (or all negative). This is widely used in bookkeeping.

 The main reason for using this type of rounding is that in large datasets this gives the least distortion on the results for averaging.

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 4 of 4
(2,843 Views)