Skip to content

Commit

Permalink
langref: clarify functionality of the round builtin (#19503)
Browse files Browse the repository at this point in the history
A test has also been added to demonstrate the expected behavior.

* std.math: update round doc comment to match the builtin
  • Loading branch information
torque authored Aug 14, 2024
1 parent b7a1ef3 commit eb7f318
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
6 changes: 4 additions & 2 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -5618,9 +5618,11 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
{#header_open|@round#}
<pre>{#syntax#}@round(value: anytype) @TypeOf(value){#endsyntax#}</pre>
<p>
Rounds the given floating point number to an integer, away from zero. Uses a dedicated hardware instruction
when available.
Rounds the given floating point number to the nearest integer. If two integers are equally close, rounds away from zero.
Uses a dedicated hardware instruction when available.
</p>
{#code|test_round_builtin.zig#}

<p>
Supports {#link|Floats#} and {#link|Vectors#} of floats.
</p>
Expand Down
10 changes: 10 additions & 0 deletions doc/langref/test_round_builtin.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const expect = @import("std").testing.expect;

test "@round" {
try expect(@round(1.4) == 1);
try expect(@round(1.5) == 2);
try expect(@round(-1.4) == -1);
try expect(@round(-2.5) == -3);
}

// test
3 changes: 2 additions & 1 deletion lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,8 @@ test ByteAlignedInt {
try testing.expect(ByteAlignedInt(u129) == u136);
}

/// Rounds the given floating point number to an integer, away from zero.
/// Rounds the given floating point number to the nearest integer.
/// If two integers are equally close, rounds away from zero.
/// Uses a dedicated hardware instruction when available.
/// This is the same as calling the builtin @round
pub inline fn round(value: anytype) @TypeOf(value) {
Expand Down

0 comments on commit eb7f318

Please sign in to comment.