Commit 926a0666 authored by Attila Lendvai's avatar Attila Lendvai

numbers.lisp/lerp: The correct version is numerically stable,

at the expense of an extra multiply. The unstable version can often be
converted to a fast instruction on a lot of machines, though this is
machine/implementation specific. As alexandria is more about correct code,
than efficiency, and we're only talking about a single extra multiply,
many would prefer the stable version.

Patch by github.com/mfiano.
parent afaf1a16
......@@ -90,7 +90,14 @@ Examples:
(defun lerp (v a b)
"Returns the result of linear interpolation between A and B, using the
interpolation coefficient V."
(+ a (* v (- b a))))
;; The correct version is numerically stable, at the expense of an
;; extra multiply. See (lerp 0.1 4 25) with (+ a (* v (- b a))). The
;; unstable version can often be converted to a fast instruction on
;; a lot of machines, though this is machine/implementation
;; specific. As alexandria is more about correct code, than
;; efficiency, and we're only talking about a single extra multiply,
;; many would prefer the stable version
(+ (* (- 1.0 v) a) (* v b)))
(declaim (inline mean))
(defun mean (sample)
......
......@@ -989,6 +989,10 @@
(lerp 0.1 1 2)
1.1)
(deftest lerp.3
(lerp 0.1 4 25)
6.1)
(deftest mean.1
(mean '(1 2 3))
2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment