File tree Expand file tree Collapse file tree
main/java/com/baeldung/maths
test/java/com/baeldung/maths Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,30 +13,37 @@ public static void main(String[] args) {
1313 System .out .println ("b = " + b );
1414 System .out .println ("c = " + c );
1515
16- double ab = a + b ;
17- System .out .println ("a + b = " + ab );
16+ double sum_ab = a + b ;
17+ System .out .println ("a + b = " + sum_ab );
1818
1919 double abc = a + b + c ;
2020 System .out .println ("a + b + c = " + abc );
2121
22- double ac = a + c ;
23- System .out .println ("a + c = " + ac );
22+ double ab_c = sum_ab + c ;
23+ System .out .println ("ab + c = " + ab_c );
24+
25+ double sum_ac = a + c ;
26+ System .out .println ("a + c = " + sum_ac );
2427
2528 double acb = a + c + b ;
2629 System .out .println ("a + c + b = " + acb );
2730
28- double ac_b = ac + b ;
31+ double ac_b = sum_ac + b ;
2932 System .out .println ("ac + b = " + ac_b );
3033
34+ double ab = 18.1 ;
35+ double ac = 34.67 ;
36+ double sum_ab_c = ab + c ;
37+ double sum_ac_b = ac + b ;
38+ System .out .println ("ab + c = " + sum_ab_c );
39+ System .out .println ("ac + b = " + sum_ac_b );
40+
3141 BigDecimal d = new BigDecimal (String .valueOf (a ));
3242 BigDecimal e = new BigDecimal (String .valueOf (b ));
3343 BigDecimal f = new BigDecimal (String .valueOf (c ));
3444
35- BigDecimal def ;
36- BigDecimal dfe ;
37-
38- def = d .add (e ).add (f );
39- dfe = d .add (f ).add (e );
45+ BigDecimal def = d .add (e ).add (f );
46+ BigDecimal dfe = d .add (f ).add (e );
4047
4148 System .out .println ("d + e + f = " + def );
4249 System .out .println ("d + f + e = " + dfe );
Original file line number Diff line number Diff line change 1+ package com .baeldung .maths ;
2+
3+ import java .math .BigDecimal ;
4+
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+
8+ public class FloatingPointArithmeticTest {
9+
10+ @ Test
11+ public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult () {
12+ double a = 13.22 ;
13+ double b = 4.88 ;
14+ double c = 21.45 ;
15+ double result = 39.55 ;
16+
17+ double abc = a + b + c ;
18+ double acb = a + c + b ;
19+
20+ Assert .assertEquals (result , abc , 0 );
21+ Assert .assertNotEquals (result , acb , 0 );
22+
23+ double ab = 18.1 ;
24+ double ac = 34.67 ;
25+
26+ double ab_c = ab + c ;
27+ double ac_b = ac + b ;
28+
29+ Assert .assertEquals (result , ab_c , 0 );
30+ Assert .assertNotEquals (result , ac_b , 0 );
31+
32+ BigDecimal d = new BigDecimal (String .valueOf (a ));
33+ BigDecimal e = new BigDecimal (String .valueOf (b ));
34+ BigDecimal f = new BigDecimal (String .valueOf (c ));
35+ BigDecimal sum = new BigDecimal ("39.55" );
36+
37+ BigDecimal def = d .add (e ).add (f );
38+ BigDecimal dfe = d .add (f ).add (e );
39+
40+ Assert .assertEquals (0 , def .compareTo (sum ));
41+ Assert .assertEquals (0 , dfe .compareTo (sum ));
42+
43+ Assert .assertNotEquals (0 , sum .compareTo (new BigDecimal (String .valueOf (acb ))));
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments