Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Output is off by very small decimal place

  1. #1
    jester-race is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default Output is off by very small decimal place

    Hello again.

    I'm having another problem with the MyProgrammingLab exercises.

    The task is to write a class that accepts a value (the instructions don't specify what type, so I assume that they want a double) and assigns it to an instance variable, diameter. That value is then used to calculate the volume and surface area of a sphere.

    The following methods are to be included in the class:
    calcVolume //calculates and returns the volume
    calcSurfaceArea // " " the surface area
    toString // outputs a string with the following format ("sphere with diameter " + diameter), displaying diameter to three decimal places

    After I submit my code, it tells me that the output value is wrong based on the computer-generated input. MyProgrammingLab tries two different values for the diameter: Once using the constructor and once using the setDiameter method. Comparing the output of my code with the output of the "correct" code shows me that everything is correct until after the setDiameter method is used. getDiameter returns the correct value, but when that value is used in the calcVolume method, the return value is slightly off:

    My calcVolume method's return value: 381.7035074111598
    The "correct" calcVolume return value: 381.7035074111599

    What on earth could cause the value to be off by such a small margin? I suspected that it might have been because I used int values in my call to the Math.pow method (example: Math.pow(diameter/2, 2) instead of Math.pow(diameter/2.0, 2.0) but this didn't make any difference.

    Here's the code for my class:

    Java Code:
    import java.text.DecimalFormat;
    public class Sphere
    	private double diameter;
    	public Sphere(double d)
    	{diameter = d;}
    	public void setDiameter(double d)	//I've tried using a different param. name from what the constructor uses, but it didn't change anything
    	{diameter = d;}
    	public double getDiameter()
    	{return diameter;}
    	public double calcVolume()
    		double volume = (4.0/3.0) * Math.PI * Math.pow(diameter/2.0, 3.0);
    		return volume;
    	public double calcSurfaceArea()
    		double surfaceArea = 4.0 * Math.PI * Math.pow(diameter/2.0, 2.0);
    		return surfaceArea;
    	public String toString()
    		DecimalFormat fmt = new DecimalFormat("0.###");	//putting this outside of the method generated an error
    		return ("sphere with diameter " + fmt.format(diameter));
    Any solutions, advice or hints that you can give will be greatly appreciated. Thank you. :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Output is off by very small decimal place

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    For arbitrary precision, you need to use BigDecimal.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Output is off by very small decimal place

    You could try avoiding Math.pow() and multiply things directly:

    Java Code:
    double radius = diameter / 2;  // diameter is a double
    return (4.0/3) * Math.PI * radius * radius * radius;
    After I submit my code, it tells me that the output value is wrong
    This would annoy me intensely unless the assignment had specified the accuracy to which the volume and area were to be calculated. If the accuracy was not stated as part of what was being calculated I think I would quickly decide that I was right and the answers were wrong. If getting the machine's idea of the correct last decimal place really matters to you all you can do is try and second guess what the machine is doing eg radius*radius*radius or something similar.
    Tolls likes this.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Output is off by very small decimal place

    It does look like the test framework you're using doesn't understand the link Darryl posted, unless you have had BigDecimal explained to you of course.

Similar Threads

  1. Concerning a small output problem with PRINTF
    By Kaizer in forum New To Java
    Replies: 2
    Last Post: 11-04-2011, 06:27 PM
  2. Replies: 7
    Last Post: 09-14-2011, 03:06 PM
  3. Output a Two Decimal Value
    By Hillsid3 in forum New To Java
    Replies: 7
    Last Post: 07-17-2011, 12:24 PM
  4. Decimal to binary, octal to decimal
    By matejm1994 in forum New To Java
    Replies: 3
    Last Post: 12-26-2010, 09:59 AM
  5. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts