LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Exponent Different ?

Hi

 

Im doing a calculation in CVI which im porting from VB.

This is the calculation in CVI:

F2A =310000/((504100+pow((123.132168421052630*3) ,2))*pow(2,100.415));

 

This is the function in VB:

F2A =310000/((504100+(123.132168421052630*3)^2)*2^100.415)

 

In CVI F2A = 2.863380

In VB F2A = 2.863409

 

Why do i recieve different values from the two languages ?

Help share your knowlegde
0 Kudos
Message 1 of 4
(2,903 Views)

For any of several reasons.

 

VB may be using a float type rather than a double, your code snippet doesn't include the data types.  Is F2A a float or a double?  In standard C, pow is a function of type double.

 

Your floating point constant 123.132 ... can be represented exactly by IEEE 754 double type but it likely exceeds the precision of a double type when you multiply it and square it.

 

The term 2^100.415 is almost certainly not represented exactly in IEEE 754 double format.

 

decimal precision of IEEE 754 double type is 15.95 decimal digits.

 

Expression evaluation order could be different between the two languages.

 

Could also depend on which CVI compiler you use - native CVI, the new one in latest CVI, Intel, Visual Studio - all of these will work with CVI and all may well produce slightly different results.

 

I'd tend to believe the CVI result to be more precise.

 

You could try compiling it on a machine that supports long double or double extended or "quadruple" precision and get a more accurate result.  Or use an arbitrary precision package like Maple  (or hack it in C# or Java with arbitrary precision class) and see what the "true" result should be.

0 Kudos
Message 2 of 4
(2,893 Views)

Thanks for the reply.

I think is a compiler diffrence.

The VB code i am port to CVI is ran wilth GWBasic.

 Im not sure in the VB wether F2A is a float you a double.

Who ever created the VB program did not specify a type.

 

Help share your knowlegde
0 Kudos
Message 3 of 4
(2,887 Views)

Shako,

 

On top of the suggestions already present, you may also be getting some difference from how the compiler reads the 310000 and the 504100. It may be reading them as integers instead of doubles, causing some roundoff error. 

 

Regards,

 

Kyle Mozdzyn

Applications Engineering

National Instruments

Regards,

Kyle M.
Applications Engineering
National Instruments
0 Kudos
Message 4 of 4
(2,870 Views)