1

Why don't DoubleStream or LongStream have similar methods as these ones (which exist in IntStream):

public static IntStream range(int startInclusive, int endExclusive);
public static IntStream rangeClosed(int startInclusive, int endInclusive);

Is there any particular reason?

15
  • 6
    yes there is a very valuable reason. How many double can you fit in the range 1.0 to 2.0 ?
    – jhamon
    Sep 16, 2019 at 13:48
  • 2
    LongStream does have range and rangeClosed. Sep 16, 2019 at 13:50
  • 2
  • 2
    @M.Prokhorov you surely do not want to pay the price of iterate, takeWhile, and/or limit for a simple counting loop. There’s a clean and efficient alternative.
    – Holger
    Sep 16, 2019 at 14:42
  • 3
    …or just consider why for(double d = 0; d < 1; d += 0.1) { } does eleven iterations instead of ten.
    – Holger
    Sep 16, 2019 at 15:32

1 Answer 1

7

First of all, LongStream does have range() methods.

DoubleStream probably doesn't have range() methods because mathematically there are infinite real numbers within any range of 2 double numbers, but only a finite subset of them can be represented with the double type.

Therefore, it's not clear what you want to get when you ask for a DoubleStream in the range of -1.0 to 1.0, for example.

5
  • Ok looks reasonable. Thanks.
    – fidudidu
    Sep 16, 2019 at 13:52
  • 9
    Well, the computer’s double range is finite, but iterating over all values is rarely what the programmer intended. On the other hand, IntStream.range(0, 10).asDoubleStream() or IntStream.range(0, 10).mapToDouble(i -> i*0.1) do already work and it’s obvious to the reader, what these expressions will do.
    – Holger
    Sep 16, 2019 at 14:40
  • 5
    A not-so-rare task is also to create a sequence of values like 0.0, 0.1, 0.2, ... , 1.0, and one could consider some range(min, max, stepSize) method for this. But due to the limited precision of double, this has many caveats. It's probably good to keep this can of worms closed.
    – Marco13
    Sep 16, 2019 at 14:52
  • 3
    @Marco13 This is exactly why we didn't include DoubleStream.range. Do callers want values derived from increments added to the base value, or do they want values at equal intervals? We felt it would be better for callers to derive values explicitly from (say) an IntStream as Holger mentioned above. Sep 21, 2019 at 1:03
  • 1
    @StuartMarks That modest ambiguity did not stop numpy to have linspace : which is a very popular method Mar 13, 2021 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.