Kann eine IEEE-754-Gleitkommazahl <1 (dh mit einem Zufallsgenerator generiert, der eine Zahl> = 0.0 und <1.0 generiert) jemals mit einer ganzen Zahl (in Gleitkommaform) multipliziert werden, um eine Zahl gleich oder größer als zu erhalten? diese ganze Zahl wegen der Rundung?
dh
double r = random() ; // generates a floating point number in [0, 1)
double n = some_int ;
if (n * r >= n) {
print 'Rounding Happened' ;
}
This might be equivalent to saying that does there exist an N and R such that if R is the largest number less than 1 which can be represented in IEEE-754 then N * R >= N (where * and >= are appropriate IEEE-754 operators)
This comes from this question based on this documentation and the postgresql random function
numerical-analysis
floating-point
rounding
Cade Roux
quelle
quelle
Antworten:
Assuming round-to-nearest and thatN>0 , then N∗R<N always. (Be careful not to convert an integer that's too large.)
Letc2−q=N , where c∈[1,2) is the significand and q is the integer exponent. Let 1−2−s=R and derive the bound
with equality if and only ifc=1 . The right-hand side is less than N and, since 2−q−s is exactly 0.5 units in the last place of N , either c=1 and 2−q−2−q−s is exactly representable (since N is normal and not the smallest normal), or c>1 , and the nearest rounding is down. In both cases, N∗R is less than N .
Upward rounding can cause a problem, not that it should ever be selected in the presence of unsuspecting users. Here's some C99 that prints
"0\n1\n"
on my machine.quelle