<< Chapter < Page Chapter >> Page >

The first thing to note is the behavior of the program produced by the following code fragment.

int x = 2147483647; square(x);

The above fragment assigns a large integer value (2147483647)to the int variable and passes that variable to the square method. This fragment produces the following output on the screen:

float 2.14748365E9

As you can see, the system selected the overloaded method that requires an incoming parameter of type float for execution in this case (rather than the version that requires type double ).

Conversion from int to float loses accuracy

Correspondingly, it converted the incoming int value to type float , losing one decimal digit of accuracy in the process. (The original int value contained ten digits of accuracy. This was approximated by a nine-digit float value with an exponent value of 9.)

This seems like an unfortunate choice of overloaded method. Selecting the other version that requires a double parameter as input would not have resulted in any loss of accuracy.

A more dramatic case

Now, consider an even more dramatic case, as illustrated in the following fragment where a very large long integer value(9223372036854775807) is passed to the square method.

long y = 9223372036854775807L; square(y);

The above code fragment produced the following output:

float 9.223372E18

A very serious loss of accuracy

Again, unfortunately, the system selected the version of the square method that requires a float parameter for execution. This caused the long integer to be converted to a float . As a result, the long value containing 19 digits of accuracy was converted to an estimate consisting of only seven digits plus an exponent. (Even if the overloaded square method requiring a double parameter had been selected, the conversion process would have lost about threedigits of accuracy, but that would have been much better than losing twelve digits of accuracy.)

The moral to the story is ...

Don't assume that just because the system knows how to automatically convert your integer data to floating data, it will protect the integrity of your data.Oftentimes it won't.

To be really safe ...

To be really safe, whenever you need to convert either int or long types to floating format, you should write your code in such a way as to ensure that it will be converted to type double instead of type float .

For example, the following modification would solve the problem for the int data and would greatly reduce the magnitude of the problem for the long data. Note the use of the (double) cast to force the double version of the square method to be selected for execution.

int x = 2147483647; square((double)x);long y = 9223372036854775807L; square((double)y);

The above modification would cause the program to produce the following output:

double 2.147483647E9 double 9.223372036854776E18double 4.2

This output shows no loss of accuracy for the int value, and the loss of three digits of accuracy for the long value.

(Because a long and a double both store their data in 64 bits, it is not possible to convert a very large long value to a double value without some loss in accuracy, but even that is much better thanconverting a 64-bit long value to a 32-bit float value.)

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