[Python-ideas] real numbers with SI scale factors: next steps

Ken Kundert python-ideas at shalmirane.com
Wed Aug 31 00:08:01 EDT 2016


> What's the mnemonic here? Why "r" for scale factor?

My thinking was that r stands for real like f stands for float.
With the base 2 scale factors, b stands for binary.

> (1) Why no support for choosing a particular scale? If this only auto-scales, 
> I'm not interested.

Auto-scaling is kind of the point. There is really little need for a special 
mechanism if your going to specify the scale factor yourself.

    >>> print('Attenuation = {:.1f} dB at {:r}m.'.format(-13.7, 50e3))
    Attenuation = -13.7 dB at 50 km.

If you wanted to force the second number to be in km, you use a %f format and 
scale the argument:

    >>> print('Attenuation = {:.1f} dB at {:.1f} km.'.format(-13.7, 50e3/1e3))
    Attenuation = -13.7 dB at 50 km.

> (2) Support for full prefix names, so we can format (say) "kilograms" as well 
> as "kg"?

This assumes that somehow this code can access the units so that it can switch 
between long form 'grams' and short form 'g'. That is a huge expansion in the 
complexity for what seems like a small benefit.

> (3) Scientific notation and engineering notation?
> 
> (4) 1e5 versus 1×10^5 notation?

Ah, okay. But all of these require auto-scaling. And I was still thinking that 
we need to provide input and output capability (ie, we still need be able to 
convert whatever format we output back from strings into floats). Are you 
thinking that we should parse 1×10^5? And why 1×10^5 and not 1×10⁵?

> (5) Is this really something that format() needs to understand? We can get 
> a *much* richer and more powerful interface by turning it into a generalise 
> numeric pretty-printing library, at the cost of a little less convenience.

This is suddenly a much bigger project than what I was envisioning.

-Ken



More information about the Python-ideas mailing list