public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: neha.gnu.gcc@gmail.com
Cc: gcc-help <gcc-help@gcc.gnu.org>, xry111@mengyan1223.wang
Subject: Re: Should rand() return a RAND_MAX value for 32 bit target?
Date: Tue, 18 Sep 2018 13:49:00 -0000	[thread overview]
Message-ID: <CAH6eHdR9sk51BbfRZxvAv21uuxDUk_T4WN2LwdhwJyn58gmcxw@mail.gmail.com> (raw)
In-Reply-To: <4f0b1402f9c0dd7ecec3fc0b23ae46bc5387af2d.camel@mengyan1223.wang>

On Tue, 18 Sep 2018 at 14:09, Xi Ruoyao wrote:
>
> On 2018-09-18 17:52 +0530, Neha Gowda wrote:
> > Hi,
> >
> > I am trying to run the following testcase for Random Number Generator
> > function.
> > The testcase returns true when generated number is RAND_MAX and returns
> > false when its not generated.
> >
> > Command :- gcc -m32 -O2 test.c
> > ======================================================
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main()
> > {
> >     unsigned int i;
> >     float f;
> >     srand(0);
> >     for (i = 0; i <= RAND_MAX; i++) {
> >         f = rand();
>
> There is a rounding error introduced since a float can not represent
> all integer values in [0, RAND_MAX].
>
> >         if (f == RAND_MAX) {
>
> RAND_MAX is 2147483647 on 32-bit GNU/Linux.  It can not be represented
> by a float.

Which leads to the question, why are you using float anyway?

rand() returns an int, not a float.

  parent reply	other threads:[~2018-09-18 13:49 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 [this message]
2018-09-18 13:46 ` Andrew Haley
2018-09-20 14:42   ` Vincent Lefevre
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=CAH6eHdR9sk51BbfRZxvAv21uuxDUk_T4WN2LwdhwJyn58gmcxw@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=neha.gnu.gcc@gmail.com \
    --cc=xry111@mengyan1223.wang \
    /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).