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

clean up the rest of the math methods #523

Merged
merged 3 commits into from
Jun 24, 2020
Merged
Changes from 1 commit
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
120 changes: 60 additions & 60 deletions boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.atan2
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2
pub(crate) fn atan2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() {
f64::NAN
} else {
f64::from(args.get(0).expect("Could not get argument"))
.atan2(args.get(1).expect("Could not get argument").to_number())
}))
match (args.get(0), args.get(1)) {
(Some(y), Some(x)) => Ok(Value::from(f64::from(y).atan2(f64::from(x)))),
_ => Ok(Value::from(f64::NAN)),
}
n14little marked this conversation as resolved.
Show resolved Hide resolved
}

/// Get the cubic root of a number.
Expand Down Expand Up @@ -226,17 +224,17 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log
pub(crate) fn log(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() {
f64::NAN
} else {
let value = f64::from(args.get(0).expect("Could not get argument"));

if value <= 0.0 {
f64::NAN
} else {
value.log(f64::consts::E)
}
}))
Ok(args
.get(0)
.map_or(f64::NAN, |value| {
let x = f64::from(value);
if x <= 0.0 {
f64::NAN
} else {
x.log(f64::consts::E)
}
})
.into())
}

/// Get the base 10 logarithm of the number.
Expand All @@ -248,17 +246,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log10
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
pub(crate) fn log10(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() {
f64::NAN
} else {
let value = f64::from(args.get(0).expect("Could not get argument"));

if value <= 0.0 {
f64::NAN
} else {
value.log10()
}
}))
Ok(args
.get(0)
.map_or(f64::NAN, |value| {
let x = f64::from(value);

if x <= 0.0 {
f64::NAN
} else {
x.log10()
}
})
.into())
}

/// Get the base 2 logarithm of the number.
Expand All @@ -270,17 +269,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.log2
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2
pub(crate) fn log2(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() {
f64::NAN
} else {
let value = f64::from(args.get(0).expect("Could not get argument"));

if value <= 0.0 {
f64::NAN
} else {
value.log2()
}
}))
Ok(args
.get(0)
.map_or(f64::NAN, |value| {
let x = f64::from(value);

if x <= 0.0 {
f64::NAN
} else {
x.log2()
}
})
.into())
}

/// Get the maximum of several numbers.
Expand Down Expand Up @@ -309,12 +309,12 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.min
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min
pub(crate) fn min(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let mut max = f64::INFINITY;
let mut min = f64::INFINITY;
for arg in args {
let num = f64::from(arg);
max = max.min(num);
min = min.min(num);
}
Ok(Value::from(max))
Ok(Value::from(min))
}

/// Raise a number to a power.
Expand All @@ -326,13 +326,12 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.pow
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow
pub(crate) fn pow(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.len() >= 2 {
let num = f64::from(args.get(0).expect("Could not get argument"));
let power = f64::from(args.get(1).expect("Could not get argument"));
num.powf(power)
} else {
f64::NAN
}))
match (args.get(0), args.get(1)) {
(Some(base), Some(exponent)) => {
Ok(Value::from(f64::from(base).powf(f64::from(exponent))))
}
_ => Ok(Value::from(f64::NAN)),
}
n14little marked this conversation as resolved.
Show resolved Hide resolved
}

/// Generate a random floating-point number between `0` and `1`.
Expand Down Expand Up @@ -371,17 +370,18 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.sign
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
pub(crate) fn sign(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(Value::from(if args.is_empty() {
f64::NAN
} else {
let value = f64::from(args.get(0).expect("Could not get argument"));

if value == 0.0 || value == -0.0 {
value
} else {
value.signum()
}
}))
Ok(args
.get(0)
.map_or(f64::NAN, |value| {
let x = f64::from(value);

if x == 0.0 || x == -0.0 {
x
} else {
x.signum()
}
})
.into())
}

/// Get the sine of a number.
Expand Down