www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000236

When Created: 07/10/1998 15:25:50
Against DJGPP version: 2.01
By whom: stotesberry@usa.net
Abstract: pow (x,y) does not calculate correctly
In the following example when compiled 5^2=24 and 10^2=99. Also doesn't compute many other powers, just a small example.

Todd

--------------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

int main (void) {
	
	int int1 = 5;
	int int2 = 10;
	int power = 2;
	printf ("       input          input^2\n") ;
	while  (int1 <= int2) {
		printf ("%11d%14d\n",int1, (int) pow (int1,power)) ;
		int1++ ;
                }
	return 0 ;
}

--------------------------------------------------------------------------------

Note added: 07/13/1998 13:40:03
By whom: broeker@physik.rwth-aachen.de
The calculation of pow() is correct, i.e. there is not really
a bug, here. It's just that improper rounding of a complicated
floating point result like pow(5.0,2.0) in the default implementation
as exp(2.0*log(5.0)) sometimes returns unexpected results.

Changing the printf() to print '(int)(pow(int1,power) + 0.5)'
would fix that by use of somewhat more correct rounding.

Note added: 07/27/1998 09:57:53
By whom: rudd@cyberoptics.com
I am in the process of upgrading all the math functions for DJGPP; for pow() I
was considering a test for integer exponents, which would invoke an optimized
series of multiplications instead of the exp(y*log(x)) method. Unfortunately,
this still wouldn't put an end to complaints such as this one, since still one
can have pow(x, -1.)*x != 1. for x not a power of 2. One shouldn't, in general,
expect exact results from floating point computations.

Closed on 10/13/1998 17:00:59: Non-bug
By whom: broeker@mail.desy.de



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010