Code Monkey home page Code Monkey logo

si's Introduction

si

GoDoc Go Report Card

NOTE: this is still a work in progress

si is a unit annotation and conversion library. Its different from other strongly typed si/unit libraries in the way that all types are aliases to float64 instead of concrete types. The benefit of this approach is that casting can be avoided which results in more readable code that somewhat resembles the math equation.

To prove my point i have created an example using Einstein's mass-energy equivalence formular $E=mc^2$.

// possible using aliased si types
24.965421631578266*MWh == 1*mg * math.Pow(c, 2)

// problem with conrete si types...
24.965421631578266*MWh == MWh(float64(1*mg) * math.Pow(c, 2))

As it can be seen in the example above casting ends up hurting readability and it really doesn't add anything in terms of type safety because casting will be so prevalent. It will only be as safe as the aliased approach used in this library.

Another benefit is that derived si types can easily be defined on the fly, this is not possible when using concrete types unless casting is involved.

const SpeedOfLight = 299792458 * (Meter/Second)

It will also work well with the math package from standard lib or any other thirdparty library which is using float64 as input or output types. Below is an example of how it could be used with a vector package.

Vector{10 * (Meter/Second)}.Rotate(45*Degree)

Install

go get github.com/quartercastle/si

Usage

func MassEnergyEquivalence(energy si.Energy, mass si.Mass) bool {
  return energy == mass * math.Pow(si.SpeedOfLight, 2)
}


MassEnergyEquivalence(24.965421631578266*si.MegawattHour, 1*si.Milligram)

Credits

This project is heavily inspired by github.com/martinlindhe/unit, which is a strongly typed unit conversion library that i have used a lot previously. It would not exist without the knowledge i have gained by using this library.

License

This project is licensed under the MIT License.

si's People

Contributors

quartercastle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.