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 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 multiplying value 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 of value.

      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 causes value to be returned unmodified).
      Returns:
      the truncated value.
    • truncate

      public static double[] truncate​(double[] value, int significantFractionalBinaryDigits)
      Applies truncate(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 uses Integer.MAX_VALUE to this purpose.

      Parameters:
      value - an array.
      significantFractionalBinaryDigits - the number of significant fractional binary digits (Integer.MAX_VALUE causes the contents of value to be returned unmodified).
      Returns:
      value.
      See Also:
      truncate(double, int)