#πŸ”’ Pint quirk with units in ratio

24 messages Β· Page 1 of 1 (latest)

orchid basin
#

First of all... is there a way to make pint use decimal.Decimal to avoid floating point errors, second of all, why does this: ```py
from pint import UnitRegistry

ureg = UnitRegistry(auto_reduce_dimensions=True)
Q_ = ureg.Quantity

def Dosage(bodyweight, doseperweight, dosepervolume):
bw = Q_(bodyweight)
dpw = Q_(doseperweight)
dpv = Q_(dosepervolume)

return (bw*dpw)/dpv

print("input all quantities with units")
print(Dosage(
input("Bodyweight: "),
input("Dose per weight: "),
input("Dose per volume: ")
))
``` correctly produce 7.5 millileter for the inputs: 20kg, 150mcg/kg and 0.4mg/ml, but incorrectly produce 7.5 / millileter when the dose per volume is input at 2mg/5ml (equivalent)

round oysterBOT
#

@orchid basin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

orchid basin
#

context (before you think im doing something dodgy πŸ˜‚):

round oysterBOT
#

pint/registry.py line 128

non_int_type=float,```
noble nest
#

You probably want to define that as Decimal, it could work, but I'm not sure

orchid basin
#

ah okay, cool, any ideas about the other part?

noble nest
#

You could split the input and apply the Q_ to both

#

If that doesn't work, I'm out of ideas, never used pint before

orchid basin
#

i mean, ideally i could yeah, but i want it to work on user input

#

and in the screenshot i posted, the reel that comment is on gave it exactly as "2mg/5ml" so i guess that thats just how they write it naturally

noble nest
orchid basin
#

oh true, yeah, cheers

#

cause its always gunna be a divide for a concentration, yeh

noble nest
#

!e ```py
foo = "2mg/5ml"
print(foo.split("/"))

round oysterBOT
orchid basin
#

yeah, i was overthinking it lmao

noble nest
orchid basin
#

.close

quartz elbowBOT
#
Did you mean:
orchid basin
#

!close

round oysterBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.