ExtraMath (Jython API documentation)
Date: 2022-09-10

org.python.core.util

## Class ExtraMath

- java.lang.Object
- - org.python.core.util.ExtraMath

-     public class ExtraMath
      extends java.lang.Object

  A static utility class with two additional math functions.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static double` | `CLOSE`
    `static double` | `EPSILON`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ExtraMath()`

  - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static boolean` | `close(double v, double w)`
    `static boolean` | `close(double v, double w, double tol)` Are v and w "close" to each other?
    `static double` | `closeFloor(double v)` Returns floor(v) except when v is very close to the next number, when it returns ceil(v);
    `static double` | `round(double x, int n)` Round the argument x to n decimal places.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### EPSILON

          public static double EPSILON

    - #### CLOSE

          public static double CLOSE

  - ### Constructor Detail

    - #### ExtraMath

          public ExtraMath()

  - ### Method Detail

    - #### close

          public static boolean close(double v,
                                      double w,
                                      double tol)

      Are v and w "close" to each other? Uses a scaled tolerance.

    - #### close

          public static boolean close(double v,
                                      double w)

    - #### closeFloor

          public static double closeFloor(double v)

      Returns floor(v) except when v is very close to the next number, when it returns ceil(v);

    - #### round

          public static double round(double x,
                                     int n)

      Round the argument x to n decimal places. (Rounding is half-up in Python 2.) The method uses BigDecimal, to compute r(x*10n)*10-n, where r() round to the nearest integer. It takes some short-cuts for extreme values.

      For sufficiently small x*10n, the rounding is to zero, and the return value is a signed zero (same sign as x). Suppose x = a*2b, where the significand we must have a<2. Sufficiently small means such that n log210 < -(b+2).

      For sufficiently large x*10n, the adjustment of rounding is too small to affect the least significant bit. That is a*2b represents an amount greater than one, and rounding no longer affects the value, and the return is x. Since the matissa has 52 fractional bits, sufficiently large means such that n log210 > 52-b.

      Parameters:
        `x` - to round
        `n` - decimal places
      Returns:
        x rounded.

Jython homepage