Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 7/1/84; site CSL-Vax.ARPA
Path: utzoo!linus!decvax!decwrl!CSL-Vax!mogul
From: mogul@CSL-Vax.ARPA (Jeff Mogul)
Newsgroups: net.bugs.4bsd
Subject: frexp(3) does not do what the manual page says it does (4.[12]BSD)
Message-ID: <2853@CSL-Vax.ARPA>
Date: Thu, 16-Aug-84 14:12:35 EDT
Article-I.D.: CSL-Vax.2853
Posted: Thu Aug 16 14:12:35 1984
Date-Received: Wed, 22-Aug-84 03:44:59 EDT
Distribution: net
Organization: Stanford University
Lines: 60

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.