Søren Højsgaard
2013-Jun-23 09:54 UTC
[R] Built-in function for extracting mantissa and exponent of a numeric
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
Prof Brian Ripley
2013-Jun-23 11:05 UTC
[R] Built-in function for extracting mantissa and exponent of a numeric
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 "hack" seems clumpsy:format() is doing a lot of work to produce a decimal approximation to 'x', including choosing the precision. It is not clear what you want: if you merely want to express x as a*10^b for 1 <= a < 10 then b <- floor(log10(x)) a <- x/10^b does the job (but might get values of a very slightly less than 1 or above 10, so if you care this might need checking and refinement).> >> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Duncan Murdoch
2013-Jun-23 11:08 UTC
[R] Built-in function for extracting mantissa and exponent of a numeric
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 - floor(log)) } } exponent <- function(x) { if (x == 0) 0 else floor(log10(abs(x))) } Duncan Murdoch> > 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Rui Barradas
2013-Jun-23 12:25 UTC
[R] Built-in function for extracting mantissa and exponent of a numeric
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 Barradas Em 23-06-2013 10:54, S?ren H?jsgaard escreveu:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >