scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
401 stars 67 forks source link

Add scale factor for interpretations #298

Closed scott-griffiths closed 8 months ago

scott-griffiths commented 10 months ago

Not at all sure how this would work, but the fp8 formats should really have a configurable scale factor when being interpreted as floats.

The scale bias has a default for the e4m3 and e5m2 floats, but there should be a way to specify a different one. I'm not a fan of having a special case, so perhaps something in the Dtype specification could be used?

So the MetaDtype contains a 'scale' field, which is set to 1 for everything except e4m3float (scale=8) and e5m2float (scale=16). Or perhaps set to None for most things as we don't want it to be used (I don't think it's actually useful elsewhere?)

Then a Dtype can be created as dt = Dtype('e4m3float', scale=10) for example. But not sure how this would be converted to a string. Perhaps e4m3float{10} or something similar? We can't have a number at the end as that conflicts with the length.

There's not really any good way to make a general property from this though. You can't say x.e4m3float{10} or anything similar, except using underscores, which is also messy. We could allow it the other way around, with dt(x) for example...

So given an 8 bit bitstring b, the user could write Dtype('e4m3float', scale=6)(b) to get the value with a new scale bias. In practice you'd end up predefining objects such as e4m3_6 = Dtype('e4m3float', scale=6)?

scott-griffiths commented 9 months ago

On further investigation I think this isn't needed. The default scales are really part of the format, so it's up to the user to scale the incoming and outgoing values themselves.