Core\Math::log
Core\Math::log returns the logarithm of a number. The logarithm is the power you raise the base to, to get that number. In base 10, the logarithm of 1000.0 is 3.0, because 10 ** 3 is 1000.
The base is E (about 2.718) when you leave it out. You choose another base with the base option, for example { base: 10.0 } or { base: 2.0 }. In base 10.0 and base 2.0, the result for a power of that base is exact.
The logarithm of 0.0 is -INFINITY. A negative number has no logarithm, so the result is NaN (a value that means "not a number"). A base of zero or below throws a RuntimeError.
The examples below show the three common bases, then the numbers with no logarithm, then how long it takes for savings to double.
Parameters
$nfloatThe number to take the logarithm of.
Options
basefloatoptionalThe base. It must be greater than zero, and it isEwhen you leave it out. For a power of10.0or2.0in that base, the result is exact.
Return value
Returns float. The logarithm. When $n is 0.0 and base is greater than 1.0, the result is -INFINITY. When $n is negative, or base is 1.0, the result is NaN.
Errors
RuntimeErrorWhenbaseis zero or negative.
Examples
Three bases
<?nvs// Shows the logarithm in the three bases programs use most.
// With no base, the base is `E`. The logarithm of `E` is 1, so this prints 1.echo Core\Math::log(Core\Math::E), "\n";
// `10 ** 3` is 1000, so this prints 3.echo Core\Math::log(1000.0, { base: 10.0 }), "\n";
// `2 ** 10` is 1024, so this prints 10.echo Core\Math::log(1024.0, { base: 2.0 }), "\n";
// Any number above zero can be the base. `3 ** 4` is 81, so this prints 4.echo Core\Math::round(Core\Math::log(81.0, { base: 3.0 }), { precision: 6 }), "\n";13104Numbers with no logarithm
<?nvs// Shows the result for zero and for a negative number, and what happens when the base is zero.
// The logarithm of zero is `-INFINITY`. This prints -INF.echo Core\Math::log(0.0), "\n";
// A negative number has no logarithm. This prints NAN.echo Core\Math::log(-5.0), "\n";
// `Core\Math::isNan` tests for that result. This prints "no logarithm".if (Core\Math::isNan(Core\Math::log(-5.0))) { echo "no logarithm\n";}
// A base of zero or below throws a `RuntimeError`.try { echo Core\Math::log(8.0, { base: 0.0 }), "\n";} catch (RuntimeError $error) { echo "The base must be greater than zero.\n";}-INFNANno logarithmThe base must be greater than zero.Years to double
<?nvs// Shows how a savings calculator finds how many years it takes for money to double.
class Savings{ // Returns the whole years until the money doubles. Each year the money is multiplied // by `1 + $rate`, so the logarithm of 2 in that base is the number of years. public static function yearsToDouble(float $rate): float { return Core\Math::ceil(Core\Math::log(2.0, { base: 1.0 + $rate })); }}
// This prints 15 for 5%, 8 for 10% and 70 for 1%.echo 'At 5%: ', Savings::yearsToDouble(0.05), " years\n";echo 'At 10%: ', Savings::yearsToDouble(0.10), " years\n";echo 'At 1%: ', Savings::yearsToDouble(0.01), " years\n";At 5%: 15 yearsAt 10%: 8 yearsAt 1%: 70 years