connorferster / forallpeople

Python SI units library: your 'daily driver' for calculations.
Apache License 2.0
307 stars 40 forks source link

Changing unit #38

Open DiegoTheWolf opened 3 years ago

DiegoTheWolf commented 3 years ago

I made a new json which includes

"cm": {
    "Dimension": [0,1,0,0,0,0,0],
    "Value": 0.01},
"mm": {
    "Dimension": [0,1,0,0,0,0,0],
    "Value": 0.001},

now i have a calculation of m * cm * mm and it's result is . when i leave out the m i get a result of mm².

On what basis is the resulting unit calculated.

And how can i change it? (i want cm in both lines)

eagle-tex commented 3 years ago

I have the same problem. In my notebook, some forces are displayed as kN (F1 = 674 kN) and others as MN (F2 = 1.508 MN). Can I explicitely convert/require F2 to be in kN (for a more straightforward comparison)? It would be nice to have such an option in the library.

connorferster commented 2 years ago

Hi @DiegoTheWolf and @eagle-tex,

There is a method called .prefix() that accepts a string prefix to be "forced" upon the unit so that it always displays that way. It works like this:

a = 4 * m
b = 200 *mm
c = (a+b).prefix('c')
c

420.000 cm

The same would work as follows:

F2 = 1508*kN
F2

1.508 MN

F2.prefix('k')

1508 kN

eagle-tex commented 2 years ago

@connorferster

Thanks a lot for the reply. I tried the code you provided and it worked... to a point.

L = 3347.526*m

When I try to display L, it shows 3.348 km (automatic conversion in km) a.prefix("") is supposed to display L back into m (meters). But for some reason, it does not work.

Do you have any idea why and what could be done to solve that problem?

Btw, on the GitHub page of forallpeople, the method prefix does not exist. prefixed exists but that one doesn't work. That was probably a typo in the markdown file.

connorferster commented 2 years ago

Hi @eagle-tex,

Ah, yes this part is less obvious. For specifying no prefix, as follows:

L = 3347.526*m
a = L.prefix('unity')

I need to update the documentation, badly. Ideally with a proper readthedocs site. Coming soon. Will update the Github page in the mean time.

sononicola commented 2 years ago

Hi @connorferster At this purpose, you can just assign the prefix default value with the prefix of creation?

I mean, instead of doing this:

%%render param
f = 5000*si.kN.prefix('k')

it automatically add the prefix k since I defined f with kN.

And maybe adding a switch with this behaviur like you did in HandCalcs automatically_convert_units_over_1000 True | False

connorferster commented 2 years ago

@sononicola

It actually does this with all SI units that are a power of 1000 between 1e24 and 1e-24. The behavior is not so much defined by the creation of the unit with kN but more because 1 kN is actually 1000 N and it will "auto-prefix" the value because of this.

One way to manage this would be to have a toggle to just auto-prefixing on and off.