Should rand() return a RAND_MAX value for 32 bit target?
Dennis Clarke
dclarke@blastwave.org
Thu Sep 20 15:46:00 GMT 2018
On 09/20/2018 11:05 AM, Vincent Lefevre wrote:
> Actually, there may be a bug with -m32:
>
> #include <stdio.h>
> #include <float.h>
>
> int main (void)
> {
> float f = 2147483646;
> printf ("FLT_EVAL_METHOD = %d\n", (int) FLT_EVAL_METHOD);
> if (f == 2147483647)
> {
> printf("%.20g\n", (double) f);
> }
> return 0;
> }
>
> $ gcc -std=c99 -m32 -O tst.c -o tst
> $ ./tst
> FLT_EVAL_METHOD = 2
> 2147483648
>
> Since FLT_EVAL_METHOD = 2, float_t is long double, thus shouldn't
> 2147483647 be converted to long double directly (as f is regarded
> as an operand of type long double), so that the result of the
> equality test should be false?
>
> The C standard says for FLT_EVAL_METHOD = 2: "evaluate all operations
> and constants to the range and precision of the long double type".
> ^^^^^^^^^^^^^
>
> Converting f first to float is contrary to the idea behind
> FLT_EVAL_METHOD = 2, which is to avoid loss of precision in
> intermediate operations.
>
Hrmmm ... I see FLT_EVAL_METHOD = 0 here with gcc 8.1.0 on sparc :
$ file ./flt_eval
./flt_eval: ELF 32-bit MSB executable SPARC32PLUS Version 1, V8+
Required, dynamically linked, not stripped, no debugging information
available
$ ./flt_eval
FLT_EVAL_METHOD = 0
2147483648
I see similar on ppc64 :
$ file flt_eval
flt_eval: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1
(SYSV), dynamically linked, interpreter /lib/ld.so.1, for GNU/Linux
3.2.0, not stripped
$ ./flt_eval
FLT_EVAL_METHOD = 0
2147483648
$
Not sure how you are getting FLT_EVAL_METHOD as 2.
What platform is this on ?
Dennis
More information about the Gcc-help
mailing list