// This is better than what used to be, but still relies on converting the integer
// part of the float to an integer, which could overflow.
f64_to_str :: (f: f64, buf: [] u8, digits_after_decimal := 4) -> str {
- math :: package core.math
+ math :: package core.math;
+
+ if math.is_nan(f) {
+ return format(buf, "NaN");
+ }
+
+ if math.is_inf(f) {
+ if f > 0 do return format(buf, "Inf");
+ else do return format(buf, "-Inf");
+ }
len := 0;
return (a * b) / gcd(a, b);
}
+is_nan :: #match #local {}
+
+#overload
+is_nan :: (x: f32) -> bool {
+ v := x;
+ i := *cast(^u32) ^v;
+ return (i & 0x7f800000) == 0x7f800000
+ && (i & 0x007fffff) != 0;
+}
+
+#overload
+is_nan :: (x: f64) -> bool {
+ v := x;
+ i := *cast(^u64) ^v;
+ return (i & 0x7ff0000000000000) == 0x7ff0000000000000
+ && (i & 0x000fffffffffffff) != 0;
+}
+
+
+is_inf :: #match #local {}
+
+#overload
+is_inf :: (x: f32) -> bool {
+ v := x;
+ i := *cast(^u32) ^v;
+ return (i & 0x7f800000) == 0x7f800000
+ && (i & 0x007fffff) == 0;
+}
+
+#overload
+is_inf :: (x: f64) -> bool {
+ v := x;
+ i := *cast(^u64) ^v;
+ return (i & 0x7ff0000000000000) == 0x7ff0000000000000
+ && (i & 0x000fffffffffffff) == 0;
+}
+
//