Skip to content

Commit cefe168

Browse files
twilcoalexcrichton
authored andcommitted
Implement Math.min() and Math.max() bindings (#542)
* Add Number.isNaN() binding * Add binding for Math.hypot() * Implement Math.min() and Math.max() bindings
1 parent f437e06 commit cefe168

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

crates/js-sys/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,18 @@ extern "C" {
11911191
#[wasm_bindgen(static_method_of = Math)]
11921192
pub fn log2(x: f64) -> f64;
11931193

1194+
/// The Math.max() function returns the largest of two numbers.
1195+
///
1196+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
1197+
#[wasm_bindgen(static_method_of = Math)]
1198+
pub fn max(x: f64, y: f64) -> f64;
1199+
1200+
/// The static function Math.min() returns the lowest-valued number passed into it.
1201+
///
1202+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
1203+
#[wasm_bindgen(static_method_of = Math)]
1204+
pub fn min(x: f64, y: f64) -> f64;
1205+
11941206
/// The Math.pow() function returns the base to the exponent power, that is, base^exponent.
11951207
///
11961208
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow

crates/js-sys/tests/wasm/Math.rs

+18
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ fn log2() {
172172
assert_eq!(Math::log2(0.), NEG_INFINITY);
173173
}
174174

175+
#[wasm_bindgen_test]
176+
fn max() {
177+
assert_eq!(Math::max(3., 1.), 3.);
178+
assert_eq!(Math::max(-3., 1.), 1.);
179+
assert_eq!(Math::max(9913., 43.4), 9913.);
180+
assert_eq!(Math::max(-27., -43.), -27.);
181+
assert_eq!(Math::max(-423.27, -43.1), -43.1);
182+
}
183+
184+
#[wasm_bindgen_test]
185+
fn min() {
186+
assert_eq!(Math::min(3., 1.), 1.);
187+
assert_eq!(Math::min(-3., 1.), -3.);
188+
assert_eq!(Math::min(9913., 43.4), 43.4);
189+
assert_eq!(Math::min(-27., -43.), -43.);
190+
assert_eq!(Math::min(-423.27, -43.1), -423.27);
191+
}
192+
175193
#[wasm_bindgen_test]
176194
fn pow() {
177195
assert_eq!(Math::pow(7., 2.), 49.);

0 commit comments

Comments
 (0)