\ yuko.fs -- YUnit KOnverter \ +JMJ 2013-2014 David Meyer <papa@sdf.org> \ help-yuko -- Print module help text. : help-yuko ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO -- YUnit KOnverter" cr ." YUKO is a Forth module that provides functions for converting between" cr ." various units of measurement. Most functions take the number of" cr ." source units of a given quantity as an unsigned integer from the top" cr ." of the stack and returns the equivalent number of target units." cr ." Temperature functions take input and output as signed integers." cr ." See also: HELP-YUKO-LENGTH, HELP-YUKO-MASS, HELP-YUKO-TEMP," cr ." HELP-YUKO-TIME, HELP-YUKO-VOLUME, HELP-YUKO-PRICE" cr ; \ help-yuko-length -- Print help for length/distance conversions. : help-yuko-length ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO LENGTH/DISTANCE CONVERSIONS" cr ." Units: foot (ft), inch (in), kilometer (km), meter (m), mile (mi)," cr ." millimeter (mm)" cr ." Conversions: ft>in ft>km ft>m ft>mi ft>mm in>ft in>km in>m in>mi" cr ." in>mm km>ft km>in km>m km>mi km>mm m>ft m>in m>km m>mi m>mm mi>ft" cr ." mi>in mi>km mi>m mi>mm mm>ft mm>in mm>km mm>m mm>mi" cr ; \ help-yuko-mass -- Print help for mass/weight conversions. : help-yuko-mass ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO MASS/WEIGHT CONVERSIONS" cr ." Units: gram (g), kilogram (kg), ounce (oz), pound (lb)," cr ." troy ounce (ozt) cr ." Conversions: kg>lb kg>g lb>oz kg>oz g>oz lb>g lb>kg g>kg oz>lb oz>kg" cr ." oz>g g>lb ozt>g g>ozt" cr ; \ help-yuko-temp -- Print help for temperature conversions. : help-yuko-temp ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO TEMPERATURE CONVERSIONS" cr ." (Input and output in signed integers.)" cr ." Units: Celsius degrees (c), Fahrenheit degrees (f)" cr ." Conversions: c>f f>c" cr ; \ help-yuko-time -- Print help for time conversions. : help-yuko-time ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO TIME CONVERSIONS" cr ." Units: day, hour (hr), minute (min), second (s), year (yr)" cr ." Conversions: s>min s>hr s>day min>hr min>day min>yr hr>day hr>yr" cr ." day>yr min>s hr>s day>s hr>min day>min yr>min day>hr yr>hr yr>day" cr ." (No second <-> year conversion due to scale difference.) cr ; \ help-yuko-volume -- Print help for volume conversions. : help-yuko-volume ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO VOLUME CONVERSIONS" cr ." Units: cup, fluid ounce (floz), gallon (gal), liter (l)," cr ." milliliter (ml), pint (pt), quart (qt), tablespoon (tbsp)," cr ." teaspoon (tsp)" cr ." Conversions: cup>floz cup>gal cup>l cup>ml cup>pt cup>qt cup>tbsp" cr ." cup>tsp floz>cup floz>gal floz>l floz>ml floz>pt floz>qt floz>tbsp" cr ." floz>tsp gal>cup gal>floz gal>l gal>ml gal>pt gal>qt gal>tbsp" cr ." gal>tsp l>cup l>floz l>gal l>ml l>pt l>qt l>tbsp l>tsp ml>cup" cr ." ml>floz ml>gal ml>l ml>pt ml>qt ml>tbsp ml>tsp pt>cup pt>floz pt>gal" cr ." pt>l pt>ml pt>qt pt>tbsp pt>tsp qt>cup qt>floz qt>gal qt>l qt>ml" cr ." qt>pt qt>tbsp qt>tsp tbsp>cup tbsp>floz tbsp>gal tbsp>l tbsp>ml" cr ." tsp>qt tsp>tbsp tbsp>pt tbsp>qt tbsp>tsp tsp>cup tsp>floz tsp>gal" cr ." tsp>l tsp>ml tsp>pt" cr ; \ help-yuko-price -- Print help for price conversions. : help-yuko-price ( -- ) \ ---------1---------2---------3---------4---------5---------6---------7 cr ." YUKO PRICE CONVERSIONS" cr ." Units: USD per troy ounce (do), JPY per gram (yg)" cr ." Conversions: do>yg yg>do" cr ." (Second argument is JPY:USD exchange rate in JPY per 1000 USD.)" cr ; \ LINEAR MEASURE \ FT foot, IN inch, KM kilometer, M meter, MI mile, MM millimeter \ All values are unsigned integers. : ft>in ( u1 -- u2 ) 12 * ; : ft>km ( u1 -- u2 ) 381 1250000 */ ; : ft>m ( u1 -- u2 ) 381 1250 */ ; : ft>mi ( u1 -- u2 ) 5280 / ; : ft>mm ( u1 -- u2 ) 1524 5 */ ; : in>ft ( u1 -- u2 ) 12 / ; : in>km ( u1 -- u2 ) 127 5000000 */ ; : in>m ( u1 -- u2 ) 127 5000 */ ; : in>mi ( u1 -- u2 ) 63360 / ; : in>mm ( u1 -- u2 ) 127 5 */ ; : km>ft ( u1 -- u2 ) 1250000 381 */ ; : km>in ( u1 -- u2 ) 5000000 127 */ ; : km>m ( u1 -- u2 ) 1000 * ; : km>mi ( u1 -- u2 ) 5000000 8047863 */ ; : km>mm ( u1 -- u2 ) 1000000 * ; : m>ft ( u1 -- u2 ) 1250 381 */ ; : m>in ( u1 -- u2 ) 5000 127 */ ; : m>km ( u1 -- u2 ) 1000 / ; : m>mi ( u1 -- u2 ) 5000 8047863 */ ; : m>mm ( u1 -- u2 ) 1000 * ; : mi>ft ( u1 -- u2 ) 5280 * ; : mi>in ( u1 -- u2 ) 63360 * ; : mi>km ( u1 -- u2 ) 8047863 5000000 */ ; : mi>m ( u1 -- u2 ) 8047863 5000 */ ; : mi>mm ( u1 -- u2 ) 1609344 * ; : mm>ft ( u1 -- u2 ) 5 1524 */ ; : mm>in ( u1 -- u2 ) 5 127 */ ; : mm>km ( u1 -- u2 ) 1000000 / ; : mm>m ( u1 -- u2 ) 1000 / ; : mm>mi ( u1 -- u2 ) 1609344 / ; \ MASS/WEIGHT \ G gram, KG kilogram, LB pound, OZ ounce, OZT troy ounce \ All values unsigned integers. \ OZT conversion to/from G only. : kg>lb ( u1 -- u2 ) 100000000 45359237 */ ; : kg>g ( u1 -- u2 ) 1000 * ; : lb>oz ( u1 -- u2 ) 16 * ; : kg>oz ( u1 -- u2 ) 1600000000 45359237 */ ; : g>oz ( u1 -- u2 ) 1600000 45359237 */ ; : lb>g ( u1 -- u2 ) 45359237 100000 */ ; : lb>kg ( u1 -- u2 ) 45359237 100000000 */ ; : g>kg ( u1 -- u2 ) 1000 / ; : oz>lb ( u1 -- u2 ) 16 / ; : oz>kg ( u1 -- u2 ) 45359237 1600000000 */ ; : oz>g ( u1 -- u2 ) 45359237 1600000 */ ; : g>lb ( u1 -- u2 ) 100000 45359237 */ ; : g>ozt ( u1 -- u2 ) 10000000 311034768 */ ; : ozt>g ( u1 -- u2 ) 311034768 10000000 */ ; \ TEMPERATURE \ C degrees Celsius, F degrees Fahrenheit \ All values signed integers : c>f ( n1 -- n2 ) 9 5 */ 32 + ; : f>c ( n1 -- n2 ) 32 - 5 9 */ ; \ TIME \ S second, MIN minute, HR hour, DAY, YR year \ (No s<->yr conversion due to scale diff.) \ All values unsigned integers. : s>min ( u1 -- u2 ) 60 / ; : s>hr ( u1 -- u2 ) 3600 / ; : s>day ( u1 -- u2 ) 86400 / ; : min>hr ( u1 -- u2 ) 60 / ; : min>day ( u1 -- u2 ) 1440 / ; : min>yr ( u1 -- u2 ) 525960 / ; : hr>day ( u1 -- u2 ) 24 / ; : hr>yr ( u1 -- u2 ) 8766 / ; : day>yr ( u1 -- u2 ) 4 1461 */ ; : min>s ( u1 -- u2 ) 60 * ; : hr>s ( u1 -- u2 ) 3600 * ; : day>s ( u1 -- u2 ) 86400 * ; : hr>min ( u1 -- u2 ) 60 * ; : day>min ( u1 -- u2 ) 1440 * ; : yr>min ( u1 -- u2 ) 525960 * ; : day>hr ( u1 -- u2 ) 24 * ; : yr>hr ( u1 -- u2 ) 8766 * ; : yr>day ( u1 -- u2 ) 1461 4 */ ; \ VOLUME (LIQUIDS) \ ML milliliter TSP teaspoon TBSP tablespoon FLOZ fluid ounce \ CUP PT pint QT quart L liter GAL gallon \ All values unsigned integers. : ml>tsp ( u1 -- u2 ) 9600000 47317723 */ ; : ml>tbsp ( u1 -- u2 ) 3200000 47317723 */ ; : ml>floz ( u1 -- u2 ) 1600000 47317723 */ ; : ml>cup ( u1 -- u2 ) 200000 47317723 */ ; : ml>pt ( u1 -- u2 ) 100000 47317723 */ ; : ml>qt ( u1 -- u2 ) 50000 47317723 */ ; : ml>l ( u1 -- u2 ) 1000 / ; : ml>gal ( u1 -- u2 ) 12500 47317723 */ ; : tsp>ml ( u1 -- u2 ) 47317723 9600000 */ ; : tsp>tbsp ( u1 -- u2 ) 3 / ; : tsp>floz ( u1 -- u2 ) 6 / ; : tsp>cup ( u1 -- u2 ) 48 / ; : tsp>pt ( u1 -- u2 ) 96 / ; : tsp>qt ( u1 -- u2 ) 192 / ; : tsp>l ( u1 -- u2 ) 47317723 9600000000 */ ; : tsp>gal ( u1 -- u2 ) 768 / ; : tbsp>ml ( u1 -- u2 ) 47317723 3200000 */ ; : tbsp>tsp ( u1 -- u2 ) 3 * ; : tbsp>floz ( u1 -- u2 ) 2 / ; : tbsp>cup ( u1 -- u2 ) 16 / ; : tbsp>pt ( u1 -- u2 ) 32 / ; : tbsp>qt ( u1 -- u2 ) 64 / ; : tbsp>l ( u1 -- u2 ) 47317723 3200000000 */ ; : tbsp>gal ( u1 -- u2 ) 256 / ; : floz>ml ( u1 -- u2 ) 47317723 1600000 */ ; : floz>tsp ( u1 -- u2 ) 6 * ; : floz>tbsp ( u1 -- u2 ) 2 * ; : floz>cup ( u1 -- u2 ) 8 / ; : floz>pt ( u1 -- u2 ) 16 / ; : floz>qt ( u1 -- u2 ) 32 / ; : floz>l ( u1 -- u2 ) 47317723 1600000000 */ ; : floz>gal ( u1 -- u2 ) 128 / ; : cup>ml ( u1 -- u2 ) 47317723 200000 */ ; : cup>tsp ( u1 -- u2 ) 48 * ; : cup>tbsp ( u1 -- u2 ) 16 * ; : cup>floz ( u1 -- u2 ) 8 * ; : cup>pt ( u1 -- u2 ) 2 / ; : cup>qt ( u1 -- u2 ) 4 / ; : cup>l ( u1 -- u2 ) 47317723 200000000 */ ; : cup>gal ( u1 -- u2 ) 16 / ; : pt>ml ( u1 -- u2 ) 47317723 100000 */ ; : pt>tsp ( u1 -- u2 ) 96 * ; : pt>tbsp ( u1 -- u2 ) 32 * ; : pt>floz ( u1 -- u2 ) 16 * ; : pt>cup ( u1 -- u2 ) 2 * ; : pt>qt ( u1 -- u2 ) 2 / ; : pt>l ( u1 -- u2 ) 47317723 100000000 */ ; : pt>gal ( u1 -- u2 ) 8 / ; : qt>ml ( u1 -- u2 ) 47317723 50000 */ ; : qt>tsp ( u1 -- u2 ) 192 * ; : qt>tbsp ( u1 -- u2 ) 64 * ; : qt>floz ( u1 -- u2 ) 32 * ; : qt>cup ( u1 -- u2 ) 4 * ; : qt>pt ( u1 -- u2 ) 2 * ; : qt>l ( u1 -- u2 ) 47317723 50000000 */ ; : qt>gal ( u1 -- u2 ) 4 / ; : l>ml ( u1 -- u2 ) 1000 * ; : l>tsp ( u1 -- u2 ) 9600000000 47317723 */ ; : l>tbsp ( u1 -- u2 ) 3200000000 47317723 */ ; : l>floz ( u1 -- u2 ) 1600000000 47317723 */ ; : l>cup ( u1 -- u2 ) 200000000 47317723 */ ; : l>pt ( u1 -- u2 ) 100000000 47317723 */ ; : l>qt ( u1 -- u2 ) 50000000 47317723 */ ; : l>gal ( u1 -- u2 ) 12500000 47317723 */ ; : gal>ml ( u1 -- u2 ) 47317723 12500 */ ; : gal>tsp ( u1 -- u2 ) 768 * ; : gal>tbsp ( u1 -- u2 ) 256 * ; : gal>floz ( u1 -- u2 ) 128 * ; : gal>cup ( u1 -- u2 ) 16 * ; : gal>pt ( u1 -- u2 ) 8 * ; : gal>qt ( u1 -- u2 ) 4 * ; : gal>l ( u1 -- u2 ) 47317723 12500000 */ ; \ PRICE \ For precious metal price comparisons \ DO usd per troy oz., YG jpy per g \ (2nd argument is JPY per 1000 USD) : do>yg ( u1 u2 -- u3 ) 10000 * 311034768 */ ; : yg>do ( u1 u2 -- u3 ) 311034768 swap 10000 * */ ; cr ." Type 'help-yuko' for help"