Home arrow static arrow Java Programming [Archive] - Math calculation error.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Math calculation error.
This topic has 21 replies on 2 pages.    « Previous | 1 | 2 |

Posts:11,200
Registered: 7/22/99
Re: Math calculation error.  
Jul 20, 2004 12:00 PM (reply 15 of 21)



 
Why is this necessary?

Look at the OP's case :) He/she has "result1 < result2" and gets more results than wanted because result1 ends up only slightly less than result2. Applying a treshold fixes this.
 

Posts:37,103
Registered: 3/30/99
Re: Math calculation error.  
Jul 20, 2004 12:11 PM (reply 16 of 21)



 
Why is this necessary?

Look at the OP's case :) He/she has "result1 result2" and gets more results than wanted
because
result1 ends up only slightly less than result2.

Not following. I'll take your word for it for now, and see if I can get my brain around later.
 

Posts:5
Registered: 7/20/04
Re: Math calculation error.  
Jul 20, 2004 12:44 PM (reply 17 of 21)



 

private static final double EPSILON = 0.000000000000005;...if (((result1-EPSILON) < result2)	|| (........) { }


This seems to work at most of cases, now I miss some results. I continue debuggin and testing it tomorrow.

The function is realy complex and what I wrote was just some simplification of it. Basically it calculates crossing points in multidimensional matrix and I don't totally understand it logic.

Thanks for help.
 

Posts:24,036
Registered: 2/3/03
Re: Math calculation error.  
Jul 20, 2004 12:44 PM (reply 18 of 21)



 
Now I'm intrigued, and being entirely unfamiliar with "epsilon", I figure I'll ask: does epsilon signify some acceptable range of deviation from something? In other words does it specify how far "off" one must be before being considered less-than, equal, or greater-than?

off to google while waiting for the forum slaves to give me my answer
 

Posts:31,095
Registered: 4/30/99
Re: Math calculation error.  
Jul 20, 2004 12:49 PM (reply 19 of 21)



 
Look at the OP's case :) He/she has "result1 result2" and gets more results than wanted because
result1 ends up only slightly less than result2.
Applying a treshold fixes this.

If you are saying that everything between X-epsilon and X+epsilon should be considered "equal to" X for floating-point comparison purposes, then it is certainly tempting to replace "Y < X" by "Y < X-epsilon" so that you don't simultaneously have Y "equal to" X and Y "less than" X. But there is really nothing wrong with having both of those statements true, is there? After all, we already have X "equal to" X-epsilon*0.5 "equal to" X-epsilon "equal to" X-epsilon*1.5 and so on, and that doesn't confuse us.

Also, it's possible that a calculation which, in infinite-precision, would produce a very small negative number, produces a very small positive number in our finite-precision hardware. For such a case perhaps we should replace "Y < 0" by "Y < +epsilon" rather than "Y < -epsilon".

Okay, I am unconvinced by my arguments against the threshold. What are the arguments for it?

PC²
 

Posts:31,095
Registered: 4/30/99
Re: Math calculation error.  
Jul 20, 2004 12:59 PM (reply 20 of 21)



 
Now I'm intrigued, and being entirely unfamiliar with
"epsilon", I figure I'll ask: does epsilon signify
some acceptable range of deviation from something? In
other words does it specify how far "off" one must be
before being considered less-than, equal, or
greater-than?

Yes, epsilon is the Greek letter traditionally used in mathematics to denote "a number that's as small as I want". It usually shows up in proofs about limits and continuous functions, and numeric analysts use it in their work too.

There's the SWAG use of epsilon, which you saw an example of in jruutu's post just a few back. Pick a pretty small number that makes your data come out right. But really the epsilon you choose should vary depending on the scale of the numbers you are working with. If your numbers are about 1, then maybe epsilon could be 0.0000001, so that anything between 0.9999999 and 1.0000001 would be considered equal to 1. But if your numbers are about 10,000,000,000 then that would be a bad choice for epsilon. Likewise if your numbers were about 0.0000001 themselves, it would be a bad choice for epsilon.

It also depends on how much accuracy you really need. If you're solving an equation while in the process of building a dam, then you probably don't need much more than 4 significant digits. But if you're doing a theoretical calculation of the gravitational constant for your PhD theses then you may well be aiming at 10 significant digits.
 

Posts:24,036
Registered: 2/3/03
Re: Math calculation error.  
Jul 20, 2004 1:04 PM (reply 21 of 21)



 
That's an excellent explanation (at least from my viewpoint). Thanks a ton. :o)
 
This topic has 21 replies on 2 pages.    « Previous | 1 | 2 |