We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 73b174d commit 0e229c2Copy full SHA for 0e229c2
1 file changed
src/msgpack.zig
@@ -1819,12 +1819,11 @@ pub fn PackWithLimits(
1819
1820
/// write float
1821
fn writeFloat(self: Self, val: f64) !void {
1822
- const tmp_val = if (val < 0) 0 - val else val;
1823
- const min_f32 = std.math.floatMin(f32);
1824
- const max_f32 = std.math.floatMax(f32);
1825
-
1826
- if (tmp_val >= min_f32 and tmp_val <= max_f32) {
1827
- try self.writeF32(@floatCast(val));
+ // A value should only be encoded as f32 if it can be
+ // represented exactly without loss of precision.
+ const val_f32: f32 = @floatCast(val);
+ if (val == @as(f64, val_f32)) {
+ try self.writeF32(val_f32);
1828
} else {
1829
try self.writeF64(val);
1830
}
0 commit comments