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

std/units #760

Open
timotheecour opened this issue Jun 19, 2021 · 0 comments
Open

std/units #760

timotheecour opened this issue Jun 19, 2021 · 0 comments

Comments

@timotheecour
Copy link
Owner

timotheecour commented Jun 19, 2021

proposal

std/units

example

runnableExamples:
  type
    Length = "m".makeUnit # can be also defined from other powers of other units
    Time = "s".makeUnit
    Mass = "kg".makeUnit

  var a = Time(1.2)
  var b = Length(3.2)
  var c = Mass(5.4)

  assert $a.float == "1.2"
  assert $a == "1.2's"
  assert $(a*a*a) == "1.728's³"
  assert $(a*a*a*b*b) == "17.69472's³*m²"
  assert $b == "3.2'm"
  assert $(a*b) == "3.84's*m"
  assert $(a/b*c*c) == "10.935's*1/m*kg²"
  assert $(Time(1.2)/Length(3.2)*Mass(5.4)*Mass(5.4)) == "10.935's*1/m*kg²"

implementation

I implemented this in a private branch.

note

it's a completely different approach from https://github.com/def-/nim-units/blob/master/units.nim, which is a hacky/not general/more complex solution that hardcodes fixed units (and fixed powers of such units, eg you can't get s^3 unless you hardcode it)

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

1 participant