Top Description Fields Constructors Methods
org.python.core.util

public Class ExtraMath

extends Object
Class Inheritance
Imports
java.math.BigDecimal, .RoundingMode

A static utility class with two additional math functions.

Field Summary

Modifier and TypeField and Description
public static double
public static double

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static boolean
close(double v, double w, double tol)

Are v and w "close" to each other?

public static boolean
close(double v, double w)

public static double
closeFloor(double v)

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

public static double

Returns:

x rounded.
round
(double
to round
x
,
int
decimal places
n
)

Round the argument x to n decimal places.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CLOSEback to summary
public static double CLOSE
EPSILONback to summary
public static double EPSILON

Constructor Detail

ExtraMathback to summary
public ExtraMath()

Method Detail

closeback to summary
public static boolean close(double v, double w, double tol)

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

closeback to summary
public static boolean close(double v, double w)
closeFloorback to summary
public static double closeFloor(double v)

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

roundback to summary
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:double

to round

n:int

decimal places

Returns:double

x rounded.