LanguageEnglish
  • C#

Mathf.Log

Switch to Manual

Declaration

public static float Log(float f, float p);

Parameters

f Value to compute the logarithm for.
p Base of the logarithm operation.

Returns

float Result of the logarithm operation.

Description

Returns the logarithm of a specified number in a specified base.

The logarithm is the inverse function of exponentiation, meaning that it will output the exponent to which p needs to be raised to obtain f.

This function will return NaN if either: - The value of f is negative. - The value of p is negative, 0, 1 or Infinity.

Additionally, when the above is true, this function will return ±Infinity when: - f equals Infinity (will return +Infinity). - f equals 0 (-Infinity when p is in the (1,∞] range, +Infinity when p is in the (0,1) range).

Additional resources: Pow.

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)); } }

Declaration

public static float Log(float f);

Parameters

f Value to compute the logarithm for.

Returns

float Result of the logarithm operation.

Description

Returns the natural (base e) logarithm of a specified number.

Computing the natural logarithm of f 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.

Additional resources: Exp.

using UnityEngine;

public class ScriptExample : MonoBehaviour { void Start() { // natural logarithm of 100 // prints 4.60517 Debug.Log(Mathf.Log(100)); } }

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961