News for hackers. Stuff that matters.
Re:Arithmetic is an underrated world-modeling technology by samatman on Thursday October 17, 2024 @06:34PM (#41874500)
Distressingly few programming languages offer an acceptable implementation of units. It's a wonder we get anything done.

One of the hidden gems of Julia is that it performs multiplication by juxtaposition, so if you define a variable `m`, then `2m` becomes `2 * m`. That plus overloading and multiple dispatch enables a rather nice library, Unitful[1], which does a fairly good job there.

[1]: (Julia indexes by one) https://github.com/PainterQubits/Unitful.jl

Re:Arithmetic is an underrated world-modeling technology by ddingus on Thursday October 17, 2024 @06:47PM (#41874592)
That is an interesting observation. I started out low level in 8 bit land. To me, units broke down to whatever close representation fit into 8 maybe 16 bits... But, you are right! A BASIC with units would have been quite useful back then.

Would have caught a fair number of my bonehead errors.

Later, moving to C and various scripting languages, I just sort of kept doing the same thing. Work units out old school, then comment whatever equations made sense in the program, and then do math, deciding on fixed or floating point depending...

I will have to give Julia a look. Seems intriguing.

Re:Arithmetic is an underrated world-modeling technology by schiffern on Thursday October 17, 2024 @07:44PM (#41874968)
You should check out Frink, a delightful unit-preserving calculator and language optimized for (but not limited to) back-of-the-envelope calculation.

Yes, it has implicit multiplication! :) It also offers exact rational fractions, transparent bigint conversion, and plus/minus intervals that automatically tracks error bars through all arithmetic operations.

Docs: https://futureboy.us/frinkdocs

Online REPL: https://futureboy.us/fsp/frink.fsp?fromVal=1+furlong+%2F+for...

[Unexpectedly humorous] units data file: https://futureboy.us/frinkdata/units.txt

Re:Arithmetic is an underrated world-modeling technology by samatman on Thursday October 17, 2024 @08:00PM (#41875060)
I am well aware of Frink, and glad you mentioned it!

Julia has the advantage that you can do things with it that aren't unitful calculations, a classic 80% solution. Frink has the advantage that, well... it's Frink. Really nothing else like it.

Re:Arithmetic is an underrated world-modeling technology by schiffern on Thursday October 17, 2024 @08:04PM (#41875090)
Awesome! I admit I'm a bit confused though: Frink doesn't force you to use units (you could ignore them if you wanted, and even skip loading the built-in units on startup), so I'm not sure where the advantage is.

Were you thinking of a specific feature set in Julia that's lacking in Frink? Thanks.

Re:Arithmetic is an underrated world-modeling technology by samatman on Thursday October 17, 2024 @08:22PM (#41875185)
What I meant is that Julia is a general purpose programming language, and Frink is a comprehensive and thorough calculation DSL.

You could use Frink for things that aren't its focus, of course. But few would want to.

Re:Arithmetic is an underrated world-modeling technology by ddingus on Thursday October 17, 2024 @09:47PM (#41875662)
I am not well aware. Thanks and just another reason why I love this little place on the web.
Re:Arithmetic is an underrated world-modeling technology by seanhunter on Friday October 18, 2024 @05:52AM (#41877799)
I have built my own implementations of units in a couple of functional languages (Haskell and Ocaml) using shadow types. How this pattern works is you tag a numeric type with an extra algebraic type defining the units and then define how the arithmetic operators handle the units. The cool thing about doing it this way is the shadow types compile away so there is zero runtime cost but you can assert and pattern match on the types and put them in your unit tests etc and your code literally won't compile if you mess the units up in some way.