mirror of
https://github.com/storytold/gridcraft.git
synced 2026-10-24 10:46:50 -06:00
Merge PR #2289
This commit is contained in:
@@ -154,7 +154,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