Re: where function not finding value [message #90571 is a reply to message #90570] |
Tue, 10 March 2015 19:34   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Tuesday, March 10, 2015 at 2:19:16 PM UTC-6, Jeff B wrote:
> On Tuesday, March 10, 2015 at 3:15:18 PM UTC-5, Jahvasc wrote:
>> Hi, Chris, thank you for your answer. But it was to avoid this floating point problems that I'm using the intervals. Besides, why when I write down the interval (mass gt 0.04 and mass le 0.05) I got the the right answer?
>
> Unfortunately you're not quite avoiding those issues. For instance, analogous to what you're doing try this
>
> IDL> print, 0.05 le 4.0*0.01+0.01
> 0
>
> IDL> print, 0.05 le 0.05
> 1
>
> The first example *should* return 1 in a perfect world, but once you do mathematical operations (such as the multiplication and addition) you will introduce floating point arithmetic issues.
One way to reduce (but not eliminate) these types of issues is to use double precision for all calculations:
IDL> print, 0.05d le 4*0.01d + 0.01d
1
Again, I can't stress enough that this is a problem with all languages. For example, in Python, if we use 32-bit floating-point numbers:
>>> import numpy as np
>>> f = np.float32
>>> f(0.05) <= 4*f(0.01) + f(0.01)
False
Cheers,
Chris
|
|
|