How do I take cube roots in emacs lisp? I thought of using fractional exponents like $8^{1/3} = 2$ but I didn't know how to write y as a fraction in (expt x y)
1 Answer
There are a couple of ways:
(expt 8 (/ 1.0 3.0)) ⇒ 2.0
Or, if you want symbolic results:
(require 'calc-arith)
(calcFunc-pow 8 (calcFunc-inv 3)) ⇒ (float 2 0)
(calcFunc-pow 8 (calcFunc-inv (list 'x))) ⇒ (^ 8 (/ 1 (x)))
See also the defmath macro described in chapter 17.5.1 Defining New Functions of the Emacs Calc manual.
-
1In the first example it suffices to pass one float so that the output will be a float. In other words,
(/ 1.0 3.0)is equivalent to(/ 1.0 3)and(/ 1 3.0).aadcg– aadcg2022-01-15 07:00:29 +00:00Commented Jan 15, 2022 at 7:00
(expt 8 (/ 1.0 3)). Be sure that you use a float in the fraction...