Skip to content

[BUG]: array/logspace returns two elements when length is 1 #16910

Done

Description

@Udit-Agarwal20

Description

Both @stdlib/array/logspace and @stdlib/array/base/logspace return two elements when the requested output length is 1.

The public API accepts a nonnegative integer as the output length. However, the base implementation initializes the array with the starting value and unconditionally appends the stopping value, resulting in two elements.

This behavior was reproduced locally and against upstream develop at commit 548ec92342c05eb797689c77df55600e3e80fbaa.

The following inputs reproduce the issue in both packages:

  • (0, 2, 1) returns [1, 100].
  • (2, 0, 1) returns [100, 1].
  • (2, 2, 1) returns [100, 100].

All 43 existing assertions across both logspace test suites pass, but neither suite covers the single-element case.

Relevant implementation:

if ( len === 0 ) {
return [];
}
// Calculate the increment:
N = len - 1;
d = ( b-a ) / N;
// Build the output array...
arr = [ pow( 10, a ) ];
for ( i = 1; i < N; i++ ) {
arr.push( pow( 10, a+(d*i) ) );
}
arr.push( pow( 10, b ) );
return arr;

A possible approach is to handle the single-element case explicitly, document the intended endpoint behavior, and add regression tests for both packages.

Related Issues

No directly related issues or pull requests were found. Issue #16335 concerns a different precision-related defect in blas/ext/logspace.

Questions

For a requested length of one, should the function return the starting value (10^a), consistent with iter/logspace, or the stopping value (10^b), consistent with the default endpoint behavior of array/linspace?

I would be happy to work on the fix and accompanying regression tests once the intended behavior is clarified.

Demo

No response

Reproduction

From the root of the stdlib checkout, run:

NODE_PATH="$PWD/lib/node_modules" node <<'JS'
for (const name of [
    '@stdlib/array/logspace',
    '@stdlib/array/base/logspace'
]) {
    const result = require(name)(0, 2, 1);
    console.log(name, JSON.stringify(result), 'length=' + result.length);
}
JS

Expected Results

Both functions should return an array containing exactly one element.

For equal exponents, logspace(0, 0, 1) should return [1].

For unequal exponents, the singleton endpoint should follow the intended behavior confirmed by the maintainers.

Actual Results

@stdlib/array/logspace [1,100] length=2
@stdlib/array/base/logspace [1,100] length=2

Version

develop (548ec92)

Environments

Node.js

Browser Version

No response

Node.js / npm Version

Node.js v24.15.0 / npm 12.0.2

Platform

macOS (Darwin 27.0.0 arm64)

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Activity

  1. stdlib-bot commented on Nov 27, 2026

    @stdlib-bot
    Contributor

    👋 Hi there! 👋

    And thank you for opening your first issue! We will get back to you shortly. 🏃 💨

    If you have any further questions while waiting for a response, please join our Zulip community to chat with project maintainers and other community members.

  2. Udit-Agarwal20 commented on Nov 27, 2026

    @Udit-Agarwal20
    Author

    I've investigated the fix further and prepared both singleton endpoint alternatives locally.

    Both implementations resolve the length mismatch with a minimal change to the shared base function. The regression tests pass for both packages, and differential testing confirms that non-singleton behavior remains unchanged across the tested inputs.

    I'd be happy to submit the implementation and accompanying regression tests once the preferred singleton endpoint is confirmed.

  3. sarthakkaushik927 commented on Nov 28, 2026

    @sarthakkaushik927
    Contributor

    On the question of which value to return: array/linspace already documents that a single-element output gets the stop value when endpoint is true. Since logspace always includes its endpoint, returning 10^b would be consistent with it.

    I've opened #16937 with that approach, plus tests and notes for both packages. It's a one-line change if maintainers prefer the start value. @Udit-Agarwal20, happy to defer to your version if you'd rather submit yours.

  4. Udit-Agarwal20 commented on Nov 28, 2026

    @Udit-Agarwal20
    Author

    Thanks for investigating and for offering to defer!

    I agree that returning 10^b is consistent with the documented endpoint behavior of array/linspace.

    I've already implemented and validated both endpoint alternatives locally, including regression, boundary, and differential tests. Since I originally reported this issue and have the implementation ready, I'd appreciate the opportunity to submit my version once the maintainers confirm the preferred behavior.

    Thanks for understanding and for your contribution!

  5. added
    Bug
    on Nov 28, 2026
  6. changed the title [-]bug: array/logspace returns two elements when length is 1[/-] [+][BUG]: array/logspace returns two elements when length is 1[/+] on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions