<< Chapter < Page Chapter >> Page >

Different ways to represent the same value

Having reached this point, by using substitution, I can rewrite the original set of representations of the value 623.57185 in the ways shown in Figure 5 .

(It is very important to for you to understand that these are simply different ways to represent the same value.)

Figure 5 . Other ways to represent the same information.
.62357185*10^+3 6.2357185*10^+262.357185*10^+1 623.57185*10^+06235.7185*10^-1 62357.185*10^-2623571.85*10^-3 6235718.5*10^-462357185.*10^-5

A simple change in notation

Finally, by making a simplifying change in notation where I replace (*10^) by (E) I can rewrite the different representations of the value of 623.57185 in theways shown in Figure 6 .

Figure 6 . Still other ways to represent 623.57185.
.62357185E+3 6.2357185E+262.357185E+1 623.57185E+06235.7185E-1 62357.185E-2623571.85E-3 6235718.5E-462357185.E-5

Getting the true value

Floating point types represent values as a mantissa containing a decimal point along with an exponent value which tells how many places to shift the decimal point to the left or to the right in order to determine the true value.

Positive exponent values mean that the decimal point should be shifted to the right. Negative exponent values mean that the decimal point should be shifted to the left.

Maintaining fractional parts

One advantage of floating-point types is that they can be used to maintain fractional parts in data values, such as 6.3 pounds of hamburger.

Accommodating a very large range of values

Another advantage is that a very large range of values can be represented using a reasonably small amount of computer memory for storage of the values.

Another example

For example (assuming that I counted the number of digits correctly) the following very large value

62357185000000000000000000000000000000.0

can be represented as

6.2357185E+37

Similarly, again assuming that I counted the digits correctly, the following very small value

0.0000000000000000000000000000062357185

can be represented as

6.2357185E-30

When would you use floating-point?

If you happen to be working in an area where you

  • need to keep track of fractional parts (such as the amount of hamburger in a package) ,
  • have to work with extremely large numbers (distances between galaxies) , or
  • have to work with extremely small values (the size of atomic particles) ,

then you will need to use the floating-point types.

Don't use floating-point in financial transactions

You probably don't want to use floating-point in financial calculations, however, because there is a lot of rounding that takes place in floating-pointcalculations. In other words, floating point calculations provide answers that are very close to the truth but the answers are often not exact.

Two floating-point types

Java supports two different floating point types:

  • float
  • double

These two types differ primarily in terms of the range of values that they can support.

Range of values for floating point types

The table in Figure 7 shows the smallest and largest values that can be accommodated by each of the floating-point types. Values of either type can be either positive or negative.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask