3

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

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

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 is:

(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

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 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

Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40
  • I understand all the derivations, including the last part x=2^(-3) in your reasoning. I could not see unfortunately how you obtain `(0.001)base2 = (x)base10 => log_2(x) = log_10(0.001)`; notice that 0.001 and x are in the opposite sides! – Erdogan CEVHER Jun 02 '19 at 00:38
  • @jenesaisquoi 2^log(0.001, 10) technique fails in some situations! Observe: 2^log(0.101, 10) # 0.5014999 whereas 0.101 = (1*1/2) + (0*1/2^2) + (1*1/2^3)=1*0.5 + 0*0.25 + 1*0.125=0.625. And 0.5014999<>0.625. – Erdogan CEVHER Jun 02 '19 at 01:19
  • I am nearby a solution: `sum(c(0,0,1)* (0.5^(1:3))) # 0.125` and `sum(c(1,0,1)* (0.5^(1:3))) # 0.625`. – Erdogan CEVHER Jun 02 '19 at 01:53

2 Answers2

2

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)))
}

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
martin_joerg
  • 1,153
  • 1
  • 13
  • 22
0
library(cwhmisc) # int, frac
from2to10 <- function(n) {
SignOfNumber <- ""
if (n < 0) {
n <- abs(n)
SignOfNumber <- "-"}

nWhole <- int(n)
nWhole <- as.character(nWhole)

nFraction <- frac(n)
nFraction <- as.character(nFraction)

DecimalWhole   <- sapply(strsplit(nWhole, split=""), function(x) sum(as.numeric(x) * 2^(rev(seq_along(x) - 1))))

if (nFraction == 0) {
DecimalFraction <- ""
paste0(SignOfNumber, DecimalWhole)
} else { # Find decimal fraction part

part3 <- function(x, y, z) { eval(parse(text=(paste(x, y, z,sep="")))) }
y <- as.numeric(strsplit(substr(part3("\"",n,"\""), which(strsplit(part3("\"",n,"\""), "")[[1]]==".") + 1, nchar(part3("\"",n,"\""))),"")[[1]])
DecimalFraction <- sum(y * (0.5^(1:length(y))))
paste0(SignOfNumber, DecimalWhole + DecimalFraction)
}
}

from2to10(0.001) # "0.125"
as.numeric(from2to10(0.001)) # 0.125
Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40