Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Define Scalar Units in Terms of Other Units #8

Open
CrownedPhoenix opened this issue Apr 17, 2024 · 1 comment
Open

Request: Define Scalar Units in Terms of Other Units #8

CrownedPhoenix opened this issue Apr 17, 2024 · 1 comment

Comments

@CrownedPhoenix
Copy link

Situation: I want to define "quart".

This is currently how I have to do it.

public static let quart = try! Units.Unit.register(
    name: "quart",
    symbol: "qt",
    dimension: [.Length: 3],
    coefficient: 0.0009463529 // This is the conversion to kiloliters
)

Problem:

  • It was not obvious to me that the coefficient should be the scalar conversion to kiloliters. I had to dig around the list of defined units to find the [.Length: 3] one with coefficient: 1.
  • Then I had to Google "quart to kiloliter" which gave me this long coefficient that I'm not sure how accurate it is. But I already know that a quart is 1/4 gallon (exactly).

Desired Solution:

extension Unit {
  public static let quart = try! Units.Unit.register(
      name: "quart",
      symbol: "qt",
      derivation: 0.25 * .gallon
    )
}
@NeedleInAJayStack
Copy link
Owner

It was not obvious to me that the coefficient should be the scalar conversion to kiloliters

The base volume unit is actually not kiloliters - it's cubic meters, which is the SI standard base unit of volume: https://en.wikipedia.org/wiki/International_System_of_Units#Coherent_and_non-coherent_SI_units. SI base units are also commented below each quantity in the Default Units file: https://github.com/NeedleInAJayStack/Units/blob/main/Sources/Units/Unit/DefaultUnits.swift#L948

Then I had to Google "quart to kiloliter" which gave me this long coefficient that I'm not sure how accurate it is. But I already know that a quart is 1/4 gallon (exactly).

I mean, if you define it based on a gallon, then it will only be as accurate as the gallon unit definition coefficient because we have to get to SI anyway. So as an alternate path, you could have just used the gallon coefficient and divided by 4: https://github.com/NeedleInAJayStack/Units/blob/main/Sources/Units/Unit/DefaultUnits.swift#L1026.

I'm definitely not opposed to adding this API. But to be completely honest, I'm not sure I want to invest much effort personally because these units should just be statically defined once, and cutting out the client doing a little math on the front side doesn't seem like a huge benefit.

I would be super interested in an MR adding quart to our list of default units though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants