Skip to content

Commit eb7f318

Browse files
authored
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
1 parent b7a1ef3 commit eb7f318

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/langref.html.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5618,9 +5618,11 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
56185618
{#header_open|@round#}
56195619
<pre>{#syntax#}@round(value: anytype) @TypeOf(value){#endsyntax#}</pre>
56205620
<p>
5621-
Rounds the given floating point number to an integer, away from zero. Uses a dedicated hardware instruction
5622-
when available.
5621+
Rounds the given floating point number to the nearest integer. If two integers are equally close, rounds away from zero.
5622+
Uses a dedicated hardware instruction when available.
56235623
</p>
5624+
{#code|test_round_builtin.zig#}
5625+
56245626
<p>
56255627
Supports {#link|Floats#} and {#link|Vectors#} of floats.
56265628
</p>

doc/langref/test_round_builtin.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const expect = @import("std").testing.expect;
2+
3+
test "@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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,8 @@ test ByteAlignedInt {
11401140
try testing.expect(ByteAlignedInt(u129) == u136);
11411141
}
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.
11441145
/// Uses a dedicated hardware instruction when available.
11451146
/// This is the same as calling the builtin @round
11461147
pub inline fn round(value: anytype) @TypeOf(value) {

0 commit comments

Comments
 (0)