What does the floor function do in MATLAB / RunMat?
floor(X) rounds each element of X toward negative infinity, returning the greatest integer less than or equal to the input. Optional arguments match MATLAB's extended syntax for rounding to fixed decimal places, significant digits, and prototype-based residency.
How does the floor function behave in MATLAB / RunMat?
- Works on scalars, vectors, matrices, and higher-dimensional tensors with MATLAB broadcasting semantics.
floor(X, N)rounds toward negative infinity withNdecimal digits (positiveN) or powers of ten (negativeN).floor(X, N, 'significant')rounds toNsignificant digits;Nmust be a positive integer.- Logical inputs are promoted to doubles (
false → 0,true → 1) before flooring. - Character arrays are interpreted numerically (their Unicode code points) and return dense double tensors.
- Complex inputs are floored component-wise:
floor(a + bi) = floor(a) + i·floor(b). - Non-finite values (
NaN,Inf,-Inf) propagate unchanged. - Empty arrays return empty arrays of the appropriate shape.
- Appending
'like', prototypeforces the result to match the residency ofprototype(CPU or GPU). Currently prototypes must be numeric.
floor Function GPU Execution Behaviour
When tensors already reside on the GPU, RunMat consults the active acceleration provider. If the provider implements the unary_floor hook, floor(X) executes entirely on the device and keeps tensors resident. When decimal or significant-digit rounding is requested—or when the provider lacks unary_floor—RunMat gathers the tensor to host memory, applies the CPU implementation, and honours any 'like' GPU prototype by uploading the result back to the device. This keeps semantics consistent even when specialised kernels are unavailable.
Examples of using the floor function in MATLAB / RunMat
Flooring positive and negative scalars
x = [-2.7, -0.3, 0, 0.8, 3.9];
y = floor(x);
Expected output:
y = [-3, -1, 0, 0, 3];
Flooring every element of a matrix
A = [1.2 4.7; -3.4 5.0];
B = floor(A);
Expected output:
B = [1 4; -4 5];
Flooring fractions stored in a tensor
t = reshape([-1.8, -0.2, 0.4, 1.9, 2.1, 3.6], [3, 2]);
floored = floor(t);
Expected output:
floored =
[-2 1;
-1 2;
0 3]
Flooring values to a fixed number of decimal places
temps = [21.456 19.995 22.501];
floored = floor(temps, 2);
Expected output:
floored = [21.45 19.99 22.50];
Flooring to significant digits
measurements = [0.001234 12.3456 98765];
sig2 = floor(measurements, 2, 'significant');
Expected output:
sig2 = [0.0012 12.0 98000];
Flooring complex numbers component-wise
z = [1.7 + 2.1i, -0.2 - 3.9i];
result = floor(z);
Expected output:
result = [1 + 2i, -1 - 4i];
Keeping GPU data on device when the provider supports unary_floor
G = gpuArray([1.8 -0.2 0.0; -1.1 2.5 -3.4]);
floored = floor(G);
H = gather(floored);
Expected output:
H =
[ 1 -1 0;
-2 2 -4]
Forcing GPU residency with a 'like' prototype
A = [1.8 -0.2; 2.7 3.4];
proto = gpuArray(0);
G = floor(A, 'like', proto); % Result remains on the GPU
result = gather(G);
Expected output:
result =
[ 1 -1;
2 3]
GPU residency in RunMat (Do I need gpuArray?)
You usually do not need to call gpuArray manually. RunMat's planner keeps tensors on the GPU when a provider implements unary_floor and the workload benefits from device execution. When the hook is missing, RunMat automatically gathers the data, applies the CPU semantics, and allows subsequent operations to re-upload if profitable. Explicit gpuArray calls remain available for compatibility with MathWorks MATLAB.
FAQ
- Does
flooralways round toward negative infinity? Yes—positive values round down toward zero, while negative values round to the more negative integer (e.g.,floor(-0.1) = -1). - How are complex numbers handled? The real and imaginary parts are floored independently, matching MATLAB's component-wise definition.
- Can I round to decimal digits or significant digits? Yes. Use
floor(X, N)for decimal digits orfloor(X, N, 'significant')for significant digits. NegativeNvalues round to powers of ten. - What happens with logical arrays? Logical values promote to doubles (
0or1) before flooring, so the outputs remain 0 or 1. - Can I pass character arrays to
floor? Yes. Character data is treated as its numeric code points, producing a double tensor of the same size. - Do
NaNandInfvalues change? No. Non-finite inputs propagate unchanged. - Will GPU execution change floating-point results? No. Providers implement IEEE-compliant flooring; when a provider lacks
unary_floor, RunMat falls back to the CPU to preserve MATLAB-compatible behaviour. - Does
'like'work withfloor? Yes. Append'like', prototypeto request output that matches the prototype's residency. Currently prototypes must be numeric (scalars or dense tensors, host or GPU). - Can fusion keep
flooron the GPU? Yes.floorparticipates in elementwise fusion, so fused graphs can stay resident on the device when supported.
See Also
ceil, round, fix, gpuArray, gather
Source & Feedback
- The full source code for
floorlives at:crates/runmat-runtime/src/builtins/math/rounding/floor.rs - Found a bug or behavioural difference? Please open an issue with details and a minimal repro.