Reputation: 1609
My main aim is to write a function that converts:
i.e.,
from10toNdp(86.125) # 0100000001010101100010000000000000000000000000000000000000000000 (sign|exponent|significand)
fromNdpTo10(0100000001010101100010000000000000000000000000000000000000000000) # 86.125
On the path to create from10toNdp
and fromNdpTo10
, I did the followings.
I wrote some helpers:
from10to2 <- function(decimalnumber) {
binaryvector <- rep(0, 1 + floor(log(decimalnumber, 2)))
while (decimalnumber >= 2) {
power <- floor(log(decimalnumber, 2))
binaryvector[1 + power] <- 1
decimalnumber <- decimalnumber - 2^power }
binaryvector[1] <- decimalnumber %% 2
paste(rev(binaryvector), collapse = "")}
from10to2(8) # "1000"
The from10to2
function works perfectly for integers less than 2^53 = 9.007199e15 (i.e. it works for integers stored as double as well; works for beyond 2147483647); for numbers bigger than 2^53 it starts to lose digits.
EXAMPLE1: Convert 0.3 to 64-bit normalized double precision number:
Sign: 0: pozitive; 1:negative
sprintf("%a", 0.3) # ("0x1.3333333333333p-2")16
library(BMS)
BMS::hex2bin("3333333333333") # (0011001100110011001100110011001100110011001100110011)2=significand=(3333333333333)16
Since sprintf
's output's last three "p-2" and "BiasedExponent=RealExponent+1023bias" (bias:2^(11-1)-1 = 1023), exponent (11bit) is -2+1023=1021.
from10to2(1021) # ("01111111101")2
Double precision storage:
0 | 01111111101 | 0011001100110011001100110011001100110011001100110011
sign | exponent | significand
EXAMPLE2: Convert -2.94 to 64-bit normalized double precision number:
Sign: 0: pozitive; 1:negative
sprintf("%a", -2.94) # "-0x1.7851eb851eb85p+1"
library(BMS)
BMS::hex2bin("7851eb851eb85") # (0111100001010001111010111000010100011110101110000101)2=significand=(7851eb851eb85)16
Since sprintf
's output's last three "p+1" and "BiasedExponent=RealExponent+1023bias" (bias:2^(11-1)-1 = 1023), exponent (11bit) is 1+1023=1024.
from10to2(1024) # ("10000000000")2
Double precision storage:
1 | 10000000000 | 0111100001010001111010111000010100011110101110000101
sign | exponent | significand
EXAMPLE3: Convert 86.125 to 64-bit normalized double precision number:
Sign: 0: pozitive; 1:negative
sprintf("%a", 86.125) # "0x1.588p+6"
library(BMS)
BMS::hex2bin("588") # (010110001000)2=significand=(588)16
Since sprintf
's output's last three "p+6" and "BiasedExponent=RealExponent+1023bias" (bias:2^(11-1)-1 = 1023), exponent (11bit) is 6+1023=1029.
from10to2(1029) # ("10000000101")2
Double precision storage:
0 | 10000000101 | 010110001000 (but this is not 64-bit, but 1+11+12=24 bit!)
sign | exponent | significand.
True 64-bit double precision must be:
0 | 10000000101 | 0101100010000000000000000000000000000000000000000000.
So, this method could not find last 40 zeros, but can find first 24 bits correctly.
Online converters:
My technique (that works for 0.3 and -2.94) stops functioning for 86.125 unexpectedly and does not produce 64-bit.
Why does the technique stop for 86.125?
Is there already a way to convert:
(I hope what I am doing is not re-discovering America from scratch)
Any help is greatly appreciated.
Upvotes: 1
Views: 429
Reputation: 1609
library(BMS)
from10toNdp <- function(my10baseNumber) {
out <- list()
# Handle special cases (0, Inf, -Inf)
if (my10baseNumber %in% c(0,Inf,-Inf)) {
if (my10baseNumber==0) { out <- "0000000000000000000000000000000000000000000000000000000000000000" }
if (my10baseNumber==Inf) { out <- "0111111111110000000000000000000000000000000000000000000000000000" }
if (my10baseNumber==-Inf) { out <- "1111111111110000000000000000000000000000000000000000000000000000" }
} else {
signBit <- 0 # assign initial value
from10to2 <- function(deciNumber) {
binaryVector <- rep(0, 1 + floor(log(deciNumber, 2)))
while (deciNumber >= 2) {
theExpo <- floor(log(deciNumber, 2))
binaryVector[1 + theExpo] <- 1
deciNumber <- deciNumber - 2^theExpo }
binaryVector[1] <- deciNumber %% 2
paste(rev(binaryVector), collapse = "")}
#Sign bit
if (my10baseNumber<0) { signBit <- 1
} else { signBit <- 0 }
# Biased Exponent
BiasedExponent <- strsplit(from10to2(as.numeric(substr(sprintf("%a", my10baseNumber), which(strsplit( sprintf("%a", my10baseNumber), "")[[1]]=="p")+1, length( strsplit( sprintf("%a", my10baseNumber), "")[[1]]))) + 1023), "")[[1]]
BiasedExponent <- paste(BiasedExponent, collapse='')
if (nchar(BiasedExponent)<11) {BiasedExponent <- paste(c( rep(0,11-nchar(BiasedExponent)), BiasedExponent),collapse='') }
# Significand
significand <- BMS::hex2bin(substr( sprintf("%a", my10baseNumber) , which(strsplit( sprintf("%a", my10baseNumber), "")[[1]]=="x")+3, which(strsplit( sprintf("%a", my10baseNumber), "")[[1]]=="p")-1))
significand <- paste(significand, collapse='')
if (nchar(significand)<52) {significand <- paste(c( significand,rep(0,52-nchar(significand))),collapse='') }
out <- paste(c(signBit, BiasedExponent, significand), collapse='')
}
out
}
from10toNdp(0.3)
# "0011111111010011001100110011001100110011001100110011001100110011"
from10toNdp(-2.94)
# "1100000000000111100001010001111010111000010100011110101110000101"
from10toNdp(86.125)
# "0100000001010101100010000000000000000000000000000000000000000000"
from10toNdp(-589546.684259)
# "1100000100100001111111011101010101011110010101110011001000010110"
from10toNdp(0)
# "0000000000000000000000000000000000000000000000000000000000000000"
from10toNdp(Inf)
# "0111111111110000000000000000000000000000000000000000000000000000"
from10toNdp(-Inf)
# "1111111111110000000000000000000000000000000000000000000000000000"
Upvotes: 0