12-09-2010 05:16 PM
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!
Solved! Go to Solution.
12-09-2010 05:21 PM
I usually will 'undulate', meaning I'll pass through an increment-decrement cycle.
12-09-2010 05:25 PM
You could also simply check the difference against machine epsilon, and if it's less than that, replace the value with zero.
12-09-2010 07:36 PM
12-09-2010 10:09 PM
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.
12-09-2010 10:16 PM
Hi Darin,
Can you elaborate a little on the undulation? Thanks!
12-09-2010 10:24 PM
Hi Smercurio,
It is possible that the even though residual number is very small, but it is still greater than epsilon?
12-09-2010 11:20 PM
12-10-2010 01:56 AM
@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.
12-10-2010 02:12 AM - edited 12-10-2010 02:14 AM
@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 ;))