This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Should rand() return a RAND_MAX value for 32 bit target?
- From: Vincent Lefevre <vincent+gcc at vinc17 dot org>
- To: Andrew Haley <aph at redhat dot com>
- Cc: Neha Gowda <neha dot gnu dot gcc at gmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Thu, 20 Sep 2018 16:42:38 +0200
- Subject: Re: Should rand() return a RAND_MAX value for 32 bit target?
- References: <CAODCPrOyjmXid=Rc_xagiud+t=b=u6DYGGBQs3z=AEAs+4JH6g@mail.gmail.com> <9194d2a2-74c0-2d51-c4b5-433d2744cd43@redhat.com>
On 2018-09-18 14:46:24 +0100, Andrew Haley wrote:
> On 09/18/2018 01:22 PM, Neha Gowda wrote:
> > Can you please let me know if its the expected behavior or some bug?
>
> First consider the value of (float)RAND_MAX. Then please consider if
> using -ffloat-store will make any difference to your program and then
> you may be enlightened. If not, please come back and ask again.
No, the program is so buggy that -ffloat-store does not solve the
problem because there is rounding in the == expression. As a short
example:
#include <stdio.h>
int main (void)
{
float f = 2147483646;
if (f == 2147483647)
{
printf("%.20g\n", (double) f);
}
return 0;
}
outputs 2147483648 because the == is done on float. Thus, from the
original program, you cannot deduce anything.
And BTW, don't try to cast f to int, that's undefined behavior here.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)