From f6a39f84bb36adad56c75ed77e411ab775d2cd54 Mon Sep 17 00:00:00 2001 From: iaforek Date: Tue, 13 Jun 2017 22:29:27 +0100 Subject: [PATCH] BAEL-837 - Added jUnit test. --- .../maths/FloatingPointArithmetic.java | 27 ++++++----- .../maths/FloatingPointArithmeticTest.java | 45 +++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java diff --git a/core-java/src/main/java/com/baeldung/maths/FloatingPointArithmetic.java b/core-java/src/main/java/com/baeldung/maths/FloatingPointArithmetic.java index 9cdeeb5e81..4163adcf09 100644 --- a/core-java/src/main/java/com/baeldung/maths/FloatingPointArithmetic.java +++ b/core-java/src/main/java/com/baeldung/maths/FloatingPointArithmetic.java @@ -13,30 +13,37 @@ public class FloatingPointArithmetic { System.out.println("b = " + b); System.out.println("c = " + c); - double ab = a + b; - System.out.println("a + b = " + ab); + 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 ac = a + c; - System.out.println("a + c = " + ac); + 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 = ac + b; + 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; - BigDecimal dfe; - - def = d.add(e).add(f); - dfe = d.add(f).add(e); + 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); diff --git a/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java b/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java new file mode 100644 index 0000000000..2066f13c6d --- /dev/null +++ b/core-java/src/test/java/com/baeldung/maths/FloatingPointArithmeticTest.java @@ -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)))); + } +}