Class FloatElementaryModMath
- java.lang.Object
-
- org.apfloat.internal.FloatElementaryModMath
-
- Direct Known Subclasses:
FloatModMath
public class FloatElementaryModMath extends java.lang.ObjectElementary modulo arithmetic functions forfloatdata. Note that although a floating-point data type is used, the data will always be integers.Since the moduli are close to 224 some attention must be paid to avoiding overflow in modular addition and subtraction. This can be done easily e.g. by casting the operands to
double. Note that an IEEE float has a mantissa with a precision of 24 bits (1 + 23).Modular multiplication is more complicated, and since it is usually the single most time consuming operation in the whole program execution, the very core of the Number Theoretic Transform (NTT), it should be carefully optimized.
Some obvious (but not very efficient) algorithms for multiplying two
floats and taking the remainder would be to callMath.IEEEremainder(), or cast the operands tolong, e.g.(float) ((long) a * (long) b % (long) modulus)Since the modulus is practically constant, it should be more efficient to calculate (once) the inverse of the modulus, and then subsequently multiply by the inverse modulus instead of dividing by the modulus.
The algorithm used in this implementation casts the operands to
double, performs the multiplication, multiplies by the inverse modulus, then takes the integer part. Getting the integer part is typically a lot faster by casting tointcompared to e.g. callingMath.floor(). Anint, holding 32 bits, can easily contain the result of the cast, which will have a maximum of 24 bits.Overflow is not a problem, since a
doublecan hold 53 bits precisely in the mantissa – more than doubly what afloatcan. Note that multiplying by the inverse modulus is also trivial, when the inverse modulus has more than twice accurate bits than what are in each of the multiplicands. Since the modulus is assumed to be prime, there can be no situations where multiplication by the inverse modulus would have a near-integer result that would be rounded incorrectly, e.g. as in0.333... * 3 = 0.999....- Version:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description private doubleinverseModulusprivate floatmodulus
-
Constructor Summary
Constructors Constructor Description FloatElementaryModMath()Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description floatgetModulus()Get the modulus.floatmodAdd(float a, float b)Modular addition.floatmodMultiply(float a, float b)Modular multiplication.floatmodSubtract(float a, float b)Modular subtraction.voidsetModulus(float modulus)Set the modulus.
-
-
-
Method Detail
-
modMultiply
public final float modMultiply(float a, float b)Modular multiplication.- Parameters:
a- First operand.b- Second operand.- Returns:
a * b % modulus
-
modAdd
public final float modAdd(float a, float b)Modular addition.- Parameters:
a- First operand.b- Second operand.- Returns:
(a + b) % modulus
-
modSubtract
public final float modSubtract(float a, float b)Modular subtraction. The result is always >= 0.- Parameters:
a- First operand.b- Second operand.- Returns:
(a - b + modulus) % modulus
-
getModulus
public final float getModulus()
Get the modulus.- Returns:
- The modulus.
-
setModulus
public final void setModulus(float modulus)
Set the modulus.- Parameters:
modulus- The modulus.
-
-