1. 程式人生 > >ppt課件動手動腦實際驗證

ppt課件動手動腦實際驗證

play system.in can 使用 wing 結構 parseint multipl package

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));
}
}

ppt課件動手動腦實際驗證