Mathf.sin

hi everybody

i have a problem with Mathf.sin. when i use Mathf.sin(Mathf.PI) in don’t return 0 WHY!!
i know sin 180 degree is 0 and i turn 180 in degree to radian by use Mathf.deg2rad in not work not return 0
i try to do deg2rad by myself (180 * Mathf.PI * 2 / 360 = Mathf.PI) but it not work again

i think Mathf.PI in unity is approximately.
what do you think? tell me pls.

thank a lot

ps. sorry for me english. :expressionless:

LOL!

it could help when you would tell what it returns instead.

i think you are doing it wrong.

+1

to be at least a bit of help (albeit thinking you made a trollpost):
have a look at the “exact” number of Pi and at floating point precision and your problem may be more clear.

in general dont compare floating point values directly as most numbers cant be expressed in float and double directly. calculate the difference and check it against a threshold.

thx for answer

MonoBehaviour.print(Mathf.Sin(Mathf.PI));
this code print -8.742278E-08 it mean -0.00000008742278…
but when i use calculator in my computer to find sinr(PI) = 0
what wrong?
i know PI is approximately. but it not my point.
pls answer my question why sin(PI) in unity is not ZERO.

thank a lot

To better articulate myself:

π is an irrational number, which means that its value cannot be expressed exactly as a fraction having integers in both the numerator and denominator. Consequently, its decimal representation never ends or repeats. wiki

So to expect an exact representation from a math library (especially one that’s designed for speed) is slightly ridiculous.

You are using floats - inaccuracies are part of life.

  • Your calculator simple rounds the result.
  • Your calculator has predefined logic for specific scenarios (e.g. Sinr with a number close to pi).
  • Your calculator uses some advanced algorithms to attempt to give ‘exact’ answers.

Oddly enough - those three options are open to you as well. Who would have thunk it?

float means single precision. double means double precision. for irrational numbers you need unlimited precision which you simply cant achieve with a limited computer. so you have to live with that and design your code to avoid problems like i told in my post above.

it would be interesting why you need that anyway. -8.742278E-08 is as close to zero as i can imagine.

What about -8.742277E-08?

so 1E-14 does matter for you? and is relevant for your game? for me its not. and if you dont do real physical simulations you will hardly have issues with floating point inaccuracy when you are aware of and handle it. but thats just me. go with the precision you require.

Nope, but I can imagine it :stuck_out_tongue:

-8.742278E-08 it mean -0.00000008742278…

1E-14 it mean 0.000000000000014

now i understand it close to zero but it not zero - -
end of my story
thank a lot

Nope.

http://en.wikipedia.org/wiki/Scientific_notation

wow. this clearly qualifies you for at least one nobel prize. plus you have won this thread.
may you share your luck and describe your imagination for the unlucky (me) who have difficulties to clearly imagine things at the size of atoms?

no, 0.00000000000001 so the decimal point of 1.0 is shifted 14 times to the left (filling in zeros).

as i told never check floating point values for equality. calc the difference and check it against a small threshold (usually called epsilon).

float a = 1.00001f;
float b = 1.0f;
float diff = Mathf.Abs(a-b);
if(diff < 0.001f)
	// your values are almost equal

floating point variables have their highest resolution at ±1 and their lowest at ± zero and ± very large values. if you encounter these values and have inaccuracies you should consider another scale. for example when you deal with billions of kilometers to map a solar system use astronomical units instead.