Discussion thread for Definite integrals.
I’m having difficulty with this problem; it passes all of the test cases except for sine.
I tried with a one-liner and then with a more conventional loop to sum up the areas, and both came out to the same answer.
As a sidenote, the provided arguments for the template in the program are confusing. They’re portrayed as
def area_of_rectangles(f, dx):
which might make the reader think that
f should be a function. It should instead be something like
def area_of_rectangles(rects, dx):
Alternatively, you can make it a problem about higher-order functions, which are cute.
I’m having the same problem with the sine case. I tried the usual way, but that didn’t work, so I tried some stable summation methods, like Kahan and Neumaier, but those didn’t work. So I tried math.fsum, but that also didn’t work. I looked into other methods of summation (that claimed to have been proven to be exact), but they also haven’t worked.
Perhaps there’s a problem with the checker.
The sine function was being integrated over a cycle so the area comes out to zero, but we were evaluating answers by checking the relative error which isn’t going to work here since we’re comparing two numbers that are close to zero (or machine epsilon).
I fixed it so it evaluates your answer using the absolute error instead. Your code should work now!
And good point about the function signature, just changed it to
def area_of_rectangles(rects, dx) to be clearer, thank you!