0

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=...).

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
heinheo
  • 557
  • 1
  • 4
  • 15

2 Answers2

5

How about

floor(log10(x))

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

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1
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
RHertel
  • 23,412
  • 5
  • 38
  • 64