Package it.unimi.dsi.law.util
Class Precision
java.lang.Object
it.unimi.dsi.law.util.Precision
public class Precision extends Object
A set of commodity methods to manipulate precision of doubles.
-
Constructor Summary
Constructors Constructor Description Precision()
-
Method Summary
Modifier and Type Method Description static double[]
truncate(double[] value, int significantFractionalBinaryDigits)
Appliestruncate(double, int)
to the given array.static double
truncate(double value, int significantFractionalBinaryDigits)
Truncates the given double value to the given number of fractional binary digits.
-
Constructor Details
-
Precision
public Precision()
-
-
Method Details
-
truncate
public static double truncate(double value, int significantFractionalBinaryDigits)Truncates the given double value to the given number of fractional binary digits. This is equivalent to multiplyingvalue
by 2significantBinaryDigits
, taking the floor and then multiplying the result by 2-significantBinaryDigits
(the computation should be performed in arbitrary precision). By choice, this method does not apply any kind of rounding.Note that
significantBinaryDigits
can be negative: in that case, on positive values the method is equivalent to applying the mask-1L << -significantBinaryDigits
to the integer part ofvalue
.As an example, if you have an estimate v ± e, the right value to be passed for v is
Math.floor(-Math.log(e)/Math.log(2))-1
.- Parameters:
value
- the value to be truncated.significantFractionalBinaryDigits
- the number of significant fractional binary digits (Integer.MAX_VALUE
causesvalue
to be returned unmodified).- Returns:
- the truncated value.
-
truncate
public static double[] truncate(double[] value, int significantFractionalBinaryDigits)Appliestruncate(double, int)
to the given array.Warning: previous implementations of this method used the special value -1 to indicate that
value
was to be left unchanged. The current version usesInteger.MAX_VALUE
to this purpose.- Parameters:
value
- an array.significantFractionalBinaryDigits
- the number of significant fractional binary digits (Integer.MAX_VALUE
causes the contents ofvalue
to be returned unmodified).- Returns:
value
.- See Also:
truncate(double, int)
-