如何在内存中将数值转换为归一化的双精度,反之亦然?

时间:2019-05-24 08:49:16

标签: r double decimal storage data-conversion

我的主要目的是编写一个可转换的函数:

  • (十进制)数值到内存中的标准化双精度
  • 内存中归一化的双精度(十进制)数值

from10toNdp(86.125) # 0100000001010101100010000000000000000000000000000000000000000000 (sign|exponent|significand)

fromNdpTo10(0100000001010101100010000000000000000000000000000000000000000000) # 86.125

在创建from10toNdpfromNdpTo10的路上,我做了以下工作。

我写了一些助手:

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"

from10to2函数适用于小于2 ^ 53 = 9.007199e15的整数(即,它也适用于存储为double的整数;适用于2147483647以上的整数);对于大于2 ^ 53的数字,它将开始丢失数字。

示例1::将0.3转换为64位归一化双精度数字:
符号:0:积极; 1:负数

sprintf("%a", 0.3) # ("0x1.3333333333333p-2")16
library(BMS)
BMS::hex2bin("3333333333333") # (0011001100110011001100110011001100110011001100110011)2=significand=(3333333333333)16

由于sprintf的输出的最后三个“ p-2”和“ BiasedExponent = RealExponent + 1023bias”(偏差:2 ^(11-1)-1 = 1023),所以指数(11bit)为-2 + 1023 = 1021。

from10to2(1021) # ("01111111101")2

双精度存储:
0 | 01111111101 | 0011001100110011001100110011001100110011001100110011
标志|指数|重要

示例2::将-2.94转换为64位归一化双精度数字:
符号:0:积极; 1:负数

sprintf("%a", -2.94) # "-0x1.7851eb851eb85p+1"
library(BMS)
BMS::hex2bin("7851eb851eb85") # (0111100001010001111010111000010100011110101110000101)2=significand=(7851eb851eb85)16

由于sprintf的输出的最后三个“ p + 1”和“ BiasedExponent = RealExponent + 1023bias”(偏差:2 ^(11-1)-1 = 1023),因此指数(11bit)为1+ 1023 = 1024。

from10to2(1024) # ("10000000000")2

双精度存储:
1 | 10000000000 | 01111000010100010001111010111000010100011110101110000101
标志|指数|重要

示例3::将86.125转换为64位归一化双精度数字:
符号:0:积极; 1:负数

sprintf("%a", 86.125) # "0x1.588p+6"
library(BMS)
BMS::hex2bin("588") # (010110001000)2=significand=(588)16

由于sprintf的输出的最后三个“ p + 6”和“ BiasedExponent = RealExponent + 1023bias”(偏差:2 ^(11-1)-1 = 1023),因此指数(11bit)为6+ 1023 = 1029。

from10to2(1029) # ("10000000101")2

双精度存储:
0 | 10000000101 | 010110001000(但这不是64位,而是1 + 11 + 12 = 24位!)
标志|指数|重要的。 真正的64位双精度必须为:
0 | 10000000101 | 0101100010000000000000000000000000000000000000000000000000000000.
因此,该方法找不到最后40个零,但可以正确找到第一个24位。

在线转换器:

  1. https://www.exploringbinary.com/floating-point-converter/
    “原始二进制(符号字段|指数字段|有效字段)Double:”
  2. http://www.binaryconvert.com/convert_double.html

我的技术(适用于0.3和-2.94)意外地停止了86.125的功能,并且不产生64位。

为什么该技术在86.125停止?

已经有一种转换方法:

  • (十进制)数值到内存中的标准化双精度
  • 内存中标准化的双精度(十进制)?

(我希望我所做的不是从头开始重新发现美国)

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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"