LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

floating point math zero

Solved!
Go to solution

I am just trying to do some simply math, and I ran into some problem.  I am trying to find the best way to fix it.  For example, sqrt(57.0 - 57.0) with numbers in floating point format.  The 57.0 - 57.0 is giving me a very small negative number, which generate a NaN with the sqrt.  If I do an absolute value of that and sqrt, I get a very small number.  The answer should be zero and not a very small number.  I guess I and round the number, but is there an easier way?  I understand that a floating point cannot represent a number precisely, but is there a way to get a zero when the answer is zero?  Thanks!

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 1 of 19
(3,519 Views)

I usually will 'undulate', meaning I'll pass through an increment-decrement cycle.

0 Kudos
Message 2 of 19
(3,517 Views)

You could also simply check the difference against machine epsilon, and if it's less than that, replace the value with zero.

0 Kudos
Message 3 of 19
(3,514 Views)
Sqrt(57.0-57.0) will give you exactly zero, eve with DBL. It seems at least one of your values is derived from a computation, and thus not 57.0, you are simply not displaying enough digits. What kind of problem are you trying to solve? What's the application? Are all your inputs typically whole numbers?
Message 4 of 19
(3,494 Views)

The program is just a simple power calculator.  it takes user input 3 phases voltage, currents, and phase for all harmonics and calculate basic power parameters, such as VLL, THD, power, etc.  The input doesn't have to be whole number, but it doesn't need too much precision.  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 5 of 19
(3,481 Views)

Hi Darin,

 

Can you elaborate a little on the undulation?  Thanks!

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 6 of 19
(3,476 Views)

Hi Smercurio,

 

It is possible that the even though residual number is very small, but it is still greater than epsilon?  

------------------------------------------------------------------

Kudos and Accepted as Solution are welcome!
0 Kudos
Message 7 of 19
(3,475 Views)
When a number smaller than epsilon is added to one (incremented) it gives one. The following decrement then gives zero. The method is simple and adapts to different types and arrays and scalars. In this case I would probably decrement and then increment. Plan B is simply to promote the value to complex, take the square root and then take the real part.
Message 8 of 19
(3,468 Views)

@jyang72211 wrote:

The program is just a simple power calculator.  it takes user input 3 phases voltage, currents, and phase for all harmonics and calculate basic power parameters, such as VLL, THD, power, etc.  The input doesn't have to be whole number, but it doesn't need too much precision.  


So there's a sqrt(3) in the calculations somewhere? If you do the calculations in DBL you can convert to SGL.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 9 of 19
(3,449 Views)

 


@jyang72211 wrote:

The program is just a simple power calculator.  it takes user input 3 phases voltage, currents, and phase for all harmonics and calculate basic power parameters, such as VLL, THD, power, etc.  The input doesn't have to be whole number, but it doesn't need too much precision.  


 

Sometimes a simple re-ordering of the operations can solve the problems.

In other times, you need to deal with the inherent floating point inaccuracies a little more explicitly.

I agree with Darin that using complex numbers could be a possible solution.

 

Why don'y you attach your code and tell us some typical input values.

 

(I would not recommend going to SGL, it simply would make the inaccuracies a little worse with no other benefits ;))

Message 10 of 19
(3,442 Views)