Comments (3)
an opportunity to both fix the bug and enhance Pint.
This is always a good combination!
The problem as I see it is that 3/2
for Python 3 is a float, not a fraction. To have a fraction we will need to do something like:
>>> from fractions import Fraction
>>> q = ureg.meter ** Fraction(3,2)
which, by the way, is now also converted to float internally:
>>> q
<Quantity(1.0, 'meter ** 1.5')>
I see two ways to implement your suggestion, and maybe we want to implement both.
1.- Change the formatting part
We can get the exponents and try to convert them from float to fraction.
>>> Fraction.from_float(1.5)
Fraction(3, 2)
The good part: it will be transparent to the user.
The bad part: it will add some complexity to the formatting code to decide when we want to do this.
2.- Allow any scalar numeric type for the exponents
The good part: will allow the user to make some calculations more accurate. In fact, this is why the implementation Pi Theorem works so well in Pint: we use Fraction
as the type to avoid precision problems associated with floats.
The bad part: User will have to define the quantity with a fractional exponent so it could be nicely printed. Additionally, when you give the freedom to use any scalar numerical type you might hit some errors like this:
>>> decimal.Decimal(0.1) + 1.
Traceback (most recent call last):
[...]
TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
We will need to guard for those.
As I said, maybe the way is to implement both.
from pint.
I think to move forward we need to maybe only allow integer
or Fraction
as power in units.
This is critical if we want to allow fractional exponent units.
Operations on integer & Fraction are easily supported using python.
Other numerical type could be casted to Fraction.
I think it deserves an issue on its own to work on.
About this issue, we can easily support it if we encounter Fraction, it will display '3/2'
and we can add pretty print after that in the dedicated function.
from pint.
I do agree that we need an issue on its own. In particular, I think it is worth discussing if we should enable a plugable architecture (like non_int_type
or similar)
from pint.
Related Issues (20)
- Could not define System from lines HOT 1
- Use `usedforsecurity=False` when calling `hashlib.blake2b` to avoid crashing on FIPS enabled systems HOT 3
- unit dB not hashable when using decimal as non_int_type when numpy is installed
- In place numpy array operations causing issues with dimensionality of quantities HOT 1
- Listing possible units based on dimensionality
- Adding vs multiplying quantities in different units
- floating point arithmetic errors HOT 3
- mypy error: ureg.m annotation is expected to be Quantity instead of Unit
- mypy error: defining a Quantity as multiplication of list and unit raises an error
- Units with offsets raise DimensionalityError in 0.23 HOT 2
- TypeError when calling `to_base_units()` for temperature.
- Check dimensions in given context HOT 1
- Warnings when using `ruff`
- `PlainQuantity.ndim` stringifys `magnitude`, which is expensive for arrays
- How to get type hinting to work? HOT 1
- Loading Quantity from PyQt6 QSettings not working
- `{:L~}` can generate `\mathrm{μ}` which becomes blank when latex compiled
- Suggestion: Add permille ratio HOT 1
- How to convert kNm to custom unit Nmm? HOT 7
- Nm definition
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pint.