Skip to content

langref: clarify functionality of the round builtin #19503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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