Hello,
Sorry I forgot to Cc the list.
And I had forgotten the case where x == 0.
extract <- function(x){
e <- ifelse(x == 0, 0, floor(log10(x)))
m <- x/10^e
list(mantissa = m, exponent = e)
}
extract(c(0, 1.234e12, 12345678901234, 123e123))
Hope this helps,
Rui Barra
On 13-06-23 5:54 AM, Søren Højsgaard wrote:
Dear all,
Given a number
x<-1.234e12
is there a built-in function for extracting 1.234 and 12 ?
I don't think so, but it is not hard to build them from log10:
mantissa <- function(x) {
if (x == 0) 0
else {
log <- log10(abs(x))
10^(log
On 23/06/2013 10:54, Søren Højsgaard wrote:
Dear all,
Given a number
x<-1.234e12
is there a built-in function for extracting 1.234 and 12 ?
No, because that is not how the number is stored (and in fact the value
stored is a binary fraction with a slightly different value).
The following
Dear all,
Given a number
x<-1.234e12
is there a built-in function for extracting 1.234 and 12 ?
The following "hack" seems clumpsy:
> a<-strsplit(format(x, scientific=T),"e")[[1]]
> a
[1] "1.234" "+12"
> as.numeric(a[1])
[1] 1.234
> as.integer(a[2])
[1] 12
Regards
Søren
___
4 matches
Mail list logo