I have a variable, tauMax
, that I want to round up to the nearest power of ten(1, 10, 100, 1000...). I am using the below expression to find the closest integer to the max value in the tau array. I am finding the max value because I am trying to calculate the power of ten that should be the x axis cutoff. In this cause, tauMax is equal to 756, so I want to have an expression that outputs either 1000, or 3(for 10^3).
tauMax = round(max(tau));
I'd really appreciate any help!
A power of 10 is as many number 10s as indicated by the exponent multiplied together. Thus, shown in long form, a power of 10 is the number 1 followed by n zeros, where n is the exponent and is greater than 0; for example, 106 is written 1,000,000.
10 times 10 times 10 is equal to 1000.
For the most accurate result, you should always round after you preform the arithmetic if possible. When asked to do arithmetic and present you answer rounded to a fixed number of decimal places, only round after performing the arithmetic. Round the answer to 2 decimal places.
Since you're talking base 10, you could just use log10
to get the number of digits.
How about:
>> ceil(log10(756))
ans =
3
I don't really do Matlab, but the usual way to do this in any language I do know is: take the logarithm base 10, then round up that number to the nearest integer, then compute 10 to the power of that number. In Python:
from math import ceil, log
def ceil_power_of_10(n):
exp = log(n, 10)
exp = ceil(exp)
return 10**exp
>>> print(ceil_power_of_10(1024)) # prints 10000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With