Differences between revisions 2 and 3
Revision 2 as of 2008-11-15 14:00:37
Size: 1626
Editor: localhost
Comment: converted to 1.6 markup
Revision 3 as of 2014-01-09 11:03:04
Size: 447
Editor: fxproalert58
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
----
{{{
#!/usr/bin/env python

r""" bsce.py

Show that the Python decimal module understands computer engineering notation,
in contrast with the Python math module that understands scientific notation.

Thus help Python newbies unfamiliar with __future__, with, decimal 0 +, etc.

>>> est(0)
'0'
>>> est(100)
'100'
>>> est(12300)
'12.3e+3'
>>> est(12345)
'~12.3e+3'
>>> est(987654321)
'~988e+6'
>>> est(987654321, 6)
'~987.654e+6'
>>> est(2 ** 40)
'~1.10e+12'
>>>

>>> estsci(12300)
'1.23e+4'
>>> estsci(987654321)
'~9.88e+8'
>>> estsci(987654321, 6)
'~9.87654e+8'
>>>

See also: http://www.google.com/search?q=python+notation+engineering+scientific
See also: http://www.etsimo.uniovi.es/python/pycon/2005/papers/24/
See also: http://docs.python.org/lib/module-decimal.html
"""

from __future__ import with_statement

import decimal
import math

def est(count, prec = 3):

 """ Return the count or an approximately equal value in engr. notation. """

 with decimal.localcontext(decimal.Context(prec = prec)):
  eng = (0 + decimal.Decimal(count)).to_eng_string().lower()
  est = eng if count == int(float(eng)) else '~' + eng
  return est

def estsci(count, prec = 3):

 """ Return the count or an approximately equal value in sci. notation. """

 exp = int(math.log(count, 10))
 sci = ('%.' + str(prec - 1) + 'fe+%d') % (count / (10.0 ** exp), exp)
 est = sci if count == int(float(sci)) else '~' + sci
 return est

if __name__ == '__main__':

 import doctest

 (failings, testings) = doctest.testmod(optionflags = doctest.ELLIPSIS)
}}}
----
CategoryFaq
I'm a 43 years old, married and work at the college (Industrial and Labor Relations).<<BR>>
In my free time I'm trying to teach myself French. I've been twicethere and look forward to go there sometime near future. I love to read, preferably on my ipad. I like to watch Game of Thrones and Family Guy as well as docus about nature. I love American football.<<BR>>
<<BR>>
Also visit my site; [[http://www.fxproalert.com|forex signals trading]]

I'm a 43 years old, married and work at the college (Industrial and Labor Relations).
In my free time I'm trying to teach myself French. I've been twicethere and look forward to go there sometime near future. I love to read, preferably on my ipad. I like to watch Game of Thrones and Family Guy as well as docus about nature. I love American football.

Also visit my site; forex signals trading

decimal (last edited 2014-01-09 15:37:47 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.