Recent Changes - Search:

edit SideBar

ComputationWithNumbersAndUnits

An evaluator for numeric expressions with units

This code UnitsPl implements an evaluator for numeric expressions with units.

The toplevel predicate is isu/2, here are some examples:

% dimension-less computation (like is/2)
?- X isu 3+4.
X = 7

% units can be used like constants
?- X isu ft.
X = 0.3048 m

% computation with simple units
?- X isu 3*m + 4*m.
X = 7 m

% simple units (with an exponent of 1) can be written in postfix notation
?- X isu 3m + 4m.
X = 7 m

% mixing units (result is always in SI units)
?- X isu 3m + 4ft.
X = 4.2192 m

% units with higher exponents (Note: multiply sign in 4*m^2 is required!)
?- X isu 3m * 4*m^2.
X = 12 * (m)^3

% How much energy is left after you eat a bar of chocolate, then spend 1 hour on the treadmill?
?- Y isu 600kcal - 1h*200'W'.
Y = 1790400.0 'J'

% What was the speed of light again? (1 lightyear per year)
?- Y isu 1ly/y.
Y = 299792458.0 m/(s)

% If you run a 50 Watt fridge for 1 year, and pay $0.2 per kilowatt hour, what will you pay over the year?
?- Y isu 1y*50'W' * 0.2/1000'W'h.
Y = 87.66

% What speed do you reach after falling 1 minute in vacuum in earth's gravity?
?- Y isu 9.81*m/s^2 * 1min.
Y = 588.6 m/(s)

% ... and how much is that in furlongs per day?
?- X isu 588.6*m/s / (fur/d).
X = 252798.85468861848

% What volume does the cake have?
?- X isu 3icup + 3tbsp + (1/2)pt + 0.5l.
X = 0.0016405 * (m)^3

% What's the resistance of a 60W lightbulb?
?- X isu (230'V')^2 / 60'W'.
X = 881.66666666666663 'Ohm'

% What current flows through it?
?- X isu 230'V' / 882'Ohm'.
X = 0.26077097505668934 'A'

% comp.lang.prolog 2013-11-05
?- X isu 5m, Y isu 10s, Z isu X/Y.
X = 5 m
Y = 10 s
Z = 0.5 m/(s)

% Examples from http://futureboy.us/frinkdocs/#SampleCalculations 
?- Water isu 1kg/l, X isu 10ft*12ft*8ft * Water / lb.
Water = 1000.0 kg / (m)^3
X = 59930.842153098834

?- Water isu 1kg/l, X isu 2t / (10ft*12ft * Water) / ft.
Water = 1000.0 kg / (m)^3
X = 0.58857777869147632
Edit - History - Print - Recent Changes - Search
Page last modified on November 21, 2013, at 12:16 PM