Unum icon indicating copy to clipboard operation
Unum copied to clipboard

Add functionality for the use of decimal type

Open Tirpitz93 opened this issue 5 years ago • 3 comments

I was a contributor to the previous repo over on bitbucket, I will see if I can find my code for this. I remember running into an precision issue and therefor wanted a to have the decimal type as an option.

Tirpitz93 avatar Nov 29 '20 11:11 Tirpitz93

I have not tested yet (mabe if I have some time next weekend I'll add that) but it should probably work already with decimals.

from unum import as_unum
from decimal import Decimal
from unum.units import m
a = as_unum(Decimal(2), m)

trzemecki avatar Nov 30 '20 09:11 trzemecki

this is the issue I am trying to fix with the imperial units:

ounce = oz_t = new_unit("oz t", 31.1034768*g, "Troy ounce")
pound = lb_t = new_unit("lb t", 12*oz_t, "Troy Pound")
pennyweight = pwt = pwt = new_unit("pwt", oz_t/20, "Troy Pennyweight")
grain = gr_t = new_unit("gr t", pwt/24, "Troy grain")

Even when defined as: ounce = oz_t = new_unit("oz t", dec(31.1034768)*g, "Troy ounce")

these test fail:

    def test_ounce(self):
        self.assertAlmostEqual(31.1034768, oz_t.cast_unit(g).number(),7) #float causes inacuracies

    def test_ounce_dec(self):
        self.assertEqual(dec("31.1034768"), as_unum(dec(1), oz_t).cast_unit(g).number())  # float causes inacuracies
        
    def test_ounce_dec2(self):
        self.assertEqual(dec("31.1034768"), oz_t.cast_unit(g).number())  # float causes inacuracies

Tirpitz93 avatar Dec 03 '20 10:12 Tirpitz93

would it be possible to set an option to use decimal type. defining all units with decimal, then if this option is disabled convert to float (losing the precision benefit in exchange for hardware arithmetic)

Tirpitz93 avatar Dec 03 '20 11:12 Tirpitz93