java.lang.Object
imagingbook.common.math.Arithmetic
This class defines static methods implementing arithmetic operations and predicates.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final double
Default tolerance used for comparingdouble
quantities.static final float
Default tolerance used for comparingfloat
quantities. -
Method Summary
Modifier and TypeMethodDescriptionstatic double
angle
(double x, double y) Returns the angle of the Cartesian point (x,y)static double
clipTo
(double x, double low, double high) Limits the first argument to the clipping interval specified by the two other arguments (double
version).static float
clipTo
(float x, float low, float high) Limits the first argument to the clipping interval specified by the two other arguments (float
version).static int
clipTo
(int x, int low, int high) Limits the first argument to the clipping interval specified by the two other arguments (float
version).static boolean
equals
(double x, double y) Test for numerical equality (double version) using the default tolerance.static boolean
equals
(double x, double y, double tolerance) Test for numerical equality (double version) using a specific tolerance.static boolean
equals
(float x, float y) Test for numerical equality (float version) using the default tolerance.static boolean
equals
(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 boolean
isZero
(double x) Test for zero (double version) using a predefined tolerance.static boolean
isZero
(double x, double tolerance) Test for zero (double version) using a specified tolerance.static boolean
isZero
(float x) Test for zero (float version) using a predefined tolerance.static boolean
isZero
(float x, float tolerance) Test for zero (float version) using a specified tolerance.static double
max
(double a, double... vals) Returns the maximum of one or more double values.static float
max
(float a, float... vals) Returns the maximum of one or more float values.static int
max
(int a, int... vals) Returns the maximum of one or more integer values.static double
min
(double a, double... vals) Returns the minimum of one or more double values.static float
min
(float a, float... vals) Returns the minimum of one or more float values.static int
min
(int a, int... vals) Returns the minimum of one or more integer values.static double
mod
(double a, double b) Non-integer version of modulus operator using floored division (see here), with results identical to Mathematica.static int
mod
(int a, int b) Integer version of the modulus operator (a mod b
).static double
radius
(double x, double y) Returns the radius of the Cartesian pointstatic double
sqr
(double x) Returns the square of its argument.static float
sqr
(float x) Returns the square of its argument.static int
sqr
(int x) Returns the square of its argument.static long
sqr
(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 comparingfloat
quantities.- See Also:
-
EPSILON_DOUBLE
Default tolerance used for comparingdouble
quantities.- 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 b
for floating-point arguments. An exception is thrown ifb
is 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 (double
version). 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 (float
version). 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 (float
version). 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
-