• 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 in Math functions

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

a) System.out.println(Math . min ( -0.0 , 0.0 ) == 0);
b) System.out.println(Math . max ( -0.0 , 0.0 ) == -0);
c) System.out.println(Math . round ( Long . MAX_VALUE + 1.5) == Long . MAX_VALUE);
d) System.out.println(Math . round ( Integer . MIN_VALUE - 6.5 ) == Integer . MIN_VALUE);
e) System.out.println(Math . round ( Integer . MIN_VALUE - 6.5f ) == Integer . MIN_VALUE);

All the above print "true" except option d)

Can anyone explain why?

Thanks in advance

Kayal
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In (d), the expression

Integer . MIN_VALUE - 6.5

is an int minus a double. Before the subtraction, MIN_VALUE is promoted to a double. The result of the subtraction is a double, whose value is a little less than Integer.MIN_VALUE. The comparison is between that double and and int; the int gets promoted to a double.


-- Phil
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even i faced same problem..

so anyone please explain all statements above
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) System.out.println(Math . min ( -0.0 , 0.0 ) == 0);

-0.0 == 0.0 is always true
Math.min(-0.0,0.0) == 0
RHS of the above operation is int ZERO and gets promoted
to 0.0 as Math.min returns a double.
i.e., it is treated as Math.min(-0.0,0.0) == 0.0
Math.min(-0.0,0.0) returns -0.0
But -0.0 == 0.0 is always true as explained above
and this always prints true.

b) System.out.println(Math . max ( -0.0 , 0.0 ) == -0);
same explanation as above

c) System.out.println(Math . round ( Long . MAX_VALUE + 1.5) == Long . MAX_VALUE);

round function returns either a long value or int values
based on the type of parameter passed to round function.
In the above statement, the argument gets promoted to
double value (because 1.5 is of type double) and the
sume of Long.MAX_VALUE & 1.5 returns a double value that
is greater than in value what a Long.MAX_VALUE can represent
and as per JLS - 5.1.3, this returns a largest representable
'long' value which Long. MAX_VALUE.

d) System.out.println(Math . round ( Integer . MIN_VALUE - 6.5 ) == Integer . MIN_VALUE);
As Philip explained above, in this statement we are doing a double
operation and the round function returns a 'long' value. But this
case the result of operation is within the range of 'long' values
and whose result is less than Integer.MIN_VALUE, priting a value of
'false'.

e) System.out.println(Math . round ( Integer . MIN_VALUE - 6.5f ) == Integer . MIN_VALUE);
Same as above except that here the operation results in a float value
resulting in returning a 'interger' value from round function. As the subtractions results in a value that is less than the Integer.MIN_VALUE, this round operation results in returning Integer.MIN_VALUE as per JLS 5.1.3.

(note : JLS - Java Language Specification, 2nd Edition).

Hope this clarifies things a bit more.
 
Kayalvizhi Umashankar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. That really helped a lot
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear -

this topic is covered on the 1.4 exam, but NOT on the 1.5 exam.

hth,

Bert
 
reply
    Bookmark Topic Watch Topic
  • New Topic