Skip to content

Commit

Permalink
fix exp of zero
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Sep 11, 2023
1 parent cc14776 commit 0196ff6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
10 changes: 3 additions & 7 deletions fuzz/fuzz_targets/compare_to_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ pub fn values_equal(jiter_value: &JiterValue, serde_value: &SerdeValue) -> bool
fn floats_approx(f1: Option<f64>, f2: Option<f64>) -> bool {
match (f1, f2) {
(Some(f1), Some(f2)) => {
if f1.is_nan() {
// TODO is this okay?
return true
}
let mut threshold = f1.abs() / 100_000_f64;
if threshold < 0.0001 {
threshold = 0.0001;
let mut threshold = f1.abs() / 1_000_000_f64;
if threshold < 0.000_000_1 {
threshold = 0.000_000_1;
}
let diff = f1 - f2;
if diff.abs() <= threshold {
Expand Down
10 changes: 7 additions & 3 deletions src/number_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ impl AbstractNumber for NumberAny {
}

fn apply_exponential(self, exponent: i32, positive: bool) -> JsonResult<Self> {
if self == Self::Int(NumberInt::Zero) {
Ok(Self::Float(0.0))
} else if exponent == i32::MAX {
match self {
Self::Int(NumberInt::Zero) => return Ok(Self::Float(0.0)),
Self::Float(f) if f == 0.0 => return Ok(Self::Float(0.0)),
_ => (),
}

if exponent == i32::MAX {
if positive {
Ok(Self::Float(f64::INFINITY))
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ single_tests! {
float_exp_massive3: ok => "2e2147483646", "Float(inf) @ 1:1";
float_exp_massive4: ok => "2e2147483646", "Float(inf) @ 1:1";
float_exp_massive5: ok => "18446744073709551615000.0", "Float(18446744073709552000000) @ 1:1";
float_exp_massive6: ok => "0.0E667", "Float(0) @ 1:1";
float_exp_tiny0: ok => "2e-2147483647", "Float(0) @ 1:1";
float_exp_tiny1: ok => "2e-2147483648", "Float(0) @ 1:1";
float_exp_tiny2: ok => "2e-2147483646", "Float(0) @ 1:1";
Expand Down

0 comments on commit 0196ff6

Please sign in to comment.