matterhorn103 / quanstants

Intuitive and unastonishing physical quantities, units, constants, and uncertainties in Python
MIT License
0 stars 0 forks source link

Support standard behaviour, operations, and functions as for numerical types #37

Open matterhorn103 opened 2 months ago

matterhorn103 commented 2 months ago

It is important that quantities support standard operations and functions for manipulation as far as possible.

Whether the behaviour should conform to standards/precedent in Python is another question.

The Python docs for numeric types list various operations that are supported by all numeric types (except complex, crucially), but not yet by Quantity:

Real types (int and float) support a few more:

This page in the docs says the following methods "can be" defined (but don't have to be) in order to emulate numeric objects (already implemented or reviewed ones are left out):

The math module also has various functions that might be sensible to support. We should check though, and if Decimal doesn't, maybe Quantity shouldn't. I also don't know how one goes about supporting them since they act on a quantity and are not methods of Quantity itself:

Consider implementing the following for special values:

The methods of Decimal are naturally all candidates. Many are not meaningful for a quantity, but should be assessed for inclusion:

float also has these additional methods:

IEEE 754 (floating point standard, also for decimal floating point) has both required operations:

and recommended operations, which often are just compound methods?:

matterhorn103 commented 2 months ago

Sometimes it is not just a question of support but also of making an opinionated decision as to what the result of an operation/function should be.

For example, Quantity currently supports int(Q), but throws an error if the quantity is not dimensionless. Moreover, the decision was already made to just use the underlying Decimal.__int__(), such that a non-integer number gets rounded. This might be sensible, as the user expects this behaviour by analogy to float, Decimal, etc.. However, the rounding is then different to the usual rounding behaviour of Quantity.

Rounding is another good example. IEEE 754 requires "ties to even" as the default for binary and the recommended default for decimal, on the basis that it results in no bias. Implementation of "ties to away" is also required for decimal implementations. This is what quanstants uses by default, though, to align with non-technical users' expectations. Again, this opinionated decision should be reviewed.

quanstants deliberately makes key decisions to break from how Python normally does things (rounding, use of decimal, decimalization of floats as strings). So the question is often whether it should match behaviour of built-in types or to go its own way.