-2

I am getting wrong output while using java Math functions. expected output = 4.32 present output= 4.30287105016515

double startHeight= 60.00;
double initaldistance=1000.00;
double speed=120;
startDistance=(initaldistance-1.688*speed);
startAngle = Math.toDegrees(Math.atan(startHeight/startDistance));
System.out.println(startAngle);
  • 2
    The result using double shouldn't be that far off, that many significant digits. Have you tested the equations in other ways -- for example using a spreadsheet or a calculator, since your assumptions could be erroneous? – Hovercraft Full Of Eels Sep 06 '19 at 12:08
  • 4
    @AndyTurner: Please do not promiscuously close questions as duplicates of [that question](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). The problem in this case is not due to inaccuracy in floating-point arithmetic but due to math errors or other error in expectation by OP. You should not just glance at a question, see it involves floating-point and an unexpected answer, and jump to the conclusion it is due to floating-point behavior. – Eric Postpischil Sep 06 '19 at 12:24
  • You expect this output why? – user207421 Sep 06 '19 at 12:29
  • @EricPostpischil yeah, I jumped the gun a bit there. – Andy Turner Sep 06 '19 at 12:30
  • @EricPostpischil: you're correct -- the question should instead be closed for "a problem that can no longer be reproduced" reason – Hovercraft Full Of Eels Sep 06 '19 at 12:30
  • @HovercraftFullOfEels: The problem can reproduced. I prepared a file with the code shown in the question (albeit translated to C due to my personal preference) and got the reported output, which differs from the stated expected output. I do not know what you think testing would have accomplished. The report of the observed output is correct: the output is as stated. Testing would not show otherwise. There is a problem here; it just lies in the expectation. Have incorrect expectations is an actual software engineering problem that occurs sometimes, and learning how to deal with it is useful. – Eric Postpischil Sep 06 '19 at 12:36
  • Aside: use `Math.atan2(y, x)` in preference to `Math.atan(y/x)`. – Andy Turner Sep 06 '19 at 12:37
  • 2
    @EricPostpischil: the *assumptions* are the problem, and they cannot be reproduced. What is more, I doubt that this question will help any future visitors to this site. – Hovercraft Full Of Eels Sep 06 '19 at 12:37
  • @HovercraftFullOfEels: There are things here that can help future visitors. Did the OP do some calculations by hand and drop digits, as is sometimes taught for approximations? People can learn that is insufficient for precise results. Or consider this: How can we determine whether the expectation or the observed result is wrong? Most people would turn to a computer or calculator for a result, but typical software or calculators is going to use limited precision arithmetic similar to Java’s `double`, so that is not an independent test. That is an opportunity to learn about potential solutions… – Eric Postpischil Sep 06 '19 at 12:47
  • 1
    … such as using arbitrary precision software, trying some interval arithmetic, or analyzing the formulas mathematically to establish error bounds. Since all the functions used are monotonic in the regions where they are used, interval arithmetic could be a simple way to prove the expectation is false without resorting to deeper analysis. I might like to write that up, but I am busy at the moment dealing with non-Stack-Overflow things. Somebody else could contribute it. – Eric Postpischil Sep 06 '19 at 12:48
  • If nothing else, the questions in the comments are the sort of things an expert programmer would try to answer, given is mismatch between expected and actual answer. – Patricia Shanahan Sep 06 '19 at 15:52

1 Answers1

3

Your expected output is incorrect. The arithmetic expressed in the code in the question does indeed have a result near 4.30287.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312