• 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:

Operators

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain how line //2 //3 //4 evaluated.

o/p is false true false true
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, the precise result of an arithmetic operation on floating point numbers cannot be counted on to match the exact result you expect from regular mathematics. This is due to the limited precision of floating-point variables. There is no way that I know of to predict the outcome of lines 2, 3, or 4 without a compiler, other than to spend a long time computing the float binary notation and the double binary notation for the numbers involved, and learning and applying the algorithms for division, multiplication and addition of those bit patterns. Suffice it to say that you can only be certain of the following:

1 - 1.0f / 3.0f * 3.0f is approximately equal to 0
1 - 1.0f / 3.0f * 3.0d is approximately equal to 0
1 - 1.0d / 3.0d * 3.0d is approximately equal to 0

You saw from compiling the code that some of two of those are exactly equal to 0, but without compiling the code, you can not safely assume that any of them are exactly equal to 0 or unequal to 0. Real exam questions would not expect you to be able to determine which floating-point results are precisely equal and which are not, but it is good to know when the results can be precisely predicted and when they cannot.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shweta,

Where do you get these type of questions? Do you think you can expect thse type of question in the exam?
 
Shweta R Dhaneshwar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chitra,
I got this example in one of the example while I was solving a mock exam.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shweta R Dhaneshwar:
please explain how line //2 //3 //4 evaluated.

o/p is false true false true



didn't get the explanation ?

pls explain in simple way if possible...
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still waiting for answer/reply... :roll:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think the way this works is based on the following snippet.

[A] float a = (float) (1 / 3 * 3.0f); yields a 0.0 but
[B] float b = (float) (1 / 3.0f * 3.0f); yields a 1.0 .

ie, when the divisor is int ([A], 3), the result (here division) yields
an int result 1/3 => 0
whenever the divisor is a float ([B], 3.0f), the result
(here division)
yields a float result, ie, 1/3.0f => 0.33f.

Hence [A] and [B] can be approximately rewritten as
(consider also the operator precedence or evaluation order),
[A] float a = (float) (0* 3.0f) = > 0.0
[B] float b = (float) (0.33f * 3.0f) => 1.0

Hope this helps.
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic