• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Lost of precision...

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Code tags are nice and make your code much more readable.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to suggest that you start a new thread for a brand new question.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code tags are nice and make your code much more readable.

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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Greg Roberts:
I tend to add comments once everything is working correctly.

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same way you restrict them when you print just about anywhere else. I suggest you ask this question in its own thread.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic