Re: where function not finding value [message #90566 is a reply to message #90562] |
Tue, 10 March 2015 12:01   |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Tuesday, March 10, 2015 at 12:12:46 PM UTC-6, Jahvasc wrote:
> Hi, guys,
>
> I have a similar problem but I'm comparing intervals to avoid the float-precision problem. Still, I can't get the right answer...
>
> I have a vector with 500 values ranging from 0.01 to 0.4 (0.01 to 0.1 by 0.01; 0.2 to 0.4 by 0.1).
> When I use the histogram function I get a certain number of counts for all intervals:
>
> 79 57 48 44 43 43
> 30 43 31 34 25 14 9
>
> i.e., the value 0.01 appears 79 times, the value 0.02, 57 times, etc.
>
> However, when I use the where function, I get some "holes". This is the bit of the code I'm using:
>
> mmod=[findgen(10)*0.01 + 0.01,findgen(3)*0.1 + 0.2]
> massi=0.
> for j=0,12 do begin
> a=where(mass gt massi and mass le mmod(j),count)
> print,massi,mmod(j),count
> massi=mmod(j)
> endfor
>
> In this case I get the following numbers:
>
> 0.00000 0.0100000 79
> 0.0100000 0.0200000 57
> 0.0200000 0.0300000 48
> 0.0300000 0.0400000 44
> 0.0400000 0.0500000 0
> 0.0500000 0.0600000 43
> 0.0600000 0.0700000 73
> 0.0700000 0.0800000 43
> 0.0800000 0.0900000 0
> 0.0900000 0.100000 31
> 0.100000 0.200000 59
> 0.200000 0.300000 14
> 0.300000 0.400000 9
>
> When I manually try, for example,
>
> a=where(mass gt 0.04 and mass le 0.05,count)
> print,count
>
> I get count=43. What's is going on?
>
Hi Jaqueline,
I think you are running into issues with floating-point precision. On every computer platform (in any language), there are floating-point numbers which are not exactly representable. For example, in IDL, try:
IDL> print, 0.1, format='(f25.16)'
0.1000000014901161
If you use a number like 0.1, which isn't exactly representable, for math operations, and you then try to compare that to other numbers, you will get surprising results:
IDL> x = 0 & for i=0,99 do x = x + 0.1
IDL> print,x
10.0000
IDL> print,x eq 10
0
IDL> print,x, format='(f25.16)'
10.0000019073486330
This is just a limitation of doing floating-point math on a computer (nothing to do with IDL).
Hope this helps.
-Chris
|
|
|