Skip to content

Commit

Permalink
Add ieee_float.square_root()
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Jun 16, 2024
1 parent 1aca621 commit 3e3beea
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/ieee_float.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@ pub fn round(f: IEEEFloat) -> Result(Int, Nil) {
}
}

/// Returns the square root of an `IEEEFloat`.
///
@external(javascript, "./ieee_float_js.mjs", "square_root")
pub fn square_root(f: IEEEFloat) -> IEEEFloat {
power(f, Finite(0.5))
}

/// Subtracts one `IEEEFloat` from another.
///
@external(javascript, "./ieee_float_js.mjs", "subtract")
Expand Down
4 changes: 4 additions & 0 deletions src/ieee_float_js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,8 @@ export function subtract(a, b) {
return a - b;
}

export function square_root(f) {
return Math.sqrt(f)
}

export function rescue_bad_arith() {}
12 changes: 12 additions & 0 deletions test/ieee_float_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,18 @@ pub fn round_test() {
})
}

pub fn square_root_test() {
let test_square_root = build_test_function1(ieee_float.square_root)

test_square_root(finite(0.0), finite(0.0))
test_square_root(finite(1.0), finite(1.0))
test_square_root(finite(16.0), finite(4.0))
test_square_root(finite(-1.0), nan())
test_square_root(positive_infinity(), positive_infinity())
test_square_root(negative_infinity(), positive_infinity())
test_square_root(nan(), nan())
}

pub fn subtract_test() {
let test_subtract = build_test_function2(ieee_float.subtract)

Expand Down

0 comments on commit 3e3beea

Please sign in to comment.