2

I have written this function in python:

import math

def radical(a, b, k): 
    return (1-((a**2-b**2)/a**2)*(math.sin(math.pi*(2*k-1)/180))**2)**.5

def f(a, b): 
 sigma = 0 
 for k in range(1,180/4):
    sigma = sigma + radical(a, b, k)
 return 8*a*math.sin(math.pi/180)*sigma

print f(25.,35.)

When I calculate this function in Wolphramapha and Maple I will get 189.797 but with python I will get 184.91089913 What is the problem in my program?

1
  • Have you tried using sympy? Commented Nov 30, 2012 at 17:33

2 Answers 2

6

You are off by one. The range method excludes the end point. Try adding one:

for k in range(1,180/4 + 1):

Result: 189.797208409

Sign up to request clarification or add additional context in comments.

Comments

2

Well, first of all, python assumes integer division, so there is some serious round off in this code. I believe there is an import in division for this. Also, Here is some information on python's floating point arithmetic issues/problems:

http://docs.python.org/2/tutorial/floatingpoint.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.