cljs.math/scalb
function | since v1.11.50 | Edit |
(scalb d scaleFactor)
Source docstring:
Returns d * 2^scaleFactor, scaling by a factor of 2. If the exponent
is between min_Float64_exponent and max_Float64_exponent.
scaleFactor is an integer
If d is ##NaN => ##NaN
If d is ##Inf or ##-Inf => ##Inf or ##-Inf respectively
If d is zero => zero of same sign as d
See: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#nextDown-double-
(defn ^number scalb
{:added "1.11.10"}
[d scaleFactor]
(let [[scale-factor
scale-increment
exp-delta] (if (< scaleFactor 0)
[(Math/max scaleFactor (- MAX_SCALE)) -512 two-to-the-double-scale-down]
[(Math/min scaleFactor MAX_SCALE) 512 two-to-the-double-scale-up])
t (unsigned-bit-shift-right (bit-shift-right scale-factor 8) 23)
exp-adjust (- (bit-and (+ scale-factor t) 511) t)]
(loop [d (* d (power-of-two exp-adjust)) scale-factor (- scale-factor exp-adjust)]
(if (zero? scale-factor)
d
(recur (* d exp-delta) (- scale-factor scale-increment))))))