public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C programing problem where <= is interpreted as < when using GCC 11.2.0
@ 2021-10-14 15:20 frijolithedog 1
  2021-10-14 15:31 ` Xi Ruoyao
  2021-10-14 17:14 ` Jonathan Wakely
  0 siblings, 2 replies; 4+ messages in thread
From: frijolithedog 1 @ 2021-10-14 15:20 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C programing problem where <= is interpreted as < when using GCC 11.2.0
  2021-10-14 15:20 C programing problem where <= is interpreted as < when using GCC 11.2.0 frijolithedog 1
@ 2021-10-14 15:31 ` Xi Ruoyao
  2021-10-14 17:14 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2021-10-14 15:31 UTC (permalink / raw)
  To: frijolithedog 1, gcc-help

On Thu, 2021-10-14 at 15:20 +0000, frijolithedog 1 via Gcc-help 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.

It's because 0.1 is not something can be represented precisely using
float: it's binary representation is infinite.  The float-type value
closet to 0.1 is 0.100000001490116119384765625, which is slightly larger
than 0.1.

Generally you shouldn't use a floating-point value as a loop iterator
unless you really know what you are doing.
-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C programing problem where <= is interpreted as < when using GCC 11.2.0
  2021-10-14 15:20 C programing problem where <= is interpreted as < when using GCC 11.2.0 frijolithedog 1
  2021-10-14 15:31 ` Xi Ruoyao
@ 2021-10-14 17:14 ` Jonathan Wakely
  1 sibling, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2021-10-14 17:14 UTC (permalink / raw)
  To: frijolithedog 1; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* C programing problem where <= is interpreted as < when using GCC 11.2.0
@ 2021-10-23 13:43 frijolithedog 1
  0 siblings, 0 replies; 4+ messages in thread
From: frijolithedog 1 @ 2021-10-23 13:43 UTC (permalink / raw)
  To: gcc-help

Thank you all so much for your prompt replies.

https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate
was an extremely interesting read and although I am not proficient
in interpreting other bases this has been a total revelation to me.

I began thinking that there must be a way to read numbers from
a file and I found out about arrays and put together the following
code which seems to work just fine for me.

Thank you again, I never would have worked out the problem by
myself.

Bob

int main()
{
        FILE *myFile;
        myFile = fopen("n-array.txt", "r");    /* n-array.txt contains */
        float  nArray[81];            /*  the numbers increasing by 0.1 */
        int  i;                      /* from 2 to 10 which was what my for */
                                      /* loop was doing in my failed coding.  */
        if (myFile == NULL)
            {
                 printf("Error Reading File\n");
                 exit (0);
             }

         for (i = 0; i < 81; i = i + 1 )    /*  81 numbers from 0 to 80  */
             {
                  fscanf(myFile, "%f", &nArray[i] );  /* read file into array */
              }

         for (i = 0; i < 81; i = i + 1 )
              {
                   printf("%f\n", nArray[i] );
               }

          fclose(myFile);

return 0;
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-23 13:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-14 15:20 C programing problem where <= is interpreted as < when using GCC 11.2.0 frijolithedog 1
2021-10-14 15:31 ` Xi Ruoyao
2021-10-14 17:14 ` Jonathan Wakely
2021-10-23 13:43 frijolithedog 1

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).