• 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

abs??

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


answers given were bcefh...
i heard that NAN comes only when u try to take the sqareroot of negative number and why not short min value when there is int minvalue ..and i heard that there is no -0.0 only 0.0 so how come h...

if these sort of questions come in exam then
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of these type of mock examination questions is to make you read the API for the Math.abs method(s). I wrote the methods' signatures and return parameters on postcards and carried them around reading them on the bus, tram, train, everywhere and everytime I had a few minutes to spare. I've forgotten them now, but I certainly know where to find the answers. So look up every option in the Java API documentation.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NAN comes only when u try to take the sqareroot of negative number



Can also get NaN with 0.0/0.0
In writing code to test NaN I've found that an expression in which any part results in an NaN will resolve to NaN.

why not short min value when there is int minvalue...



The Short.MIN_VALUE will be implicitly cast to an int in the abs() call which results in return of positive 32768. Thus cannot be a return value of abs(). Key to this part is knowing the abs() signatures. K&B book stressed knowing them for the 1.4 test.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For floating point numbers, there is a difference between -0.0 and 0.0. For integer types, there is only 0.

As noted above, this question wants you to understand how to look things up in the API. The answers are all right there. For one thing, there is no version of abs() that returns a short, so it cannot return Short.MIN_VALUE.

The docs also explain all about the other answers as well.

Layne
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all of u ....
i knew it about the return types of abs..but looking at NaN got confused..
thanks ..
int abs(int abs)
float abs(float abs)
long abs(long abs)
double abs(double abs)

so with this there cant be any short or byte or char....right ..
System.out.println(Math.abs(-5/0.0));returns me only positive infinity ..
so there cant be negative infinity ...
System.out.println(Math.abs(-0.0/0.0)); returns me NaN..and there is no as such negative value for it...
System.out.println(Math.abs(-0.0)); returns positive ..
Integer.MIN_VALUE
Long.MIN_VALUE these must be negative right then how can abs return me negative??
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

so with this there cant be any short or byte or char....right ..



Right for no short or byte MIN_VALUE returned from abs(). But yes for char since char is unsigned 16 bits. Thus Character.MIN_VALUE is '\u0000' which casts to int 0 and abs(0) is 0.

Integer.MIN_VALUE
Long.MIN_VALUE these must be negative right then how can abs return me negative??



Has to do with binary math. Here is abs() code:

public static int abs(int a) {
return (a < 0) ? -a : a;
}


So when Integer/Long.MIN_VALUE used the return value is (-a) or the negation of the argument is returned. (from API). So the two's complement of the parameter is returned. Looking at the bit pattern:

Integer.MIN_VALUE = -2147483648, or in hex = 80000000
and the two's complement of 80000000 is the same number!

 
reply
    Bookmark Topic Watch Topic
  • New Topic