Page 1 of 1

pow function problem

Posted: Mon Oct 19, 2020 9:11 am
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?

Re: pow function problem

Posted: Mon Oct 19, 2020 3:24 pm
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.

Re: pow function problem

Posted: Mon Oct 19, 2020 7:25 pm
by cameyo
Thanks for the explanation