Skip to content

Commit 6786eb1

Browse files
iaforekmaibin
authored andcommitted
BAEL-837 - Why Does Changing the Sum Order Returns a Different Result (eugenp#2070)
* Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention * BAEL-821 Added more test and updated round methods. * BAEL-837 - initial commit. A few examples of adding doubles. * BAEL-837 - Couple of smaller changes * BAEL-837 - Added jUnit test.
1 parent ba4257d commit 6786eb1

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.maths;
2+
3+
import java.math.BigDecimal;
4+
5+
public class FloatingPointArithmetic {
6+
public static void main(String[] args) {
7+
8+
double a = 13.22;
9+
double b = 4.88;
10+
double c = 21.45;
11+
12+
System.out.println("a = " + a);
13+
System.out.println("b = " + b);
14+
System.out.println("c = " + c);
15+
16+
double sum_ab = a + b;
17+
System.out.println("a + b = " + sum_ab);
18+
19+
double abc = a + b + c;
20+
System.out.println("a + b + c = " + abc);
21+
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);
27+
28+
double acb = a + c + b;
29+
System.out.println("a + c + b = " + acb);
30+
31+
double ac_b = sum_ac + b;
32+
System.out.println("ac + b = " + ac_b);
33+
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+
41+
BigDecimal d = new BigDecimal(String.valueOf(a));
42+
BigDecimal e = new BigDecimal(String.valueOf(b));
43+
BigDecimal f = new BigDecimal(String.valueOf(c));
44+
45+
BigDecimal def = d.add(e).add(f);
46+
BigDecimal dfe = d.add(f).add(e);
47+
48+
System.out.println("d + e + f = " + def);
49+
System.out.println("d + f + e = " + dfe);
50+
}
51+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)