View all functions

CategoryMath: Rounding
GPUYes

What does the rem function do in MATLAB / RunMat?

C = rem(A, B) returns the remainder after division with the quotient rounded toward zero: C = A - B .* fix(A ./ B). The result has the same sign as the dividend (A) and a magnitude strictly smaller than abs(B) when B is finite.

How does the rem function behave in MATLAB / RunMat?

  • Supports scalars, vectors, matrices, and higher-dimensional tensors with MATLAB-style implicit expansion (broadcasting).
  • Returns NaN when the divisor is zero or when the dividend is non-finite while the divisor is finite.
  • Infinite divisors leave finite dividends unchanged (because the quotient is zero).
  • Logical and integer inputs promote to double precision; character arrays operate on their Unicode code points.
  • Complex inputs follow MATLAB semantics: rem(z, w) = z - w .* fix(z ./ w) with fix applied component-wise to the complex quotient.
  • Empty inputs propagate emptiness while preserving shape.

rem Function GPU Execution Behaviour

When both operands are GPU tensors with identical shapes and the active provider exposes elem_div, unary_fix, elem_mul, and elem_sub, RunMat evaluates rem fully on the device by chaining those kernels. Intermediate buffers stay device-side, fusion-aware planners can inline the sequence, and the final remainder handle remains on the GPU for downstream work. If residency is mixed, shapes differ, or any hook is unavailable, RunMat automatically gathers to the host, executes the MATLAB-compatible fallback, and returns a CPU tensor while preserving observable semantics.

Examples of using the rem function in MATLAB / RunMat

Remainder of positive integers

r = rem(17, 5);

Expected output:

r = 2;

Remainder keeps dividend sign for negatives

values = [-7 -3 4 9];
remainders = rem(values, 4);

Expected output:

remainders = [-3 -3 0 1];

Remainder with a negative divisor

r = rem(7, -4);

Expected output:

r = 3;

Applying remainder across arrays with broadcasting

A = [4.5 7.1; -2.3 0.4];
B = rem(A, 2);

Expected output:

B = [0.5 1.1;
     -0.3 0.4];

Complex remainders follow fix-based quotient

z = [3 + 4i, -2 + 5i];
w = 2 + 1i;
res = rem(z, w);

Expected output:

res = [0.0 + 0.0i  0.0 + 1.0i];

Keeping rem on the GPU when kernels are available

G = gpuArray(-5:5);
H = rem(G, 4);
hostCopy = gather(H);

Expected output:

hostCopy = [-1 0 -3 -2 -1 0 1 2 3 0 1];

GPU residency in RunMat (Do I need gpuArray?)

Typically no. The native auto-offload planner keeps tensors on the GPU whenever the provider implements the chained kernels described above or when rem participates in a larger fused expression. If any hook is missing—or when inputs start on the host—RunMat gathers automatically, executes the CPU fallback, and returns the result. You can still call gpuArray and gather explicitly for compatibility with MathWorks MATLAB scripts or to control residency manually.

FAQ

How is rem different from mod?

rem rounds the quotient with fix, so the remainder inherits the dividend's sign. mod rounds with floor, which ties the remainder to the divisor's sign. Both functions share broadcasting rules, but they differ whenever negative values are involved.

What happens when the divisor is zero?

MATLAB defines rem(A, 0) as NaN (and NaN + NaNi for complex operands). RunMat mirrors this behaviour so error handling and edge cases match scripts written for MATLAB.

Does rem support infinite inputs?

Yes. When B is infinite and A is finite, the quotient truncates to zero and the result is exactly A. If the dividend is infinite while the divisor is finite, the remainder is NaN, preserving MATLAB's rules for indeterminate operations.

Can rem operate on complex numbers?

Absolutely. Both operands may be complex; RunMat computes rem(z, w) as z - w .* fix(z ./ w), applying fix to the real and imaginary components independently so the result matches MATLAB's complex arithmetic.

Does rem change integer or logical inputs?

Internally, inputs promote to double precision before the remainder is computed. The result is returned as doubles, aligning with MATLAB's numeric tower while still supporting logical, integer, and character array arguments.

Will rem stay on the GPU?

It stays on-device when the provider implements elem_div, unary_fix, elem_mul, and elem_sub; fusion-aware planners can keep intermediates resident for downstream kernels. Otherwise the runtime gathers the operands, executes the CPU fallback, and returns a host tensor so correctness is unaffected.

See Also

mod, fix, floor, gpuArray, gather

Source & Feedback