2
public static void main(String[] args) {
        double firstDouble = 1.8d;
        double secondDouble = 1.65d;


        float firstFloat = 1.8f;
        float secondFloat = 1.65f;
        System.out.println("DOUBLE SUM : "+(firstDouble + secondDouble));
        System.out.println("FLOAT SUM :"+(firstFloat + secondFloat));

        System.out.println("DOUBLE SUM"+firstDouble + secondDouble);
        System.out.println("FLOAT SUM"+firstFloat + secondFloat);
    }

OUT PUT:

DOUBLE SUM : 3.45
FLOAT SUM :3.4499998

DOUBLE SUM :1.81.65
FLOAT SUM :1.81.65

my questions

1) In the first set of out put why it is giving different values 3.45 and 3.4499998 for the same values 2) In the second set of out put why the out put is different from first out put.

Thanks in advance...

PSR
  • 39,804
  • 41
  • 111
  • 151
  • 1
    1) 1.8d != 1.8f and 1.65d != 1.65f If there were the same, you wouldn't need different put a `f` and `d` 2) brackets change precedence because the order you evaluate things matters. – Peter Lawrey May 17 '13 at 06:18

4 Answers4

1

System.out.println("DOUBLE SUM"+firstDouble + secondDouble);

Here argument takes as a String. and plus/addition operator works as String concatition. Bracket has more priortity so that evalutes first.

First question is already answered. Please find here

Community
  • 1
  • 1
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
  • To comprehend @Shubhrajyoti's answer, you should understand operator precedence. You can take a quick look here: http://introcs.cs.princeton.edu/java/11precedence/ – zEro May 17 '13 at 04:57
0

In the second set you missed the brackets , it is doing something else with the String in the System.out.println statement "Double sum " and "Float sum ", so add brackets with your arithmetic expressions to get the correct result.

System.out.println("DOUBLE SUM : "+(firstDouble + secondDouble));
System.out.println("FLOAT SUM :"+(firstFloat + secondFloat));

System.out.println("DOUBLE SUM"+ (firstDouble + secondDouble));
System.out.println("FLOAT SUM"+(firstFloat + secondFloat));

This gives

DOUBLE SUM 3.45
FLOAT SUM 3.4499998
DOUBLE SUM 3.45
FLOAT SUM 3.4499998
Sridhar
  • 1,832
  • 3
  • 23
  • 44
  • I know that .So what is happening there. – PSR May 17 '13 at 04:30
  • when i remove brackets, for me its giving the concatenation only like this `DOUBLE SUM1.81.65 FLOAT SUM1.81.65` – Sridhar May 17 '13 at 04:34
  • @PSR Without the brackets it is interpreted as `("DOUBLE SUM"+firstDouble) + secondDouble"`, which is two string concatenations and no floating-point additions at all. – user207421 May 17 '13 at 04:45
0

1) because float isn't precise enough to calculate sum with precision better than (3.45-3.4499998)=2E-7 http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3

2) "DOUBLE SUM"+firstDouble converts firstDouble into a String and concatenates them, then it concatenate secondDouble. When you put ( ) it calculates the sum first and the concatenates result to "DOUBLE SUM"

BlacKow
  • 281
  • 2
  • 12
0

This is very useful: http://floating-point-gui.de/languages/java/

For a short answer, try to use BigDecimal whenever you are dealing with floating points.

BigDecimal a = new BigDecimal("1.8");
BigDecimal b = new BigDecimal("1.65");
BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 3.45
Howard Lin
  • 41
  • 1
  • 4
  • BigDecimal is a good data type to use if short decimal fractions are particularly important, such as in financial transactions. It has no particular merit in other contexts, such as scientific calculations, where ten is not special, and is often less efficient and convenient than double. – Patricia Shanahan May 17 '13 at 15:37