如何在R中将二进制分数转换为十进制分数? [英] How to convert a binary fraction number into decimal fraction number in R?

查看:322
本文介绍了如何在R中将二进制分数转换为十进制分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个将R中的二进制分数转换为十进制分数的函数. f(0.001) # 0.125

I need to write a function that converts a binary fraction number into decimal fraction number in R. e.g. f(0.001) # 0.125

我做了什么:我在R包中搜索了相关功能:

What I did: I searched for the related functions in R packages:

DescTools::BinToDec(0.001) # NA
DescTools::BinToDec("0.001") # NA
base::strtoi(0.001, base=2) # NA
base::strtoi("0.001", base=2) # NA

base::packBits(intToBits(0.001), "integer") # 0
base::packBits(intToBits("0.001"), "integer") # 0

compositions::unbinary(0.001) # 0.001
compositions::unbinary("0.001") # NA

我在SOF中进行了搜索,发现了以下内容:

I searched in SOF, found the following:

base2decimal <- function(base_number, base = 2) {
  split_base <- strsplit(as.character(base_number), split = "")
  return(sapply(split_base, function(x) sum(as.numeric(x) * base^(rev(seq_along(x) - 1)))))}
base2decimal(0.001) # NA
base2decimal("0.001") # NA

0.001是:

(0 * 2^(-1)) + (0 * 2^(-2)) + (1 * 2^(-3)) # 0.125
(0 * 1/2) + (0 * (1/2)^2) + (1 * (1/2)^3) # 0.125
(0 * 0.5) + (0 * (0.5)^2) + (1 * 0.5^3) # 0.125

所以,像内积之和(0,0,1) * (0.5^1, 0.5^2, 0.5^3)之类的东西似乎可以解决问题,在一般情况下,我不知道该怎么做.

So, something like sum of the inner product (0,0,1) * (0.5^1, 0.5^2, 0.5^3) seems to finish the problem, I could not figure out how to do this in general case.

javascript案例:
如何将二进制分数转换为十进制分数数字? 如何将二进制分数转换为十进制 Lisp案例:
将分数从十进制转换为二进制

javascript case:
How to convert a binary fraction number into decimal fraction number? How to convert binary fraction to decimal lisp case:
Convert fractions from decimal to binary

推荐答案

您可以扩展发布在自己的解决方案问题还包括从小数点分隔符的位置开始的两个负数,如下所示:

You can extend the solution you posted in your question to also include the negative powers of two starting at the position of the decimal separator as follows:

base2decimal <- function(base_number, base = 2) {
    base_number = paste(as.character(base_number), ".", sep = "")
    return (mapply(function (val, sep) {
                      val = val[-sep];
                      if (val[[1]] == "-") {
                          sign = -1
                          powmax = sep[[1]] - 3
                          val = val[-1]
                      } else {
                          sign = 1
                          powmax = sep[[1]] - 2
                      };
                      sign * sum(as.numeric(val) * (base ^ seq(powmax, by = -1, length = length(val))))},
        strsplit(base_number, NULL), gregexpr("\\.", base_number)))
}

此代码还适用于小于(或等于)10的其他基数:

This code also works for other bases less than (or equal) 10:

base2decimal(c('0.101', '.101', 0.101, 1101.001, 1101, '-0.101', '-.101', -0.101, -1101.001, -1101))
#[1]   0.625   0.625   0.625  13.125  13.000  -0.625  -0.625  -0.625 -13.125
#[10] -13.000
base2decimal(1110.111)
# 14.875
base2decimal(256.3, 8)
# [1] 174.375

这篇关于如何在R中将二进制分数转换为十进制分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆