0

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"

2
  • A for loop that increments by .001? Commented Jan 13, 2017 at 7:10
  • 2
    When you have integers like 200000 to 399999, you can use a for loop for that and divide the index by 1000 Commented Jan 13, 2017 at 7:11

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

2
double start = 200.0;
double end = 399.999;
double increment = 0.001;

for (double v = start; v < end + increment / 2; v += increment) {
    System.out.printf("%.3f\n", v);
}

9 Comments

just wonder how did you know the accumulated addition of 0.001 is not going to make the last number larger than 399.99 due to approximation of floating point number?
I did know, but printf will round it anyway. This was an easy question, and there's no need to overcomplicate things at this stage...
obviously you are unaware of pitfalls of floating point calculations. As floating number cannot always accurately represent decimal number, it is possible that after bunch of calculations, the last number (399.99 in this example) will be something slightly larger (399.99000000000001, just a make-up example). This v should be included in the sequence but it will now be skipped because it is larger than 399.99 (although it should be treated as equals)
Please, don't presume to know what I know. I'm very much aware, but I deemed this quite off topic for this question.
No it is not off topic. That's why I asked in the first comment. Such way of generating sequence may cause the last value in the sequence missing, For which means giving wrong result.
|
0

Here's another way to do it:

public static void counterGenerator(double start, double end) {
    DecimalFormat counterInDecimalFormat = new DecimalFormat("0.000");
    String counterInString = counterInDecimalFormat.format(start);
    System.out.println(counterInString); // This is the counter in String format.
    double counter = Double.parseDouble(counterInString);
    if (counter < end)
        counterGenerator(start + 0.001, end);
    return;
}

In the above example, you have your counter in String format in the variable called counterInString

But you need not worry about the problems associated with incrementing a Double which is actual residing in a String variable. In the code, you can see the incrementing task is being done by a double counter which gets convert back to String by using the DecimalFormat class.

Here keeping your counter as a String helps you to retain the 0s after decimal in numbers like 200.000.

Hope it helps!

1 Comment

No offence, but don't you think that this is too complex or non efficient for this purpose?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.