This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Should rand() return a RAND_MAX value for 32 bit target?


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)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]