Log
Returns the logarithm of a specified number in a specified base.
Read time 2 minutesLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
Log(float, float)
Returns the logarithm of a specified number in a specified base.
public static float Log(float f, float p)
Parameters
Returns
Type | Description |
|---|---|
| float | Result of the logarithm operation. |
Remarks
The logarithm is the inverse function of exponentiation, meaning that it will output the exponent to which needs to be raised to obtain .
pfThis function will return NaN if either:
- The value of is negative.
f - The value of is negative, 0, 1 or Infinity.
p
Additionally, when the above is true, this function will return ±Infinity when:
- equals Infinity (will return +Infinity).
f - equals 0 (-Infinity when
fis in the (1,∞] range, +Infinity whenpis in the (0,1) range).p
Additional Resources: Mathf.Pow.
Examples
using UnityEngine;public class ScriptExample : MonoBehaviour{ void Start() { // Prints 2 Debug.Log(Mathf.Log(4, 2)); // Prints NaN Debug.Log(Mathf.Log(-4, 2)); // Prints NaN Debug.Log(Mathf.Log(4, -2)); // Prints NaN Debug.Log(Mathf.Log(4, 0)); // Prints NaN Debug.Log(Mathf.Log(4, 1)); // Prints NaN Debug.Log(Mathf.Log(4, Mathf.Infinity)); // Prints Infinity Debug.Log(Mathf.Log(Mathf.Infinity, 2)); // Prints -Infinity Debug.Log(Mathf.Log(0, 2)); // Prints Infinity Debug.Log(Mathf.Log(0, 0.2f)); }}
Log(float)
Returns the natural (base e) logarithm of a specified number.
public static float Log(float f)
Parameters
Value to compute the logarithm for.
Returns
Type | Description |
|---|---|
| float | Result of the logarithm operation. |
Remarks
Computing the natural logarithm of is the inverse operation to computing the Exponential of the output of this function. This means that 'Mathf.Exp(x) = f', with x being the output of this function.
fAdditional Resources: Mathf.Exp.
Examples
using UnityEngine;public class ScriptExample : MonoBehaviour{ void Start() { // natural logarithm of 100 // prints 4.60517 Debug.Log(Mathf.Log(100)); }}