com.devexperts.util
Class MathUtil

java.lang.Object
  extended by com.devexperts.util.MathUtil

public class MathUtil
extends Object

A collection of non-trivial mathematical utility methods.


Method Summary
static int div(int a, int b)
          Returns quotient according to number theory - i.e. when remainder is zero or positive.
static long div(long a, long b)
          Returns quotient according to number theory - i.e. when remainder is zero or positive.
static int rem(int a, int b)
          Returns remainder according to number theory - i.e. when remainder is zero or positive.
static long rem(long a, long b)
          Returns remainder according to number theory - i.e. when remainder is zero or positive.
static double roundDecimal(double x)
          Rounds a specified double number to a decimal number with at most 14 significant digits and at most 14 digits after decimal point.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

roundDecimal

public static double roundDecimal(double x)
Rounds a specified double number to a decimal number with at most 14 significant digits and at most 14 digits after decimal point. When x is integer, NaN or infinity, then x is returned (this method does not round big integers to powers of 10).

For example, suppose you have 1 dollar and 10 cents and you pay 20 cent. You should keep 90 cents. However, the following expression is false in Java:

1.1 - 0.2 == 0.9
because both 1.1 and 0.2 do not have precise representations in double. To make this comparison work, you have to use roundDecimal method:
roundDecimal(1.1 - 0.2) == 0.9

As a general rule, you should use roundDecimal after any operation (addition, subtraction, multiplication, division) on two decimal numbers if you know that the result is a decimal with at most 14 significant digits and at most 14 digits after decimal point.


div

public static int div(int a,
                      int b)
Returns quotient according to number theory - i.e. when remainder is zero or positive.

Parameters:
a - dividend
b - divisor
Returns:
quotient according to number theory

div

public static long div(long a,
                       long b)
Returns quotient according to number theory - i.e. when remainder is zero or positive.

Parameters:
a - dividend
b - divisor
Returns:
quotient according to number theory

rem

public static int rem(int a,
                      int b)
Returns remainder according to number theory - i.e. when remainder is zero or positive.

Parameters:
a - dividend
b - divisor
Returns:
remainder according to number theory

rem

public static long rem(long a,
                       long b)
Returns remainder according to number theory - i.e. when remainder is zero or positive.

Parameters:
a - dividend
b - divisor
Returns:
remainder according to number theory


Copyright © 2013 Devexperts. All Rights Reserved.