Java Theory and Practice: Where has your decimal point gone?

Source: Internet
Author: User
Tags square root wrapper

Many programmers do not use fixed-point or floating-point numbers throughout their development careers, with the possible exception being used occasionally in timing tests or benchmarking programs. The Java language and class libraries support two types of ―ieee 754 floating-point (float and double, wrapper class (wrapper class) for float and double), and arbitrary-precision decimals (java.math.BigDecimal). In this month's Java theory and practice, Brian Goetz explores some of the pitfalls and "gotcha" that are often encountered when using non-integer types in Java programs.

Although almost every processor and programming language supports floating-point operations, most programmers pay little attention to it. This is easy to understand--most of us rarely need to use a non-integer type. In addition to scientific calculations and occasional timing tests or benchmark procedures, it is almost impossible to use in other cases. In the same way, most developers can easily ignore the arbitrary-precision decimals provided by Java.math.BigDecimal-most applications do not use them. However, in an integer-dominated program, it is sometimes surprisingly necessary to represent data that is not integral. For example, JDBC uses BigDecimal as the preferred interchange format for SQL DECIMAL columns.

IEEE floating-point

The Java language supports two basic floating-point types: float and double, and the wrapper class float and double corresponding to them. They are based on the IEEE 754 standard, which defines a binary standard for a 32-bit floating-point and a 64-bit double-precision floating-point binary decimal.

IEEE 754 uses scientific notation to represent floating-point numbers with a decimal number of 2. The IEEE floating-point number is a 1-digit symbol that represents the exponent in 8 digits, and the mantissa in 23 digits, that is, the decimal part. An exponent that is a signed integer can have positive and negative points. The decimal part is represented by a binary (base 2) decimal, which means that the highest digit corresponds to a value? (2-1), the second digit corresponds to? (2-2), and so forth. For double-precision floating-point numbers, the 11-bit exponent is used, and 52 digits represent the mantissa. The IEEE floating-point values are formatted as shown in Figure 1.

Figure 1. IEEE 754 format for floating-point numbers

Because there are many ways to represent a given number in a scientific notation, you normalize the floating-point numbers so that you can use a decimal number with a base of 2 and a decimal point to the left of the dot to indicate that you need to adjust the exponent to get the numbers you want. So, for example, the number 1.25 can be expressed as the mantissa is 1.01, the exponent is 0: (-1) 0*1.01 2*2 0

The number 10.0 can be expressed as the mantissa is 1.01, the exponent is 3: (-1) 0*1.01 2*2 3

Special numbers

In addition to the standard range of values allowed for encoding (for float, from 1.4e-45 to 3.4028235e+38), there are special values that represent infinity, negative infinity, and-0 and NaN (which represents "not a number"). These values exist to represent the resulting results in the case of an error condition (such as an arithmetic overflow, a square root for negative numbers, divided by 0, and so on) that can be represented by a number in a floating-point value collection.

These special numbers have some unusual features. For example, 0 and 0 are different values, but are considered equal when comparing whether they are equal. Using a zero to remove the number of infinity, the result is equal to 0. The special number nan is unordered; The result is false when comparing Nan to other floating-point values using the = =, < and > Operators. If f is NaN, even (f = = f) will get false. If you want to compare floating-point values with NaN, use the Float.isnan () method. Table 1 shows some of the properties of Infinity and NaN.

Table 1. Properties for special floating-point values

expression result
math.sqrt ( -1.0) -> td>
0.0/0.0 -> /td>
1.0/0.0 ->
-1.0/0.0 -> negative Infinity
nan + 1.0 -> NaN
Infinity + 1.0 -> ; Infinity
Infinity + Infinity -> Infinitely large
-> false
-> false
-> td>
nan = = NaN -> Se
0.0 = = -0.01 -&G T True

Basic floating-point types and wrapper class floats have different comparison behaviors

To make matters worse, the rules for comparing NaN and-0 are different between the base float type and the wrapper class float. For float values, comparing the equality of two Nan values will get false, and using float.equals () to compare two Nan float objects will get true. The reason for this is that it is not possible to use the NaN Float object as a key in HashMap if this is not the case. Similarly, although 0 and 0 are considered equal when they are represented as floating-point values, the use of Float.compareto () to compare 0 and-0 as float objects shows-0 is less than 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.