[racket] Numbers with dimensions

From: Konrad Hinsen (konrad.hinsen at fastmail.net)
Date: Tue Oct 29 06:40:22 EDT 2013

Laurent writes:

 > So I've redesigned it somewhat, and now there are 2 calculation "modes":
 > - The normal mode is pretty much like Frink (probably the one you want), which converts
 > everything to base SI units. Conversion back to non base units can be done afterwards.

Well, what I really want is something else, but this is a useful
feature nevertheless for a unit calculator. It would be nice though if
the default units to which everything is converted were modifiable. SI
units are fine for engineering and daily life, but neither for
astrophysics nor for atomic-scale measurements.

What I personally care about most is dimensional analysis as a means
of ensuring program correctness. I want to be able to write

  (define: (kinetic-energy (m : Mass) (v : Velocity) : Energy
    (m* 1/2 m v v))

and then compute the kinetic energy of a car as

  (kinetic-energy (m 1000 kg) (m 90 km (h -1)))

whereas a typical mistake such as

  (kinetic-energy (m 1000 kg) (m 90 km h))

would get caught by the type checker (ideally) or at run time. With a
unit-converter approach such as the one you implemented, the erroneous
call would be accepted and an error would be detected only if the
result is later converted to an explicitly named energy unit.

Konrad.

Posted on the users mailing list.