Since float is a single-precision floating-point decimal, Double is a double-precision floating-point decimal
The single-precision type can be accurate to seven bits, while the double precision can be accurate to 15 bits. When you select these data types, you should pay attention to the range of values that the variables take. And the value is an integer, in order to calculate the speed, do not have to hard define the variable as floating point type.
If you want to transform between float and double, Java provides methods Float.doublevalue () and Double.floatvalue ()
See how the method is interpreted in JDK1.6
Float.doublevalue ()
Doublevalue ()
-
returns
Float
The value of this object
double
.
-
-
Designated by:
-
Number
in the class
doubleValue
-
-
Return:
The
-
value represented by this object
float
is converted to a
double
type and returns the result of the transformation.
Double.floatvalue ()
Floatvalue ()
-
returns
Double
The value of this object
float
.
-
-
Designated by:
-
Number
in the class
floatValue
-
-
Return:
-
Converts the
float
value represented by this object to a type
double
-
Start from the following versions:
-
JDK1.0
Let's start with a simple example:
public static void Main (string[] args) {float f=new float (14.1); System.out.println (F.floatvalue ()); System.out.println (F.doublevalue ());D ouble d = new Double (14.1); System.out.println (D.floatvalue ()); System.out.println (D.doublevalue ());}
The result of the output is:
14.114.10000038146972714.114.1
At this point, you can see Float.doublevalue (), the number is not accurate, it should be 14.1, and the result 14.10000381469727, which is the single-precision double-precision, double precision to the single-precision complement. Cause deviations.
That's what I'm dealing with here.
Float f=new Float (14.1); System.out.println (F.floatvalue ()); System.out.println (F.doublevalue ()); System.out.println (Double.parsedouble (F.floatvalue () + ""));
Results of the output
14.114.10000038146972714.1
Let's look at another example.
public static void Main (string[] args) {float f=new float (14.111111111111111111111111); System.out.println (F.floatvalue ()); System.out.println (F.doublevalue ());D ouble d = new Double (14.111111111111111111111111); System.out.println (D.floatvalue ()); System.out.println (D.doublevalue ());}
Output Result:
14.11111114.1111106872558614.11111114.11111111111111
At this point, due to the number of digits after the decimal point is too long, Float single-precision and double doubles to perform a truncation after the calculation, at this time, Float.doublevalue () still has a complement problem, but this complement has already destroyed the original data.
To reduce the error, convert the float data into double data, or take a previous action
System.out.println (Double.parsedouble (F.floatvalue () + ""));
The result of the output is
14.111111
And look at the following example
public static void Main (string[] args) {float f=new float (14.6666666666666666666666666666666666666); System.out.println (F.floatvalue ()); System.out.println (F.doublevalue ()); System.out.println (Double.parsedouble (F.floatvalue () + "));D ouble d = new Double ( 14.6666666666666666666666666666666666666); System.out.println (D.floatvalue ()); System.out.println (D.doublevalue ());}
The result of the output is
14.66666714.66666698455810514.66666714.66666714.666666666666666
It will be found that when the given data exceeds the single-precision number of digits, will be intercepted, the number of the remaining first digit is greater than 5 o'clock, the inevitable carry (that is, the last one + 1), less than 5 o'clock, must not carry (that is, the last one unchanged).
The first digit left after the intercept is equal to 5 o'clock, sometimes rounded and sometimes not rounded. (see source, not find out why)
Double precision is not rounded at this value
And look at the following example
public static void Main (string[] args) {float f=new float (14.777777777777777777777777); System.out.println (F.floatvalue ()); System.out.println (F.doublevalue ()); System.out.println (Double.parsedouble (F.floatvalue () + "));D ouble d = new Double (14.77777777777777777777777777777); System.out.println (D.floatvalue ()); System.out.println (D.doublevalue ());}
The result of this
14.77777814.77777767181396514.77777814.77777814.777777777777779
Therefore, in a large number of data testing, it is found that both single-precision and double-precision accurate carry is impossible to determine.
Summarize
When the single-precision double-precision is not prevented by the use of the complement problem
Double.parsedouble (F.floatvalue () + "")
To preserve the accuracy of single-precision data
The error data is not found when the double precision is transferred to the single precision. The basic can be the same as the single precision.
Finally finished writing .... This article is mainly for beginners to see ... If there is a better solution, you can comment directly ..... So that everyone can learn .....
The problem of transformation between Java float and double