srikanth reddy

Ranch Hand

Posts: 252

posted 11 years ago

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

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

Posts: 7729

posted 11 years ago

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

Posts: 39

posted 11 years ago

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.

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.

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

Posts: 3061

posted 11 years ago

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

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

posted 11 years ago

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??

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??

Thanks & Regards<br /> <br />-Srikanth

Ray Horn

Ranch Hand

Posts: 39

posted 11 years ago

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.

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

So when Integer/Long.MIN_VALUE used the return value is (-a) or

Integer.MIN_VALUE = -2147483648, or in hex = 80000000

and the two's complement of 80000000 is the same number!

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!

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |