From: Vincent Lefevre <vincent+gcc@vinc17.org>
To: Andrew Haley <aph@redhat.com>
Cc: Neha Gowda <neha.gnu.gcc@gmail.com>, gcc-help@gcc.gnu.org
Subject: Re: Should rand() return a RAND_MAX value for 32 bit target?
Date: Thu, 20 Sep 2018 14:42:00 -0000 [thread overview]
Message-ID: <20180920144238.GB24856@cventin.lip.ens-lyon.fr> (raw)
In-Reply-To: <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)
next prev parent reply other threads:[~2018-09-20 14:42 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-18 12:22 Neha Gowda
2018-09-18 13:09 ` Xi Ruoyao
2018-09-18 13:17 ` Xi Ruoyao
2018-09-18 13:49 ` Jonathan Wakely
2018-09-18 13:46 ` Andrew Haley
2018-09-20 14:42 ` Vincent Lefevre [this message]
2018-09-20 15:05 ` Vincent Lefevre
2018-09-20 15:21 ` Liu Hao
2018-09-20 15:58 ` Vincent Lefevre
2018-09-20 16:12 ` Vincent Lefevre
2018-09-21 2:49 ` Liu Hao
2018-09-22 8:17 ` Vincent Lefevre
2018-09-22 10:08 ` Vincent Lefevre
2018-09-22 13:52 ` Liu Hao
2018-09-22 22:12 ` Vincent Lefevre
2018-09-20 15:46 ` Dennis Clarke
2018-09-20 16:16 ` Vincent Lefevre
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=20180920144238.GB24856@cventin.lip.ens-lyon.fr \
--to=vincent+gcc@vinc17.org \
--cc=aph@redhat.com \
--cc=gcc-help@gcc.gnu.org \
--cc=neha.gnu.gcc@gmail.com \
/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).