public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: frijolithedog 1 <frijolithedog@outlook.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: C programing problem where <= is interpreted as < when using GCC 11.2.0
Date: Thu, 14 Oct 2021 18:14:57 +0100	[thread overview]
Message-ID: <CAH6eHdTHeg52E1tYz=cG-z_i8FzOQdPpAZv17HHgG+Wp-MJc4g@mail.gmail.com> (raw)
In-Reply-To: <PSAPR04MB4311D65521A8CD3B0E2820EBB4B89@PSAPR04MB4311.apcprd04.prod.outlook.com>

On Thu, 14 Oct 2021 at 16:21, frijolithedog 1 via Gcc-help <
gcc-help@gcc.gnu.org> wrote:

> I am having a C programing problem where <= is interpreted as < when using
> GCC 11.2.0
>
> I was debugging a larger program which I broke down into smaller sections
> of code and I noticed
> the following code was not working correctly:
>
> #include <stdio.h>
> #include <math.h>
> #include <float.h>
>
> int main(void)
>  {
>        float n, step;
>
>        step = 0.1;
>
>            for (n = 2; n <= 10; n = n + step )
>            printf("%3.4f\n", n );                     /*  This stops at
> 9.9000 and not at 10.0000  */
>
>  }
>
> I tried the above code only using the following include statement
> #include <stdio.h>
> but the result was the same.
>


See
https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate
and hundreds of other pages explaining the properties of floating-point
numbers:
https://docs.python.org/3/tutorial/floatingpoint.html
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding

For the full story:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

  parent reply	other threads:[~2021-10-14 17:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-14 15:20 frijolithedog 1
2021-10-14 15:31 ` Xi Ruoyao
2021-10-14 17:14 ` Jonathan Wakely [this message]
2021-10-23 13:43 frijolithedog 1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH6eHdTHeg52E1tYz=cG-z_i8FzOQdPpAZv17HHgG+Wp-MJc4g@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=frijolithedog@outlook.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).