Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding to a power of 10

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!

like image 451
Alex Nichols Avatar asked Jul 20 '11 22:07

Alex Nichols


People also ask

What is as a power of 10?

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.

What is the meaning of 10 to the power of 10?

10 times 10 times 10 is equal to 1000.

Do you round up when doing scientific notation?

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.


2 Answers

Since you're talking base 10, you could just use log10 to get the number of digits.

How about:

>> ceil(log10(756))

ans =

     3
like image 139
b3. Avatar answered Sep 22 '22 01:09

b3.


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
like image 25
steveha Avatar answered Sep 25 '22 01:09

steveha