Provides common types of numbers most of them implementing the
{@link org.jscience.mathematics.structure.Field 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 {@link org.jscience.mathematics.number.LargeInteger LargeInteger}
or precision for {@link org.jscience.mathematics.number.Real 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.0
The 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 {@link org.jscience.mathematics.number.Real 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.00000
Not 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.
|