Skip to content

Commit

Permalink
Fix deserialization of scientific notation with fractional values (la…
Browse files Browse the repository at this point in the history
…mbdaclass#1202)

* First draft

* Simplify implementation

* Remove test

* Add changelog entry

* Clippy
  • Loading branch information
fmoletta authored and kariy committed Jun 23, 2023
1 parent e562f14 commit d3e820c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* bugfix: Fix deserialization of scientific notation with fractional values [#1202](https://github.com/lambdaclass/cairo-rs/pull/1202)

* feat: implement `mem_eq` function to test for equality of two ranges in memory [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)
* perf: use `mem_eq` in `set_add` [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)

Expand Down
44 changes: 37 additions & 7 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,17 @@ where
}

fn deserialize_scientific_notation(n: Number) -> Option<Felt252> {
let str = n.to_string();
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;

let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
let exponent = list[1].parse::<u32>().ok()?;
match n.as_f64() {
None => {
let str = n.to_string();
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;

let result = base * Felt252::from(10).pow(exponent);
Some(result)
let exponent = list[1].parse::<u32>().ok()?;
let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
Some(base * Felt252::from(10).pow(exponent))
}
Some(float) => Felt252::parse_bytes(float.round().to_string().as_bytes(), 10),
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -1437,4 +1440,31 @@ mod tests {
Ok(x) if x == Some(Felt252::one() * Felt252::from(10).pow(27))
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_felt_from_number_with_scientific_notation_with_fractional_part() {
let n = serde_json::Value::Number(Number::from_f64(64e+74).unwrap());

assert_matches!(
felt_from_number(n),
Ok(x) if x == Some(Felt252::from_str_radix("64", 10).unwrap() * Felt252::from(10).pow(74))
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_felt_from_number_with_scientific_notation_with_fractional_part_f64_max() {
let n = serde_json::Value::Number(Number::from_f64(f64::MAX).unwrap());
assert_eq!(
felt_from_number(n).unwrap(),
Some(
Felt252::from_str_radix(
"2082797363194934431336897723140298717588791783575467744530053896730196177808",
10
)
.unwrap()
)
);
}
}

0 comments on commit d3e820c

Please sign in to comment.