SciNim / Unchained

A fully type safe, compile time only units library.
https://scinim.github.io/Unchained
110 stars 0 forks source link

Add more imperial units #6

Open HugoGranstrom opened 3 years ago

HugoGranstrom commented 3 years ago

For the units tutorial I'm planning to write someday (hopefully soon) I think it would be nice to have lots of examples mixing SI and Imperial units (because it's a mess you don't want to deal with manually). So I thought I'd create a PR for it. The only thing I really need is which imperial units do we want to support? There are so many obscure ones out there. I know you want chain to be included but do you have any other preferences @Vindaar?

Vindaar commented 3 years ago

Well, on my part I'm pretty indifferent about what units to actually add. I suppose the common imperial ones are important. Of the more esoteric ones like chain I really don't care.

If you (or someone else) provides:

I'll happily implement it.

If someone wants me to implement too many, I'll probably think about a small rewrite of the internal code. New units are a bit annoying to add right now as the stuff needs to be inserted into too many places. It's not a lot of work, but it gets tiring. It should be rather easy to automate most of that away with a DSL for unit definitions (which would be great, as it might eventually allow for arbitrary systems of units based on user defined unit classes).

edit: on a more practical side for a possible blog: think about the units you want to include in it. Tell me about them 1-2 days in advance and I'll have them ready by the time you want to use them. :)

HugoGranstrom commented 3 years ago

Well, on my part I'm pretty indifferent about what units to actually add. I suppose the common imperial ones are important. Of the more esoteric ones like chain I really don't care.

Well then we'll just implement the units I need at the moment 😜

If you (or someone else) provides: [...] I'll happily implement it.

That's nice of you :)

If someone wants me to implement too many, I'll probably think about a small rewrite of the internal code. New units are a bit annoying to add right now as the stuff needs to be inserted into too many places. It's not a lot of work, but it gets tiring. It should be rather easy to automate most of that away with a DSL for unit definitions (which would be great, as it might eventually allow for arbitrary systems of units based on user defined unit classes).

Oh that does sound quite tiring 🤔 A DSL for it is inevitable sometime in the future either way I guess? Just imagine how many lines of code that PR will remove 🤣

edit: on a more practical side for a possible blog: think about the units you want to include in it. Tell me about them 1-2 days in advance and I'll have them ready by the time you want to use them. :)

Again, very kind of you!:D I'm still doing some research into real-world examples where units caused trouble so I don't have a definite list yet but I can't imagine it being out of the most common units so I'll write you a list of the ones I think I'll use tomorrow.

On the topic of the blog, do you think Define custom units is something that should be included in a Units Basics tutorial in the current state of Unchained? (Referring to the fact you think it's quite verbose atm without a DSL)

Vindaar commented 3 years ago

If someone wants me to implement too many, I'll probably think about a small rewrite of the internal code. New units are a bit annoying to add right now as the stuff needs to be inserted into too many places. It's not a lot of work, but it gets tiring. It should be rather easy to automate most of that away with a DSL for unit definitions (which would be great, as it might eventually allow for arbitrary systems of units based on user defined unit classes).

Oh that does sound quite tiring A DSL for it is inevitable sometime in the future either way I guess? Just imagine how many lines of code that PR will remove

It probably won't remove any, because the macro magic required will eat those. But by now at least I have an idea that the whole implementation works and I can implement a macro. Originally it was more "exploratory research" and the hard part was the parsing, simplifications etc. Those will all remain unchanged fortunately (as all of that is independent of the actual units).

edit: on a more practical side for a possible blog: think about the units you want to include in it. Tell me about them 1-2 days in advance and I'll have them ready by the time you want to use them. :)

Again, very kind of you!:D I'm still doing some research into real-world examples where units caused trouble so I don't have a definite list yet but I can't imagine it being out of the most common units so I'll write you a list of the ones I think I'll use tomorrow.

You're welcome!

On the topic of the blog, do you think Define custom units is something that should be included in a Units Basics tutorial in the current state of Unchained? (Referring to the fact you think it's quite verbose atm without a DSL)

By defining a custom unit you mean some combination of existing ones (so just calling defUnit?) or adding completely new ones?

If the former, yes I'd include it, because it's just one macro call and people will need it.

If it's the latter, no. That is too clumsy at the moment and shouldn't be needed for most use cases (for that people should open issues for the time being).

HugoGranstrom commented 3 years ago

It probably won't remove any, because the macro magic required will eat those. But by now at least I have an idea that the whole implementation works and I can implement a macro. Originally it was more "exploratory research" and the hard part was the parsing, simplifications etc. Those will all remain unchanged fortunately (as all of that is independent of the actual units).

Fair enough :)

By defining a custom unit you mean some combination of existing ones (so just calling defUnit?) or adding completely new ones?

I'm not sure tbh I'm still a bit confused when you need defUnit and when you don't 😅 But I guess it's the first case.

HugoGranstrom commented 3 years ago

Ok did some digging on the Discord history and found the answer:

I've just added basic support for units written in accented quotes (`kg*m*s^-2` etc.). For arguments to procedures one needs to define essentially all units using defUnit before once (or use them using . operator before, which defines them automatically.

HugoGranstrom commented 3 years ago

Okay here comes a list of some (hopefully) useful units:

That should be it for units. Noticed you didn't have the gravitational acceleration among the constants, I guess that should be added as well?

HugoGranstrom commented 3 years ago

And no hurry, I won't be able to do much work this weekend either way :)

Vindaar commented 3 years ago

Ah, I forgot the constants. Will add them later. And I'll be so prudent as to use the correct value for G. :speak_no_evil:

HugoGranstrom commented 3 years ago

Lots of thanks! :D No hurry! You mean the value I got from Wikipedia is wrong? 🤣 (Or shouldn't I have ignored the uncertainty perhaps :P)

Vindaar commented 3 years ago

Lots of thanks! :D No hurry! You mean the value I got from Wikipedia is wrong? (Or shouldn't I have ignored the uncertainty perhaps :P)

You forgot a tiny ⁻¹¹ in there. ;) G = 6.67430.m³•kg⁻¹•s⁻² vs G = 6.67430e-11.m³•kg⁻¹•s⁻²

almost the same, haha.

HugoGranstrom commented 3 years ago

Oh that tiny thing... 😆🙈

Vindaar commented 1 year ago

Not imperial, but noting it here: Consider adding Percent as an additional UnitLess unit.

arkanoid87 commented 8 months ago

I have to copypaste si_units.nim to add

declareQuantities:
  Derived:
    SpecificEnergy:
      [(Length, 2), (Time, -2)]

declareUnits:
  Derived:
    BritishThermalUnit:
      short:
        btu
      quantity:
        Energy
      conversion:
        1055.06.J