Results 1 to 3 of 3
  1. #1
    sdeverteuil's Avatar
    sdeverteuil is offline Member
    Join Date
    Aug 2009
    Rep Power

    Default long to double trouble (Rhyme scheme not intended)

    Hello. As of lately I have been messing around with the Fraction class in type.lib, trying to get myself acquainted with it. Anyhow, for practice, I've been writing a simple program that computes 4 fractions in the equation

    A=(x + y)/(z +t) where x, y, z, and t are the 4 fractions.

    The constructor takes long values of course. However, I intend for my program to print the preceding equation as an improper fraction, a proper fraction, and then an approximate real value using double precision. Here is an outline of my program.
    Java Code:
    	   //Print instructions.
    	   output.println("Enter each fraction, enter its            numerator/denominator,");
    	   output.println("pressing enter after each.");
    	   //Prompt and input. 
    	   output.println("Enter x.");
    	   long a = input.nextLong();
    	   long b = input.nextLong();
    	   output.println("Enter y.");
    	   long c = input.nextLong();
    	   long d = input.nextLong();
    	   output.println("Enter z.");
    	   long e = input.nextLong();
    	   long f = input.nextLong();
    	   output.println("Enter t.");
    	   long g = input.nextLong();
    	   long h = input.nextLong();
    	   //Assign and compute.
    	   Fraction x = new Fraction(a, b);
    	   x.add(new Fraction(c, d));
    	   Fraction z = new Fraction(e, f);
    	   z.add(new Fraction(g, h));
    	   x.isQuoted = false;
    	   double numerator = ((a / b) + (c / d));
    	   double denominator = ((e / f) + (g / h));
    	   double answer = (numerator / denominator);
    	   output.print("A = ");
    	   output.print(" = ");
    	   output.print(" = ");
    When printing out the approximate real value, the output sometimes comes out as NaN which I assume stands for not a number. This is because the two fractions that I used for the overall denominator of the equation get automatically rounded down to zero I expect.

    Does anyone have any suggestions? :)
    Last edited by Fubarable; 08-18-2009 at 09:51 PM. Reason: Code tags added for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    You're running into "int" or here long division problems. Please understand that an int/int will return an int not a double, and likewise a long/long will return a long. The compiler will throw out anything past the decimal point rounding the answer down if positive and up if negative. You will want to cast one of the numbers as double first before dividing. e.g.,:

    Java Code:
    double numerator = (((double)a / b) + ((double)c / d));

  3. #3
    sdeverteuil's Avatar
    sdeverteuil is offline Member
    Join Date
    Aug 2009
    Rep Power


    Thank you very much! That fixed the problem. I'll remember that.


Similar Threads

  1. Output does not dispay as intended -- HELP!!
    By janalyzer in forum New To Java
    Replies: 5
    Last Post: 01-24-2012, 06:14 AM
  2. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  3. Help with a long sorting program.
    By leiferouis in forum New To Java
    Replies: 12
    Last Post: 02-04-2009, 05:46 AM
  4. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 02:49 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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