Skip to content

Commit d3e820c

Browse files
fmolettakariy
authored andcommitted
Fix deserialization of scientific notation with fractional values (lambdaclass#1202)
* First draft * Simplify implementation * Remove test * Add changelog entry * Clippy
1 parent e562f14 commit d3e820c

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* bugfix: Fix deserialization of scientific notation with fractional values [#1202](https://github.com/lambdaclass/cairo-rs/pull/1202)
6+
57
* feat: implement `mem_eq` function to test for equality of two ranges in memory [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)
68
* perf: use `mem_eq` in `set_add` [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)
79

src/serde/deserialize_program.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,17 @@ where
187187
}
188188

189189
fn deserialize_scientific_notation(n: Number) -> Option<Felt252> {
190-
let str = n.to_string();
191-
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;
192-
193-
let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
194-
let exponent = list[1].parse::<u32>().ok()?;
190+
match n.as_f64() {
191+
None => {
192+
let str = n.to_string();
193+
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;
195194

196-
let result = base * Felt252::from(10).pow(exponent);
197-
Some(result)
195+
let exponent = list[1].parse::<u32>().ok()?;
196+
let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
197+
Some(base * Felt252::from(10).pow(exponent))
198+
}
199+
Some(float) => Felt252::parse_bytes(float.round().to_string().as_bytes(), 10),
200+
}
198201
}
199202

200203
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
@@ -1437,4 +1440,31 @@ mod tests {
14371440
Ok(x) if x == Some(Felt252::one() * Felt252::from(10).pow(27))
14381441
);
14391442
}
1443+
1444+
#[test]
1445+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1446+
fn test_felt_from_number_with_scientific_notation_with_fractional_part() {
1447+
let n = serde_json::Value::Number(Number::from_f64(64e+74).unwrap());
1448+
1449+
assert_matches!(
1450+
felt_from_number(n),
1451+
Ok(x) if x == Some(Felt252::from_str_radix("64", 10).unwrap() * Felt252::from(10).pow(74))
1452+
);
1453+
}
1454+
1455+
#[test]
1456+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1457+
fn test_felt_from_number_with_scientific_notation_with_fractional_part_f64_max() {
1458+
let n = serde_json::Value::Number(Number::from_f64(f64::MAX).unwrap());
1459+
assert_eq!(
1460+
felt_from_number(n).unwrap(),
1461+
Some(
1462+
Felt252::from_str_radix(
1463+
"2082797363194934431336897723140298717588791783575467744530053896730196177808",
1464+
10
1465+
)
1466+
.unwrap()
1467+
)
1468+
);
1469+
}
14401470
}

0 commit comments

Comments
 (0)