[英]64 bit data not returning 64 bit value
I'm trying to make a code to return a normalized value for a random number.我正在尝试编写一个代码来返回一个随机数的标准化值。 The code is the following:代码如下:
// add libraries
# include <stdio.h>
# include <stdlib.h>
# include <stdint.h>
# include <inttypes.h>
# include <math.h>
//function declaration
double rand_number_f (void);
// since the compure is 64 bit, we take M as 2^64 - 1
unsigned long long M = 184467440709551615;
// choice of a (random but will be verified)
// a ≡ 3 or 5 modulo 8 ; in our case 3845694483 modulo 8 is 3
// a to be of the same order of magnitude
// sqrt ((2^64)/3845694483) = 1.11 which is between 1/10 and 10
unsigned long long a = 9448928019;
// initial seed x0
unsigned long long SEED = 41768947553;
int main(void)
{
printf("the number is : %f \n", rand_number_f());
}
// function to return random number between 0 and 1
double rand_number_f ()
{
SEED = SEED * a;
// return variable
double y;
y = (double)SEED/M;
return y;
}
However, the output is the following:但是,输出如下:
the number is : 39.520000 Program ended with exit code: 0数字是:39.520000 程序以退出代码结束:0
Why am I getting the value 39?为什么我得到的值是 39? By dividing by the Max value of a 64 bit unsigned value, shouldn't I only have the decimal part?通过除以 64 位无符号值的最大值,我不应该只有小数部分吗?
Your value for M
is wrong.你对M
值是错误的。 You should use ULLONG_MAX
, which is 18446744073709551615
(you're missing 37
).您应该使用ULLONG_MAX
,即18446744073709551615
(您缺少37
)。
#include <limits.h>
unsigned long long M = ULLONG_MAX;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.