\ cora_4.0.fs -- Coraphyco COnversion RAtios and PHYsical COnstants
\ Version 4.0
\ 2011/6/14 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.

\ Changes in version 4.0
\ - Convert to integer arithmetic from floating-point for
\   portability and usability.
\ - Rewrite user interface to present unique word for each
\   conversion instead of unit-to-base constants and generic
\   conversion word ->.

\ Notes:
\ - On Zaurus, one cell is four bytes, therefore max. single-
\   precision integer is $ffffffff = 4294967295 (unsigned) or
\   $7fffffff = 2147483647 (signed)
\ - NetBSD (SDF) compilation uses eight-byte cells. Max. single-
\   precision integer: $ffffffffffffffff = 18446744073709551615
\   (unsigned), $7fffffffffffffff = 9223372036854775807 (signed)
\ - Largest conversion ratio: parsec:mm (30856775813057300000.)
\   (requires double-precision for integer arith., even on SDF)

: *pi ( n -- PI*n )
   \ Chick Moore's multiply-by-pi
   355 113 */ ;

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

: cc>m^3 ( n-cc -- m-m^3 ) 1000000 / ;


\ 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

\ Distance

: in>cm 254 100 */ ;
: in>ft 12 / ;
: in>km 254 10000000 */ ;
: in>m 254 10000 */ ;
: in>mi 64560 / ;
: in>mm 254 10 */ ;
: in>nmi 254 18520000 */ ;
: in>yd 36 / ;

: ft>cm 774720 100 */ ;
: ft>in 12 * ;
: ft>km 774720 10000000 */ ;
: ft>m 774720 10000 */ ;
: ft>mi 5280 / ;
: ft>mm 774720 10 */ ;
: ft>nmi 774720 18520000 */ ;
: ft>yd 3 / ;

: yd>cm 9144 100 */ ;
: yd>ft 3 * ;
: yd>in 36 * ;
: yd>km 9144  10000000 */ ;
: yd>m 9144 10000 */ ;
: yd>mi 1760 / ;
: yd>mm 9144 10 */ ;
: yd>nmi 9144 18520000 */ ;

: mi>cm 16398240 100 */ ;
: mi>ft 5280 * ;
: mi>in 64560 * ;
: mi>km 16398240  10000000 */ ;
: mi>m 16398240 10000 */ ;
: mi>mm 16398240 10 */ ;
: mi>nmi 16398240 18520000 */ ;
: mi>yd 1760 * ;

: mm>cm 10 / ;
: mm>ft 10 3048 */ ;
: mm>in 10 254 */ ;
: mm>km 1000000 / ;
: mm>m 1000 / ;
: mm>mi 10 16398240 */ ;
: mm>nmi 1852000 / ;
: mm>yd 10 9144 */ ;

: cm>ft 100 3048 */ ;
: cm>in 100 254 */ ;
: cm>km 100000 / ;
: cm>m 100 / ;
: cm>mi 100 16398240 */ ;
: cm>mm 10 * ;
: cm>nmi 185200 / ;
: cm>yd 100 9144 */ ;

: m>cm 100 * ;
: m>ft 10000 3048 */ ;
: m>in 10000 254 */ ;
: m>km 1000 / ;
: m>mi 10000 16398240 */ ;
: m>mm 1000 * ;
: m>nmi 1852 / ;
: m>yd 10000 9144 */ ;

: km>cm 100000 * ;
: km>ft  10000000 3048 */ ;
: km>in 10000000 254 */ ;
: km>m 1000 * ;
: km>mi  10000000 16398240 */ ;
: km>mm 1000000 * ;
: km>nmi 1000 1852 */ ;
: km>yd  10000000 9144 */ ;

: nmi>cm 185200 * ;
: nmi>ft 18520000 3048 */ ;
: nmi>in 18520000 254 */ ;
: nmi>km 1852 1000 */ ;
: nmi>m 1852 * ;
: nmi>mi 18520000 16398240 */ ;
: nmi>mm 1852000 * ;
: nmi>yd 18520000 9144 */ ;

\ 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


\ 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