Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 5 - Loops - Programming Exercises - Page 163: 5.25

Answer

code

Work Step by Step

# 5.25 (Demonstrate cancellation errors) A cancellation error occurs when you are # manipulating a very large number with a very small number. The large number # may cancel out the smaller number. For example, the result of 100000000.0 + # 0.000000001 is equal to 100000000.0. To avoid cancellation errors and obtain # more accurate results, carefully select the order of computation. For example, in # computing the following series, you will obtain more accurate results by computing # from right to left rather than from left to right: # Write a program that compares the results of the summation of the preceding # series, computing both from left to right and from right to left with n 50000. N = 50000 print("From left to right, the sum is: ", end='') sum = 0 for i in range(1, N + 1): sum += 1 / i print(sum) print("From right to left, the sum is: ", end='') sum = 0 for i in range(N, 0, -1): sum += 1 / i print(sum)
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.