• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Bill Brodgen's exam cram mock test, Q.no.28

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, the given code is from Bill Brodgen's exam cram mock test, Q.no.28

a. The program compiles and prints "Not equal".
b. The program compiles and prints "Equal".
c. The compiler objects to line 3.
d. The compiler objects to using == with primitives
Correct answer given is a. But when I compiled and ran the above code, "Equal" was printed.
However, if I change the type of a to double, then "Not equal" is printed.
So I think b should be the right answer.
Could someone please explain what is the answer?
Thank you,
Prasanna.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, as printed the values were badly chosen to illustrate the point about floating point representation. The errata for the book at:
http://www.lanw.com/books/errata/
includes a correction which changes the 3.0f to 3.0 in the if statement.
if((a*3.0 )
Bill

------------------
author of:
 
Prasanna Wamanacharya
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Bill.
Prasanna.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello bill. i am not able to get it, how it is possible.
would u please explain it.
Thanks
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The floating point format can not represent many numbers exactly - things like repeating fractions 1.33333333333333 etc. - with only 32 bits to represent the number, something gets lost.
This causes a lot of trouble for beginning programmers because they conduct some floating point math and expect the == comparison to result in true. You might see a calculation resulting in 4.9999999998 instead of 5.0 for example.
Bill
 
That is a really big piece of pie for such a tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic