Navigation Menu

Skip to content

Commit

Permalink
Introduce Math.frexp method. (#4560)
Browse files Browse the repository at this point in the history
  • Loading branch information
akzhan authored and asterite committed Jun 13, 2017
1 parent 3d5e845 commit 68b1e62
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/std/math_spec.cr
Expand Up @@ -83,6 +83,11 @@ describe "Math" do
Math.scalbln(0.11_f32, 2).should be_close(0.44, 1e-7)
Math.scalbln(0.11, 2).should be_close(0.44, 1e-7)
end

it "frexp" do
Math.frexp(0.2_f32).should eq({0.8_f32, -2})
Math.frexp(0.2).should eq({0.8, -2})
end
end

describe "Logarithms" do
Expand Down
2 changes: 2 additions & 0 deletions src/math/libm.cr
Expand Up @@ -86,6 +86,8 @@ lib LibM
fun erf_f64 = erf(value : Float64) : Float64
fun expm1_f32 = expm1f(value : Float32) : Float32
fun expm1_f64 = expm1(value : Float64) : Float64
fun frexp_f32 = frexpf(value : Float32, exp : Int32*) : Float32
fun frexp_f64 = frexp(value : Float64, exp : Int32*) : Float64
fun gamma_f32 = lgammaf(value : Float32) : Float32
fun gamma_f64 = lgamma(value : Float64) : Float64
fun hypot_f32 = hypotf(value1 : Float32, value2 : Float32) : Float32
Expand Down
17 changes: 17 additions & 0 deletions src/math/math.cr
Expand Up @@ -229,6 +229,23 @@ module Math
scalbln(value.to_f, exp.to_i64)
end

# Decomposes given floating point *value* into a normalized fraction and an integral power of two.
def frexp(value : Float32)
frac = LibM.frexp_f32(value, out exp)
{frac, exp}
end

# ditto
def frexp(value : Float64)
frac = LibM.frexp_f64(value, out exp)
{frac, exp}
end

# ditto
def frexp(value)
frexp(value.to_f)
end

# Computes the next highest power of 2 of *v*.
#
# ```
Expand Down

0 comments on commit 68b1e62

Please sign in to comment.