wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abs?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abs??" Watch "abs??" New topic
Author

abs??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252


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


Thanks & Regards<br /> <br />-Srikanth
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ray Horn
Ranch Hand

Joined: Oct 20, 2005
Posts: 39
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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

Joined: Oct 20, 2005
Posts: 39
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!

 
 
subject: abs??
 
Similar Threads
Division by zero floating point question
Dan Chisholm's Question on java.lang.Math.abs
Hasanov Study list question
K&B Master Exam SCJP6 class mineral question: why is the answer 'null null'
MATH Problem - Shor.MIN_VALUE