aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about Math class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about Math class" Watch "question about Math class" New topic
Author

question about Math class

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Which of the following statements are true in terms of the java.lang.Math.abs method?
a. Four overloaded versions of abs exist.
b. An ArithmeticException is declared in the throws clause.
c. The type of the return value depends on the type of the argument.
d. The returned value is always of a floating-point primitive type.
e. If the argument is greater than or equal to zero then the returned value is equal to the argument.
f. If the argument, arg, is less than zero then the returned value is -arg.
g. None of the Above
My answer: a,c,e
Corrects : a, c, e,f
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Think about it:
If the argument is less than zero then the returned value is -arg...
Let's say that the arg is -15 (a number less than zero). Then the returned value will be +15. But +15 is -(-15). If abs returned arg then it would return -15 which isn't what abs does. It only returns positive numbers.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Math.abs(Integer.MIN_VALUE) == -Integer.MIN_VALUE
-Integer.MIN_VALUE == Integer.MIN_VALUE
Integer.MIN_VALUE < 0
[ June 13, 2003: Message edited by: Marlene Miller ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
In general, option (f) is true ... ,try the code

output :
Integer.MIN_VALUE = -2147483648
Math.abs(Integer.MIN_VALUE) = -2147483648
I believe Dan's mock exam has something similar.
[ June 13, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.
[ June 13, 2003: Message edited by: Marlene Miller ]
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
In math, the absolute value of a real number is always >= 0.
In Java, there is one int and one long whose absolute value < 0.


What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??
TIA
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Andres Gonzalez:
why option f? I'd say:
f. If the argument, arg, is less than zero then the returned value is arg.

I think the Math.abs is doing exactly of what is expected of it - with a few exception.
From Math.abs API doc:

If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

I think the confussion stems from the rewording of the phrase "negation of the argument" to "-arg". (f) could be interpreted as abs returning a negative value if you pass it a negative value.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
What could be the rational behind this just providing one int/long whose abs value is < 0...any thoughts??

I don�t know. What else could Math.abs(Integer.MIN_VALUE) be? an ArithmeticException. Interger.MAX_VALUE + 1 < 0. The addition does not cause an exception. We live with it.
The Java Programming Language (where I get my insights into language design) says "the absolute value of the most negative value of an int or long its itself and therefore negative; that's how two's complement integers work.
Here is the source code.
public static int abs(int a) { return (a < 0) ? -a : a; }
[ June 14, 2003: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about Math class
 
Similar Threads
Dan's - Math.abs Question..
Dan's Mock- Math.abs()
Dan Chisholm's Question on java.lang.Math.abs
Maybe a error in Mr Dan's Mock
Seed Value