简体   繁体   English

R中的log2:如何计算指数和尾数

[英]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? 有谁知道如何在R中执行相同的MATLAB函数[F,E] = log2(X)

[F,E] = log2(X) returns arrays F and E such that X=F*2^E. [F,E] = log2(X)返回数组F和E,使得X = F * 2 ^ E。 The values in F are typically in the range 0.5 <= abs(F) < 1. F中的值通常在0.5 <= abs(F)<1的范围内。

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

For example in MATLAB, 例如在MATLAB中,

[F,E] = log2(15) [F,E] = log2(15)

F = F =

 0.9375 

E = E =

  4 

Thus, 从而,

F*2^E = 15 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. 编辑:对于x == 0进行更改。

I'm not entirely sure what you're asking but log2 gives you the logarithm base 2 in R. For example 我不确定您要问的是什么,但是log2给您R的对数以2为底。例如

log2(2);
#[1] 1

log2(2^10)
#[1] 10

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

See ?log for details. 有关详细信息,请参见?log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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