authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2017-08-07 18:06:06+12:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2017-08-07 18:08:09+12:00
log0705b711f8dd447f4eb4f1517be985ce09bce22f
treef1285716b6bce9a6ba538808c4825fb86a8e6024
parentd8227c79a2ff7882a52bf53f9ded2db4e7081fc8

Correct floating-point literal allowed ranges

The exponent range for floating-point values is [-1022, 1023]. Fixes #399.

4 files changed, 22 insertions(+), 2 deletions(-)

src/ir.cpp+1-1
......@@ -3704,7 +3704,7 @@ static IrInstruction *ir_gen_float_lit(IrBuilder *irb, Scope *scope, AstNode *no
37043704 assert(node->type == NodeTypeFloatLiteral);
37053705
37063706 if (node->data.float_literal.overflow) {
3707 add_node_error(irb->codegen, node, buf_sprintf("float literal too large to be represented in any type"));
3707 add_node_error(irb->codegen, node, buf_sprintf("float literal out of range of any type"));
37083708 return irb->codegen->invalid_instruction;
37093709 }
37103710
src/tokenizer.cpp+1-1
......@@ -333,7 +333,7 @@ static void end_float_token(Tokenize *t) {
333333 } else {
334334 int significand_magnitude_in_bin = __builtin_clzll(1) - __builtin_clzll(significand);
335335 t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
336 if (!(-1023 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec < 1023)) {
336 if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
337337 t->cur_tok->data.float_lit.overflow = true;
338338 return;
339339 } else {
test/cases/math.zig+6
......@@ -251,3 +251,9 @@ test "allow signed integer division/remainder when values are comptime known and
251251test "float literal parsing" {
252252 comptime assert(0x1.0 == 1.0);
253253}
254
255test "hex float literal within range" {
256 const a = 0x1.0p1023;
257 const b = 0x0.1p1027;
258 const c = 0x1.0p-1022;
259}
test/compile_errors.zig+14
......@@ -1931,4 +1931,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
19311931 \\}
19321932 ,
19331933 ".tmp_source.zig:1:13: error: struct 'Foo' contains itself");
1934
1935 cases.add("float literal too large error",
1936 \\comptime {
1937 \\ const a = 0x1.0p1024;
1938 \\}
1939 ,
1940 ".tmp_source.zig:2:15: error: float literal out of range of any type");
1941
1942 cases.add("float literal too small error (denormal)",
1943 \\comptime {
1944 \\ const a = 0x1.0p-1023;
1945 \\}
1946 ,
1947 ".tmp_source.zig:2:15: error: float literal out of range of any type");
19341948}