frexp(3) does not do what the manual page says it does (4.[12]BSD)
Jeff Mogul
mogul at CSL-Vax.ARPA
Fri Aug 17 04:12:35 AEST 1984
Index: lib/libc/vax/gen/frexp.c 4.2BSD Fix
/usr/src/libc/gen/frexp.c 4.1BSD Fix
Description:
The manual page for frexp(3) states that it ``returns the mantissa
... as a double ... of magnitude less than 1''. In fact, it does
not always do this: if the input is a power of two, the value
returned is equal to 1, not less than 1.
Also, if the input is zero, then the function goes into an infinite
loop.
The first problem can be solved either by changing the manual
page or by fixing the function. The second problem is obviously
a bug.
Repeat-By:
Compile this program:
double frexp();
main()
{
double value;
double mantissa;
int exponent;
while (1) {
scanf("%lf",&value);
mantissa = frexp(value, &exponent);
printf("%f == %f * 2^%d\n", value, mantissa, exponent);
}
}
and run it with the input:
1.0
2.0
0.0
It will print:
1.000000 == 1.000000 * 2^0
2.000000 == 1.000000 * 2^1
and then hang
Fix:
If you elect to fix the function, instead of changing the
manual, the required changes are:
8a9
> if the argument is 0.0, return 0.0 mantissa and 0 exponent
24,25c25,26
< if(x>1.0)
< while(x>1){
---
> if(x>=1.0)
> while(x>=1.0){
29c30
< else if(x<0.5)
---
> else if((x<0.5) && (x != 0.0))
The last change (at line 29) should be applied in any case, to
avoid an infinite loop.
More information about the Comp.bugs.4bsd.ucb-fixes
mailing list