Skip to content

Commit

Permalink
[boa-dev#524] implement Math.log1p()
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-rodgers committed Jun 25, 2020
1 parent c061b69 commit c6642c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions boa/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ impl Math {
.into())
}

/// Get approximation to the natural logarithm of 1 + x.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-math.log1p
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p
pub(crate) fn log1p(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
Ok(args
.get(0)
.map_or(f64::NAN, |x| f64::from(x).ln_1p())
.into())
}

/// Get the base 10 logarithm of the number.
///
/// More information:
Expand Down Expand Up @@ -560,6 +575,7 @@ impl Math {
make_builtin_fn(Self::fround, "fround", &math, 1);
make_builtin_fn(Self::hypot, "hypot", &math, 1);
make_builtin_fn(Self::log, "log", &math, 1);
make_builtin_fn(Self::log1p, "log1p", &math, 1);
make_builtin_fn(Self::log10, "log10", &math, 1);
make_builtin_fn(Self::log2, "log2", &math, 1);
make_builtin_fn(Self::max, "max", &math, 2);
Expand Down
32 changes: 31 additions & 1 deletion boa/src/builtins/math/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ fn hypot() {
var c = Math.hypot(5, 12);
var d = Math.hypot(3, 4, -5);
var e = Math.hypot(4, [5], 6);
var f = Math.hypot(3, Infinity);
var f = Math.hypot(3, -Infinity);
"#;

eprintln!("{}", forward(&mut engine, init));
Expand Down Expand Up @@ -411,6 +411,36 @@ fn log() {
assert_eq!(c, String::from("NaN"));
}

#[test]
fn log1p() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let init = r#"
var a = Math.log1p(1);
var b = Math.log1p(0);
var c = Math.log1p(-0.9999999999999999);
var d = Math.log1p(-1);
var e = Math.log1p(-1.000000000000001);
var f = Math.log1p(-2);
"#;

eprintln!("{}", forward(&mut engine, init));

let a = forward_val(&mut engine, "a").unwrap();
let b = forward_val(&mut engine, "b").unwrap();
let c = forward_val(&mut engine, "c").unwrap();
let d = forward(&mut engine, "d");
let e = forward(&mut engine, "e");
let f = forward(&mut engine, "f");

assert_eq!(a.to_number(), f64::consts::LN_2);
assert_eq!(b.to_number(), 0f64);
assert_eq!(c.to_number(), -36.736_800_569_677_1);
assert_eq!(d, "-Infinity");
assert_eq!(e, String::from("NaN"));
assert_eq!(f, String::from("NaN"));
}

#[test]
fn log10() {
let realm = Realm::create();
Expand Down

0 comments on commit c6642c0

Please sign in to comment.