Re: inconsistent for() and while() behavior when using floating point

From: Hans Petter Selasky <hps_at_selasky.org>
Date: Mon, 15 Jan 2018 15:45:51 +0100
On 01/15/18 15:38, Yuri Pankov wrote:
> Hi,
> 
> Looking at https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=217149, I 
> noticed that it isn't a seq(1) problem per se, rather for() and while() 
> loops behaving inconsistently while using floating point, i.e.:
> 
>          double i;
> 
>          for (i = 1; i <= 2.00; i += 0.1)
>                  printf("%g\n", i);
> 
> would produce:
> 
>          1
>          ...
>          1.9
> 
> but:
> 
>          double i;
> 
>          for (i = 1; i <= 2; i += 0.2)
>                  printf("%g\n", i);
> 
> would correctly end with 2:
> 
>          1
>          ...
>          2
> 

Hi,

The decimal value "0.2" is the same like the fraction "1/5", which 
cannot be represented by a float nor double without rounding error. The 
more times you iterate the bigger the error becomes.

When you compare an integer with a float rounding happens. Check this out:

if ((int)(float)0.999999999999999999999 >= (int)1)
         printf("OK\n");

Sequences using floating point should technically only use steps which 
can be written like this: "remainder * pow(2, -shift)".

--HPS
Received on Mon Jan 15 2018 - 13:48:44 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:14 UTC