\ cora.fs -- Coraphyco COnversion RAtios and PHYsical COnstants in Forth
\ Version 1.1
\ 2010/7/13 David Meyer <[email protected]>

\ Coraphyco provides a Forth environment to facilitate conversion of
\ quantities among a large variety of measurement units and systems.
\ Inspired by Frink by Allan Eliasen.
\ This version is a simple implementation providing Forth constants
\ for conversion and physical quantities and a few words to simplify
\ display and conversion.

\ Display quantity r1 of units r2 in standard unit amount
: mks  ( r1 r2 -- )  F* F. ;

\ Convert quantity r1 from r2 units to r3 units and display
: ->  ( r1 r2 r3 -- )  F/ F* F. ;

\ Convert quantity r1 from r2 units to r3 units and display
\ in engineering notation
: ->e  ( r1 r2 r3 -- )  F/ F* FE. ;

\ Convert Celcius temperature to Fahrenheit
: c>f  ( r -- )  9e0 f* 5e0 f/ 32e0 f+ f. ;

\ Convert Fahrenheit temperature to Celcius
: f>c  ( r -- )  32e0 f- 5e0 f* 9e0 f/ f. ;

\ Online help
: help ( -- )
( Eventually print/page help file ...
 s" /usr/mnt.rom/card/Documents/Cavenet_Files/green/forth/cora-help.txt"  r/o open-file throw Value fd-in
 begin
   line-buffer max-line fd-in read-line throw
 while
   type
 repeat ;
)
 cr ." (See file cora-help.txt for help.)"
 cr ;

\ Speed (standard unit: m/s (meters per second)

1e0             FCONSTANT  m/s  \ meters per second (standard)

331.46e0        FCONSTANT  mach \ speed of sound in dry air at STP

299792458e0     FCONSTANT  c    \ light in vacuum

\ Acceleration (standard unit: m/s^2 (meters per second per second)

1e0             FCONSTANT  m/s^2        \ meters per second per second (standard)

980665e-5       FCONSTANT  gee  \ standard gravitational acceleration

\ Time (standard unit: s (second))

1e0             FCONSTANT  s    \ second (standard)
60e0 60e0 F*    FCONSTANT  hr   \ hour
24e0 hr F*      FCONSTANT  day  \ day

\ Use ms as standard time unit to match Forth -
\ Replace s, hr, day above with following:
\ Also switch from float to double}
1e              FCONSTANT  ms   \ millisecond (standard)
1e3             FCONSTANT  s    \ second
60e s F*        FCONSTANT  minute       \ minute
60e minute F*   FCONSTANT  hr   \ hour
24e hr F*       FCONSTANT  day  \ day
7e day F*       FCONSTANT  wk   \ week
365.25e day F*  FCONSTANT  yr   \ year (average)
: monthms  ( uyear umonth -- r )
  dup 2 =
  ;

\ Angular measure (standard unit: radian (dimensionless))

2e0 pi F*       FCONSTANT  circle
circle 360e0 F/ FCONSTANT  deg  \ degree
deg 60e0 F/     FCONSTANT  amin \ arc minute
amin 60e0 F/    FCONSTANT  asec \ arc second

\ Linear measure (standard unit: m (meter))

1e0             FCONSTANT  m    \ meter (standard)
1e3             FCONSTANT  km   \ kilometer
1e-2            FCONSTANT  cm   \ centimeter
1e-3            FCONSTANT  mm   \ millimeter

3048e-4         FCONSTANT  ft   \ foot
ft 12e0 F/      FCONSTANT  in   \ inch
3e0 ft F*       FCONSTANT  yd   \ yard
5280e0 ft F*    FCONSTANT  mi   \ mile
1852e0          FCONSTANT  nmi  \ nautical mile

149597870691e0  FCONSTANT  au   \ astronomical unit
365.25e0 day F* c F*    FCONSTANT  ly   \ light year
au asec F/      FCONSTANT  pc   \ parsec

\ Area (standard unit: m^2 (square meter))

1e0             FCONSTANT  m^2  \ square meter (standard)
ft ft F*        FCONSTANT  ft^2 \ square feet
43560e0 ft^2 F* FCONSTANT  acre \ acre
1e2             FCONSTANT  are  \ are
1e4             FCONSTANT  hectare      \ hectare

\ Volume (standard unit: m^3 (cubic meter))

1e0             FCONSTANT  m^3  \ cubic meter (standard)
1e-6            FCONSTANT  cc   \ cubic centimeter
in in in F* F*  FCONSTANT  in^3 \ cubic inch
231e0 in^3 F*   FCONSTANT  gal  \ gallon
gal 4e0 F/      FCONSTANT  qt   \ quart
qt 2e0 F/       FCONSTANT  pt   \ pint
pt 16e0 F/      FCONSTANT  floz \ fluid ounce
42e0 gal F*     FCONSTANT  bbl  \ petroleum barrel
8e0 floz F*     FCONSTANT  cup  \ cup
cup 16e0 F/     FCONSTANT  tbsp \ tablespoon
tbsp 3e0 F/     FCONSTANT  tsp  \ teaspoon
1e3 cc F*       FCONSTANT  l    \ liter
cc              FCONSTANT  ml   \ milliliter

\ Mass (standard unit: kg (kilogram))

1e0             FCONSTANT  kg   \ kilogram (standard)
1e-3            FCONSTANT  g    \ gram
1e3             FCONSTANT  mt   \ tonne, metric ton

45359237e-8     FCONSTANT  lb   \ pound
2e3 lb F*       FCONSTANT  t    \ ton
lb 16e0 F/      FCONSTANT  oz   \ ounce

\ Print opening greeting
cr ." *********************************************************************"
cr ." ***          Welcome to Coraphyco. Type `help' for help           ***"
cr ." *********************************************************************" cr