forth/cora_1.fs

138 lines
3.6 KiB
Forth
Executable File

\ cora.fs -- Cora Phyco package of conversion ratios and physical constants
\ Version 1
\ 2005/9/9 David Meyer
\ Cora Phyco 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 creates a word for each measurement unit that both holds the
\ unit's conversion factor and converts the quantity at TOS. Physical constants
\ are represented as Forth constants.
.( Loading Cora Phyco version 1 ...)
\ Pending unit conversion flag
VARIABLE <cvt> FALSE <cvt> !
\ Pending display engineering notation flag
VARIABLE <eng> FALSE <eng> !
\ Store unit conversion factor r as float. On reference convert r1 quantity to
\ r2 standard units or display r1 quantity converted to target units.
: unit ( r "name" -- )
CREATE F,
DOES> ( r1 -- r2| )
<cvt> @
IF F@ F/ <eng> @ IF FE. FALSE <eng> ! ELSE F. THEN FALSE <cvt> !
ELSE F@ F* THEN ;
\ Set conversion flag
: -> ( -- ) TRUE <cvt> ! ;
\ Set conversion and engineering notation flags
: ->e ( -- ) TRUE DUP <cvt> ! <eng> ! ;
\ Physical constants (standard units)
2e0 pi F* FCONSTANT circle \ radians per full circle angle
299792458e0 FCONSTANT c \ speed of light in vacuum (m/s)
\ Speed (standard unit: m/s (meters per second)
1e0 unit m/s \ meters per second (standard)
331.46e0 unit mach \ speed of sound in dry air at STP
\ Acceleration (standard unit: m/s^2 (meters per second per second)
1e0 unit m/s^2 \ meters per second per second (standard)
980665e-5 unit gee \ standard gravitational acceleration
\ Time (standard unit: s (second))
1e0 unit s \ second (standard)
60e0 60e0 F* unit hr \ hour
24e0 hr unit day \ day
\ Angular measure (standard unit: radian (dimensionless))
circle 360e0 F/ unit deg \ degree
1e0 deg 60e0 F/ unit amin \ arc minute
1e0 amin 60e0 F/ unit asec \ arc second
\ Linear measure (standard unit: m (meter))
1e0 unit m \ meter (standard)
1e3 unit km \ kilometer
1e-2 unit cm \ centimeter
1e-3 unit mm \ millimeter
3048e-4 unit ft \ foot
1e0 ft 12e0 F/ unit in \ inch
3e0 ft unit yd \ yard
5280e0 ft unit mi \ mile
1852e0 unit nmi \ nautical mile
149597870691e0 unit au \ astronomical unit
365.25e0 day c F* unit ly \ light year
1e0 au 1e0 asec F/ unit pc \ parsec
\ Area (standard unit: m^2 (square meter))
1e0 unit m^2 \ square meter (standard)
1e0 ft 2e0 F** unit ft^2 \ square feet
43560e0 ft^2 unit acre \ acre
1e2 unit are \ are
1e4 unit hectare \ hectare
\ Volume (standard unit: m^3 (cubic meter))
1e0 unit m^3 \ cubic meter (standard)
1e-6 unit cc \ cubic centimeter
1e0 in 3e0 F** unit in^3 \ cubic inch
231e0 in^3 unit gal \ gallon
1e0 gal 4e0 F/ unit qt \ quart
1e0 qt 2e0 F/ unit pt \ pint
1e0 pt 16e0 F/ unit floz \ fluid ounce
42e0 gal unit bbl \ petroleum barrel
8e0 floz unit cup \ cup
1e0 cup 16e0 F/ unit tbsp \ tablespoon
1e0 tbsp 3e0 F/ unit tsp \ teaspoon
1e3 cc unit l \ liter
1e0 cc unit ml \ milliliter
\ Mass (standard unit: kg (kilogram))
1e0 unit kg \ kilogram (standard)
1e-3 unit g \ gram
1e3 unit mt \ tonne, metric ton
45359237e-8 unit lb \ pound
2e3 lb unit t \ ton
1e0 lb 16e0 F/ unit oz \ ounce
\ Temperature (standard system: Kelvin)
1e0 unit kel \ Kelvin (standard)
5e0 9e0 f/ FCONSTANT degfah \ Fahrenheit degree
255.372e0 FCONSTANT 0fah \ 0 degrees Fahrenheit
273.15e0 FCONSTANT 0cel \ 0 degrees Celsius
: fah ( r1 -- r2| )
<cvt> @
IF 0fah F- degfah F/ F. FALSE <cvt> !
ELSE degfah F* 0fah F+ THEN ;
: cel ( r1 -- r2| )
<cvt> @
IF 0cel F- F. FALSE <cvt> !
ELSE 0cel F+ THEN ;
.( done) cr