Repository navigation
Description
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:
stdlib/lib/node_modules/@stdlib/array/base/logspace/lib/main.js
Lines 46 to 59 in 548ec92
| 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);
}
JSExpected 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=2Version
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
stdlib-bot commentedon Nov 27, 2026
👋 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.
Udit-Agarwal20 commentedon Nov 27, 2026
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.
sarthakkaushik927 commentedon Nov 28, 2026
On the question of which value to return:
array/linspacealready documents that a single-element output gets thestopvalue whenendpointistrue. Sincelogspacealways includes its endpoint, returning10^bwould 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.
Udit-Agarwal20 commentedon Nov 28, 2026
Thanks for investigating and for offering to defer!
I agree that returning
10^bis consistent with the documented endpoint behavior ofarray/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!
[-]bug: array/logspace returns two elements when length is 1[/-][+][BUG]: array/logspace returns two elements when length is 1[/+]fix: return a single element in `array/base/logspace` when `length` i…