java.lang.Object
imagingbook.common.math.Arithmetic
This class defines static methods implementing arithmetic operations and predicates.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDefault tolerance used for comparingdoublequantities.static final floatDefault tolerance used for comparingfloatquantities. -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleangle(double x, double y) Returns the angle of the Cartesian point (x,y)static doubleclipTo(double x, double low, double high) Limits the first argument to the clipping interval specified by the two other arguments (doubleversion).static floatclipTo(float x, float low, float high) Limits the first argument to the clipping interval specified by the two other arguments (floatversion).static intclipTo(int x, int low, int high) Limits the first argument to the clipping interval specified by the two other arguments (floatversion).static booleanequals(double x, double y) Test for numerical equality (double version) using the default tolerance.static booleanequals(double x, double y, double tolerance) Test for numerical equality (double version) using a specific tolerance.static booleanequals(float x, float y) Test for numerical equality (float version) using the default tolerance.static booleanequals(float x, float y, float tolerance) Test for numerical equality (float version) using a specific tolerance.static double[]getRealRoots(double a, double b, double c) Returns the two real roots of the quadratic function f(x) = ax^2 + bx + c.static booleanisZero(double x) Test for zero (double version) using a predefined tolerance.static booleanisZero(double x, double tolerance) Test for zero (double version) using a specified tolerance.static booleanisZero(float x) Test for zero (float version) using a predefined tolerance.static booleanisZero(float x, float tolerance) Test for zero (float version) using a specified tolerance.static doublemax(double a, double... vals) Returns the maximum of one or more double values.static floatmax(float a, float... vals) Returns the maximum of one or more float values.static intmax(int a, int... vals) Returns the maximum of one or more integer values.static doublemin(double a, double... vals) Returns the minimum of one or more double values.static floatmin(float a, float... vals) Returns the minimum of one or more float values.static intmin(int a, int... vals) Returns the minimum of one or more integer values.static doublemod(double a, double b) Non-integer version of modulus operator using floored division (see here), with results identical to Mathematica.static intmod(int a, int b) Integer version of the modulus operator (a mod b).static doubleradius(double x, double y) Returns the radius of the Cartesian pointstatic doublesqr(double x) Returns the square of its argument.static floatsqr(float x) Returns the square of its argument.static intsqr(int x) Returns the square of its argument.static longsqr(long x) Returns the square of its argument.static double[]toCartesian(double[] polar) Converts polar point coordinates to Cartesian coordinates.static double[]toCartesian(double radius, double angle) Converts polar point coordinates to Cartesian coordinates.static double[]toPolar(double[] xy) Returns the polar coordinates of the Cartesian point xy.static double[]toPolar(double x, double y) Returns the polar coordinates of the Cartesian point (x,y).
-
Field Details
-
EPSILON_FLOAT
Default tolerance used for comparingfloatquantities.- See Also:
-
EPSILON_DOUBLE
Default tolerance used for comparingdoublequantities.- See Also:
-
-
Method Details
-
sqr
Returns the square of its argument.- Parameters:
x- argument- Returns:
- square of argument
-
sqr
Returns the square of its argument.- Parameters:
x- argument- Returns:
- square of argument
-
sqr
Returns the square of its argument.- Parameters:
x- argument- Returns:
- square of argument
-
sqr
Returns the square of its argument.- Parameters:
x- argument- Returns:
- square of argument
-
radius
Returns the radius of the Cartesian point- Parameters:
x- x-componenty- y-component- Returns:
- the radius
-
angle
Returns the angle of the Cartesian point (x,y)- Parameters:
x- x-componenty- y-component- Returns:
- the angle
-
toPolar
Returns the polar coordinates of the Cartesian point (x,y).- Parameters:
x- x-componenty- y-component- Returns:
- a 2-element array holding the polar coordinates (radius, angle)
-
toPolar
Returns the polar coordinates of the Cartesian point xy.- Parameters:
xy- the Cartesian coordinates- Returns:
- a 2-element array holding the polar coordinates (radius, angle)
-
toCartesian
Converts polar point coordinates to Cartesian coordinates.- Parameters:
radius- the radiusangle- the angle- Returns:
- a 2-element array holding the Cartesian coordinates (x,y)
-
toCartesian
Converts polar point coordinates to Cartesian coordinates.- Parameters:
polar- the polar coordinates (radius, angle)- Returns:
- a 2-element array holding the Cartesian coordinates (x,y)
-
mod
Integer version of the modulus operator (a mod b). Also see here. CallsMath.floorMod(a,b)(available in Java 8 and higher).- Parameters:
a- dividendb- divisor (modulus), must be nonzero- Returns:
a mod b
-
mod
Non-integer version of modulus operator using floored division (see here), with results identical to Mathematica. Calculatesa mod bfor floating-point arguments. An exception is thrown ifbis zero. Examples:mod( 3.5, 2.1) = 1.4 mod(-3.5, 2.1) = 0.7 mod( 3.5,-2.1) = -0.7 mod(-3.5,-2.1) = -1.4
- Parameters:
a- dividendb- divisor (modulus), must be nonzero- Returns:
a mod b
-
isZero
Test for zero (float version) using a predefined tolerance. Returns true if the argument's absolute value is less thanEPSILON_FLOAT.- Parameters:
x- quantity to be tested- Returns:
- true if argument is close to zero
-
isZero
Test for zero (float version) using a specified tolerance. Returns true if the argument's absolute value is less than the specified tolerance.- Parameters:
x- quantity to be testedtolerance- the tolerance to be used- Returns:
- true if argument is close to zero
-
isZero
Test for zero (double version) using a predefined tolerance. Returns true if the argument's absolute value is less thanEPSILON_DOUBLE.- Parameters:
x- quantity to be tested- Returns:
- true if argument is close to zero
-
isZero
Test for zero (double version) using a specified tolerance. Returns true if the argument's absolute value is less than the specified tolerance.- Parameters:
x- quantity to be testedtolerance- the tolerance to be used- Returns:
- true if argument is close to zero
-
equals
Test for numerical equality (double version) using the default tolerance. Returns true if the absolute difference of the arguments is less thanEPSILON_DOUBLE.- Parameters:
x- first argumenty- second argument- Returns:
- true if the absolute difference of the arguments is less than the tolerance
-
equals
Test for numerical equality (double version) using a specific tolerance.- Parameters:
x- first argumenty- second argumenttolerance- the maximum (absolute) deviation- Returns:
- true if the absolute difference of the arguments is less than the tolerance
-
equals
Test for numerical equality (float version) using the default tolerance. Returns true if the absolute difference of the arguments is less thanEPSILON_FLOAT.- Parameters:
x- first argumenty- second argument- Returns:
- true if the absolute difference of the arguments is less than the tolerance
-
equals
Test for numerical equality (float version) using a specific tolerance.- Parameters:
x- first argumenty- second argumenttolerance- the maximum (absolute) deviation- Returns:
- true if the absolute difference of the arguments is less than the tolerance
-
max
Returns the maximum of one or more integer values.- Parameters:
a- the first valuevals- more values- Returns:
- the maximum value
-
min
Returns the minimum of one or more integer values.- Parameters:
a- the first valuevals- more values- Returns:
- the minimum value
-
max
Returns the maximum of one or more float values.- Parameters:
a- the first valuevals- more values- Returns:
- the maximum value
-
min
Returns the minimum of one or more float values.- Parameters:
a- the first valuevals- more values- Returns:
- the minimum value
-
max
Returns the maximum of one or more double values.- Parameters:
a- the first valuevals- more values- Returns:
- the maximum value
-
min
Returns the minimum of one or more double values.- Parameters:
a- the first valuevals- more values- Returns:
- the minimum value
-
clipTo
Limits the first argument to the clipping interval specified by the two other arguments (doubleversion). The clipped value is returned. Throws an exception if the clipping interval is empty.- Parameters:
x- the value to be clippedlow- the lower boundary of the clipping intervalhigh- the upper boundary of the clipping interval- Returns:
- the clipped value
-
clipTo
Limits the first argument to the clipping interval specified by the two other arguments (floatversion). The clipped value is returned. Throws an exception if the clipping interval is empty.- Parameters:
x- the value to be clippedlow- the lower boundary of the clipping intervalhigh- the upper boundary of the clipping interval- Returns:
- the clipped value
-
clipTo
Limits the first argument to the clipping interval specified by the two other arguments (floatversion). The clipped value is returned. Throws an exception if the clipping interval is empty.- Parameters:
x- the value to be clippedlow- the lower boundary of the clipping intervalhigh- the upper boundary of the clipping interval- Returns:
- the clipped value
-
getRealRoots
Returns the two real roots of the quadratic function f(x) = ax^2 + bx + c. Null is returned if roots are non-real.- Parameters:
a- function coefficientb- function coefficientc- function coefficient- Returns:
- an array with the two roots x1, x2 (in no particular order) or null if the function has complex roots
-