fix — Round scalars, vectors, matrices, or N-D tensors toward zero.
fix(X) removes the fractional part of each element in X by rounding toward zero. Positive numbers behave like floor, negatives behave like ceil, and zeros stay zero. The operation works element-wise on scalars, vectors, matrices, N-D tensors, and complex values.
How fix works in RunMat
fixrounds positive inputs down toward zero and negative inputs up toward zero.- Integer-valued elements are returned unchanged. Logical inputs are promoted to double before rounding.
- Complex numbers are rounded component-wise (
fix(a + bi) = fix(a) + i·fix(b)), matching MATLAB. NaN,Inf, and-Infpropagate unchanged.- Character arrays are treated as their numeric code points and return dense double tensors.
- Empty arrays return empty arrays with identical shape.
How fix runs on the GPU
When a tensor already resides on the GPU, RunMat Accelerate checks whether the active provider implements the optional unary_fix hook. If available, the truncation is executed directly on the device and the result stays on the GPU. If the hook is missing, RunMat gathers the values to the host, applies the CPU implementation, and returns a host-resident result—ensuring MATLAB-compatible semantics everywhere.
GPU memory and residency
G = gpuArray(linspace(-2.4, 2.4, 6));
truncGpu = fix(G);
hostValues = gather(truncGpu);Expected output:
hostValues = [-2 -1 0 0 1 2];Examples
Truncating positive and negative values toward zero
values = [-3.7 -2.4 -0.6 0 0.6 2.4 3.7];
truncated = fix(values)Expected output:
truncated = [-3 -2 0 0 0 2 3]Removing fractional parts from a matrix
A = [1.9 4.1; -2.8 0.5];
B = fix(A)Expected output:
B = [1 4; -2 0]Keeping GPU residency when kernels are available
G = gpuArray(linspace(-2.4, 2.4, 6));
truncGpu = fix(G);
hostValues = gather(truncGpu)Expected output:
hostValues = [-2 -1 0 0 1 2]Dropping fractional parts of complex numbers
z = [1.9 + 2.6i, -3.4 - 0.2i];
fixed = fix(z)Expected output:
fixed = [1 + 2i, -3 + 0i]Converting character arrays to truncated numeric codes
letters = ['A' 'B' 'C'];
codes = fix(letters)Expected output:
codes = [65 66 67]FAQ
How is fix different from floor and ceil?
fix always rounds toward zero. Positive numbers behave like floor, negatives behave like ceil. Use floor or ceil when you specifically want to round toward negative or positive infinity.
What happens to existing integers?
Integer-valued inputs (including logical values) are returned unchanged; the output is still reported as a double tensor, matching MATLAB's default numeric type.
Does fix change NaN or Inf values?
No. NaN, Inf, and -Inf propagate unchanged.
How are complex numbers handled?
fix rounds the real and imaginary components independently, exactly like MATLAB.
Will fix stay on the GPU?
Yes, when the active provider implements unary_fix. Otherwise, RunMat gathers to the host and applies the CPU implementation, guaranteeing MATLAB-compatible behaviour.
Related functions to explore
These functions work well alongside fix. Each page has runnable examples you can try in the browser.
floor, ceil, round, gpuArray, gather, mod, rem
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fix works, line by line, in Rust.
- View fix.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.