mirror of
https://github.com/storytold/gridcraft.git
synced 2026-10-24 02:36:48 -06:00
Print numbers near f64::MAX in a form that parses back
Found while testing crates/formula/src/printer.rs:134 Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
d1db71d0f0
commit
9cbb674c55
@@ -132,7 +132,16 @@ fn write_struct_ref(s: &StructRef, at_shorthand: bool) -> String {
|
||||
}
|
||||
|
||||
fn number(n: f64) -> String {
|
||||
number_to_text(n)
|
||||
let text = number_to_text(n);
|
||||
// 15 significant digits can round past f64::MAX, which no longer parses.
|
||||
if text.parse::<f64>().is_ok_and(f64::is_finite) {
|
||||
return text;
|
||||
}
|
||||
let exact = format!("{:E}", n);
|
||||
match exact.split_once('E') {
|
||||
Some((m, e)) if !e.starts_with('-') => format!("{m}E+{e}"),
|
||||
_ => exact,
|
||||
}
|
||||
}
|
||||
|
||||
/// Prints an expression in A1 notation.
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
use gridcraft_formula::{Expr, parse, print};
|
||||
|
||||
#[test]
|
||||
fn number_near_f64_max_round_trips() {
|
||||
for src in ["=1.7976931348623157E308", "=-1.7976931348623157E308"] {
|
||||
let e = parse(src).unwrap();
|
||||
let printed = print(&e);
|
||||
let back = parse(&printed).unwrap_or_else(|err| panic!("{printed}: {err:?}"));
|
||||
assert_eq!(print(&back), printed);
|
||||
match (&e, &back) {
|
||||
(Expr::Number(a), Expr::Number(b)) => assert_eq!(a, b),
|
||||
_ => assert_eq!(e, back),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user