help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Floating Point computations in elisp


From: Emanuel Berg
Subject: Re: Floating Point computations in elisp
Date: Fri, 18 Dec 2020 18:08:10 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> How can I do floating point computations in elisp
>
> (setq frc (/ tpd 60.0))

As you did, just add a .0 to avoid integer division.

Other than that, just compute whatever you like.

(defun mean-value (vs)
  (let*((sum  (apply #'+ vs))
        (mean (/ sum (length vs) 1.0)) )
    mean) )

(defun hypotenuse (c1 c2)
  (sqrt (+ (* c1 c1) (* c2 c2))) )

(defun speed (hour minute second km)
  (let*((s (+ second (* 60 minute) (* 60 60 hour)))
        (m (* 1000 km))
        (mps (/ m s)) )
    (* 3.6 mps) ))

Hm ... what does that do?

Anyway, you can then use `format' to - that's right - format
the output. It is very versatile, see the docstring.

(defun compute-space-distance (dist num-beams beam-width)
  (let*((spaces         (1+ num-beams))
        (dist-covered   (* num-beams beam-width))
        (dist-uncovered (- dist dist-covered))
        (space          (/ dist-uncovered spaces 1.0) ))
    space ))

;; (insert (format "\n;; %.2f" (compute-space-distance 262 16 12)))
;; 4.12

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]