10-07-2008 05:56 AM
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
10-07-2008 06:00 AM
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
10-07-2008 07:18 AM
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
10-07-2008 07:26 AM
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
10-07-2008 07:28 AM
10-07-2008 07:31 AM
10-07-2008 07:36 AM
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
10-08-2008 05:15 AM
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,
10-08-2008 06:15 AM - edited 10-08-2008 06:19 AM
Added "Round towards +Infinity"
10-08-2008 07:01 AM
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