Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double increments in Java [duplicate]

Possible Duplicate:
How to iterate between 0.1f and 1.0f with 0.1f increments in Java?

Part of my program needs to use values inside a while loop as:

0.1

0.2

0.3

...

0.9

so I need to provide them inside that loop. Here is the code:

double x = 0.0;
while ( x<=1 )
{
// increment x by 0.1 for each iteration
x += 0.1;
}

I need the output to be EXACTLY:

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

But it actually gives me something like:

0.1

0.2

0.300000000000000000000000004

0.4

0.5

0.6

0.79999999999999999999999999

0.89999999999999999999999999

0.99999999999999999999999999

like image 780
user1483799 Avatar asked Dec 12 '12 04:12

user1483799


People also ask

How do you add two increments in Java?

Can I increment by 2 in a for loop in Java? If you want to increment or decrement a variable by an amount other than 1 , you can use += and -= . For example, i += 2 increments i by 2 : int i = 2; while (i <= 8) { System.

Can you use ++ on doubles in Java?

The postfix increment operator ++ adds one to a variable. Usually the variable is an integer type (byte, short, int, or long) but it can be a floating point type (float or double). No character is allowed between the two plus signs.

Can we increment 2 in for loop Java?

We need to use i+=2 in case we need to increment for loop by 2 in java. Let's say we want print all even numbers between 1 to 20 in Java. We can write a for loop and start the loop from 2 by using i=2 in initialization part and increment the loop by 2 in each iteration by using i+=2 .

How do you double increment in a for loop?

the best things in life are simple. x = 12; // or some arbitruary value for( int i = 0; i < 10; i++, x++ ) { // Code... }


2 Answers

Welcome to the world of floating point, where 0.1 isn't 0.1. The problem is that many numbers, including 0.1, cannot be represented exactly in a double. So you aren't really adding exactly 0.1 to x each time through the loop.

One approach is to use integer arithmetic and divide by 10:

int i = 0;
while (i <= 10) {
    double x = i / 10.0;
    . . .
    i++;
}

Another approach is to make x a BigDecimal, where you can specify that you want a particular precision. It basically is doing what the above loop does (an integer plus a scale), but packaged up in a nice class with lots of bells and whistles. Oh, and it has arbitrary precision.

like image 137
Ted Hopp Avatar answered Sep 20 '22 18:09

Ted Hopp


you need to use the decimal formatter to get the expected output.

Below is the code for generating the expected output:

import java.text.DecimalFormat;


public class FloatIncrement {

    public static void main (String args[]){

        double x= 0.0;
        DecimalFormat form = new DecimalFormat("#.#");      
        while(x<0.9){
            x= x+0.1;
            System.out.println("X : "+Double.valueOf(form.format(x)));          

        }

    }
}
like image 32
simbu94 Avatar answered Sep 20 '22 18:09

simbu94