Created
December 3, 2012 16:01
-
-
Save jeanlaurent/4195941 to your computer and use it in GitHub Desktop.
Double Precision Calculation in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import static DoubleMath.Operation.ADD; | |
import static DoubleMath.Operation.DIVIDE; | |
import static DoubleMath.Operation.MULTIPLY; | |
import static DoubleMath.Operation.SUBSTRACT; | |
import static java.math.RoundingMode.HALF_EVEN; | |
public class DoubleMath { | |
private double leftOperand; | |
private Operation operation; | |
private DoubleMath(Operation operation, double leftOperand) { | |
this.operation = operation; | |
this.leftOperand = leftOperand; | |
} | |
public static DoubleMath divide(double divisor) { | |
return new DoubleMath(DIVIDE, divisor); | |
} | |
public static DoubleMath add(double divisor) { | |
return new DoubleMath(ADD, divisor); | |
} | |
public static DoubleMath substract(double divisor) { | |
return new DoubleMath(SUBSTRACT, divisor); | |
} | |
public static DoubleMath multiply(double divisor) { | |
return new DoubleMath(MULTIPLY, divisor); | |
} | |
public double by(double rightOperand) { | |
BigDecimal leftDecimal = new BigDecimal(leftOperand); | |
BigDecimal rightDecimal = new BigDecimal(rightOperand); | |
switch (operation) { | |
case ADD: | |
return leftDecimal.add(rightDecimal).setScale(2, HALF_EVEN).doubleValue(); | |
case SUBSTRACT: | |
return leftDecimal.subtract(rightDecimal).setScale(2, HALF_EVEN).doubleValue(); | |
case MULTIPLY: | |
return leftDecimal.multiply(rightDecimal).setScale(2, HALF_EVEN).doubleValue(); | |
case DIVIDE: | |
return leftDecimal.divide(rightDecimal, 2, HALF_EVEN).doubleValue(); | |
} | |
throw new RuntimeException("no op : " + operation); | |
} | |
public double to(double rightOperand) { | |
return by(rightOperand); | |
} | |
enum Operation { | |
ADD, MULTIPLY, DIVIDE, SUBSTRACT; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import static DoubleMath.*; | |
import static org.fest.assertions.Assertions.assertThat; | |
public class DoubleMathTest { | |
@Test | |
public void should_add_with_two_digits() throws Exception { | |
assertThat(add(5.6).to(5.8)).isEqualTo(11.4); | |
} | |
@Test | |
public void should_substract_with_two_digits() throws Exception { | |
assertThat(substract(1.35).to(1.3)).isEqualTo(0.05); | |
} | |
@Test | |
public void should_multiply_with_two_digits() throws Exception { | |
assertThat(multiply(12).by(25.4)).isEqualTo(304.8); | |
} | |
@Test | |
public void should_divide_with_two_digits() throws Exception { | |
assertThat(divide(527).by(480)).isEqualTo(1.1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment