com.quadrique.jbuildingblocks.core.math
Class JbbMath

java.lang.Object
  extended by com.quadrique.jbuildingblocks.core.math.JbbMath

public class JbbMath
extends Object

A Utility class that provides some simple mathematical functions.


Method Summary
 double convertFloatToDouble(float in)
          This method performs a slower but more acurate conversion than the default (implicit or explicit) float to double conversion.
 boolean equalsDouble(double a, double b)
          uses Dr.
 boolean equalsDouble(double a, double b, double precision)
           
 int getModulo(double value, double bucket)
           
 int getRandomInteger(int max)
           
 String getRandomString(int len)
          returns String containing 0-128 characters for the given length.
static JbbMath newInstance()
           
 double round(double in, int nbOfFractionalDigits)
           
 double round(double in, int nbOfFractionalDigits, RoundingMode roundingMode)
           
 float round(float in, int nbOfFractionalDigits)
           
 float round(float in, int nbOfFractionalDigits, RoundingMode roundingMode)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newInstance

public static JbbMath newInstance()

getRandomInteger

public int getRandomInteger(int max)
                     throws Exception
Parameters:
max - an integer value superior or equal to 1 (if 1, the result will always be 0)
Returns:
a random integer value in the range {0,...,max-1} For instance, getRandomInteger(3) return a random value in the range {0,1,2}
Throws:
Exception

getModulo

public int getModulo(double value,
                     double bucket)
              throws Exception
Parameters:
value -
bucket -
Returns:
the modulo value i.e. the integer number of bucket(s) that are contained in the value (e.g. getNumberOfBucket(5,1) returns 5, getNumberOfBuckets(5,10) returns 0, getNumberOfBuckets(15,10) returns 1...).
Throws:
Exception

round

public float round(float in,
                   int nbOfFractionalDigits,
                   RoundingMode roundingMode)
Parameters:
in -
nbOfFractionalDigits -
roundingMode -
Returns:
the rounded value to the given number of decimals. For instance,

round

public float round(float in,
                   int nbOfFractionalDigits)
Parameters:
in -
nbOfFractionalDigits -
Returns:
the rounded value to the given number of decimals (using the most commonly used rounding mode HALF_UP). For instance,

round

public double round(double in,
                    int nbOfFractionalDigits)
Parameters:
in -
nbOfFractionalDigits -
Returns:
double

round

public double round(double in,
                    int nbOfFractionalDigits,
                    RoundingMode roundingMode)
Parameters:
in -
nbOfFractionalDigits -
roundingMode -
Returns:
double

convertFloatToDouble

public double convertFloatToDouble(float in)
                            throws Exception
This method performs a slower but more acurate conversion than the default (implicit or explicit) float to double conversion. For instance, the following code:

float in = 280085.12f;
System.out.println("INPUT FLOAT VALUE:" + in);

double out = (double)in;
System.out.println("default explicit conversion:" + out);
out = in;
System.out.println("default implicit conversion:" + out);
out=JbbMathHandler.convertFloatToDouble(in);
System.out.println("DOUBLE VALUE:" + out);

gives the following results:
INPUT FLOAT VALUE:          280085.12
default explicit conversion: 280085.125
default implicit conversion: 280085.125
DOUBLE VALUE:                 280085.12

Similarly, the same code with the input value 0.4695f gives the following results:
INPUT FLOAT VALUE:          0.4695
default explicit conversion: 0.46950000524520874
default implicit conversion: 0.46950000524520874
DOUBLE VALUE:                 0.4695

Another interesting and tricky situation when you use 0.0010:
INPUT FLOAT VALUE:          0.0010
default explicit conversion: 0.0010
default implicit conversion: 0.0010
DOUBLE VALUE:                 0.0010 INPUT FLOAT VALUE: default explicit conversion:0.0010000000474974513 default implicit conversion:0.0010000000474974513 DOUBLE VALUE:0.0010

Parameters:
in -
Returns:
the corresponding double value
Throws:
Exception

equalsDouble

public boolean equalsDouble(double a,
                            double b)
uses Dr. Didier Besset's algorithm to calculate the machine precision

Returns:
true if the difference between a and b is less than the default numerical precision

equalsDouble

public boolean equalsDouble(double a,
                            double b,
                            double precision)
Returns:
true if the relative difference between a and b is less than precision

getRandomString

public String getRandomString(int len)
                       throws Exception
returns String containing 0-128 characters for the given length.

Parameters:
len -
Returns:
Throws:
Exception


Copyright © 2001-2008 Quadrique Corporation. All Rights Reserved.