fix — Round values toward zero in MATLAB and RunMat.

fix(X) removes the fractional part of each element by rounding toward zero. Positive values behave like floor, negative values behave like ceil, and complex values are processed component-wise. Input coercions and array-shape behavior follow MATLAB semantics.

Syntax

Y = fix(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesNumeric, logical, or complex input array.

Returns

NameTypeDescription
YNumericArrayRounded values toward zero.

Errors

IdentifierWhenMessage
RunMat:fix:InvalidInputInput cannot be interpreted as numeric, logical, or complex data.fix: invalid input
RunMat:fix:InternalInternal tensor conversion or allocation failed.fix: internal error

How fix works

  • fix rounds 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 -Inf propagate unchanged.
  • Character arrays are treated as their numeric code points and return dense double tensors.
  • Empty arrays return empty arrays with identical shape.

Does RunMat run fix 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]

Using fix with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how fix changes the result.

Run a small fix example, explain the result, then change one input and compare the output.

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.

Rounding

ceil · floor · mod · rem · round

Elementwise

abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fix is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.