Java的:浮点转换二进制浮点小数 [英] Java: convert floating point binary to floating point decimal

查看:428
本文介绍了Java的:浮点转换二进制浮点小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个字符串再presenting一个IEEE754的尾数部分双转换。
找不到,如果有在Java这种转换方法中,为了避免手动添加1 + 2 + 4 + 1/8等

| 0100000011001010000111110000000000000000000000000000000000000000 - > 13374在IEEE754
| ------------ 1010000111110000000000000000000000000000000000000000 - >尾数部分
| 1.1010000111110000000000000000000000000000000000000000 - >恢复固定值1

 一个String =1.1010000111110000000000000000000000000000000000000000
双mant10 = Double.readFromFloatBinary(S); //没有这样的方法在Java中的存在?


解决方案

是的,有办法从二进制重新presentation阅读。但你不必再presentation在IEEE格式。

我会忽略周期,读作的BigInteger BASE2,然后创建一个价值还通过使用来划分的BigInteger

 私有静态双binaryStringToDouble(String s)将{
    返回字符串到(S,2);
}私有静态双字符串到(一个String,诠释基地){
    串withoutPeriod = s.replace(,。);
    双值=新的BigInteger(withoutPeriod,基地).doubleValue();
    串binaryDivisor =1+ s.split(\\\\。)[1] .replace(1,0);
    双除数=新的BigInteger(binaryDivisor,基地).doubleValue();
    返回值/除数;
}@测试
公共无效test_one_point_5(){
    字符串s =1.1;
    双D = binaryStringToDouble(S);
    的assertEquals(1.5,D,0.0001);
}@测试
公共无效test_6_8125(){
    字符串s =110.1101
    双D = binaryStringToDouble(S);
    的assertEquals(6.8125,D 0.0001);
}@测试
公共无效test_yours(){
    字符串s =1.1010000111110000000000000000000000000000000000000000;
    双D = binaryStringToDouble(S);
    的assertEquals(1.632568359375,D,0.000000000000000001);
}@测试
公共无效test_yours_no_trailing_zeros(){
    字符串s =1.101000011111;
    双D = binaryStringToDouble(S);
    的assertEquals(1.632568359375,D,0.000000000000000001);
}

I want to convert a string representing the mantissa portion of a IEEE754 double. Cannot find if there is such a conversion method in Java, in order to avoid manually adding 1 + 1/2 + 1/4 + 1/8 etc.

|0100000011001010000111110000000000000000000000000000000000000000 --> 13374 in IEEE754 |------------1010000111110000000000000000000000000000000000000000 --> mantissa part | 1.1010000111110000000000000000000000000000000000000000 --> restoring fixed value 1

String s = "1.1010000111110000000000000000000000000000000000000000"
double mant10 = Double.readFromFloatBinary(s); // does such method exists in Java?

解决方案

Yes, there are ways to read from a binary representation. But you don't have a representation in an IEEE format.

I would ignore the period and read as a BigInteger base2, then create a value to divide by also using BigInteger:

private static double binaryStringToDouble(String s) {
    return stringToDouble(s, 2);
}

private static double stringToDouble(String s, int base) {
    String withoutPeriod = s.replace(".", "");
    double value = new BigInteger(withoutPeriod, base).doubleValue();
    String binaryDivisor = "1" + s.split("\\.")[1].replace("1", "0");
    double divisor = new BigInteger(binaryDivisor, base).doubleValue();
    return value / divisor;
}

@Test
public void test_one_point_5() {
    String s = "1.1";
    double d = binaryStringToDouble(s);
    assertEquals(1.5, d, 0.0001);
}

@Test
public void test_6_8125() {
    String s = "110.1101";
    double d = binaryStringToDouble(s);
    assertEquals(6.8125, d, 0.0001);
}

@Test
public void test_yours() {
    String s = "1.1010000111110000000000000000000000000000000000000000";
    double d = binaryStringToDouble(s);
    assertEquals(1.632568359375, d, 0.000000000000000001);
}

@Test
public void test_yours_no_trailing_zeros() {
    String s = "1.101000011111";
    double d = binaryStringToDouble(s);
    assertEquals(1.632568359375, d, 0.000000000000000001);
}

这篇关于Java的:浮点转换二进制浮点小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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