• 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

Math.round question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a question in the roundup that asks what you will get from this call:
math.round(-3.22)
The API for Math has two round methods
public static int round(float a) return the nearest int
public static long round(double a)return the nearest long

The answer is the int.
All float literals are doubles. There is no for F suffix to make the double cast down to a float.
What am I forgetting?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple test confirms that the expression returns a long. For example, when attempting to compile this code...

...the result is...

But speaking of "loss of precision," Java is case sensitive, so "math" is not the same as "Math." Are you certain this is what the roundup question is asking? Or is there more context to the question?

For example, is this the one...?

(#147) What is the result of the following code
System.out.println( Math.round(-3.22) );

-3.0
-4
-3
-4.0


[ September 04, 2007: Message edited by: marc weber ]
 
Zhia-zhia Xiu Bai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I meant Math
 
Zhia-zhia Xiu Bai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks I see it is with a println now
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But is there more context to the question?

Note that the roundup question I posted is not asking whether an int or a long will be returned. Both an int and a long will print the same, as -3.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Zhia-zhia Xiu Bai:
thanks I see it is with a println now


Cool.
 
Zhia-zhia Xiu Bai
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to confuse the issue. It does return a long. I thought it asked for more information than it did.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remember also that this topic is on the 1.4 exam, but it's NOT on the 5.0 exam
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why output is:


it is different?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Math is not on the 5.0 exam as far as I know.

Bu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic