Commit 0705b711f8

Marc Tiehuis <marctiehuis@gmail.com>
2017-08-07 08:06:06
Correct floating-point literal allowed ranges
The exponent range for floating-point values is [-1022, 1023]. Fixes #399.
1 parent d8227c7
Changed files (4)
src/ir.cpp
@@ -3704,7 +3704,7 @@ static IrInstruction *ir_gen_float_lit(IrBuilder *irb, Scope *scope, AstNode *no
     assert(node->type == NodeTypeFloatLiteral);
 
     if (node->data.float_literal.overflow) {
-        add_node_error(irb->codegen, node, buf_sprintf("float literal too large to be represented in any type"));
+        add_node_error(irb->codegen, node, buf_sprintf("float literal out of range of any type"));
         return irb->codegen->invalid_instruction;
     }
 
src/tokenizer.cpp
@@ -333,7 +333,7 @@ static void end_float_token(Tokenize *t) {
         } else {
             int significand_magnitude_in_bin = __builtin_clzll(1) - __builtin_clzll(significand);
             t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
-            if (!(-1023 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec < 1023)) {
+            if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
                 t->cur_tok->data.float_lit.overflow = true;
                 return;
             } else {
test/cases/math.zig
@@ -251,3 +251,9 @@ test "allow signed integer division/remainder when values are comptime known and
 test "float literal parsing" {
     comptime assert(0x1.0 == 1.0);
 }
+
+test "hex float literal within range" {
+    const a = 0x1.0p1023;
+    const b = 0x0.1p1027;
+    const c = 0x1.0p-1022;
+}
test/compile_errors.zig
@@ -1931,4 +1931,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
         \\}
     ,
         ".tmp_source.zig:1:13: error: struct 'Foo' contains itself");
+
+    cases.add("float literal too large error",
+        \\comptime {
+        \\    const a = 0x1.0p1024;
+        \\}
+    ,
+        ".tmp_source.zig:2:15: error: float literal out of range of any type");
+
+    cases.add("float literal too small error (denormal)",
+        \\comptime {
+        \\    const a = 0x1.0p-1023;
+        \\}
+    ,
+        ".tmp_source.zig:2:15: error: float literal out of range of any type");
 }