1 about double precision
Source:
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));
}
}
Experimental results:
0.05+0.01=0.060000000000000005
1.0-0.42=0.5800000000000001
4.015*100=401.49999999999994
123.3/100=1.2329999999999999
Reason:
The reason is that our computer is binary. Floating-point numbers do not have to be represented in a binary way. Our CPU indicates that the floating-point number is composed of two parts: exponent and mantissa, so the expression method generally loses certain accuracy, some floating-point arithmetic will produce some error. For example, a binary representation of 2.4 is not exactly 2.4. Instead the nearest binary representation is 2.3999999999999999
double:100000000100000000000000000000000000000000000000000000000000000
float:1000001000000000000000000000000
The results of the output analysis are as follows. For a binary that does not double the left side of the symbol bit 0 just can get 64 bits of binary number. According to the notation of double, the number of symbols, power exponent and mantissa are divided into three parts as follows:
0 10000010111 0011000101100111100101110000000000000000000000000000
For float the left side of the sign bit 0 just can get 32 bit binary number. According to the notation of float, the number of symbols, power exponent and mantissa are also divided into three parts as follows:
0 10010111 00110001011001111001100
The green part is the sign bit, the red part is the power exponent, the blue part is the mantissa.
The comparison can be drawn: The sign bit is 0, the power exponent is the shift code representation, both are equal. The only difference is the mantissa.
The mantissa in the double is: 001100010110011110010111 0000000000000000000000000000, omitting the subsequent 0, which requires at least 24 bits to be correctly represented.
And the mantissa below float is: 00110001011001111001100, total 23 bits.
Why is that? The reason is obvious, because the float mantissa can only represent 23 bits, so the 24-bit 001100010110011110010111 is rounded down to 23-bit 00110001011001111001100 under float. So 20014999 has become 20015000 under float.
That is to say that 20014999 is within the range of float, but the float representation in IEEE 754 has no way to represent 20014999, but only by rounding to get an approximate value.
Floating-point arithmetic is rarely accurate, as long as it exceeds the range of precision can be expressed error. Often the error is not due to the size of the number, but because of the precision of the number. Therefore, the resulting result is close but not equal to the desired result. Especially careful when using float and double for precise calculations.
2. Enumeration
public class Enumtest {
public static void Main (string[] args) {
Size S=size.small;
Size T=size.large;
S and T refer to the same object?
System.out.println (s==t); //
is the original data type?
System.out.println (S.getclass (). isprimitive ());
Convert from String
Size u=size.valueof ("SMALL");
System.out.println (S==u); True
List all of its values
For (Size value:Size.values ()) {
System.out.println (value);
}
}
}
Enum Size{small,medium,large};
Output Result:
False
False
True
SMALL
MEDIUM
LARGE
3. Summation
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. Keyboard input
Source
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 is 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. Using the BigDecimal class
Source
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 ("Using string as the calculation result of the BigDecimal constructor parameter:");
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 ("Use double as the calculation result of the BigDecimal constructor parameter:");
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. Select structure
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 ("Using string as the calculation result of the BigDecimal constructor parameter:");
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 ("Use double as the calculation result of the BigDecimal constructor parameter:");
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));
}
}
Practical verification of the hands-on brain of PPT courseware