mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
inf -> Infinity
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// test_output! [["0e+0","1.2e+0","1.2e+1","1.2e+2","1.2e-1","1.2e-2","1.2e-3","1.2e-4"],["0.00e+0","1.23e+0","1.23e+1","1.23e-1","1.23e-2","1.23e-3","1.23e-4"],["NaN"]]
|
||||
// test_output! [["0e+0","1.2e+0","1.2e+1","1.2e+2","1.2e-1","1.2e-2","1.2e-3","1.2e-4"],["0.00e+0","1.23e+0","1.23e+1","1.23e-1","1.23e-2","1.23e-3","1.23e-4"],["NaN","Infinity"]]
|
||||
|
||||
export default function () {
|
||||
const withoutPrecision = [
|
||||
@@ -27,7 +27,7 @@ export default function () {
|
||||
|
||||
const negative = [
|
||||
(NaN).toExponential(),
|
||||
// (Infinity).toExponential(), TODO: Fix "inf", should be "Infinity"
|
||||
(Infinity).toExponential(),
|
||||
];
|
||||
|
||||
return [withoutPrecision, withPrecision, negative];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// test_output! [["0.00","1.23","123.00","0.01","0.00123","12345.68","3.14","-1.00"],["NaN"]]
|
||||
// test_output! [["0.00","1.23","123.00","0.01","0.00123","12345.68","3.14","-1.00"],["NaN","Infinity"]]
|
||||
|
||||
export default function () {
|
||||
const positive = [
|
||||
@@ -14,7 +14,7 @@ export default function () {
|
||||
|
||||
const negative = [
|
||||
(NaN).toFixed(2),
|
||||
// (Infinity).toFixed(2), TODO: Fix "inf", should be "Infinity"
|
||||
(Infinity).toFixed(2),
|
||||
];
|
||||
|
||||
return [positive, negative];
|
||||
|
||||
@@ -499,11 +499,11 @@ impl std::fmt::Display for Value {
|
||||
Value::Null => write!(f, "null"),
|
||||
Value::Bool(value) => write!(f, "{}", value),
|
||||
Value::Number(value) => {
|
||||
if f64::is_infinite(*value) {
|
||||
if *value < 0.0 {
|
||||
write!(f, "-Infinity")
|
||||
} else {
|
||||
if value.is_infinite() {
|
||||
if value.is_sign_positive() {
|
||||
write!(f, "Infinity")
|
||||
} else {
|
||||
write!(f, "-Infinity")
|
||||
}
|
||||
} else {
|
||||
write!(f, "{}", value)
|
||||
|
||||
@@ -22,6 +22,14 @@ static TO_FIXED: NativeFunction = NativeFunction {
|
||||
fn_: |this: &mut Val, params: Vec<Val>| -> Val {
|
||||
match this {
|
||||
Val::Number(number) => {
|
||||
if number.is_infinite() {
|
||||
return if number.is_sign_positive() {
|
||||
Val::String(Rc::new("Infinity".to_string()))
|
||||
} else {
|
||||
Val::String(Rc::new("-Infinity".to_string()))
|
||||
};
|
||||
}
|
||||
|
||||
let mut precision = match params.get(0) {
|
||||
Some(p) => p.to_number(),
|
||||
_ => return Val::String(Rc::new(this.val_to_string())),
|
||||
@@ -94,6 +102,14 @@ static VALUE_OF: NativeFunction = NativeFunction {
|
||||
};
|
||||
|
||||
fn format_exponential(number: f64, precision: Option<usize>) -> Val {
|
||||
if number.is_infinite() {
|
||||
return if number.is_sign_positive() {
|
||||
Val::String(Rc::new("Infinity".to_string()))
|
||||
} else {
|
||||
Val::String(Rc::new("-Infinity".to_string()))
|
||||
};
|
||||
}
|
||||
|
||||
let exp_format = match precision {
|
||||
Some(p) => format!("{:.*e}", p, number),
|
||||
None => format!("{:e}", number),
|
||||
|
||||
@@ -96,7 +96,17 @@ impl ValTrait for Val {
|
||||
Undefined => "undefined".to_string(),
|
||||
Null => "null".to_string(),
|
||||
Bool(b) => b.to_string(),
|
||||
Number(x) => x.to_string(), // TODO: Match js's number string format
|
||||
Number(x) => {
|
||||
if x.is_infinite() {
|
||||
if x.is_sign_positive() {
|
||||
"Infinity".to_string()
|
||||
} else {
|
||||
"-Infinity".to_string()
|
||||
}
|
||||
} else {
|
||||
x.to_string()
|
||||
}
|
||||
} // TODO: Match js's number string format
|
||||
String(s) => s.to_string(),
|
||||
Array(vals) => {
|
||||
if vals.elements.len() == 0 {
|
||||
|
||||
Reference in New Issue
Block a user