[Python-ideas] real numbers with SI scale factors
Bruce Leban
bruce at leban.us
Sun Aug 28 22:56:00 EDT 2016
On Sun, Aug 28, 2016 at 6:44 PM, Ken Kundert <python-ideas at shalmirane.com>
wrote:
> When working with a general purpose programming language, the above numbers
> become:
>
> 780kpc -> 7.8e+05
> 108MPa -> 1.08e+08
> 600TW -> 6e+14
> 3.2Gb -> 3.2e+09
> 53pm -> 5.3e-11
> $8G -> 8e+09
>
These are not equal. 780kpc is a *number with units*. 7.8e+05 == 780000 is
a *unitless number*. All the numbers on the right hand side above have no
units so I can't tell which are pc or W or m or $. It's asking for trouble
to go halfway in representing units. On the left hand side, 780kpc + 108MPa
is invalid while 780kpc + 53pm is valid. On the right hand side, sums of
any two numbers are valid as they would be with the unitless SI prefixes.
So if you want to solve this problem, write a module that supports units.
For example, m(780, 'kpc') == m('780kpc') == m(780, kpc) and it's legal
to write m(780, kpc) + m(53, pm) but an exception gets raised if you
write m(2, kW) + m(3, kh) instead of m(2, kW) * m(3, km) == m(6, MWh).
In fact, several people have already done that. Here are three I found in <
1 minute of searching:
https://pint.readthedocs.io/en/0.7.2/
https://pypi.python.org/pypi/units/
https://python-measurement.readthedocs.io/en/latest/
Should support for this ever get added to the core language? I doubt it.
But if one of these modules becomes enormously popular and semi-standard,
you never know. I think you're much more likely to get this into your
Python code by way of a preprocessor.
--- Bruce
Check out my puzzle book and get it free here:
http://J.mp/ingToConclusionsFree (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160828/6b0ac3c4/attachment.html>
More information about the Python-ideas
mailing list