authorgravatar for torque@users.noreply.github.comT <torque@users.noreply.github.com> 2024-08-14 10:29:45-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-14 10:29:45-07:00
logeb7f318ea897d51082b70c84591242735c8a2c53
tree3c77d9f03480a6fa5ec80cffc6ef5fa5884dbf36
parentb7a1ef3e19039690b182a295dff7cffe5fcc521f
signaturebadge-check Signed by PGP key B5690EEEBB952194

langref: clarify functionality of the round builtin (#19503)

A test has also been added to demonstrate the expected behavior. * std.math: update round doc comment to match the builtin

3 files changed, 16 insertions(+), 3 deletions(-)

doc/langref.html.in+4-2
...@@ -5618,9 +5618,11 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val...@@ -5618,9 +5618,11 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
5618 {#header_open|@round#}5618 {#header_open|@round#}
5619 <pre>{#syntax#}@round(value: anytype) @TypeOf(value){#endsyntax#}</pre>5619 <pre>{#syntax#}@round(value: anytype) @TypeOf(value){#endsyntax#}</pre>
5620 <p>5620 <p>
5621 Rounds the given floating point number to an integer, away from zero. Uses a dedicated hardware instruction5621 Rounds the given floating point number to the nearest integer. If two integers are equally close, rounds away from zero.
5622 when available.5622 Uses a dedicated hardware instruction when available.
5623 </p>5623 </p>
5624 {#code|test_round_builtin.zig#}
5625
5624 <p>5626 <p>
5625 Supports {#link|Floats#} and {#link|Vectors#} of floats.5627 Supports {#link|Floats#} and {#link|Vectors#} of floats.
5626 </p>5628 </p>
doc/langref/test_round_builtin.zig created+10
...@@ -0,0 +1,10 @@
1const expect = @import("std").testing.expect;
2
3test "@round" {
4 try expect(@round(1.4) == 1);
5 try expect(@round(1.5) == 2);
6 try expect(@round(-1.4) == -1);
7 try expect(@round(-2.5) == -3);
8}
9
10// test
lib/std/math.zig+2-1
...@@ -1140,7 +1140,8 @@ test ByteAlignedInt {...@@ -1140,7 +1140,8 @@ test ByteAlignedInt {
1140 try testing.expect(ByteAlignedInt(u129) == u136);1140 try testing.expect(ByteAlignedInt(u129) == u136);
1141}1141}
11421142
1143/// Rounds the given floating point number to an integer, away from zero.1143/// Rounds the given floating point number to the nearest integer.
1144/// If two integers are equally close, rounds away from zero.
1144/// Uses a dedicated hardware instruction when available.1145/// Uses a dedicated hardware instruction when available.
1145/// This is the same as calling the builtin @round1146/// This is the same as calling the builtin @round
1146pub inline fn round(value: anytype) @TypeOf(value) {1147pub inline fn round(value: anytype) @TypeOf(value) {