authorgravatar for 38307091+MadMax129@users.noreply.github.comMaks S <38307091+MadMax129@users.noreply.github.com> 2020-06-28 17:42:37-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-13 22:08:37+03:00
log50139aa23245c7b6b6a5faff95f1a2c854051a38
treee3f24b26d5a1d929f74218907616e3cfd108211a
parent6e0fb060109349b4fb7855c1587b6d9c396d926b
signaturelock-open Commit is signed but in an unrecognized format.

langref: explain why comptime_float cannot be divided by comptime_int

Co-authored-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: Veikka Tuominen <git@vexu.eu>

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

doc/langref.html.in+19-3
......@@ -248,7 +248,7 @@ pub fn main() !void {
248248 </p>
249249 <p>
250250 Following the <code>hello.zig</code> Zig code sample, the {#link|Zig Build System#} is used
251 to build an executable program from the <code>hello.zig</code> source code. Then, the
251 to build an executable program from the <code>hello.zig</code> source code. Then, the
252252 <code>hello</code> program is executed showing its output <code>Hello, world!</code>. The
253253 lines beginning with <code>$</code> represent command line prompts and a command.
254254 Everything else is program output.
......@@ -293,7 +293,7 @@ pub fn main() !void {
293293 <p>
294294 In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and
295295 <code>}</code> curly-braces. Inside of the <code>main</code> function are expressions that perform
296 the task of outputting <code>Hello, world!</code> to standard output.
296 the task of outputting <code>Hello, world!</code> to standard output.
297297 </p>
298298 <p>
299299 First, a constant identifier, <code>stdout</code>, is initialized to represent standard output's
......@@ -5135,6 +5135,22 @@ test "float widening" {
51355135 var c: f64 = b;
51365136 var d: f128 = c;
51375137 assert(d == a);
5138}
5139 {#code_end#}
5140 {#header_close#}
5141 {#header_open|Type Coercion: Coercion Float to Int#}
5142 <p>
5143 A compiler error is appropriate because this ambiguous expression leaves the compiler
5144 two choices about the coercion.
5145 <ul>
5146 <li> Cast {#syntax#}54.0{#endsyntax#} to {#syntax#}comptime_int{#endsyntax#} resulting in {#syntax#}@as(comptime_int, 10){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10){#endsyntax#}</li>
5147 <li> Cast {#syntax#}5{#endsyntax#} to {#syntax#}comptime_float{#endsyntax#} resulting in {#syntax#}@as(comptime_float, 10.8){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10.8){#endsyntax#}</li>
5148 </ul>
5149 </p>
5150 {#code_begin|test_err#}
5151// Compile time coercion of float to int
5152test "implicit cast to comptime_int" {
5153 var f: f32 = 54.0 / 5;
51385154}
51395155 {#code_end#}
51405156 {#header_close#}
......@@ -8179,7 +8195,7 @@ const expect = std.testing.expect;
81798195test "@src" {
81808196 doTheTest();
81818197}
8182
8198
81838199fn doTheTest() void {
81848200 const src = @src();
81858201