• ppt课件动手动脑实际验证


    1关于double精度

      源代码:
    public class Doublejingdu {
    public static void main(String[] args) {
    System.out.println("0.05+0.01="+(0.05+0.01));
    System.out.println("1.0-0.42="+(1.0-0.42));
    System.out.println("4.015*100="+(4.015*100));
    System.out.println("123.3/100="+(123.3/100));
    }
    }

    实验结果:

    0.05+0.01=0.060000000000000005
    1.0-0.42=0.5800000000000001
    4.015*100=401.49999999999994
    123.3/100=1.2329999999999999

    原因:

    原因在于我们的计算机是二进制的。浮点数没有办法是用二进制进行精确表示。我们的CPU表示浮点数由两个部分组成:指数和尾数,这样的表示方法一般都会失去一定的精确度,有些浮点数运算也会产生一定的误差。如:2.4的二进制表示并非就是精确的2.4。反而最为接近的二进制表示是 2.3999999999999999

    Double:100000000100000000000000000000000000000000000000000000000000000

    Float:1000001000000000000000000000000

    对于输出结果分析如下。对于都不 double 的二进制左边补上符号位 0 刚好可以得到 64 位的二进制数。根据double的表 示法,分为符号数、幂指数和尾数三个部分如下:

    0 10000010111 0011000101100111100101110000000000000000000000000000

    对于 float 左边补上符 号位 0 刚好可以得到 32 位的二进制数。 根据float的表示法, 也分为 符号数、幂指数和尾数三个部分如下 :

    0 10010111 00110001011001111001100

    绿色部分是符号位,红色部分是幂指数,蓝色部分是尾数。

    对比可以得出:符号位都是 0 ,幂指数为移码表示,两者刚好也相等。唯一不同的是尾数。

    在 double 的尾数 为: 001100010110011110010111 0000000000000000000000000000 ,省略后面的零,至少需要24位才能正确表示 。

    而在 float 下面尾数 为: 00110001011001111001100 ,共 23 位。

    为什么会这样?原因很明显,因为 float尾数 最多只能表示 23 位,所以 24 位的 001100010110011110010111 在 float 下面经过四舍五入变成了 23 位的 00110001011001111001100 。所以 20014999 在 float 下面变成了 20015000 。
    也就是说 20014999 虽然是在float的表示范围之内,但 在 IEEE 754 的 float 表示法精度长度没有办法表示出 20014999 ,而只能通过四舍五入得到一个近似值。

    浮点运算很少是精确的,只要是超过精度能表示的范围就会产生误差。往往产生误差不是 因为数的大小,而是因为数的精度。因此,产生的结果接近但不等于想要的结果。尤其在使用 float 和 double 作精确运 算的时候要特别小心。

    2.枚举


    public class EnumTest {

    public static void main(String[] args) {
    Size s=Size.SMALL;
    Size t=Size.LARGE;
    //s和t引用同一个对象?
    System.out.println(s==t); //
    //是原始数据类型吗?
    System.out.println(s.getClass().isPrimitive());
    //从字符串中转换
    Size u=Size.valueOf("SMALL");
    System.out.println(s==u); //true
    //列出它的所有值
    for(Size value:Size.values()){
    System.out.println(value);
    }
    }

    }
    enum Size{SMALL,MEDIUM,LARGE};

    输出结果:

    false
    false
    true
    SMALL
    MEDIUM
    LARGE

    3.求和

    package text;


    //An addition program

    import javax.swing.JOptionPane; // import class JOptionPane

    public class Addition {
    public static void main( String args[] )
    {
    String firstNumber, // first string entered by user
    secondNumber; // second string entered by user
    int number1, // first number to add
    number2, // second number to add
    sum; // sum of number1 and number2

    // read in first number from user as a string
    firstNumber =
    JOptionPane.showInputDialog( "Enter first integer" );

    // read in second number from user as a string
    secondNumber =
    JOptionPane.showInputDialog( "Enter second integer" );

    // convert numbers from type String to type int
    number1 = Integer.parseInt( firstNumber );
    number2 = Integer.parseInt( secondNumber );

    // add the numbers
    sum = number1 + number2;

    // display the results
    JOptionPane.showMessageDialog(
    null, "The sum is " + sum, "Results",
    JOptionPane.PLAIN_MESSAGE );

    System.exit( 0 ); // terminate the program
    }
    }

     4.键盘输入

    源代码

    package text;
    /**
    @version 1.10 2004-02-10
    @author Cay Horstmann
    */

    import java.util.*;

    public class InputTest
    {
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);

    // get first input
    System.out.print("What is your name? ");
    String name = in.nextLine();

    // get second input
    System.out.print("How old are you? ");
    int age = in.nextInt();


    /* int i;
    String value="100";
    i=Integer.parseInt(value);
    i=200;
    String s=String.valueOf(i);*/

    // display output on console
    System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));


    }
    }

    5.使用BigDecimal类

    源代码


    import java.math.BigDecimal;

    public class TestBigDecimal
    {
    public static void main(String[] args)
    {
    BigDecimal f1 = new BigDecimal("0.05");
    BigDecimal f2 = BigDecimal.valueOf(0.01);
    BigDecimal f3 = new BigDecimal(0.05);
    System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
    System.out.println("0.05 + 0.01 = " + f1.add(f2));
    System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
    System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
    System.out.println("0.05 / 0.01 = " + f1.divide(f2));
    System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
    System.out.println("0.05 + 0.01 = " + f3.add(f2));
    System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
    System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
    System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
    }

    5.选择结构


    import java.math.BigDecimal;

    public class TestBigDecimal
    {
    public static void main(String[] args)
    {
    BigDecimal f1 = new BigDecimal("0.05");
    BigDecimal f2 = BigDecimal.valueOf(0.01);
    BigDecimal f3 = new BigDecimal(0.05);
    System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
    System.out.println("0.05 + 0.01 = " + f1.add(f2));
    System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
    System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
    System.out.println("0.05 / 0.01 = " + f1.divide(f2));
    System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
    System.out.println("0.05 + 0.01 = " + f3.add(f2));
    System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
    System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
    System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
    }

  • 相关阅读:
    Filter过滤器
    jsp-JSTL表达式
    jsp-EL表达式
    JSP概述
    servlet编码问题
    微信开放平台-踩坑1
    Supervisor的安装以及使用
    laravel-mix的安装
    Laravel框架中打印sql
    Laravel 5.7 使用 PHP artisan migrate 的问题
  • 原文地址:https://www.cnblogs.com/zlc364624/p/9750630.html
Copyright © 2020-2023  润新知