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

Use correct exponential representation for rational values #493

Merged
merged 7 commits into from
Jul 16, 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
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ rustc-hash = "1.1.0"
num-bigint = { version = "0.3.0", features = ["serde"] }
num-integer = "0.1.43"
bitflags = "1.2.1"
ryu-js = "0.2.0"

# Optional Dependencies
serde = { version = "1.0.114", features = ["derive"], optional = true }
Expand Down
16 changes: 6 additions & 10 deletions boa/src/builtins/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,15 @@ impl Display for Value {
None => write!(f, "Symbol()"),
},
Self::String(ref v) => write!(f, "{}", v),
Self::Rational(v) => write!(
f,
"{}",
match v {
_ if v.is_nan() => "NaN".to_string(),
_ if v.is_infinite() && v.is_sign_negative() => "-Infinity".to_string(),
_ if v.is_infinite() => "Infinity".to_string(),
_ => v.to_string(),
}
),
Self::Rational(v) => format_rational(*v, f),
Self::Object(_) => write!(f, "{}", log_string_from(self, true)),
Self::Integer(v) => write!(f, "{}", v),
Self::BigInt(ref num) => write!(f, "{}n", num),
}
}
}

fn format_rational(v: f64, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buffer = ryu_js::Buffer::new();
write!(f, "{}", buffer.format(v))
}
34 changes: 34 additions & 0 deletions boa/src/builtins/value/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ fn get_types() {
);
}

#[test]
fn to_string() {
let f64_to_str = |f| Value::Rational(f).to_string();

assert_eq!(f64_to_str(f64::NAN), "NaN");
assert_eq!(f64_to_str(0.0), "0");
assert_eq!(f64_to_str(f64::INFINITY), "Infinity");
assert_eq!(f64_to_str(f64::NEG_INFINITY), "-Infinity");
assert_eq!(f64_to_str(90.12), "90.12");
assert_eq!(f64_to_str(111111111111111111111.0), "111111111111111110000");
assert_eq!(
f64_to_str(1111111111111111111111.0),
"1.1111111111111111e+21"
);

assert_eq!(f64_to_str(-90.12), "-90.12");

assert_eq!(
f64_to_str(-111111111111111111111.0),
"-111111111111111110000"
);
assert_eq!(
f64_to_str(-1111111111111111111111.0),
"-1.1111111111111111e+21"
);

assert_eq!(f64_to_str(0.0000001), "1e-7");
assert_eq!(f64_to_str(0.000001), "0.000001");
assert_eq!(f64_to_str(0.0000002), "2e-7");
assert_eq!(f64_to_str(-0.0000001), "-1e-7");

assert_eq!(f64_to_str(3e50), "3e+50");
}

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