[][src]Function nikisas::pow2

pub fn pow2(p: f32) -> f32

Computes 2 raised to a power.

Notes

The input domain is limited to approximately [log2(min(positive f32)), log2(max(f32))] ≈ [-126.0, 127.9] due to limits of machine representation.

Examples

use nikisas::pow2;
assert_eq!(pow2(-1.0), 0.5);

Implementation details

First, the special case when x is near zero is handled such that the result is simply 1. Otherwise, the input x is reduced to an integer k and real y such that

  x = k + y and |y| ≤ 1/2

Let us denote z = |y|. Approximation of 2^z is done using polynomial in the form:

  2^z ≈ 1 + z * P(z)

The "prefix" corresponds to coefficients of low-degree Taylor polynomial of 2^z for z = 0 and P is found using special minimax algorithm in Sollya.

Now we have

  2^y = if y ≥ 0 then 2^z else 1 / 2^z

The reconstruction of original value is then

2^x = 2^(k + y) = 2^k * 2^y

Computation of 2^y is (transitively) done using aforementioned polynomial approximation and multiplying by 2^k can be implemented exactly using bit manipulation of floating point number representation.