Taking the Integer Power of a Number

41 Views Asked by At

In computer science, most languages incorporate integer division and integer multiplication (even if the initial values are decimals, the result will always be an integer - probably by truncating or rounding the decimal part), and they have simple mathematical definitions. Is there such an operation for taking the "integer power" of a number?

For instance, lets say I want to do $10^{1.5}$, the answer would be $32$ instead of $31.6228$. Likewise, if I did $1.5^{10}$, the answer would be $58$ instead of $57.665$. Is there a formula for such an operation?

1

There are 1 best solutions below

0
On

You can use the floor and ceiling functions to accomplish this -- or for any other operation, really.

If you want to truncate the decimal, you'd use the floor function:

$$\lfloor 10^{1.5} \rfloor = 57.$$

If you want to round up, you'd use the ceiling function:

$$\lceil 10^{1.5} \rceil = 58.$$

If you want to round to the nearer integer, you can check the decimal part like this:

$$10^{1.5}-\lfloor 10^{1.5}\rfloor \doteq 0.665,$$

and then use the appropriate function.