The Artima Developer Community
Sponsored Link

Java Answers Forum
Float Compare

1 reply on 1 page. Most recent reply: Jul 8, 2002 7:24 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
ssanjay

Posts: 3
Nickname: pssanjay
Registered: Apr, 2002

Float Compare Posted: Jul 8, 2002 2:20 PM
Reply to this message Reply
Advertisement
Hi

Why the below code always evaluate to be true/0?
Can anybody tell me how to do it correctly

Float f= new Float (999999.99f);
Float f2 = new Float(1000000.00f);

System.out.println(f.compareTo(f2));
System.out.println(f.floatValue()== f2.floatValue());


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Float Compare Posted: Jul 8, 2002 7:24 PM
Reply to this message Reply
> Hi
>
> Why the below code always evaluate to be true/0?
> Can anybody tell me how to do it correctly
>
> Float f= new Float (999999.99f);
> Float f2 = new Float(1000000.00f);
>
> System.out.println(f.compareTo(f2));
> System.out.println(f.floatValue()==
> = f2.floatValue());

Java uses IEEE format to store float and double values in memor 0.99 cannot be represented in binary in finite number of bits , so Java uses the closest value instead of exact one. And, in your case 999999.99f is closest to 1000000.00f. Therefore, both values are stored as 1000000.00f inside.

Thanks
Kishori

Flat View: This topic has 1 reply on 1 page
Topic: jbuilder and mysql [easy question for data search] Previous Topic   Next Topic Topic: web projects using java swings

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use