Skip to content

Commit

Permalink
remove negative zero
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Nov 23, 2023
1 parent 4f001f4 commit 3a6edcb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ num-traits = { version = "0.2.17", default-features = false }
num-derive = { version = "0.4.1", default-features = false }
bin-pool = { version = "0.1.1", default-features = false }
rand = { version = "0.8.5", default-features = false }
checked-float = { version = "0.1.4", default-features = false, features = ["serde"] }
checked-float = { version = "0.1.5", default-features = false, features = ["serde"] }
educe = { version = "0.4.23", default-features = false, features = ["Debug", "Clone", "Copy", "PartialOrd", "Ord", "PartialEq", "Eq", "Default"] }
libm = { version = "0.2.8", default-features = false }
monostate = { version = "0.1.9", default-features = false }
Expand Down
12 changes: 6 additions & 6 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ pub enum NumberError {
pub struct NumberChecker;
impl FloatChecker<f64> for NumberChecker {
type Error = NumberError;
fn check(value: f64) -> Result<(), Self::Error> {
fn check(value: f64) -> Result<f64, Self::Error> {
if value.is_nan() { return Err(NumberError::Nan); }
if value.is_infinite() { return Err(NumberError::Infinity); }
Ok(())
Ok(if value.to_bits() == 0x8000000000000000 { 0.0 } else { value }) // we don't support signed zero - only useful in conjunction with infinity
}
}

Expand Down Expand Up @@ -699,7 +699,7 @@ impl SimpleValue {
pub fn into_json<C: CustomTypes<S>, S: System<C>>(self) -> Result<Json, IntoJsonError<C, S>> {
Ok(match self {
SimpleValue::Bool(x) => Json::Bool(x),
SimpleValue::Number(x) => Json::Number(JsonNumber::from_f64(x.get()).unwrap()), // Number forbids NaN and Infinity, so this is infallible
SimpleValue::Number(x) => Json::Number(JsonNumber::from_f64(x.get()).unwrap()), // Json and Number forbid NaN and Infinity, so this is infallible
SimpleValue::String(x) => Json::String(x),
SimpleValue::List(x) => Json::Array(x.into_iter().map(SimpleValue::into_json).collect::<Result<_,_>>()?),
SimpleValue::Image(_) => return Err(IntoJsonError::ComplexType(Type::Image)),
Expand All @@ -713,7 +713,7 @@ impl SimpleValue {
Ok(match value {
Json::Null => return Err(FromJsonError::Null),
Json::Bool(x) => SimpleValue::Bool(x),
Json::Number(x) => SimpleValue::Number(Number::new(x.as_f64().unwrap()).unwrap()), // Json forbids NaN and Infinity, so this is infallible
Json::Number(x) => SimpleValue::Number(Number::new(x.as_f64().unwrap()).unwrap()), // Json and Number forbid NaN and Infinity, so this is infallible
Json::String(x) => SimpleValue::String(x),
Json::Array(x) => SimpleValue::List(x.into_iter().map(SimpleValue::from_json).collect::<Result<_,_>>()?),
Json::Object(x) => SimpleValue::List(x.into_iter().map(|(k, v)| {
Expand All @@ -726,7 +726,7 @@ impl SimpleValue {
pub fn into_netsblox_json(self) -> Json {
match self {
SimpleValue::Bool(x) => Json::Bool(x),
SimpleValue::Number(x) => Json::Number(JsonNumber::from_f64(x.get()).unwrap()), // Number forbids NaN and Infinity, so this is infallible
SimpleValue::Number(x) => Json::Number(JsonNumber::from_f64(x.get()).unwrap()), // Json and Number forbid NaN and Infinity, so this is infallible
SimpleValue::String(x) => Json::String(x),
SimpleValue::List(x) => Json::Array(x.into_iter().map(SimpleValue::into_netsblox_json).collect()),
SimpleValue::Image(img) => {
Expand All @@ -741,7 +741,7 @@ impl SimpleValue {
Ok(match value {
Json::Null => return Err(FromNetsBloxJsonError::Null),
Json::Bool(x) => SimpleValue::Bool(x),
Json::Number(x) => SimpleValue::Number(Number::new(x.as_f64().unwrap()).unwrap()), // Json forbids NaN and Infinity, so this is infallible
Json::Number(x) => SimpleValue::Number(Number::new(x.as_f64().unwrap()).unwrap()), // Json and Number forbid NaN and Infinity, so this is infallible
Json::Array(x) => SimpleValue::List(x.into_iter().map(SimpleValue::from_netsblox_json).collect::<Result<_,_>>()?),
Json::Object(x) => SimpleValue::List(x.into_iter().map(|(k, v)| {
Ok(SimpleValue::List(vec![SimpleValue::String(k), SimpleValue::from_netsblox_json(v)?]))
Expand Down
5 changes: 4 additions & 1 deletion src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ fn assert_values_eq<'gc>(got: &Value<'gc, C, StdSystem<C>>, expected: &Value<'gc
}
(Value::Number(got), Value::Number(expected)) => {
let (got, expected) = (got.get(), expected.get());
let good = if got.is_finite() && expected.is_finite() { (got - expected).abs() <= epsilon } else { got == expected };
let good = match epsilon <= 0.0 {
true => got.to_bits() == expected.to_bits(),
false => if got.is_finite() && expected.is_finite() { (got - expected).abs() <= epsilon } else { got == expected },
};
if !good { panic!("{} - number error - got {} expected {}", path, got, expected) }
}
(Value::String(got), Value::String(expected)) => {
Expand Down
14 changes: 7 additions & 7 deletions src/test/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1708,16 +1708,16 @@ fn test_proc_signed_zero() {

run_till_term(&mut env, |mc, _, res| {
let expect = Value::from_simple(mc, SimpleValue::from_json(json!([
[0.0, -0.0, -0.0],
["0", "-0", "-0"],
[false, true, true, false, true, false, false],
[0.0, 0.0, 0.0],
["0", "0", "0"],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, false],
[false, true, true, false, true, false, false],
[false, true, true, false, true, false, true],
[false, true, true, false, true, false, false],
])).unwrap());
assert_values_eq(&res.unwrap().0, &expect, 1e-5, "signed zero");
assert_values_eq(&res.unwrap().0, &expect, 0.0, "signed zero");
});
}

Expand Down

0 comments on commit 3a6edcb

Please sign in to comment.