cospi — Compute cos(X*pi) accurately with exact integer and half-integer results.

cospi(X) computes cos(X*pi) without first materializing the approximate floating-point value X*pi. Odd half-integers return exactly zero and integers return exactly +1 or -1.

Syntax

Y = cospi(X)

Inputs

NameTypeRequiredDefaultDescription
XAnyYesInput scalar, array, logical array, complex value, or gpuArray.

Returns

NameTypeDescription
YAnyElement-wise cos(X*pi) result with exact integer and half-integer handling.

Errors

IdentifierWhenMessage
RunMat:cospi:InvalidInputInput cannot be interpreted as supported numeric/logical/complex data.cospi: invalid input
RunMat:cospi:InternalInternal gather/conversion/allocation flow failed.cospi: internal error

How cospi works

  • Operates element-wise on scalars, vectors, matrices, and N-D tensors.
  • Integer and logical inputs are promoted to double precision before evaluation.
  • Returns exactly 0 for odd half-integer inputs and exactly ±1 for integer inputs.
  • Complex inputs use the analytic extension of cos(pi*z) while preserving exact real-axis sine/cosine factors when possible.
  • Output shape matches the input shape; non-finite real inputs produce NaN.
  • String inputs are unsupported and raise a builtin-scoped error.

Examples

Cosine of multiples of pi

Y = cospi([0 1/2 1 3/2 2])

Expected output:

Y = [1 0 -1 0 1]

Avoid floating-point drift at half-integer multiples

y = cospi(1/2)

Expected output:

y = 0

Using cospi with coding agents

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

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

FAQ

Why use cospi(X) instead of cos(X*pi)?

pi is not exactly representable in binary floating point. cospi short-circuits integer and half-integer multiples so common exact results do not contain small rounding noise.

Does cospi support gpuArray inputs?

Yes. RunMat currently gathers gpuArray inputs and evaluates the exact host implementation; no GPU kernel is launched yet.

Trigonometry

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

Elementwise

abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · uint32

Reduction

all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

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

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Discrete

lcm · primes

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cospi 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.