Results 1 to 3 of 3
- 08-18-2009, 07:03 PM #1
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.
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.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.divide(z); x.isQuoted = false; double numerator = ((a / b) + (c / d)); double denominator = ((e / f) + (g / h)); double answer = (numerator / denominator); //output output.print("A = "); output.print(x.toString()); output.print(" = "); output.print(x.toProperString()); output.print(" = "); output.println(answer);
Does anyone have any suggestions? :)Last edited by Fubarable; 08-18-2009 at 09:51 PM. Reason: Code tags added for readability
-
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));
- 08-18-2009, 11:02 PM #3
Similar Threads
-
Output does not dispay as intended -- HELP!!
By janalyzer in forum New To JavaReplies: 5Last Post: 01-24-2012, 05:14 AM -
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
Help with a long sorting program.
By leiferouis in forum New To JavaReplies: 12Last Post: 02-04-2009, 04:46 AM -
Long Cannot Be Dereferenced?
By caps_lock in forum New To JavaReplies: 1Last Post: 01-18-2009, 01:49 AM -
Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01-12-2009, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks