Search Unity

Sin(pi) not equal to zero

Discussion in 'Scripting' started by rj191951, Jul 28, 2019.

  1. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    8
    In console, it's showing the value of Mathf.Sin(Mathf.PI) not equal to 0.

    Is there any other way to do so.
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,837
    Then what is it equal to?

    Whats your code?
     
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    502
    Why do you need exact zero?

    The reason it will show a very tiny value close to zero, is because PI is not exactly PI. It's only a floating point number very close to PI, which itself is an irrational number, that cannot be represented in the computer. There are some hoops to jump through if you want to create a custom sine function which gives you exactly zero at these values, but there's almost never a good reason to do so. Most systems will just work with the tiny value. You shouldn't be comparing against zero anyway, but in case you must: use Mathf.Approximately or Round your value to zero.
     
    Last edited: Jul 29, 2019
    dgoyette and SparrowsNest like this.
  4. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    8

    yeah, i know that. But what i needed was a function that can round off the value upto some decimals.
    Basically i was trying to rotate a camera about a free point with "Mouse X" value.
    So, i needed to calculate the value of ( rcos(theta), rsin(theta) ).

    And i found a function for that, System.Math.Round(float value, int decimal points).
     
  5. rj191951

    rj191951

    Joined:
    Oct 30, 2018
    Posts:
    8
    The code was for rotating camera about a free point with "Mouse X" value, with the formula ( rcos(theta), rsin(theta) ).

    The value was around 10 to the power -8, but when multiplied by a magnitude 'r', it somehow shows a value of -2.(something) and that can't be neglected, because it has a significant effect on the position of the camera.

    So, what I needed was a function that can round off the value up to some decimals.