# Board index » delphi » Floating point math

## Floating point math

I have been frustrated (what, again?) trying to do what seems to be a
really simple thing. How do I get a floating point variable to equal
precisely zero in Delphi?

I though it was as simple as declaring the variable (I've tried 3
variations of the Real types: real, single and double) and assign 0 to
it like this.

var
PrevValue: Single;

and then
PrevValue := 0;

Delphi insists on making the value something ridiculously small like
2.345678E-10, but not zero! Of course a subsequent test for a zero value
fails. If I go to the {*word*193} extreme of subtracting the number from
itself after the above assignment, guess what, it equals zero (actually
0.0 to be precise).

I'm sure I'm missing something obvious here. Can someone help?

Thanks
Paul

## Re:Floating point math

Try this
PrevValue := 0.0;

##### Quote
Paul Petersen (paul.peter...@canrem.com) wrote:

: I have been frustrated (what, again?) trying to do what seems to be a
: really simple thing. How do I get a floating point variable to equal
: precisely zero in Delphi?

: I though it was as simple as declaring the variable (I've tried 3
: variations of the Real types: real, single and double) and assign 0 to
: it like this.

: var
:   PrevValue: Single;

: and then
:   PrevValue := 0;

: Delphi insists on making the value something ridiculously small like
: 2.345678E-10, but not zero! Of course a subsequent test for a zero value
: fails. If I go to the {*word*193} extreme of subtracting the number from
: itself after the above assignment, guess what, it equals zero (actually
: 0.0 to be precise).

: I'm sure I'm missing something obvious here. Can someone help?

: Thanks
: Paul

## Re:Floating point math

In article: <60.2126.3474.0N1F1...@canrem.com>  paul.peter...@canrem.com (Paul Petersen)
writes:

##### Quote

>var
>  PrevValue: Single;

>and then
>  PrevValue := 0;

>Delphi insists on making the value something ridiculously small like
>2.345678E-10, but not zero! Of course a subsequent test for a zero value
>fails. If I go to the {*word*193} extreme of subtracting the number from
>itself after the above assignment, guess what, it equals zero (actually
>0.0 to be precise).

The above code works on my machine, so I cannot give you a definitive answer, but I would
suggest setting PrevValue to 0.0 not 0, and checking for 0.0, not 0. The reason for this
is that 0 is an integer value, and 0.0 is a floating point value, therefore there is a
chance of cast errors being introduced.

Hope this helps
--
Phil Arundell (p...@pacom.demon.co.uk)
He Who Demands Respect
Must First Respect
Himself
------------------------------------------------------------------------

## Re:Floating point math

##### Quote
Phil Arundell <p...@pacom.demon.co.uk> wrote:
>In article: <60.2126.3474.0N1F1...@canrem.com>  paul.peter...@canrem.com (Paul Petersen)
>writes:

>>var
>>  PrevValue: Single;

>>and then
>>  PrevValue := 0;

>>Delphi insists on making the value something ridiculously small like
>>2.345678E-10, but not zero! Of course a subsequent test for a zero value
>>fails. If I go to the {*word*193} extreme of subtracting the number from
>>itself after the above assignment, guess what, it equals zero (actually
>>0.0 to be precise).

>The above code works on my machine, so I cannot give you a definitive answer, but I would
>suggest setting PrevValue to 0.0 not 0, and checking for 0.0, not 0. The reason for this
>is that 0 is an integer value, and 0.0 is a floating point value, therefore there is a
>chance of cast errors being introduced.

Also, you should try to avoid using the "=" relation with floating-point
in any language, certainly if you have done any multiplication or
division on any of the numbers involved.  What you should do is
define some (very small) tolerance constant and treat
(Abs (a - b) <= tolerance) as meaning (a = b)

##### Quote
>Hope this helps
>--
>Phil Arundell (p...@pacom.demon.co.uk)
>                                                 He Who Demands Respect
>                                                 Must First Respect
>                                                 Himself
>------------------------------------------------------------------------

-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------