Class DFastLog
- java.lang.Object
-
- micycle.peasygradients.utilities.fastLog.FastLog
-
- micycle.peasygradients.utilities.fastLog.DFastLog
-
public class DFastLog extends FastLog
Implementation of the ICSILog algorithm as described in O. Vinyals, G. Friedland, N. Mirghafori "Revisiting a basic function on current CPUs: A fast logarithm implementation with adjustable accuracy" (2007).This class is based on the original algorithm description and a Java implementation by Hanns Holger Rutz. It has been adapted for use with double-precision data.
Note: log(float) is not provided as it is dynamically cast up to a double and float values can be represented as a double. If your computation generates a float value to be logged then use
FFastLog
.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description float
fastLog(double x)
Calculate the logarithm to the base given in the constructor.float
fastLog(float x)
Calculate the logarithm to the configured base.float
fastLog2(double x)
Calculate the logarithm to base 2.float
fastLog2(float x)
Calculate the logarithm to base 2.double
getBase()
Gets the base.int
getN()
Gets the number of most significant bits to keep from the mantissa, i.e.int
getQ()
Gets the number of least signification bits to ignore from the mantissa of a double (52-bits).double
getScale()
Gets the scale to convert from log2 to logB (the base given byFastLog.getBase()
) by multiplication.float
log(double x)
Calculate the logarithm to the configured base, handling special cases.float
log(float x)
Calculate the logarithm to the configured base, handling special cases.float
log2(double x)
Calculate the logarithm to base 2, handling special cases.float
log2(float x)
Calculate the logarithm to base 2, handling special cases.
-
-
-
Constructor Detail
-
DFastLog
public DFastLog()
Create a new natural logarithm calculation instance.
-
DFastLog
public DFastLog(int n)
Create a new logarithm calculation instance. This will hold the pre-calculated log values for base E and a table size depending on a given mantissa quantization.- Parameters:
n
- The number of bits to keep from the mantissa. Table storage = 2^(n+1) * 4 bytes, e.g. 64Kb for n=13
-
DFastLog
public DFastLog(double base, int n)
Create a new logarithm calculation instance. This will hold the pre-calculated log values for a given base and a table size depending on a given mantissa quantization.- Parameters:
base
- the logarithm base (e.g. 2 for log duals, 10 for decibels calculations, Math.E for natural log)n
- The number of bits to keep from the mantissa. Table storage = 2^(n+1) * 4 bytes, e.g. 64Kb for n=13
-
-
Method Detail
-
getBase
public double getBase()
Description copied from class:FastLog
Gets the base.
-
getScale
public double getScale()
Description copied from class:FastLog
Gets the scale to convert from log2 to logB (the base given byFastLog.getBase()
) by multiplication.
-
getN
public int getN()
Description copied from class:FastLog
Gets the number of most significant bits to keep from the mantissa, i.e. the binary precision of the floating point number before computing the log.
-
getQ
public int getQ()
Gets the number of least signification bits to ignore from the mantissa of a double (52-bits).- Returns:
- the number of least signification bits to ignore
-
log2
public float log2(double x)
Description copied from class:FastLog
Calculate the logarithm to base 2, handling special cases.Special cases:
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
-
log2
public float log2(float x)
Calculate the logarithm to base 2, handling special cases.Special cases:
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
This just calls
log2(double)
. UseFFastLog
for a dedicatedfloat version
.
-
fastLog2
public float fastLog2(double x)
Calculate the logarithm to base 2. Requires the argument be finite and positive.Special cases:
- If the argument is NaN, then the result is incorrect.
- If the argument is negative, then the result is incorrect (log(-x)).
- If the argument is positive infinity, then the result is incorrect (Math.log(Double.MAX_VALUE)).
- If the argument is positive zero or negative zero, then the result is negative infinity.
-
fastLog2
public float fastLog2(float x)
Calculate the logarithm to base 2. Requires the argument be finite and strictly positive.Special cases:
- If the argument is NaN, then the result is incorrect.
- If the argument is negative, then the result is incorrect.
- If the argument is positive infinity, then the result is incorrect.
- If the argument is positive zero or negative zero, then the result is incorrect.
Sub-classes may handle some of the special cases.
This just calls
fastLog2(double)
. UseFFastLog
for a dedicatedfloat version
.
-
log
public float log(double x)
Description copied from class:FastLog
Calculate the logarithm to the configured base, handling special cases.Special cases:
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
- Specified by:
log
in classFastLog
- Parameters:
x
- the argument- Returns:
- log(x)
- See Also:
FastLog.getBase()
-
log
public float log(float x)
Calculate the logarithm to the configured base, handling special cases.Special cases:
- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
This just calls
log(double)
. UseFFastLog
for a dedicatedfloat version
.- Specified by:
log
in classFastLog
- Parameters:
x
- the argument- Returns:
- log(x)
- See Also:
FastLog.getBase()
-
fastLog
public float fastLog(double x)
Calculate the logarithm to the base given in the constructor. Requires the argument be finite and positive.Special cases:
- If the argument is NaN, then the result is incorrect.
- If the argument is negative, then the result is incorrect (log(-x)).
- If the argument is positive infinity, then the result is incorrect (Math.log(Double.MAX_VALUE)).
- If the argument is positive zero or negative zero, then the result is negative infinity.
- Specified by:
fastLog
in classFastLog
- Parameters:
x
- the argument (must be strictly positive)- Returns:
- log( x )
- See Also:
FastLog.getBase()
-
fastLog
public float fastLog(float x)
Calculate the logarithm to the configured base. Requires the argument be finite and strictly positive.Special cases:
- If the argument is NaN, then the result is incorrect.
- If the argument is negative, then the result is incorrect.
- If the argument is positive infinity, then the result is incorrect.
- If the argument is positive zero or negative zero, then the result is incorrect.
Sub-classes may handle some of the special cases.
This just calls
fastLog(double)
. UseFFastLog
for a dedicatedfloat version
.- Specified by:
fastLog
in classFastLog
- Parameters:
x
- the argument (must be strictly positive)- Returns:
- log(x)
- See Also:
FastLog.getBase()
-
-