LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

data type conversion

Hi, some thing very interesting happens when I am using data type conversion in C based DLL.
 

int nd = (int) (double) delay/(double) Ts

delay = 1.98, Ts = 0.01

result : nd = 99;

 

int nd =  delay/Ts

nd = 197

 

int nd = ceil(delay/Ts)

nd = 198

 

does anyone know why?

Happy thanksgiving.

 

machman

 

0 Kudos
Message 1 of 7
(3,735 Views)

Where is the connection to LabVIEW?

Can you enlighten us what you perceive as the "interesting" part?

0 Kudos
Message 2 of 7
(3,720 Views)
the code is used in a labview code interface node running on a RT platform. the expecting answer should be 198. but it seems that the answer depends on how you program the data type conversion.
0 Kudos
Message 3 of 7
(3,693 Views)
That is not really "interesting" so much as it is expected behaviour.
 
I cant comment on the first case because there is no reason I know of for it to return 99 unless you made a typo, however the other two cases are fully expected.
 
in your second case you are dividing two floating point numbers (giving you a third floating point number), and this is being converted to an integer. In almost every language (including C), this conversion is effected by simply truncating (or flooring) the floating point number. Floating points cannot represent EVERY number, I am not going to go into a super in depth explanation of that, there are other discussions that do that, however the point is that floating points are not always exact values, this is why you shouldnt do straight equality checks with floating point numbers. Chances are that instead of giving you 198.000, the division is giving you 197.999999999999....and when that is converted to integer, it is floored to 197.
 
In your third case, exactly the same thing is happening, however before your conversion takes place, you perform a ceiling operator on the result, which effectively returns an integer, so that becomes 198 (as expected).
 
That is just how floating point numbers work...
Jeff


Using Labview 7 Express
0 Kudos
Message 4 of 7
(3,684 Views)
I'm not sure about execution order but guessing from the result I would say that in your first example the delay is converted into a double and then into an int, so it's 1. Then 1 is divided by 0.01 which can, due to representation errors as pointed out by jrpe, lead to the 99 (instead of 100).

Daniel

0 Kudos
Message 5 of 7
(3,677 Views)

oh yeah thats totally what is happening to get that first result! Good call
If it was
int nd = int(delay)/Ts;

instead of

int nd = int(delay/Ts);

then it would do exactly as dan said.

In C, if you have an int divided by a floating point, the integer is immediately promoted to floating point, and the result is floating point, however you have converted a float to an int, truncating all precision, then it is reconverted to a float for the divide operation, however it can't regain precision, so it is ~ 1/0.01, and for the reasons I mentioned before, that ends up as 99.999999, which is converted to an int...floor(99.99999) = 99.

Jeff


Using Labview 7 Express
0 Kudos
Message 6 of 7
(3,673 Views)

Thanks guys, I think I know what's going on.

 

Machman

0 Kudos
Message 7 of 7
(3,665 Views)