Skip to content

Implement Math.min() and Math.max() bindings #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,18 @@ extern "C" {
#[wasm_bindgen(static_method_of = Math)]
pub fn log2(x: f64) -> f64;

/// The Math.max() function returns the largest of two numbers.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
#[wasm_bindgen(static_method_of = Math)]
pub fn max(x: f64, y: f64) -> f64;

/// The static function Math.min() returns the lowest-valued number passed into it.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
#[wasm_bindgen(static_method_of = Math)]
pub fn min(x: f64, y: f64) -> f64;

/// The Math.pow() function returns the base to the exponent power, that is, base^exponent.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
Expand Down
18 changes: 18 additions & 0 deletions crates/js-sys/tests/wasm/Math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ fn log2() {
assert_eq!(Math::log2(0.), NEG_INFINITY);
}

#[wasm_bindgen_test]
fn max() {
assert_eq!(Math::max(3., 1.), 3.);
assert_eq!(Math::max(-3., 1.), 1.);
assert_eq!(Math::max(9913., 43.4), 9913.);
assert_eq!(Math::max(-27., -43.), -27.);
assert_eq!(Math::max(-423.27, -43.1), -43.1);
}

#[wasm_bindgen_test]
fn min() {
assert_eq!(Math::min(3., 1.), 1.);
assert_eq!(Math::min(-3., 1.), -3.);
assert_eq!(Math::min(9913., 43.4), 43.4);
assert_eq!(Math::min(-27., -43.), -43.);
assert_eq!(Math::min(-423.27, -43.1), -423.27);
}

#[wasm_bindgen_test]
fn pow() {
assert_eq!(Math::pow(7., 2.), 49.);
Expand Down