Package org.jscience.mathematics.number
Provides common types of numbers most of them implementing the
field
interface.
Although numbers defined in this package are not as fast as primitives types
(e.g. int
or double
). They have many
advantages (such as arbitrary size for LargeInteger
or precision for Real
) which
make them irreplaceable in some calculations. This can be illustrated with the following example:
double x = 10864; double y = 18817; double z = 9 * Math.pow(x, 4.0)- Math.pow(y, 4.0) + 2 * Math.pow(y, 2.0); System.out.println("Result : " + z); > Result : 2.0The mathematically correct value is z=1. However, Java compilers using ANSI/IEEE double precision numbers evaluate z=2. Not even the first digit is correct! This is due to a rounding error occurring when subtracting two nearly equal floating point numbers. Now, lets write the same formula using
Real
numbers:int accuracy = 20; // 20 decimal zeros for integer values. Real x = Real.valueOf(10864, accuracy); Real y = Real.valueOf(18817, accuracy); Real z = x.pow(4).times(9).plus(y.pow(4).opposite()).plus(y.pow(2).times(2)); System.out.println("Result : " + z); > Result : 1.00000Not only the correct result is returned, but this result is also guaranteed to be
1 ± 0.00001
.
Only exact digits are written out, for example the following displays the first exact
digits of sqrt(2)
: Real two = Real.valueOf(2, 100); // 2.0000..00 (100 zeros after decimal point). Real sqrt2 = two.sqrt(); System.out.println("sqrt(2) = " + sqrt2); System.out.println("Precision = " + sqrt2.getPrecision() + " digits."); > sqrt(2) = 1.414213562373095048801688724209698078569671875376948 > Precision = 53 digits.
-
Class Summary Class Description Complex This class represents an immutable complex number.Float64 This class represents a 64 bits floating point number.FloatingPoint This class represents a floating point number of arbitrary precision.Integer64 This class represents a 64 bits integer number.LargeInteger This class represents an immutable integer number of arbitrary size.ModuloInteger This class represents a modulo integer.Number<T extends Number<T>> This class represents avalue-type
number.Rational This class represents the ratio of twoLargeInteger
numbers.Real This class represents a real number of arbitrary precision with known/guaranteed uncertainty.