2

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
  • 2
    You could use (expt 8 (/ 1.0 3)). Be sure that you use a float in the fraction... Jan 14, 2022 at 20:06

1 Answer 1

4

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.

1
  • 1
    In 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
    Jan 15, 2022 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.