Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The two values that i have are:

firstval=200.000 
Secondval=399.999,

I have to generate a numbers such that when the first decimal part should get incremented till 999 for the integral part, next the integral part should be incremented and then decimal part resets to 000 and starts incrementing for the new number . And this happens till 399. Like

200.001,200.002.....200.999,201.000,201.002....399.998,399.999"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is a nice way to get required array with Java 8 Stream API

(1) Use double incrementation

 double[] sequence = DoubleStream.iterate(200.0, d -> d + 0.001).limit((int) (1 + (399.999 - 200.0) / 0.001)).toArray();

Note, that summing up lots of doubles will likely give some error, for example on my laptop the last number in the sequence is 399.99899999686227

(2) Better way is to generate integer stream and map it to doubles:

 double[] sequence = IntStream.range(200000, 400000).mapToDouble( i -> i * 0.001).toArray();

In this case no error from adding multiple doubles will be accumulated


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...