|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
error with "if" !!!
Hi ,everybody I have encounter with strange error ( strange in my point view !) In program, I need to find the sample values of function named f . I use these 3 commands : 1) if (f<0.0005D0 ) then 2) if (abs(f-0.0005D0)<0.001D0) then 3) if (abs(f-0.0005D0)<0.01D0 ) then to see the answer . I was surprised to see the answer of this commands are different ! for example , 7.3124E-5 only given by second if !! my question is what is the best way to find f near a number and why the above commands give various answers thanks nakisa
nakisa wrote: > Hi ,everybody > I have encounter with strange error ( strange in my point view !) > In program, I need to find the sample values of function named f . I > use these 3 commands : > 1) if (f<0.0005D0 ) then > 2) if (abs(f-0.0005D0)<0.001D0) then > 3) if (abs(f-0.0005D0)<0.01D0 ) then > to see the answer . I was surprised to see the answer of this commands > are different ! > for example , 7.3124E-5 only given by second if !!
I tried with 3 different compilers (g95, ifort and F) all gave the same answer: condition is true. Maybe you have to check the rest of your code. Giorgio
On Apr 28, 3:52 am, nakisa <nakisa.noor@gmail.com> wrote: > Hi ,everybody > I have encounter with strange error ( strange in my point view !) > In program, I need to find the sample values of function named f . I > use these 3 commands : > 1) if (f<0.0005D0 ) then > 2) if (abs(f-0.0005D0)<0.001D0) then > 3) if (abs(f-0.0005D0)<0.01D0 ) then > to see the answer . I was surprised to see the answer of this commands > are different ! > for example , 7.3124E-5 only given by second if !! > my question is what is the best way to find f near a number and why > the above commands give various answers > thanks > nakisa
The answer is inherent in the nature of floating point representation -- see the following http://docs.sun.com/source/806-3568/ncg_goldberg.html
dpb <bozart @gmail.com> wrote: > On Apr 28, 3:52 am, nakisa <nakisa.noor @gmail.com> wrote: > > I have encounter with strange error ( strange in my point view !) > > In program, I need to find the sample values of function named f . I > > use these 3 commands : > > 1) if (f<0.0005D0 ) then > > 2) if (abs(f-0.0005D0)<0.001D0) then > > 3) if (abs(f-0.0005D0)<0.01D0 ) then > > to see the answer . I was surprised to see the answer of this commands > > are different ! > > for example , 7.3124E-5 only given by second if !! > The answer is inherent in the nature of floating point representation
While that's certainly one of the first things that comes to mind when people ask about comparing floating point numbers, it doesn't explain the OP's observation. Indeed, I doubt that there is any explanation as it is posed. Rather, I suspect that the OP has errors in parts of the code not shown or something of the sort. There is no compiler in the world that is going to get those simple expressions wrong for that value. This leads me to suspect that something is misreported here. Either the value is not as expected, or the information that the "answer" to the "commands" is different. I'll also note that the syntax shown isn't compilable at all if f is indeed a function, as claimed. I'm assuming that the OP means a variable, but there is enough confusion here that I could be wrong. This is a prime example of a case where the OP needs to show actual code so that other can see what is actually happening instead of the OP's explanation of what he thinks is happening. -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain
On Apr 28, 9:59 am, nos@see.signature (Richard Maine) wrote:
> dpb <bozart @gmail.com> wrote: > > On Apr 28, 3:52 am, nakisa <nakisa.noor @gmail.com> wrote: > > > I have encounter with strange error ( strange in my point view !) > > > In program, I need to find the sample values of function named f . I > > > use these 3 commands : > > > 1) if (f<0.0005D0 ) then > > > 2) if (abs(f-0.0005D0)<0.001D0) then > > > 3) if (abs(f-0.0005D0)<0.01D0 ) then > > > to see the answer . I was surprised to see the answer of this commands > > > are different ! > > > for example , 7.3124E-5 only given by second if !! > > The answer is inherent in the nature of floating point representation > While that's certainly one of the first things that comes to mind when > people ask about comparing floating point numbers, it doesn't explain > the OP's observation. Indeed, I doubt that there is any explanation as > it is posed. Rather, I suspect that the OP has errors in parts of the > code not shown or something of the sort.
... You're right, Richard, being in a hurry I reacted w/o reading the code...bad idea! :(
dpb wrote: > On Apr 28, 3:52 am, nakisa <nakisa.noor @gmail.com> wrote: >>1) if (f<0.0005D0 ) then >>2) if (abs(f-0.0005D0)<0.001D0) then >>3) if (abs(f-0.0005D0)<0.01D0 ) then >>to see the answer . I was surprised to see the answer of this >> commandsare different ! >>for example , 7.3124E-5 only given by second if !! >>my question is what is the best way to find f near a >> number and whythe above commands give various answers > The answer is inherent in the nature of floating point representation Understanding floating point is important, but the three IFs are very different, even without considering floating point. The third one should be mathematically true for f between 0.0095 and 0.0105, exclusive, and between -.0105 and -0.0095. (and on machines with scaled fixed decimal or float decimal arithmetic.) The second one between 0.0005 and 0.0015, exclusive, and between -0.0015 and -0.0005 exclusive. Usually when finding f near a number, one subtracts the number, and compares the absolute value of the difference to the allowable difference. In that case, the allowed difference would normally be smaller than the number being compared. Be sure that the expressions do what you want them to do. -- glen
nakisa wrote: > Hi ,everybody > I have encounter with strange error ( strange in my point view !) > In program, I need to find the sample values of function named f .
Do you mean a function f, or a variable f?
|
 |
 |
 |
 |
|