public final class LuaMathOperators extends Object
This class and its methods exploits the isomorphism between Lua integers and
Java long
on the one hand, and between Lua floats and Java double
on the other.
For each operation, there are as many variants in the form of a static method
as there are valid type combinations. While all arithmetic operations
are defined in two variants (for two long
s and two double
s, e.g.
idiv(long, long)
and idiv(double, double)
), bitwise operations
have single variants (taking two long
s, e.g. band(long, long)
),
and numerical comparison operations have four variants (for argument type combination).
It is the task of a Lua implementation to select the appropriate method and supply it with the required values; however, note that the method selection is well-behaved under the type conversion rules of the Java programming language.
Modifier and Type | Method and Description |
---|---|
static double |
add(double a,
double b)
Returns the result of the addition of two
double s, equivalent to the addition
of two floats in Lua. |
static long |
add(long a,
long b)
Returns the result of the addition of two
long s, equivalent to the addition
of two integers in Lua. |
static long |
band(long a,
long b)
Returns the result of the bitwise AND of two
long s, equivalent to the Lua
bitwise AND of two integers. |
static long |
bnot(long n)
Returns the result of the bitwise unary NOT of a
long , equivalent to the Lua
bitwise unary NOT on an integer. |
static long |
bor(long a,
long b)
Returns the result of the bitwise OR of two
long s, equivalent to the Lua
bitwise OR of two integers. |
static long |
bxor(long a,
long b)
Returns the result of the bitwise exclusive OR of two
long s,
equivalent to the Lua bitwise exclusive OR of two integers. |
static double |
div(double a,
double b)
Returns the result of the division of two
double s, equivalent to the float
division of two floats in Lua. |
static double |
div(long a,
long b)
Returns the result of the division of two
long s, equivalent to the float
division of two integers in Lua. |
static boolean |
eq(double a,
double b)
Returns
true iff the double s a and b denote
the same mathematical value. |
static boolean |
eq(double a,
long b)
Returns
true iff the double a denotes the same mathematical
value as the long b . |
static boolean |
eq(long a,
double b)
Returns
true iff the long a denotes the same mathematical
value as the double b . |
static boolean |
eq(long a,
long b)
Returns
true iff the long s a and b denote
the same mathematical value. |
static boolean |
hasExactFloatRepresentation(long l)
Returns
true iff the long l can be represented by
a double without the loss of precision, i.e. |
static boolean |
hasExactIntegerRepresentation(double d)
Returns
true iff the double d can be represented by
a long without the loss of precision, i.e. |
static double |
idiv(double a,
double b)
Returns the result of floor division of two
double s, equivalent to the Lua
floor division of two floats: |
static long |
idiv(long a,
long b)
Returns the result of floor division of two
long s, equivalent to the Lua
floor division of two integers. |
static boolean |
le(double a,
double b)
Returns
true iff the mathematical value of the double a
is lesser than or equal to the mathematical value of the double b . |
static boolean |
le(double a,
long b)
Returns
true iff the mathematical value of the double a
is lesser than or equal to the mathematical value of the long b . |
static boolean |
le(long a,
double b)
Returns
true iff the mathematical value of the long a
is lesser than or equal to the mathematical value of the double b . |
static boolean |
le(long a,
long b)
Returns
true iff the mathematical value of the long a
is lesser than or equal to the mathematical value of the long b . |
static boolean |
lt(double a,
double b)
Returns
true iff the mathematical value of the double a
is lesser than the mathematical value of the double b . |
static boolean |
lt(double a,
long b)
Returns
true iff the mathematical value of the double a
is lesser than the mathematical value of the long b . |
static boolean |
lt(long a,
double b)
Returns
true iff the mathematical value of the long a
is lesser than the mathematical value of the double b . |
static boolean |
lt(long a,
long b)
Returns
true iff the mathematical value of the long a
is lesser than the mathematical value of the long b . |
static double |
mod(double a,
double b)
Returns the floor modulus of two
double s, equivalent to the modulus
of two floats in Lua. |
static long |
mod(long a,
long b)
Returns the floor modulus of two
long s, equivalent to the modulus
of two integers in Lua. |
static double |
mul(double a,
double b)
Returns the result of the multiplication of two
double s, equivalent to
the multiplication of two floats in Lua. |
static long |
mul(long a,
long b)
Returns the result of the multiplication of two
long s, equivalent to
the multiplication of two integers in Lua. |
static double |
pow(double a,
double b)
Returns the result of the exponentiation of two
double s, equivalent to Lua
exponentiation of two floats. |
static double |
pow(long a,
long b)
Returns the result of the exponentiation of two
long s, equivalent to the Lua
exponentiation of two integers. |
static long |
shl(long a,
long b)
Returns the result of the (bitwise) logical left shift, equivalent to the Lua bitwise
left shift on two integers.
|
static long |
shr(long a,
long b)
Returns the result of the (bitwise) logical right shift, equivalent to the Lua bitwise
right shift on two integers.
|
static double |
sub(double a,
double b)
Returns the result of the subtraction of two
double s, equivalent to the subtraction
of two floats in Lua. |
static long |
sub(long a,
long b)
Returns the result of the subtraction of two
long s, equivalent to the subtraction
of two integers in Lua. |
static double |
unm(double n)
Returns the result of the (arithmetic) negation of a
long , equivalent to
the Lua unary minus on a float. |
static long |
unm(long n)
Returns the result of the (arithmetic) negation of a
long , equivalent to
the Lua unary minus on an integer. |
public static long add(long a, long b)
long
s, equivalent to the addition
of two integers in Lua.a
- first addendb
- second addend(a + b)
,
where a
and b
are Lua integerspublic static double add(double a, double b)
double
s, equivalent to the addition
of two floats in Lua.a
- first addendb
- second addend(a + b)
,
where a
and b
are Lua floatspublic static long sub(long a, long b)
long
s, equivalent to the subtraction
of two integers in Lua.a
- the minuendb
- the subtrahend(a - b)
,
where a
and b
are Lua integerspublic static double sub(double a, double b)
double
s, equivalent to the subtraction
of two floats in Lua.a
- the minuendb
- the subtrahend(a - b)
,
where a
and b
are Lua floatspublic static long mul(long a, long b)
long
s, equivalent to
the multiplication of two integers in Lua.a
- first factorb
- second factor(a * b)
,
where a
and b
are Lua integerspublic static double mul(double a, double b)
double
s, equivalent to
the multiplication of two floats in Lua.a
- first factorb
- second factor(a * b)
,
where a
and b
are Lua floatspublic static double div(long a, long b)
long
s, equivalent to the float
division of two integers in Lua. The result is always a double
; when b
is zero, the result is NaN.
Note that this behaviour differs from the standard Java integer division.
a
- the dividendb
- the divisor(a / b)
,
where a
and b
are Lua integerspublic static double div(double a, double b)
double
s, equivalent to the float
division of two floats in Lua.a
- the dividendb
- the divisor(a / b)
,
where a
and b
are Lua floatspublic static long mod(long a, long b)
long
s, equivalent to the modulus
of two integers in Lua.
Note that in Lua,
Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division).
This definition is not equivalent to the standard Java definition of modulo (which is the remainder of a division rounding toward zero).
a
- the dividendb
- the divisor(a % b)
,
where a
and b
are Lua integersArithmeticException
- if b
is zeropublic static double mod(double a, double b)
double
s, equivalent to the modulus
of two floats in Lua.
Note that in Lua,
Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division).
This definition is not equivalent to the standard Java definition of modulo (which is the remainder of a division rounding toward zero).
a
- the dividendb
- the divisor(a % b)
,
where a
and b
are Lua floatspublic static long idiv(long a, long b)
long
s, equivalent to the Lua
floor division of two integers.
In Lua,
Floor division (//) is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands.
a
- the dividendb
- the divisor(a // b)
where a
and b
are Lua integersArithmeticException
- if b
is zeropublic static double idiv(double a, double b)
double
s, equivalent to the Lua
floor division of two floats:
In Lua,
Floor division (//) is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands.
a
- the dividendb
- the divisor(a // b)
where a
and b
are Lua floatspublic static double pow(long a, long b)
long
s, equivalent to the Lua
exponentiation of two integers. Note that the resulting value is a double
.a
- the baseb
- the exponent(a ^ b)
,
where a
and b
are Lua integerspublic static double pow(double a, double b)
double
s, equivalent to Lua
exponentiation of two floats.a
- the baseb
- the exponent(a ^ b)
,
where a
and b
are Lua floatspublic static long unm(long n)
long
, equivalent to
the Lua unary minus on an integer.n
- the operand(-n)
,
where n
is a Lua integerpublic static double unm(double n)
long
, equivalent to
the Lua unary minus on a float.n
- the operand(-n)
,
where n
is a Lua floatpublic static long band(long a, long b)
long
s, equivalent to the Lua
bitwise AND of two integers.a
- the first operandb
- the second operand(a & b)
,
where a
and b
are Lua integerspublic static long bor(long a, long b)
long
s, equivalent to the Lua
bitwise OR of two integers.a
- the first operandb
- the second operand(a | b)
,
where a
and b
are Lua integerspublic static long bxor(long a, long b)
long
s,
equivalent to the Lua bitwise exclusive OR of two integers.a
- the first operandb
- the second operand(a ~ b)
,
where a
and b
are Lua integerspublic static long shl(long a, long b)
b
is negative,
the result is equal to the result of a right shift by -b
.
Note that Lua's behaviour differs from Java's <<
operator in that if
b
is greater than 64, the result is zero, as all bits have been shifted out.
a
- the left-hand side operandb
- the right-hand side operand (shift distance)(a << b)
,
where a
and b
are Lua integerspublic static long shr(long a, long b)
b
is negative,
the result is equal to the result of a left shift by -b
.
Note that Lua's behaviour differs from Java's >>>
operator in that if
b
is greater than 64, the result is zero, as all bits have been shifted out.
a
- the left-hand side operandb
- the right-hand side operand (shift distance)(a << b)
,
where a
and b
are Lua integerspublic static long bnot(long n)
long
, equivalent to the Lua
bitwise unary NOT on an integer.n
- the operand(~b)
,
where n
is a Lua integerpublic static boolean hasExactIntegerRepresentation(double d)
true
iff the double
d
can be represented by
a long
without the loss of precision, i.e. if ((long) d)
and d
denote the same mathematical value.d
- the double
in questiontrue
iff d
can be represented by a long
without the loss of precisionpublic static boolean hasExactFloatRepresentation(long l)
true
iff the long
l
can be represented by
a double
without the loss of precision, i.e. if ((double) l)
and l
denote the same mathematical value.l
- the long
in questiontrue
iff l
can be represented by a double
without the loss of precisionpublic static boolean eq(long a, long b)
true
iff the long
s a
and b
denote
the same mathematical value. This is equivalent to the Lua numerical equality
comparison of two integers.a
- a long
b
- a long
to be compared with a
for mathematical equalitytrue
iff the Lua expression (a == b)
,
where a
and b
are Lua integers,
would evaluate to (Lua) truepublic static boolean eq(long a, double b)
true
iff the long
a
denotes the same mathematical
value as the double
b
. This is equivalent to the Lua numerical
equality comparison of an integer and a float.a
- a long
b
- a double
to be compared with a
for mathematical equalitytrue
iff the Lua expression (a == b)
,
where a
is a Lua integer and b
is a Lua float,
would evaluate to (Lua) truepublic static boolean eq(double a, long b)
true
iff the double
a
denotes the same mathematical
value as the long
b
. This is equivalent to the Lua numerical equality
comparison of a float and an integer.a
- a double
b
- a long
to be compared with a
for mathematical equalitytrue
iff the Lua expression (a == b)
,
where a
is a Lua float and b
is a Lua integer,
would evaluate to (Lua) truepublic static boolean eq(double a, double b)
true
iff the double
s a
and b
denote
the same mathematical value. This is equivalent to the Lua numerical equality
comparison of two doubles.a
- a double
b
- a double
to be compared with a
for mathematical equalitytrue
iff the Lua expression (a == b)
,
where a
and b
are Lua floats,
would evaluate to (Lua) truepublic static boolean lt(long a, long b)
true
iff the mathematical value of the long
a
is lesser than the mathematical value of the long
b
. This is equivalent
to the Lua numerical lesser-than comparison of two integers.a
- a long
b
- a long
to be compared with a
true
iff the Lua expression (a < b)
,
where a
and b
are Lua integers,
would evaluate to (Lua) truepublic static boolean lt(long a, double b)
true
iff the mathematical value of the long
a
is lesser than the mathematical value of the double
b
. This
is equivalent to the Lua numerical lesser-than comparison of an integer and a float.a
- a long
b
- a double
to be compared with a
true
iff the Lua expression (a < b)
,
where a
is a Lua integer and b
is a Lua float,
would evaluate to (Lua) truepublic static boolean lt(double a, long b)
true
iff the mathematical value of the double
a
is lesser than the mathematical value of the long
b
. This
is equivalent to the Lua numerical lesser-than comparison of a float and an integer.a
- a double
b
- a long
to be compared with a
true
iff the Lua expression (a < b)
,
where a
is a Lua float and b
is a Lua integer,
would evaluate to (Lua) truepublic static boolean lt(double a, double b)
true
iff the mathematical value of the double
a
is lesser than the mathematical value of the double
b
. This
is equivalent to the Lua numerical lesser-than comparison of two floats.a
- a double
b
- a double
to be compared with a
true
iff the Lua expression (a < b)
,
where a
and b
are Lua floats,
would evaluate to (Lua) truepublic static boolean le(long a, long b)
true
iff the mathematical value of the long
a
is lesser than or equal to the mathematical value of the long
b
.
This is equivalent to the Lua numerical lesser-than-or-equal comparison of
two integers.a
- a long
b
- a long
to be compared with a
true
iff the Lua expression (a <= b)
,
where a
and b
are Lua integers,
would evaluate to (Lua) truepublic static boolean le(long a, double b)
true
iff the mathematical value of the long
a
is lesser than or equal to the mathematical value of the double
b
.
This is equivalent to the Lua numerical lesser-than-or-equal comparison of
an integer and a float.a
- a long
b
- a double
to be compared with a
true
iff the Lua expression (a <= b)
,
where a
is a Lua integer and b
is a Lua float,
would evaluate to (Lua) truepublic static boolean le(double a, long b)
true
iff the mathematical value of the double
a
is lesser than or equal to the mathematical value of the long
b
.
This is equivalent to the Lua numerical lesser-than-or-equal comparison of
a float and an integer.a
- a double
b
- a long
to be compared with a
true
iff the Lua expression (a <= b)
,
where a
is a Lua float and b
is a Lua integer,
would evaluate to (Lua) truepublic static boolean le(double a, double b)
true
iff the mathematical value of the double
a
is lesser than or equal to the mathematical value of the double
b
.
This is equivalent to the Lua numerical lesser-than-or-equal comparison of
two floats.a
- a double
b
- a double
to be compared with a
true
iff the Lua expression (a <= b)
,
where a
and b
are Lua floats,
would evaluate to (Lua) trueCopyright © 2016. All rights reserved.