round — Round scalars, vectors, matrices, or N-D tensors to the nearest integers, decimal digits, or significant digits.
round(X) rounds numeric values to the nearest integers using MATLAB-compatible half-away-from-zero semantics. Additional arguments allow rounding to a specified number of decimal digits or significant digits.
How round works in RunMat
round(X)rounds each element ofXto the nearest integer; ties (e.g.,±0.5) round away from zero.round(X, N)rounds toNdecimal digits whenNis positive and to powers of ten whenNis negative.round(X, N, 'significant')rounds toNsignificant digits.Nmust be a positive integer.- Logical inputs are promoted to double before rounding;
round(true)returns1. - Complex inputs are rounded component-wise (
round(a + bi) = round(a) + i·round(b)), matching MATLAB. - Non-finite values (
NaN,Inf,-Inf) propagate unchanged regardless of precision arguments. - Character arrays are treated as their numeric code points and return double tensors of the same size.
How round runs on the GPU
When a tensor already resides on the GPU, RunMat checks whether the active acceleration provider implements a specialised unary_round kernel. If available, round(X) executes entirely on the device. Advanced modes (round(X, N) and round(X, N, 'significant')) currently gather tensors to the host before rounding to keep semantics aligned with MATLAB. Providers that add digit-aware kernels can extend this path in the future.
GPU memory and residency
You usually do **not** need to call gpuArray manually. RunMat's planner keeps tensors on the GPU when the provider exposes the required kernels and it is profitable to do so. round takes advantage of this mechanism for the plain round(X) form. When you specify digits or the 'significant' option, RunMat currently gathers data to the host to match MATLAB exactly. Future providers can extend unary_round or add digit-aware kernels to keep those workloads on the device.
Examples
Rounding values to the nearest integers
X = [-3.5 -2.2 -0.5 0 0.5 1.7];
Y = round(X)Expected output:
Y = [-4 -2 -1 0 1 2]Rounding to a fixed number of decimal places
temps = [21.456 19.995 22.501];
rounded = round(temps, 2)Expected output:
rounded = [21.46 20.00 22.50]Rounding to negative powers of ten
counts = [1234 5678 91011];
rounded = round(counts, -2)Expected output:
rounded = [1200 5700 91000]Rounding to significant digits
measurements = [0.001234 12.3456 98765];
sig3 = round(measurements, 3, 'significant')Expected output:
sig3 = [0.00123 12.3 98800]Rounding GPU tensors and gathering the results
G = gpuArray(linspace(-2.5, 2.5, 6));
rounded = round(G);
hostValues = gather(rounded)Expected output:
hostValues = [-3 -2 -1 1 2 3]FAQ
Does round always round half values away from zero?
Yes. MATLAB and RunMat both use half-away-from-zero semantics, so round(0.5) returns 1 and round(-0.5) returns -1.
Can I round to decimal places and significant digits?
Yes. Use round(X, N) for decimal places and round(X, N, 'significant') for significant digits. Negative N values round to tens, hundreds, and so on.
What happens if I pass a non-integer N?
N must be an integer scalar. RunMat raises a MATLAB-compatible error when N is not an integer or is non-finite.
How are complex numbers handled?
RunMat rounds the real and imaginary components independently, matching MATLAB's component-wise behaviour.
Do NaN or Inf values change when rounded?
No. Non-finite values propagate unchanged for every rounding mode, just like MATLAB.
Will rounding stay on the GPU?
round(X) stays on the GPU when the provider implements unary_round. Rounding with digit arguments currently gathers to the host; providers can override this by adding specialised kernels.
Can I round logical or character arrays?
Yes. Logical values are converted to doubles (0 or 1) and characters are rounded as their numeric code points, returning dense double tensors.
What does round do in MATLAB?
round(X) rounds each element of X to the nearest integer. round(X, N) rounds to N decimal places. Ties (e.g., 2.5) round away from zero.
What is the difference between round, floor, and ceil in MATLAB?
round rounds to the nearest integer, floor rounds toward negative infinity, and ceil rounds toward positive infinity. For 2.5: round returns 3, floor returns 2, ceil returns 3.
Does round support GPU acceleration in RunMat?
Yes. round runs on the GPU with elementwise fusion support. It accepts f32 and f64 precisions and supports MATLAB-compatible broadcasting.
Related functions to explore
These functions work well alongside round. Each page has runnable examples you can try in the browser.
floor, ceil, fix, gpuArray, gather, mod, rem
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how round works, line by line, in Rust.
- View round.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.