Skip to content

[BUG] linspace should always include the stop value #1872

@acsweet

Description

@acsweet

Describe the bug
linspace truncates values in some cases

To Reproduce

(lazily casting to numpy to print to see precision)

import mlx.core as mx
import jax.numpy as jnp
import numpy as np

range = (0.21436651, 0.96568555)
num = 6
a = mx.linspace(range[0], range[1], num, dtype=mx.float32)
print(np.array(a))
a = jnp.linspace(range[0], range[1], num, dtype=jnp.float32)
print(np.array(a))
# [0.21436651 0.3646303  0.5148941  0.6651579  0.8154217  0.9656855 ]
# [0.21436651 0.3646303  0.5148941  0.6651579  0.81542176 0.96568555]

Expected behavior
I'd expect linspace to produce the same results as jax, without truncation for the same dtype

Desktop (please complete the following information):

  • OS Version: 15.2
  • Version: 0.23.0

Additional context
I was using this to create bin edges for a histogram, and it was excluding the right most value because of the truncation when calculating counts. I can work around this by setting the last value if it's not an easy fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions