[Edu-sig] introducing euler's number

kirby urner kirby.urner at gmail.com
Thu Dec 3 21:03:51 EST 2015

Again from tonight's class prep.

Such scripts might be useful exhibits if you're still trying to get
approval to move beyond a TI calculator in math class.  Playing with
extended precision helps bring the concepts of limits and convergence alive.

My students are employed adults so winning such approval is a non-issue.
But then I ask them to imagine themselves back in school, with Python a
tool of choice in math class.  Wouldn't that have been great!?


# -*- coding: utf-8 -*-
Created on Thu Dec  3 16:40:46 2015

See:  http://www.miniwebtool.com/first-n-digits-of-e/?number=300

@author: kurner


Write a unittest to confirm convergence to e to 300 places.
after n steps.

import unittest

from decimal import *

def euler(n):
    n = Decimal(n)
    one = Decimal(1)
    return (one + one/n) ** n

class Test_e(unittest.TestCase):

    def test_outcome(self):
        expected = ('2.718281828459045235360287471352662'
        with localcontext() as c:
            c.prec = 400
            result = euler('1' + '0' * 301)

        self.assertEqual(str(result)[:len(expected)], expected)

if __name__ == "__main__":
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20151203/ca1aa659/attachment.html>

More information about the Edu-sig mailing list