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

# Lost of precision...

Greg Roberts
Ranch Hand
Posts: 72
I have written a simple class that is passed 3 doubles and does some calculations with them. Problem is, I keep getting loss of precision errors, even though I am using the correct variable types (doubles).The error states "possible loss of precision, found: double, required: int" and the arrow for each error points to the "Math" in Math.pow, and Math.sqrt,

Why is it telling me it requires an int when all the math methods I'm calling require doubles? The only method that requires int is remainder(), and I converted the doubles to int for that method only.

Another weird thing is that I'm also getting loss of precision erros on the accessor methods. Why would it give me loss of precision on "return num1;"?

Any ideas?

Here's the code:
public class Calc
{

public Calc()
{
num1 = 0;
num2 = 0;
exp = 1;
}

public Calc(double newNum1, double newNum2, double newExp)
{
num1 = newNum1;
num2 = newNum2;
exp = newExp;
n1 = (int)newNum1; //only used in remainder()
n2 = (int)newNum2; //only used in remainder()
}

public int remainder()
{
return (n1 % n2);
}

public int sumSquare()
{
return (Math.pow(num1, 2.0) + Math.pow(num2, 2.0));
}

public int diffSquare()
{
return (Math.pow(num1, 2.0) - Math.pow(num2, 2.0));
}

public int sumPow()
{
return (Math.pow(num1, exp) + Math.pow(num2, exp));
}

public int diffPow()
{
return (Math.pow(num1, exp) - Math.pow(num2, exp));
}

public int sqrt()
{
return (Math.sqrt(num1) + Math.sqrt(num2));
}

public int getNum1()
{
return num1;
}

public int getNum2()
{
return num2;
}

public int getExp()
{
return exp;
}

private double num1;
private double num2;
private double exp;
private int n1;
private int n2;
}

Greg Roberts
Ranch Hand
Posts: 72
Man, I wish I could delete that post. With all the screwing around I did in changing the types from double to int and back again, I forgot to change the return types in the method declerations. Notice how all of them say "public int something()"? I just noticed that and changed them all to "public double something()" and it compiles.

I am having problems with the driver though. Its a simple driver program to test the class and I'm getting "cannot resolve symbol" errors.

Every one of the System.out.printl lines have the error, and the arrow points to the method call. So, in the line :

System.out.println("Remainder of the first number divided by the second number is " + newCalc.remainder);

The arrow points to "remainder" in "newCalc.remainder".
I'll probably figure this one out as soon as I hit Post, but just in case I don't...

import javax.swing.JOptionPane;

public class CalcTest
{
public static void main(String[] args)
{

String input = JOptionPane.showInputDialog("Please enter an integer");
double newNum1 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Please enter another integer");
double newNum2 = Double.parseDouble(input);

input = JOptionPane.showInputDialog("Please enter an exponent integer");
double newExp = Double.parseDouble(input);

Calc newCalc = new Calc(newNum1, newNum2, newExp);

System.out.println("Remainder of the first number divided by the second number is " + newCalc.remainder);
System.out.println("The sum of squares of the two numbers is " + newCalc.sumSquare);
System.out.println("The difference of squares of the two numbers is " + newCalc.diffSquare);
System.out.println("The sum of the two numbers by the power of " + newCalc.getExp + " is " + newCalc.sumPow);
System.out.println("The difference of the two numbers by the power of " + newCalc.getExp + " is " + newCalc.diffPow);
System.out.println("The sum of the roots of the two numbers is " + newCalc.sqrt);

System.exit(0);
}
}

Marilyn de Queiroz
Sheriff
Posts: 9064
12

Marilyn de Queiroz
Sheriff
Posts: 9064
12
I'd like to suggest that you start a new thread for a brand new question.

Marilyn de Queiroz
Sheriff
Posts: 9064
12
Originally posted by Greg Roberts:
Man, I wish I could delete that post.

You can, but I'd rather that you didn't. Seeing people's mistakes can be useful for other students.

I am having problems with the driver though. Its a simple driver program to test the class and I'm getting "cannot resolve symbol" errors.

Every one of the System.out.printl lines have the error, and the arrow points to the method call. So, in the line :

System.out.println("Remainder of the first number divided by the second number is " + newCalc.remainder);

The arrow points to "remainder" in "newCalc.remainder".

If it is a method call, shouldn't it have () after it?
i.e.
System.out.println("Remainder of the first number divided by the second number is " + newCalc.remainder());

Greg Roberts
Ranch Hand
Posts: 72

What are code tags? Are you referring to formatting, or comments? This is my first post in this forum and I'm still trying to figure out how it formats posts. When I made the posts they came out looking different when it actually posted. In my editor the formatting is nice and lined up like it should be. If you meant comments, I was still trying to make the program work, I tend to add comments once everything is working correctly.

I'd like to suggest that you start a new thread for a brand new question.

Sorry, I'll do that next time. I'm used to forum members wanting the pages with as little clutter as possible.

If it is a method call, shouldn't it have () after it?

You're right, it should. Problem solved. I have a tendency to miss the small things. You've been a great help. Thanks!

Marilyn de Queiroz
Sheriff
Posts: 9064
12
When you compose your post, just below the box you're typing in are a few buttons. If you click on the CODE button, some [ code ] [ /code ] tags will appear. When you put any code you put between those tags, the formatting of that code is maintained. (See the example output in my other response above where I added code tags to your code).

I'm happy I was able to assist you.
[ February 05, 2005: Message edited by: Marilyn de Queiroz ]

David Harkness
Ranch Hand
Posts: 1646
Originally posted by Greg Roberts:
I can't stress enough how highly I recommend that you break that habit now before it becomes too entrenched. Comments aren't always necessary, so I'm not advocating commenting your accessor methods. But I am recommending that you practice learning where comments are needed and where they are not. After you write the code usually means no comments.

I speak from experience. I had to break myself of the habit, and it wasn't easy. I still catch myself thinking, "I know what this does. Why should I comment it?" When I come back six months later to fix a bug, I realize why I should have commented it.

Greg Roberts
Ranch Hand
Posts: 72
Originally posted by David Harkness:
I can't stress enough how highly I recommend that you break that habit now before it becomes too entrenched.

Thanks for the advice. I'm just a student who has only taken VB, C++, and currently Java. I'm early enough in my experience to change now and start commenting as I code.

Anjanesh Lekshminarayanan
Ranch Hand
Posts: 46
BTW, when printing in CUI, how do we retrict the number of digits ?
If the result is 23.5698, how do I format it as 0023.56 ?
Thanks

Marilyn de Queiroz
Sheriff
Posts: 9064
12
The same way you restrict them when you print just about anywhere else. I suggest you ask this question in its own thread.

Ilja Preuss
author
Sheriff
Posts: 14112
By the way, more tips on posting questions on JavaRanch (such as using code tags) can be found at http://faq.javaranch.com/view?HowToAskQuestionsTheSmartWay

Have fun!