* RE: C programming problem where <= is interpreted as < when using GCC 11.2.0
@ 2021-10-14 15:34 Dallman, John
0 siblings, 0 replies; only message in thread
From: Dallman, John @ 2021-10-14 15:34 UTC (permalink / raw)
To: frijolithedog 1, gcc-help
> 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.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-10-14 15:34 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14 15:34 C programming problem where <= is interpreted as < when using GCC 11.2.0 Dallman, John
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).