BAEL-837 - Why Does Changing the Sum Order Returns a Different Result (#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.
This commit is contained in:
iaforek 2017-06-14 18:26:12 +01:00 committed by maibin
parent ba4257d233
commit 6786eb13d8
2 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package com.baeldung.maths;
import java.math.BigDecimal;
public class FloatingPointArithmetic {
public static void main(String[] args) {
double a = 13.22;
double b = 4.88;
double c = 21.45;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
double sum_ab = a + b;
System.out.println("a + b = " + sum_ab);
double abc = a + b + c;
System.out.println("a + b + c = " + abc);
double ab_c = sum_ab + c;
System.out.println("ab + c = " + ab_c);
double sum_ac = a + c;
System.out.println("a + c = " + sum_ac);
double acb = a + c + b;
System.out.println("a + c + b = " + acb);
double ac_b = sum_ac + b;
System.out.println("ac + b = " + ac_b);
double ab = 18.1;
double ac = 34.67;
double sum_ab_c = ab + c;
double sum_ac_b = ac + b;
System.out.println("ab + c = " + sum_ab_c);
System.out.println("ac + b = " + sum_ac_b);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
System.out.println("d + e + f = " + def);
System.out.println("d + f + e = " + dfe);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Test;
public class FloatingPointArithmeticTest {
@Test
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
double a = 13.22;
double b = 4.88;
double c = 21.45;
double result = 39.55;
double abc = a + b + c;
double acb = a + c + b;
Assert.assertEquals(result, abc, 0);
Assert.assertNotEquals(result, acb, 0);
double ab = 18.1;
double ac = 34.67;
double ab_c = ab + c;
double ac_b = ac + b;
Assert.assertEquals(result, ab_c, 0);
Assert.assertNotEquals(result, ac_b, 0);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal sum = new BigDecimal("39.55");
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
Assert.assertEquals(0, def.compareTo(sum));
Assert.assertEquals(0, dfe.compareTo(sum));
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
}
}