Skip to content
fn

mlx_scalar

→mlx::core::array
::mlx::core::array mlx_scalar(double v, int dt)

Build a 0-D MLX array holding v cast to dt.

Used as a broadcastable RHS in arithmetic helpers (e.g. x + 1) to avoid materialising a same-shape constant tensor. The literal flows into the kernel inline, paying zero allocation cost.

Parameters

vdouble
Scalar value to embed. Cast to float before MLX construction.
dtmlx::core::Dtype
Final MLX dtype the scalar should be cast to.

Returns

mlx::core::array

A rank-0 MLX array of dtype dt holding v.

Notes

The intermediate float cast loses precision for very large double-precision values; callers needing exact int64 broadcasts should construct the array directly.