Is there anyway I can do this without using ifs?

Like I want to do Math.Pow(-5f,2f) to come out as -25 not 25?

有帮助吗?

解决方案

How about with Math.Sign:

Returns a value indicating the sign of a single-precision floating-point number.

So the computation would be:

Math.Sign(x) * Math.Pow(x, y)

If the idea is to always use the sign of the first operand even when the Pow operation returns a negative value (instead of just negating the result when the first operand is negative), you can do:

Math.Sign(x) * Math.Abs(Math.Pow(x, y))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top