function to get the power of a number

686 views Asked by At

looking for a way to get from an floating point number the power of 10 to which it is noted

6.45e-8 - would be 8

3.21e-4 would be 4

0.013 would be 2

or minus in all

is ther e a function which would do the following instead of multiplying with 6.45e_8 it would be at first dividing by 1e-8 and then multiply with (6.45e-8/1e8=...).

2

There are 2 answers

1
Ben Bolker On

How about

floor(log10(x))

? log10 computes the log base 10, floor finds the next smaller integer.

0
RHertel On
tenexp <- function(x){c <- trunc(log10(abs(x))); return(abs(c-1*(c<0)))}

Here's the (desired?) result:

> tenexp(0.0134)
[1] 2
> tenexp(6.45e-8)
[1] 8
> tenexp(6.45e+3)
[1] 3
> tenexp(-1.28e+4)
[1] 4