www.digitalmars.com         C & C++   DMDScript  

c++ - powl bug

reply "Steve Hall" <sthall lorrexinc.com> writes:
The following code produce wrong result:

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

int main()
{
 long double a = powl(-0.0397739, 310.954);
 long double b = -5.12198e11;

 printf("(%Lg)^(%Lg) = %Lg", a, b, powl(a, b));
 return 0;
}

The used compiler is Digital Mars Compiler v8.29n.
Apr 21 2003
parent reply Larry Brasfield <larry_brasfield snotmail.com> writes:
In article <b81iu3$2smm$1 digitaldaemon.com>, 
Steve Hall (sthall lorrexinc.com) says...
 The following code produce wrong result:
 
 #include <math.h>
 #include <stdio.h>
 
 int main()
 {
  long double a = powl(-0.0397739, 310.954);
  long double b = -5.12198e11;
 
  printf("(%Lg)^(%Lg) = %Lg", a, b, powl(a, b));
  return 0;
 }

What do believe is the "right" result? According to the math I learned, there is no meaningful way to raise negative numbers to fractional powers. -- -Larry Brasfield (address munged, s/sn/h/ to reply)
Apr 22 2003
parent "Steve Hall" <sthall lorrexinc.com> writes:
I agree with you, but according to C99, the math and lcc-win32 v3.8 the
result of the following code should be: (nan)^(3.5) = nan.

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

int main()
{
 long double a = powl(-1, 0.5);
 long double b = 3.5;

 printf("(%Lg)^(%Lg) = %Lg", a, b, powl(a, b));
 return 0;
}

The result from dmc v8.29n is: (nan)^(3.5) = 3.36128e-4932.
The software that I develop depend on precision C99 floating-point
arithmetic.

"Larry Brasfield" <larry_brasfield snotmail.com> wrote in message
news:MPG.190ea07eab0c3d7e9896a4 news.digitalmars.com...
 In article <b81iu3$2smm$1 digitaldaemon.com>,
 Steve Hall (sthall lorrexinc.com) says...
 The following code produce wrong result:

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

 int main()
 {
  long double a = powl(-0.0397739, 310.954);
  long double b = -5.12198e11;

  printf("(%Lg)^(%Lg) = %Lg", a, b, powl(a, b));
  return 0;
 }

What do believe is the "right" result? According to the math I learned, there is no meaningful way to raise negative numbers to fractional powers. -- -Larry Brasfield (address munged, s/sn/h/ to reply)

Apr 22 2003