pow function problem

Q&A's, tips, howto's
Locked
cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

pow function problem

Post by cameyo »

I have some problems with the pow function:

Code: Select all

(pow 3 0.33)
;-> 1.436977652184852
(pow -3 0.33)
;-> 1.#IND
In Mathematica (WolframAlpha):

Code: Select all

 3^0.33 = 1.436977652184852
-3^0.33 = -1.436977652184852
A simple solution:

Code: Select all

(define (pow-ext x n)
  (if (< x 0)
      (sub 0 (pow (sub 0 x) n))
      (pow x n)))
(pow-ext 3 0.33)
;-> 1.436977652184852
;(pow-ext -3 0.33)
;-> -1.436977652184852
Why newLISP result is 1.#IND?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: pow function problem

Post by Lutz »

The newLISP function pow works like the Perl and Python function pow:

Code: Select all

>>> pow(3, 0.33)
1.4369776521848516
>>> pow(-3, 0.33)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> 
All use the Standard C Library function pow(a,b) using double floats.

cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: pow function problem

Post by cameyo »

Thanks for the explanation

Locked