Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error with JQPlus Question

 
Ken Pullin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was working on this question and this was the answer. When I ran it on my machine, I came up with "True".
The following code will print 'false'.
float f = 1.0F/3.0F;
System.out.println( (f * 3.0F) == 1.0F );
 
Pond Lee
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct. I tried this code. The output is also TRUE.
 
Ken Pullin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - I just wanted to make sure I wasn't looking at something wrong.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the point of it is it will not always print true on every machine. Since float values lose precision, when you do 1.0F/3.0F you may get .333333333334 and then when you mutiply that by 3.0f you may get 1.00000000001 which won't equal 1.0f.
Bill
 
Tejas Nakawala
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,
Yes u r right,
When i tried to run the code on my machine it was 'True'.
So there may me a error with JQPlus.
Tejas Nakawala
 
Mukti Bajaj
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I guess there is no error,
As the question is,"will the code below print, false"
So the answer has to be false....as you all know that, the code will print "true".
NO ERROR....it's the way you read the question
Mukti
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic