Skip to content

Commit 7a268e5

Browse files
committed
Add cube root (cbrt) to Math
1 parent fa97453 commit 7a268e5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/js.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ extern {
294294
#[wasm_bindgen(static_method_of = Math)]
295295
pub fn atanh(x: i32) -> Number;
296296

297+
298+
/// The Math.cbrt() function returns the cube root of a number, that is
299+
/// Math.cbrt(x) = x^3 = the unique y such that y^3 = x
300+
///
301+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt
302+
#[wasm_bindgen(static_method_of = Math)]
303+
pub fn cbrt(x: i32) -> Number;
304+
297305
}
298306

299307
// Number.

tests/all/js_globals/Math.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,29 @@ fn atanh() {
212212
"#)
213213
.test()
214214
}
215+
216+
#[test]
217+
fn cbrt() {
218+
project()
219+
.file("src/lib.rs", r#"
220+
#![feature(proc_macro, wasm_custom_section)]
221+
222+
extern crate wasm_bindgen;
223+
use wasm_bindgen::prelude::*;
224+
use wasm_bindgen::js;
225+
226+
#[wasm_bindgen]
227+
pub fn cbrt(x: i32) -> js::Number {
228+
js::Math::cbrt(x)
229+
}
230+
"#)
231+
.file("test.ts", r#"
232+
import * as assert from "assert";
233+
import * as wasm from "./out";
234+
235+
export function test() {
236+
assert.equal(wasm.cbrt(27), 3);
237+
}
238+
"#)
239+
.test()
240+
}

0 commit comments

Comments
 (0)