mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-02 16:29:34 -06:00
Fix code export printing inf for very large finite numbers (#1757)
* Fix code export printing inf for very large finite numbers Found while testing crates/engine/src/dev/codegen.rs:185 (num) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * Keep rem values finite past num6's scaling overflow num6 had the same x1e6 overflow as num: a huge finite length printed 0rem (CodeRabbit on #1757). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Haiku 5.5 <noreply@anthropic.com> Co-authored-by: Brandon Thomas <echelon@gmail.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
Brandon Thomas
parent
0abbe44cd4
commit
b957160379
@@ -182,7 +182,9 @@ pub(crate) fn num(v: f64) -> String {
|
||||
if !v.is_finite() {
|
||||
return "0".into();
|
||||
}
|
||||
let r = (v * 100.0).round() / 100.0;
|
||||
let scaled = v * 100.0;
|
||||
// Past the x100 overflow every value is whole anyway, so keep it as is.
|
||||
let r = if scaled.is_finite() { scaled.round() / 100.0 } else { v };
|
||||
if (r - r.round()).abs() < 1e-9 {
|
||||
// `as i64` saturates past 2^63, so larger whole values are printed from the float.
|
||||
if r.abs() < 9e18 { format!("{}", r.round() as i64) } else { format!("{:.0}", r.round()) }
|
||||
@@ -193,7 +195,9 @@ pub(crate) fn num(v: f64) -> String {
|
||||
|
||||
/// `v` without trailing zeros (6 decimals), for lengths that must convert back to the same pixels.
|
||||
fn num6(v: f64) -> String {
|
||||
let r = (v * 1_000_000.0).round() / 1_000_000.0;
|
||||
let scaled = v * 1_000_000.0;
|
||||
// Past the x1e6 overflow every value is whole anyway, so keep it as is.
|
||||
let r = if scaled.is_finite() { scaled.round() / 1_000_000.0 } else { v };
|
||||
if r.is_finite() && r != 0.0 { format!("{r}") } else { "0".into() }
|
||||
}
|
||||
|
||||
@@ -458,6 +462,21 @@ mod tests {
|
||||
use crate::params::id_of;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn num_keeps_finite_values_past_the_x100_overflow() {
|
||||
for v in [1e307, -1e307, f64::MAX] {
|
||||
let s = num(v);
|
||||
assert_eq!(s, format!("{v:.0}"));
|
||||
assert_eq!(s.parse::<f64>(), Ok(v));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn num6_keeps_finite_values_past_the_x1e6_overflow() {
|
||||
assert_eq!(num6(1e305).parse::<f64>(), Ok(1e305));
|
||||
assert_eq!(num6(0.1234567), "0.123457");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn num_keeps_huge_whole_numbers() {
|
||||
assert_eq!(num(1e20), "100000000000000000000");
|
||||
|
||||
Reference in New Issue
Block a user