简体   繁体   中英

log2 in R: How to calculate the exponent and mantissa

Does anyone know how to carry out the same MATLAB function [F,E] = log2(X) in R?

[F,E] = log2(X) returns arrays F and E such that X=F*2^E. The values in F are typically in the range 0.5 <= abs(F) < 1.

See https://www.mathworks.com/help/matlab/ref/log2.html

For example in MATLAB,

[F,E] = log2(15)

F =

 0.9375 

E =

  4 

Thus,

F*2^E = 15

You'll need to calculate them manually. I don't think there's a builtin to extract them. Try this:

x<-15
E <- ifelse(x == 0, 0, floor(log2(abs(x)))+1 )
F<-x/2^E

Edit: Made the change for the case of x==0.

I'm not entirely sure what you're asking but log2 gives you the logarithm base 2 in R. For example

log2(2);
#[1] 1

log2(2^10)
#[1] 10

2^(log2(10))
#[1] 10

See ?log for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM