When I execute program
#include <stdio.h>
#include <math.h>
#include <unistd.h>
double exponential(double u);
double exponential(double u)
{
double a = (double)rand();
return (-u * log(1.0 - a));
}
int main(void)
{
printf("%e\n",exponential(2.3));
return 0;
}
I obtain:
nan
Why?
Because rand()
returns an integer (between 0 and RAND_MAX
), most often this integer is larger than 1 and your log
expression will be negative. log
returns NaN
(not a number) for negative inputs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.