Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Trouble with rint()

Tina Parks
Ranch Hand
Posts: 35
I am trying to implement the rint() method, but I am baffled. Here is a sample of my (simplified) code:
public void drawVector(Graphics g){
double theta, xarg;
int x;

xarg = Math.cos(theta);
x = Math.rint(xarg);
and so on.
When I compile, it complains about the argument of rint():
_____________________
found double:
required int:
x = Math.rint(xarg);
^
1 error
_____________________
This makes no sense. According to Java, rint() is supposed to take a double argument, but the compiler is saying that xarg must be an integer. Why on Earth would rint() take an integer argument when its sole purpose is to convert a double into an integer?
So what mistake am I making?
BTW, which forum category should this question belong? I couldn't find one specifically for general programming errors.

Ashish Hareet
Ranch Hand
Posts: 375
try changing
int x;
to
double x;//rint returns a double
or try this
x = (int)Math.rint(xarg);
hope that helps

Tina Parks
Ranch Hand
Posts: 35
rint() returns a DOUBLE? I don't understand its purpose. I thought the whole idea behind rint() was to return an INT.
BTW, the error message I typed in is off a little in the formatting. The carat symbol "^" was supposed to be placed at the start of the rint argument. I think the problem is clearly with the argument of rint(), not the return type.
If I have a number like 4.6654, there has to be a method for turning that into the integer 5, no?

Ashish Hareet
Ranch Hand
Posts: 375
Here's what the API sez 'bout this
rint
public static double rint(double a)
returns the closest integer to the argument.
Parameters:
a - a double value.
Returns:
the closest double value to a that is equal to a mathematical integer. If two double values that are mathematical integers are equally close to the value of the argument, the result is the integer value that is even.
See , it returns a value that is a matematical integer & not a Java integer type .
In a rush . later .

Ashish Hareet
Ranch Hand
Posts: 375
The specific error i get when i compile u'r exact code is
---------- javac ----------
Vec.java:9: Incompatible type for =. Explicit cast needed to convert double to int.
x = Math.rint(xarg);
^
1 error
Normal Termination
Output completed (2 sec consumed).
The carat is supposed to be under the " = " .

Try this -
x = (new Double(Math.rint(xarg))).intValue();
--Ashish
[This message has been edited by Ashish Hareet (edited July 21, 2001).]

Tina Parks
Ranch Hand
Posts: 35
Okay, I stand corrected. Although, to return a mathematical integer as a double seems to be a dumb thing to do since the round() method is already available.
The code I had previously is similar to what you provided. I was trying to find a more elegant way of doing the same thing. But it looks like Java isn't going to let me.
As for the error message, I am using a compiler on a Win98 machine. The error message it gives me seems to be badly worded. The one your compiler provides is much better.
Thanks for the help.