3 ms·
Julia has the Unitful.jl package for handling units: https://github.com/PainterQubits/Unitful.jl https://github.com/PainterQubits/Unitful.jl
by stellalo 7y ago
Julia has the Unitful.jl package for handling units: https://github.com/PainterQubits/Unitful.jl https://github.com/PainterQubits/Unitful.jl
- dunefox 7y agoThis looks worse than the Java units: julia> 1u"kg" == 1000u"g" # Equivalence implies unit conversion true Java: Length distance = 100 mph * 3 hr;
- sents 7y agoYou can do this: julia> using Unitful.DefaultSymbols julia> 50m / s 50 m s^-1
- dunefox 7y agoNice, that's much better.
- ChrisRackauckas 7y agoAnd you can manually define them: const m = u"m" 50m There's actually nothing special in the compiler for unit implementation. This is just using the fact that `2x` is valid Julia code, where now x is something that overloads a dispatch to turn the number into a number with units. By default it uses string macros to keep the namespace clear, but you can fill the namespace with whatever you want.
- ChrisRackauckas 7y agoAlso note that these are zero runtime overhead: they act at compile-time through the type system and type-check or add in multiplications when conversions need to occur, but the compiled code is just floating point operations without reference to units or unit checking. It's a great system since it means you can always use units, even in performance critical code. And it works throughout the ecosystem, so you can even use it in linear algebra and things like ODE solves. Here's an example of solving an ODE with units: https://tutorials.juliadiffeq.org/html/type_handling/03-unitful.html https://tutorials.juliadiffeq.org/html/type_handling/03-unit...