Re: Why does this occasionally fail?
- From: "Charles Appel" <charlesappel@xxxxxxxxxxxxxx>
- Date: Fri, 05 Aug 2005 18:41:02 GMT
"gandalf" <gandalf@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3lhiqkF11fm6lU1@xxxxxxxxxxxxxxxxx
> Hi,
>
> I'm using Delphi 2005 (Update 3) and am struggling over something simple.
> And that is; why does the following statement function properly for a
> while and then suddenly function incorrectly and continue to function
> incorrectly until I rewrite it - and even then maybe not work any more?
>
> if StrToFloat(Edit2.Text)>Temp then
> whatever code
>
> Temp is of type : Real
>
> when, say, Edit2.Text is '3.05' and Temp is 3.05 it will evaluate as
> though Edit2 > Temp, which it isn't. (the 3.05 is not a relevant number,
> any number will do) Yet it will run quite correctly for ages and then fail
> and continue to fail whatever the values. I run the debugger and check the
> values and sure enough they are the same but it still executes the
> 'whatever code'.
Computers store an approximation of a real number, not an exact
value. This is a necessity. There are an infinite number of real
numbers. An infinite number cannot be accurately stored in a finite
space. Because of this comparing real numbers is problematic.
If you only need two decimal places, you could multiply the input
value be 100, truncate the result and compare to the integer 305.
--
Charles Appel
"A generation which ignores history has no past - and no future."
Robert Anson Heinlein
.
- References:
- Why does this occasionally fail?
- From: gandalf
- Why does this occasionally fail?
- Prev by Date: Re: Why does this occasionally fail?
- Next by Date: Re: Why does this occasionally fail?
- Previous by thread: Re: Why does this occasionally fail?
- Next by thread: Re: Why does this occasionally fail?
- Index(es):
Relevant Pages
|