public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Dallman, John" <john.dallman@siemens.com>
To: frijolithedog 1 <frijolithedog@outlook.com>,
	"gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: RE: C programming problem where <= is interpreted as < when using GCC 11.2.0
Date: Thu, 14 Oct 2021 15:34:19 +0000	[thread overview]
Message-ID: <e9fb6b5ff2444a8d98bfb13fd8945eda@siemens.com> (raw)

> I am new to programming ...

The full explanation of what you've encountered is complicated, but the fix is very simple, and most programmers do it so instinctively they forget it isn't quite obvious.

Do not use variables of types float or double as loop counters. Ever. At all.

Floating point variables are like physical measurements: nothing is ever exactly 12.0 millimetres, or 765 grams. There's always a margin of error, and you've tripped over it. Change your code to use integers for loop counters.

--
John Dallman

> -----Original Message-----
> From: Gcc-help <gcc-help-
> bounces+john.dallman=siemens.com@gcc.gnu.org> On Behalf Of
> frijolithedog 1 via Gcc-help
> Sent: 14 October 2021 16:21
> To: gcc-help@gcc.gnu.org
> Subject: C programing problem where <= is interpreted as < when using GCC
> 11.2.0
>
> 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.
>
> And I also wrote the following code to try and do the same thing in a
> different way and the output was the same as the above code:
>
> #include <stdio.h>
> #include <math.h>
> #include <float.h>
>
> int main(void)
>  {
>        float n, step;
>
>        n = 2.0;
>        step = 0.1;
>
>            do
>           {
>              printf("%3.4f\n", n);       /*  This stops at 9.9000 and not at 10.0000   */
>              n  = n + step;
>
>            } while  (n <= 10);
>  }
>
> I am new to programing and the following details may help you:
>
> I followed the instructions at this site:
> https://winlibs.com/
> WinLibs standalone build of GCC and MinGW-w64 for Windows
>
> and I downloaded the following:
>
> Release versions
> UCRT runtime
>
>     GCC 11.2.0 + LLVM/Clang/LLD/LLDB 13.0.0 + MinGW-w64 9.0.0 - UCRT -
> release 2   (LATEST)
>         Win32: 7-Zip archive* | Zip archive   -   without LLVM/Clang/LLD/LLDB: 7-
> Zip archive* | Zip archive
>         Win64: 7-Zip archive* | Zip archive   -   without LLVM/Clang/LLD/LLDB: 7-
> Zip archive* | Zip archive
>
> I clicked on the Zip archive highlighted in yellow which does not include
> LLVM/Clang/LLD/LLDB
>
> I then went to this site
> https://github.com/brechtsanders/winlibs_mingw/releases/download/11.2.
> 0-13.0.0-9.0.0-ucrt-r2/winlibs-x86_64-posix-seh-gcc-11.2.0-mingw-w64ucrt-
> 9.0.0-r2.zip
> and downloaded
> winlibs-x86_64-posix-seh-gcc-11.2.0-mingw-w64ucrt-9.0.0-r2.zip
>
> The Operating System I am using is Windows 7.
>
> I have been to your site https://gcc.gnu.org/pipermail/gcc-help/
> and searched your Archives back to July viewing by Thread and by Subject
> and the search term I used was the less than or equal to symbol  <= but if the
> answer was there I am sorry but I missed it.
>
> I have also searched the GCC Manual for version gcc-11.2.0 but if the answer
> was there I am sorry but I missed it.
>
> Thank you for any assistance you can give me.
>
> Bob
-----------------
Siemens Industry Software Limited is a limited company registered in England and Wales.
Registered number: 3476850.
Registered office: Faraday House, Sir William Siemens Square, Frimley, Surrey, GU16 8QD.

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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=e9fb6b5ff2444a8d98bfb13fd8945eda@siemens.com \
    --to=john.dallman@siemens.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).