authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2017-06-22 19:29:57+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2017-06-22 19:29:57+12:00
loge81bf1c38c9330d6f68584627992d3ab820ed50f
tree71d862d5149ebdf6e3721ebda8cddb783e6a6050
parent5aff641f4b93c52db3ec29bad5bfbcc738ad58c3

Return undefined in frexp instead of 0 on nan input

This is more in line what usual C implementations do.

1 files changed, 19 insertions(+), 6 deletions(-)

std/math/frexp.zig+19-6
......@@ -2,7 +2,7 @@
22//
33// - frexp(+-0) = +-0, 0
44// - frexp(+-inf) = +-inf, 0
5// - frexp(nan) = nan, 0
5// - frexp(nan) = nan, undefined
66
77const math = @import("index.zig");
88const assert = @import("../debug.zig").assert;
......@@ -46,9 +46,15 @@ fn frexp32(x: f32) -> frexp32_result {
4646 }
4747 return result;
4848 } else if (e == 0xFF) {
49 // frexp(nan) = (nan, 0)
49 // frexp(nan) = (nan, undefined)
5050 result.significand = x;
51 result.exponent = 0;
51 result.exponent = undefined;
52
53 // frexp(+-inf) = (+-inf, 0)
54 if (math.isInf(x)) {
55 result.exponent = 0;
56 }
57
5258 return result;
5359 }
5460
......@@ -77,8 +83,15 @@ fn frexp64(x: f64) -> frexp64_result {
7783 }
7884 return result;
7985 } else if (e == 0x7FF) {
86 // frexp(nan) = (nan, undefined)
8087 result.significand = x;
81 result.exponent = 0;
88 result.exponent = undefined;
89
90 // frexp(+-inf) = (+-inf, 0)
91 if (math.isInf(x)) {
92 result.exponent = 0;
93 }
94
8295 return result;
8396 }
8497
......@@ -137,7 +150,7 @@ test "math.frexp32.special" {
137150 assert(math.isNegativeInf(r.significand) and r.exponent == 0);
138151
139152 r = frexp32(math.nan(f32));
140 assert(math.isNan(r.significand) and r.exponent == 0);
153 assert(math.isNan(r.significand));
141154}
142155
143156test "math.frexp64.special" {
......@@ -156,5 +169,5 @@ test "math.frexp64.special" {
156169 assert(math.isNegativeInf(r.significand) and r.exponent == 0);
157170
158171 r = frexp64(math.nan(f64));
159 assert(math.isNan(r.significand) and r.exponent == 0);
172 assert(math.isNan(r.significand));
160173}