-2

I am writing a program in python and I want my solutions to return within 8 significant figures of the exact solution.

I have searched the forums here, however there doe not seem to be any direct answer. I want a simple solution to make sure that the answer the program is computing is within that limit. I know it should only take 2, maybe three lines of code, I'm just not sure how to make it happen.

For example:

    TOL = 1.0000001 to 0.99999999

The solution should be in this range.

  • 3
    ``abs(obtained_value - expected_value) / abs(expected_value) < 1e-8``? – fjarri Sep 17 '13 at 02:50
  • 1
    Please clarify. Do you know the solution beforehand (why do you need the program then)? If not, then what is the problem you are trying to solve? – sashkello Sep 17 '13 at 02:51
  • Unless you already know the right result, this is not something you can solve with a few lines of boilerplate code. You have to do actual numerical analysis to establish error bounds. – user2357112 Sep 17 '13 at 03:03

2 Answers2

1
epsilon = 0.00000001
if abs(solution_expected - solution) <= epsilon:
    print "OK"
sashkello
  • 17,306
  • 24
  • 81
  • 109
0
while x < 0.99999999 or x > 1.0000001:
    # stuff
Brendan Long
  • 53,280
  • 21
  • 146
  • 188