- Home /
Keep sign when exponentiating variable?
Is there some function that keeps variables sign after exponentiating it or is this best way to do it:
Mathf.Pow(speedLocal.x, 2)*Mathf.Sign(speedLocal.x)
I was wondering why anyone would want to do this. $$anonymous$$aybe some special type of math everyone knows but me? But from OPs comments below, not really a math guy, so I feel more confident writing this:
If you think you need to square a number and keep the sign, recheck the math. You probably don't.
There are obscure cases when you might need to, but for the most part, you don't.
Answer by Hoeloe · Sep 30, 2013 at 09:12 AM
That is the best way to do it. Squaring any number will necessarily make it positive (as will any even power), so you will lose the information. Getting and multiplying by the sign using a built-in method is the most appropriate and fastest method.
Thanks
I was just wondering if there is some build in method like this:
$$anonymous$$athf.Pow2Sign(speedLocal.x)
=
$$anonymous$$athf.Pow(speedLocal.x, 2)*$$anonymous$$athf.Sign(speedLocal.x)
cause there are lot of things in physics that are related to square of some other value. And usually it's needed to keep signs so vectors go right direction.
re: square ... keep signs so vectors go right direction
Not as much as you'd think. The sign is usually added back naturally. Take normalizing. In x_n = x/(sqrt(x*x+y*y))
you want x and y squared to be always positive, so you always get a positive length. The sign of x is added back in that first "x divided by".
No-one mentioned logarithms at all, and if you ever want to be at all successful at program$$anonymous$$g, mathematics of all form is necessary.
"In x_n" << darn, I read that like ln x... something and stopped reading. If someone mentions Neper or natural logarithm I will refuse to understand.