- Home /
Mathf.Log() with negative numbers?
In the code below I'm doing a Mathf.Log function and i want to get the common power from the variable position (which I am changing in game with a slider). The value is than represented in a UI text object. Well, everything works fine, just that the LOG function will return NaN when the number is "negative".
For example:
Mathf.Log(100, 10); // Returns 2
Mathf.Log(-100, 10); // Returns NaN
How do I achieve to get a negative log?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CameraController : MonoBehaviour {
private float position = 0.0f;
private float cameraSmooth = 0.01f;
private Text powerText;
// Use this for initialization
void Start () {
powerText = GameObject.Find("Power").GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
powerText.text = Mathf.Log(position, 10.0f).ToString();
Debug.Log(powerText.text);
}
void LateUpdate () {
transform.position = new Vector3(transform.position.x, transform.position.y, Mathf.Lerp(transform.position.z, position, cameraSmooth));
}
public void AdjustPosition(float newPosition)
{
position = newPosition;
}
}
Comment
Best Answer
Answer by Eric5h5 · Dec 11, 2016 at 11:52 PM
It's not something that makes any mathematical sense, so you can't. You can always use positive numbers in Log by using Mathf.Abs to get the absolute value.