Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Bug in java.lang.FLOAT?

Posted on 2003-03-12
10
Medium Priority
?
204 Views
Last Modified: 2010-03-31
hi, I have the follwoign problem:
Double.parseDouble("398381.91") return me 398381.91 which is ok
new Float(398381.91) returns me 398381.9 !!!!! the last 1 is missing
WHY???
0
Comment
Question by:mruff
10 Comments
 
LVL 9

Accepted Solution

by:
yongsing earned 200 total points
ID: 8118696
A Float is a wrapper for a float value, which is 32 bits. A Double is a wrapper for a double value, which is 64 bits. Since a float is less precise than a double, it is not surprising that you are getting a less precise result.

If precision is very important to you, consider using the BigDecimal class.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8125496
You should use the methods floatValue () and doubleValue () to print the values wrapped by objects of Float, Double, etc.

Float obj = new Float ( 398381.91 ) ;
System.out.println ( obj.doubleValue () ) ;

Floating-point values are single-precision type, that's why you get that result.

Mayank.
0
 

Expert Comment

by:CleanupPing
ID: 9058750
mruff:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:yongsing
ID: 9069480
>> You should use the methods floatValue () and doubleValue () to print the values wrapped by objects of Float, Double, etc.

>> Float obj = new Float ( 398381.91 ) ;
>> System.out.println ( obj.doubleValue () ) ;

This comment does not explain why the questioner is having the problem.


>>Floating-point values are single-precision type, that's why you get that result.

This comment is a repeat of what I had said.


Therefore, I should get the points since my comment is correct.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9069650
Are we fighting for points here? If so, then please go ahead and give all points to yongsing

Anyways, yongsing, I have a question for you. Try this in C (Turbo C preferably):

float a = 0.6 ;

if ( a < 0.6 )
  printf ( "Lesser " ) ;
else
  printf ( "Not lesser. " ) ;

a = 0.7 ;

if ( a < 0.7 )
  printf ( "Lesser " ) ;
else
  printf ( "Not lesser. " ) ;


Mayank.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9069902
What's the point? My first comment recommended using the BigDecimal class for true precision, and this will fix the question's problem.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9070261
No no.... just for the sake of experimentation and fun - try it once.... its not got to do anything with this question.... just a problem in C.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 9070330
I know that your code will give some unexpected result, but I don't have a C compiler now, and it's been a while since I've done any C programming. Sorry to disappoint you. :-(
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9072230
Yes, it will. Actually, the problem happens because of the 0.6 and 0.7 constants in the if () condition. In the if condition, the 'a' is a float, whereas the 0.6 and 0.7 values on the right-hand-side of the inequality are treated as doubles by default. Turns out, there is a temporary type-conversion, which results in loss of precision - in Turbo C, after values starting from 0.645299 - this value differs for gcc and other compilers. Turns out, the values smaller than this one are converted with full precision and the values greater than this are not but there is loss in precision while converting bigger values. So, ( a < 0.6 ) still gives false as expected because here, the precision is maintained, but ( a < 0.7 ) gives true because here, the precision is lost. So the output you get is:

Not Lesser.
Lesser.

Though you were expecting:

Not Lesser.
Not Lesser.

So how do we go about fixing it? To avoid the type-conversion, declare 'a' as double - in that case, there is no temporary type-conversion and so no loss in precision. In that case, the output you get is:

Not Lesser.
Not Lesser.

Another way to do it is by using the constants 0.6f and 0.7f instead of 0.6 and 0.7 respectively, in the RHS of the if () condition's inequality.

if ( a < 0.6f )

and

if ( a < 0.7f )

Then the output will again be:

Not Lesser.
Not Lesser.

Its always better to use 'double', in fact, because you never know when such constants come into expressions and there is some kind of loss of precision which drives you mad and you'll never be able to figure out what went wrong!

But of course, this code in Java will give a compilation error saying: 'Possible loss of precision'. Man, that's why I love that language! It makes sure that you don't make many mistakes which go unnoticed in C/ C++.

Cheers,
Mayank.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9714372
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to yongsing

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question