zoukankan      html  css  js  c++  java
  • Java的浮点数和整数的进制转换

    整数的表达
    –原码:第一位为符号位(0为正数,1为负数)
    –反码:符号位不动,原码取反
    –负数补码:符号位不动,反码加1
    –正数补码:和原码相同
     
      -6      5
    原码 10000110 00000101
    反码 11111001 01111010
    补码 11111010 00000101

                                                          

    补码运输的例子:

    -6+5                                                         -4+5

      11111010                                                 11111100

    + 00000101                                            + 00000101

    = 11111111                                              = 00000001

    –打印整数的二进制表示
    int a=-6;
    for(int i=0;i<32;i++){
      int t=(a & (0x80000000)>>>i)>>>(31-i);
      System.out.print(t);
    }
     
    Float的表示与定义 –支持 IEEE 754
    • s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
    • e全0 尾数附加位为0  否则尾数附加位为1, 附加位对应2^0
     •s*m*2^(e-127)
    IEEE754 符号位  指数位 尾数位  指数偏移量 计算公式 附加位
    float(单精度浮点数)  1[31] 8[23-30] 23[0-22] 127 s*m*2^(e-127) e全0 尾数附加位为0  否则为1, 附加位对应2^0
    double(双进度浮点数) 1[63] 11[52-62]  52[0-51]  1023 s*m*2^(e-1023) e全0 尾数附加位为0  否则为1, 附加位对应2^0

    -5的IEEE 754表示.

    11000000101000000000000000000000

    -1*2^(129-127)*(2^0+2^-2) 红色的部分对应附加位

    一个IEEE754在线小工具: http://www.h-schmidt.net/FloatConverter/IEEE754.html

    打印float的IEEE754表示的二进制串

    public class FloatString {
    
    public static void main(String[] args){
    
      float f = 100.2f;
      int intString = Float.floatToIntBits(f);
      if(intString!=0x7f800000||intString!=0xff800000||intString!=0x7fc00000)
      {
        for(int i =0;i<32;i++)
        {
          int t =( intString & (0x80000000>>>i) ) >>> (31-i);
          System.out.print(t);
        }
      }
    }
    }

    Java 的进制间转换.

    十进制转成十六进制:Integer.toHexString(int i)   

    十进制转成八进制:Integer.toOctalString(int i) 

    十进制转成二进制:Integer.toBinaryString(int i)

    十进制转成别的进制的通用方法:Integer.toString(1, 8);

    十六进制转成十进制:Integer.valueOf("FFFF",16).toString()   

    八进制转成十进制:Integer.valueOf("876",8).toString() 

    二进制转十进制:Integer.valueOf("0101",2).toString()

    转成十进制的通用方法 Integer.parseInt(s, radix) /Integer.valueOf(s, radix)

    Float 到double的转换错误例子:

    问题1:double类型转float类型没有出错,float转double类型出错。
    float f = 121.1f;
    double d = f;
    System.out.println(d);
    输出:
    121.0999984741211

    解决办法1:
    float f = 121.1f;
    double d ;
    BigDecimal bd = new BigDecimal(String.valueOf(f));
    d = bd.doubleValue();
    System.out.println(d);
    输出:
    121.1

    问题2:
    double d = (3.3-2.1)/0.1;
    System.out.println(d);
    输出:
    11.999999999999996

    解决办法2:

    BigDecimal b = new BigDecimal("3.3").subtract(new BigDecimal("2.1")).divide(new BigDecimal("0.1"));
    System.out.println(b.doubleValue());

    小数运算时,或可能会产生小数的运算时,都转换成BigDecimal后再进行,得到结果后再转换成相应类型。
    如果感觉麻烦,可以写一个基本运算的工具类,参考博客

    说明:

    java中整数默认是int,小数默认是double类型。(如要得到float类型,则加f如:12.1f)

    int:4字节,long:8字节,float:4字节,double:8字节

    float类型的变量只有7位的精度,而double类型的变量有15位的精度。

    java在运算时会自动的提升变量的精度来进行运算。(double比float精度更高,所以可以自动的从float转化至double再进行运算。)

    BigDecimal介绍:

    float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用java.math.BigDecimal.

    BigDecimal(double val)  Translates a double into a BigDecimal.这个方法会丢失精度.下面是该API中的说明.

    Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
    The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.

    BigDecimal(String val) Translates the String repre sentation of a BigDecimal into a BigDecimal. 这个方法不会丢失精度.

    做浮点数运算的工具类

    import java.math.BigDecimal;
      
      /**
      
      * 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精
      
      * 确的浮点数运算,包括加减乘除和四舍五入。
      
      */
      
      public class Arith{
      
      //默认除法运算精度
      
      private static final int DEF_DIV_SCALE = 10;
      
      //这个类不能实例化
      
      private Arith(){
      
      }
      
      /**
      
      * 提供精确的加法运算。
      
      * @param v1 被加数
      
      * @param v2 加数
      
      * @return 两个参数的和
      
      */
      
      public static double add(double v1,double v2){
      
      BigDecimal b1 = new BigDecimal(Double.toString(v1));
      
      BigDecimal b2 = new BigDecimal(Double.toString(v2));
      
      return b1.add(b2).doubleValue();
      
      }
      
      /**
      
      * 提供精确的减法运算。
      
      * @param v1 被减数
      
      * @param v2 减数
      
      * @return 两个参数的差
      
      */
      
      public static double sub(double v1,double v2){
      
      BigDecimal b1 = new BigDecimal(Double.toString(v1));
      
      BigDecimal b2 = new BigDecimal(Double.toString(v2));
      
      return b1.suBTract(b2).doubleValue();
      
      }
      
      /**
      
      * 提供精确的乘法运算。
      
      * @param v1 被乘数
      
      * @param v2 乘数
      
      * @return 两个参数的积
      
      */
      
      public static double mul(double v1,double v2){
      
      BigDecimal b1 = new BigDecimal(Double.toString(v1));
      
      BigDecimal b2 = new BigDecimal(Double.toString(v2));
      
      return b1.multiply(b2).doubleValue();
      
      }
      
      /**
      
      * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
      
      * 小数点以后10位,以后的数字四舍五入。
      
      * @param v1 被除数
      
      * @param v2 除数
      
      * @return 两个参数的商
      
      */
      
      public static double div(double v1,double v2){
      
      return div(v1,v2,DEF_DIV_SCALE);
      
      }
      
      /**
      
      * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
      
      * 定精度,以后的数字四舍五入。
      
      * @param v1 被除数
      
      * @param v2 除数
      
      * @param scale 表示表示需要精确到小数点以后几位。
      
      * @return 两个参数的商
      
      */
      
      public static double div(double v1,double v2,int scale){
      
      if(scale<0){
      
      throw new IllegalArgumentException(
      
      "The scale must be a positive integer or zero");
      
      }
      
      BigDecimal b1 = new BigDecimal(Double.toString(v1));
      
      BigDecimal b2 = new BigDecimal(Double.toString(v2));
      
      return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
      
      }
      
      /**
      
      * 提供精确的小数位四舍五入处理。
      
      * @param v 需要四舍五入的数字
      
      * @param scale 小数点后保留几位
      
      * @return 四舍五入后的结果
      
      */
      
      public static double round(double v,int scale){
      
      if(scale<0){
      
      throw new IllegalArgumentException(
      
      "The scale must be a positive integer or zero");
      
      }
      
      BigDecimal b = new BigDecimal(Double.toString(v));
      
      BigDecimal one = new BigDecimal("1");
      
      return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
      
      }
      
      }; 

    参考:http://blog.chinaunix.net/uid-26434689-id-3554399.html

  • 相关阅读:
    CSS——before和after伪元素
    CSS——滑动门技术及应用
    CSS案例3(在线教育网站)
    CSS——背景渐变
    CSS字体图标
    CSS——精灵技术
    CSS——溢出文字隐藏
    Intellij IDEA -01 如何配置项目!
    Intellij Idea -02 如何将项目工程横向排列变成纵向排列
    java8 --新特性汇总
  • 原文地址:https://www.cnblogs.com/princessd8251/p/3902437.html
Copyright © 2011-2022 走看看