• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will print 'false'.

float f = 1.0F/3.0F;
System.out.println( (f * 3.0F) == 1.0F );

Answer : A True
Answer : B False

Your comments please... with explanations.


M sorry... I must admit I have not read the faq / forum rules... So lemme know if m violating any.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi AJAY,
Execute this code it will print definitely true.
why because everywhere we are using the float variable...............!

Thanks and Regards
Manjunath
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You topic title: "Doubt" seems to imply you have a question? What exactly is your question? On my system with Java 5.0 the program prints "true".
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though it is printing true but I am surprised with the answer.In my opinion it should be false because dividing 1.0f/3.0f will give a resulting float with somevalue like .33333...
Again multiplying this number by 3 will not print 1..(will be slightly less than one).
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjeev,

First I apologize for the explaination.....answer is correct but explaination is not satisfiable...I'm apologize all for that.

Mathematically you are right........Sanjeev,
but if you print(f*3.0F) its printing 1.0 why because
as of Java 5 our JVM is considering 0.99999999 as 1.0
so when we devide 1.0 by 3.0 it will be 0.33333333.....
when again we multiply this factor by 3.0 then the answer is 0.99999999...
Mathematically we can't prdict the no.of 9's but our JVM is having limitation of memory. Thats why its considering it 1.0 and printing true.......!

This is how I think about it...any other views about it..
always welcome....!

Thanks and Regards
Manjunath
 
AJAY JOSE PAUL
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You topic title: "Doubt" seems to imply you have a question? What exactly is your question? On my system with Java 5.0 the program prints "true".



@Barry Gaunt ::: I compiled and ran the proggy... it prints true.

According to me the output should b false.

I wanted to know the reason for printing true as the output.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Posted by Manjunath
as of Java 5 our JVM is considering 0.99999999 as 1.0



Compiling and running code with jdk 1.4 is also giving the answer as true.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic