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 @@...@@ -2,7 +2,7 @@
2//2//
3// - frexp(+-0) = +-0, 03// - frexp(+-0) = +-0, 0
4// - frexp(+-inf) = +-inf, 04// - frexp(+-inf) = +-inf, 0
5// - frexp(nan) = nan, 05// - frexp(nan) = nan, undefined
66
7const math = @import("index.zig");7const math = @import("index.zig");
8const assert = @import("../debug.zig").assert;8const assert = @import("../debug.zig").assert;
...@@ -46,9 +46,15 @@ fn frexp32(x: f32) -> frexp32_result {...@@ -46,9 +46,15 @@ fn frexp32(x: f32) -> frexp32_result {
46 }46 }
47 return result;47 return result;
48 } else if (e == 0xFF) {48 } else if (e == 0xFF) {
49 // frexp(nan) = (nan, 0)49 // frexp(nan) = (nan, undefined)
50 result.significand = x;50 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
52 return result;58 return result;
53 }59 }
5460
...@@ -77,8 +83,15 @@ fn frexp64(x: f64) -> frexp64_result {...@@ -77,8 +83,15 @@ fn frexp64(x: f64) -> frexp64_result {
77 }83 }
78 return result;84 return result;
79 } else if (e == 0x7FF) {85 } else if (e == 0x7FF) {
86 // frexp(nan) = (nan, undefined)
80 result.significand = x;87 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
82 return result;95 return result;
83 }96 }
8497
...@@ -137,7 +150,7 @@ test "math.frexp32.special" {...@@ -137,7 +150,7 @@ test "math.frexp32.special" {
137 assert(math.isNegativeInf(r.significand) and r.exponent == 0);150 assert(math.isNegativeInf(r.significand) and r.exponent == 0);
138151
139 r = frexp32(math.nan(f32));152 r = frexp32(math.nan(f32));
140 assert(math.isNan(r.significand) and r.exponent == 0);153 assert(math.isNan(r.significand));
141}154}
142155
143test "math.frexp64.special" {156test "math.frexp64.special" {
...@@ -156,5 +169,5 @@ test "math.frexp64.special" {...@@ -156,5 +169,5 @@ test "math.frexp64.special" {
156 assert(math.isNegativeInf(r.significand) and r.exponent == 0);169 assert(math.isNegativeInf(r.significand) and r.exponent == 0);
157170
158 r = frexp64(math.nan(f64));171 r = frexp64(math.nan(f64));
159 assert(math.isNan(r.significand) and r.exponent == 0);172 assert(math.isNan(r.significand));
160}173}