Files
DOSSIER/src-tauri/hatchgen/src/jsnum.rs
T
Claude 0971db3af6 [217] Kern: Panic durch f64::clamp mit vertauschten/NaN-Grenzen behoben
wall_top_at (wall_piece_mesh, 3D, Exporte) klemmte die Oberkante auf
[Unterkante, Unterkante + Höhe]; bei negativer/NaN-Höhe panikierte clamp und
legte das WASM-Modul lahm. Neu jsnum::clamp_safe / ClampSafe (Grenzen tauschen,
NaN wie JS); 28 weitere clamp-Aufrufe mit berechneten Grenzen umgestellt.
Regressionstests in Rust und im Robustheitstest. TODO [217] angelegt.
2026-11-12 19:50:21 +00:00

151 lines
4.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Zahl-Helfer mit JavaScript-Semantik ([126]).
//!
//! - Trigonometrie und `pow` laufen über das Crate `libm` (reines Rust, auf allen Plattformen und im
//! Web-Build dieselben Werte; kein Aufruf in die JS-Engine).
//! - `Math.min/max` liefern bei NaN NaN (Rust `f64::min/max` ignorieren NaN).
//! - `Math.round` rundet halbe Werte Richtung +∞ (Rust `round` weg von 0).
//! - `x | 0` ist ToInt32 (Rest modulo 2³², Rust `as i32` sättigt).
pub use libm::{acos, atan, atan2, cos, sin, tan};
/// `x ** y` (libm).
#[inline]
pub fn pow(x: f64, y: f64) -> f64 {
libm::pow(x, y)
}
/// Endliche Zahl oder 0 (NaN/±∞ → 0; Summen der Kennzahlen/Flächen).
#[inline]
pub fn or_zero(v: f64) -> f64 {
if v.is_finite() {
v
} else {
0.0
}
}
/// `Math.min(a, b)` (NaN gewinnt; −0 < +0).
#[inline]
pub fn min(a: f64, b: f64) -> f64 {
if a.is_nan() || b.is_nan() {
f64::NAN
} else if a < b || (a == b && a.is_sign_negative()) {
a
} else {
b
}
}
/// `Math.max(a, b)` (NaN gewinnt; +0 > −0).
#[inline]
pub fn max(a: f64, b: f64) -> f64 {
if a.is_nan() || b.is_nan() {
f64::NAN
} else if a > b || (a == b && a.is_sign_positive()) {
a
} else {
b
}
}
/// `Math.min(Math.max(x, a), b)` ohne Panic: vertauschte Grenzen (a > b) werden getauscht, NaN in `x`
/// oder einer Grenze liefert NaN (wie JS; `f64::clamp` würde bei min > max oder NaN-Grenzen panikieren).
#[inline]
pub fn clamp_safe(x: f64, a: f64, b: f64) -> f64 {
let (lo, hi) = if a > b { (b, a) } else { (a, b) };
min(max(x, lo), hi)
}
/// Methodenform von [`clamp_safe`]: `x.clamp_safe(a, b)` statt `x.clamp(a, b)` bei Grenzen, die aus Modellwerten
/// berechnet werden (können vertauscht oder NaN sein).
pub trait ClampSafe {
fn clamp_safe(self, a: f64, b: f64) -> f64;
}
impl ClampSafe for f64 {
#[inline]
fn clamp_safe(self, a: f64, b: f64) -> f64 {
clamp_safe(self, a, b)
}
}
/// `Math.round(x)`: halbe Werte Richtung +∞, −0 bleibt −0, Werte in (−0.5, 0) → −0.
#[inline]
pub fn round(x: f64) -> f64 {
if !x.is_finite() || x == 0.0 {
return x;
}
if x > 0.0 && x < 0.5 {
return 0.0;
}
if x < 0.0 && x >= -0.5 {
return -0.0;
}
let r = x.ceil();
if r - 0.5 > x {
r - 1.0
} else {
r
}
}
/// ToInt32 (`x | 0`).
#[inline]
pub fn to_int32(x: f64) -> i32 {
// Schneller Weg im i32-Bereich (`as` schneidet wie ToInt32 Richtung 0 ab; NaN → 0).
if (-2147483648.0..=2147483647.0).contains(&x) {
return x as i32;
}
if !x.is_finite() {
return 0;
}
let t = x.trunc();
let m = t.rem_euclid(4294967296.0);
(m as u64 as u32) as i32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clamp_ohne_panic() {
assert_eq!(clamp_safe(5.0, 0.0, 3.0), 3.0);
assert_eq!(clamp_safe(-5.0, 0.0, 3.0), 0.0);
assert_eq!(clamp_safe(1.0, 3.0, 0.0), 1.0, "vertauschte Grenzen werden getauscht");
assert_eq!(clamp_safe(9.0, 3.0, 0.0), 3.0);
assert!(clamp_safe(f64::NAN, 0.0, 1.0).is_nan());
assert!(clamp_safe(0.5, f64::NAN, 1.0).is_nan());
assert!(clamp_safe(0.5, 0.0, f64::NAN).is_nan());
assert_eq!(clamp_safe(f64::INFINITY, 0.0, 1.0), 1.0);
}
#[test]
fn rundung_wie_js() {
assert_eq!(round(2.5), 3.0);
assert_eq!(round(-2.5), -2.0);
assert_eq!(round(-0.5).to_bits(), (-0.0f64).to_bits());
assert_eq!(round(0.49999999999999994), 0.0);
assert_eq!(round(-126.5), -126.0);
assert!(round(f64::NAN).is_nan());
}
#[test]
fn min_max_wie_js() {
assert!(min(1.0, f64::NAN).is_nan());
assert!(max(f64::NAN, 1.0).is_nan());
assert_eq!(max(0.0, -0.0).to_bits(), 0.0f64.to_bits());
assert_eq!(min(0.0, -0.0).to_bits(), (-0.0f64).to_bits());
assert_eq!(max(-1.0, min(1.0, 3.0)), 1.0);
}
#[test]
fn int32_wie_js() {
assert_eq!(to_int32(4294967296.0 + 5.0), 5);
assert_eq!(to_int32(2147483648.0), -2147483648);
assert_eq!(to_int32(-1.7), -1);
assert_eq!(to_int32(f64::NAN), 0);
assert_eq!(to_int32(-4294967297.0), -1);
}
}