Note that there are some explanatory texts on larger screens.

plurals
  1. PODecimalFormat the answer?
    primarykey
    data
    text
    <p>Please forgive my ignorance, I'm a beginner.</p> <p>Can anyone tell me why I am getting my error code in the if/else statement below? I think my result for the ln multiplication is is not exactly the same as the standard multiplication (there is a .00000000006 of a difference or something like that). Is there a work around for this? I have tried to use DecimalFormat but to no avail. </p> <p>If I add:</p> <pre><code>DecimalFormat fmt = new DecimalFormat ("0.###"); </code></pre> <p>to the tester and </p> <pre><code> if (fmt.format(z) != fmt.format(result)){ </code></pre> <p>to the if statement I receive my own same error statement. What is wrong!?</p> <p>Thank you very much.</p> <p>Joe</p> <pre><code>import java.util.Scanner; import java.lang.Math; import java.text.DecimalFormat; public class Logs { //integer x field &amp; encapsulation private static double x; // x encapsulation public double getX(){ return x; } public void setX (double ex){ if (ex &gt;= 0){ x = ex; } else{ System.out.println("You may not choose a negative number, 'x' = 0"); } } //integer y field private static double y; // y encapsulation public double getY(){ return y; } public void setY (double why){ if (why &gt;= 0){ y = why; } else{ System.out.println("You may not choose a negative number, 'y' = 0"); } } //tester public static void main (String [] args){ Logs sample =new Logs(); Scanner var = new Scanner(System.in); DecimalFormat fmt = new DecimalFormat ("0.###"); sample.setX (var.nextDouble()); sample.setY (var.nextDouble()); x = sample.getX(); y = sample.getY(); double z = (x*y); double logX = Math.log(y); double logY = Math.log(x); double logZ = (logX +logY); double result = (Math.exp(logZ)); if (z != result){ System.out.printf("%s", "Incorrect answer: be a better coder"); } else { System.out.printf("%s %.3d %s %.3d %s %.3d", "The product of the values you chose is: ", + z, "\nln(x) + ln(y) is: ", + logZ, "\nCompute to e: ", + result); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload