138 Chapter 6 • Additional Topics §6.2 programming language 3 (chosen here for simplicity) the decimal module can be used to set any level of precision. 4 Below we show how to get accuracy up to 50 decimal places using Heron’s formula for the triangle in Example 2.16 from Section 2.4, by using the python interactive command shell: : from decimal import * : getcontext().prec = 50 : a = Decimal("1000000") : b = Decimal("999999.9999979") : c = Decimal("0.0000029") : s = (a+b+c)/2 : K = s * (s-a) * (s-b) * (s-c) : print Decimal(K).sqrt() 0.99999999999894999999999894874999999889618749999829 (Note: The bracketed line numbers are just command prompts, not part of the code.) Notice in this case that we do get the correct answer; the high level of precision eliminates the rounding errors shown by many calculators when using Heron’s formula. Another software option is Sage 5 , a powerful and free open-source mathematics package based on Python. It can be run on your own computer, but it can also be run through a web
This is the end of the preview. Sign up
access the rest of the document.
This note was uploaded on 01/21/2012 for the course MAC 1130 taught by Professor Dr.cheun during the Fall '11 term at FSU.