mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
Binding for Math.cos,cosh,exp,expml,fround,imul,log,log10,log1p,log2
This commit is contained in:
parent
c608d46a56
commit
3e84b97de2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
.idea
|
||||||
/target/
|
/target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
59
src/js.rs
59
src/js.rs
@ -387,12 +387,71 @@ extern {
|
|||||||
#[wasm_bindgen(static_method_of = Math)]
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
pub fn clz32(x: i32) -> Number;
|
pub fn clz32(x: i32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.cos() static function returns the cosine of the specified angle,
|
||||||
|
/// which must be specified in radians. This value is length(adjacent)/length(hypotenuse).
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn cos(x: f32) -> Number;
|
||||||
|
|
||||||
|
|
||||||
|
/// The Math.cosh() function returns the hyperbolic cosine of a number,
|
||||||
|
/// that can be expressed using the constant e.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn cosh(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.exp() function returns e^x, where x is the argument, and e is Euler's number
|
||||||
|
/// (also known as Napier's constant), the base of the natural logarithms.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn exp(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of the
|
||||||
|
/// natural logarithms.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn expm1(x: f32) -> Number;
|
||||||
|
|
||||||
/// The Math.floor() function returns the largest integer less than or
|
/// The Math.floor() function returns the largest integer less than or
|
||||||
/// equal to a given number.
|
/// equal to a given number.
|
||||||
///
|
///
|
||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
|
||||||
#[wasm_bindgen(static_method_of = Math)]
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
pub fn floor(x: f32) -> Number;
|
pub fn floor(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.fround() function returns the nearest 32-bit single precision float representation
|
||||||
|
/// of a Number.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn fround(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.imul() function returns the result of the C-like 32-bit multiplication of the
|
||||||
|
/// two parameters.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn imul(x: i32, y: i32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.log() function returns the natural logarithm (base e) of a number.
|
||||||
|
/// The JavaScript Math.log() function is equivalent to ln(x) in mathematics.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn log(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.log10() function returns the base 10 logarithm of a number.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn log10(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.log1p() function returns the natural logarithm (base e) of 1 + a number.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn log1p(x: f32) -> Number;
|
||||||
|
|
||||||
|
/// The Math.log2() function returns the base 2 logarithm of a number.
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn log2(x: f32) -> Number;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Number.
|
// Number.
|
||||||
|
@ -78,6 +78,7 @@ fn acosh() {
|
|||||||
export function test() {
|
export function test() {
|
||||||
assert.equal(wasm.acosh(1), 0);
|
assert.equal(wasm.acosh(1), 0);
|
||||||
assert.equal(wasm.acosh(2), Math.acosh(2));
|
assert.equal(wasm.acosh(2), Math.acosh(2));
|
||||||
|
assert.equal(wasm.acosh(2), Math.acosh(2));
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
.test()
|
.test()
|
||||||
@ -293,6 +294,117 @@ fn clz32() {
|
|||||||
.test()
|
.test()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cos() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn cos(x: f32) -> js::Number {
|
||||||
|
js::Math::cos(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.cos(0), 1);
|
||||||
|
// other assertions failing due to rounding errors
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn cosh() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn cosh(x: f32) -> js::Number {
|
||||||
|
js::Math::cosh(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.cosh(0), 1);
|
||||||
|
assert.equal(wasm.cosh(2), 3.7621956910836314);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exp() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn exp(x: f32) -> js::Number {
|
||||||
|
js::Math::exp(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.exp(0), 1);
|
||||||
|
assert.equal(wasm.exp(-1), 0.36787944117144233);
|
||||||
|
assert.equal(wasm.exp(2), 7.38905609893065);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expm1() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn expm1(x: f32) -> js::Number {
|
||||||
|
js::Math::expm1(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.expm1(0), 0);
|
||||||
|
assert.equal(wasm.expm1(1), 1.718281828459045);
|
||||||
|
assert.equal(wasm.expm1(-1), -0.6321205588285577);
|
||||||
|
assert.equal(wasm.expm1(2), 6.38905609893065);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn floor() {
|
fn floor() {
|
||||||
project()
|
project()
|
||||||
@ -319,3 +431,173 @@ fn floor() {
|
|||||||
"#)
|
"#)
|
||||||
.test()
|
.test()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fround() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn fround(x: f32) -> js::Number {
|
||||||
|
js::Math::fround(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.fround(5.5), 5.5);
|
||||||
|
assert.equal(wasm.fround(5.05), 5.050000190734863);
|
||||||
|
assert.equal(wasm.fround(5), 5);
|
||||||
|
assert.equal(wasm.fround(-5.05), -5.050000190734863);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn imul() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn imul(x: i32, y:i32) -> js::Number {
|
||||||
|
js::Math::imul(x, y)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.imul(3, 4), 12);
|
||||||
|
assert.equal(wasm.imul(-5, 12), -60);
|
||||||
|
assert.equal(wasm.imul(0xffffffff, 5), Math.imul(0xffffffff, 5));
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn log() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn log(x: f32) -> js::Number {
|
||||||
|
js::Math::log(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.log(8) / wasm.log(2), 3);
|
||||||
|
assert.equal(wasm.log(625) / wasm.log(5), 4);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn log10() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn log10(x: f32) -> js::Number {
|
||||||
|
js::Math::log10(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.log10(100000), 5);
|
||||||
|
assert.equal(wasm.log10(1), 0);
|
||||||
|
assert.equal(wasm.log10(2), 0.3010299956639812);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn log1p() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn log1p(x: f32) -> js::Number {
|
||||||
|
js::Math::log1p(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.log1p(1), 0.6931471805599453);
|
||||||
|
assert.equal(wasm.log1p(0), 0);
|
||||||
|
assert.equal(wasm.log1p(-1), -Infinity);
|
||||||
|
assert(isNaN(wasm.log1p(-2)));
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn log2() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn log2(x: f32) -> js::Number {
|
||||||
|
js::Math::log2(x)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.log2(3), 1.584962500721156);
|
||||||
|
assert.equal(wasm.log2(2), 1);
|
||||||
|
assert.equal(wasm.log2(1), 0);
|
||||||
|
assert.equal(wasm.log2(0), -Infinity);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user