Skip to content
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

Add methods with f64 std equivelant to Math object #260

Merged
merged 3 commits into from
Mar 18, 2020
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
139 changes: 136 additions & 3 deletions boa/src/builtins/math.rs → boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use crate::{
use rand::random;
use std::f64;

#[cfg(test)]
mod tests;

/// Get the absolute value of a number
pub fn abs(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -28,6 +31,16 @@ pub fn acos(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.acos()
}))
}
/// Get the hyperbolic arccos of a number
pub fn acosh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.acosh()
}))
}
/// Get the arcsine of a number
pub fn asin(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -38,6 +51,16 @@ pub fn asin(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.asin()
}))
}
/// Get the hyperbolic arcsine of a number
pub fn asinh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.asinh()
}))
}
/// Get the arctangent of a number
pub fn atan(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -48,6 +71,16 @@ pub fn atan(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.atan()
}))
}
/// Get the hyperbolic arctangent of a number
pub fn atanh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.atanh()
}))
}
/// Get the arctangent of a numbers
pub fn atan2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand Down Expand Up @@ -88,6 +121,16 @@ pub fn cos(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.cos()
}))
}
/// Get the hyperbolic cosine of a number
pub fn cosh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.cosh()
}))
}
/// Get the power to raise the natural logarithm to get the number
pub fn exp(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -113,9 +156,44 @@ pub fn log(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.log(f64::consts::E)
let value = from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64");

if value <= 0.0 {
f64::NAN
} else {
value.log(f64::consts::E)
}
}))
}
/// Get the base 10 logarithm of the number
pub fn log10(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
let value = from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64");

if value <= 0.0 {
f64::NAN
} else {
value.log10()
}
}))
}
/// Get the base 2 logarithm of the number
pub fn log2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
let value = from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64");

if value <= 0.0 {
f64::NAN
} else {
value.log2()
}
}))
}
/// Get the maximum of several numbers
Expand Down Expand Up @@ -162,6 +240,21 @@ pub fn round(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.round()
}))
}
/// Get the sign of a number
pub fn sign(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
let value = from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64");

if value == 0.0 || value == -0.0 {
value
} else {
value.signum()
}
}))
}
/// Get the sine of a number
pub fn sin(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -172,6 +265,16 @@ pub fn sin(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.sin()
}))
}
/// Get the hyperbolic sine of a number
pub fn sinh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.sinh()
}))
}
/// Get the square root of a number
pub fn sqrt(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
Expand All @@ -192,6 +295,26 @@ pub fn tan(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
.tan()
}))
}
/// Get the hyperbolic tangent of a number
pub fn tanh(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.tanh()
}))
}
/// Get the integer part of a number
pub fn trunc(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(to_value(if args.is_empty() {
f64::NAN
} else {
from_value::<f64>(args.get(0).expect("Could not get argument").clone())
.expect("Could not convert argument to f64")
.trunc()
}))
}
/// Create a new `Math` object
pub fn create_constructor(global: &Value) -> Value {
let math = ValueData::new_obj(Some(global));
Expand All @@ -205,22 +328,32 @@ pub fn create_constructor(global: &Value) -> Value {
math.set_field_slice("PI", to_value(f64::consts::PI));
make_builtin_fn!(abs, named "abs", with length 1, of math);
make_builtin_fn!(acos, named "acos", with length 1, of math);
make_builtin_fn!(acosh, named "acosh", with length 1, of math);
make_builtin_fn!(asin, named "asin", with length 1, of math);
make_builtin_fn!(asinh, named "asinh", with length 1, of math);
make_builtin_fn!(atan, named "atan", with length 1, of math);
make_builtin_fn!(atanh, named "atanh", with length 1, of math);
make_builtin_fn!(atan2, named "atan2", with length 2, of math);
make_builtin_fn!(cbrt, named "cbrt", with length 1, of math);
make_builtin_fn!(ceil, named "ceil", with length 1, of math);
make_builtin_fn!(cos, named "cos", with length 1, of math);
make_builtin_fn!(cosh, named "cosh", with length 1, of math);
make_builtin_fn!(exp, named "exp", with length 1, of math);
make_builtin_fn!(floor, named "floor", with length 1, of math);
make_builtin_fn!(log, named "log", with length 1, of math);
make_builtin_fn!(log10, named "log10", with length 1, of math);
make_builtin_fn!(log2, named "log2", with length 1, of math);
make_builtin_fn!(max, named "max", with length 2, of math);
make_builtin_fn!(min, named "min", with length 2, of math);
make_builtin_fn!(pow, named "pow", with length 2, of math);
make_builtin_fn!(_random, named "random", of math);
make_builtin_fn!(round, named "round", with length 1, of math);
make_builtin_fn!(sign, named "sign", with length 1, of math);
make_builtin_fn!(sin, named "sin", with length 1, of math);
make_builtin_fn!(sinh, named "sinh", with length 1, of math);
make_builtin_fn!(sqrt, named "sqrt", with length 1, of math);
make_builtin_fn!(tan, named "tan", with length 1, of math);
make_builtin_fn!(tanh, named "tanh", with length 1, of math);
make_builtin_fn!(trunc, named "trunc", with length 1, of math);
math
}
Loading