rpn is a lightweight Javascript macro for basic math operations in TiddlyWiki. It is inspired by formerly popular calculators using Reverse Polish Notation (see Wikipedia for more info).
<<rpn 50 2.5 * 2 p>> output: 125.00
The plugin available on https://tid.li/tw5/plugins.html includes some documentation. Additional test and demos are on https://tid.li/tw5/hacks.html#rpnTest.
===
As always: Make a backup first and let me know how it goes.
Suggestions for optimisations are always welcome but might need some time.
Happy calculations,
Thomas
... I showed my little pony some new tricks.
Pony Weight in Kgs = (heart-girth2 cms x length-from-shoulder-to-tip-of-buttocks cms) / 11,880
Note: the small "2" on "heart-girth" means "squared". **
Joe Bloggs, British, weighs 10 Stone, 3.5 Pounds.
What weight is Joe Bloggs in Kilos? ... given ...
1 Stone = 14 Pounds
1 Kilo = 2.20462 Pounds\define sqr(x) <$macrocall $name="rpn" a=$x$ b=$x$ operation="*"/>
\define pony(hg,len)
<$wikify name=hg2 text="<<rpn $hg$ $hg$ *>>">
<$wikify name=hg2len text='<$macrocall $name="rpn" a=<<hg2>> b=$len$ operation="*"/>'>
<$wikify name=weight text='<$macrocall $name="rpn" a=<<hg2len>> b="11800" operation="/" decimals=2/>'>
<$macrocall $name="rpn" a=<<weight>> b=" kgs" operation="&" />
</$wikify></$wikify></$wikify>
\end
<<pony 100 200>>
I notice that horse calculations tack on 50lbs.
The only use for these measurements that comes to mind is possibly loading a horse trailer
\define pounds2kgs(lbs) <<rpn $lbs$ 2.20462 / 2>>
\define stones2pounds()
<$wikify name="stonelbs" text='<$macrocall $name="rpn" a=$(stones)$ b=14 operation="*"/>'>
<$wikify name="totlbs" text='<$macrocall $name="rpn" a=<<stonelbs>> b=$(pounds)$ operation="+" />'>
$(name)$ weighs <$macrocall $name=pounds2kgs lbs=<<totlbs>>/> kgs
</$wikify>
</$wikify>
\end
!! Anachronistic Weight Calculator
Name of person: <$edit-text tiddler="temp/stone/person" tag="input" size=50 default="Enter Name"/><br/>
Weight, stones: <$edit-text tiddler="temp/stone/stones" tag="input" size=10 placeholder="Weight in stones" default="0"/><br/>
Weight, pounds: <$edit-text tiddler="temp/stone/pounds" tag="input" size=10 placeholder="Weight in pounds" default="0"/>
<$vars name={{temp/stone/person}} stones={{temp/stone/stones}} pounds={{temp/stone/pounds}}>
<<stones2pounds>>
</$vars>
The metric weight of <$edit-text tiddler="$:/temp/british-weight-converter" field="text" tag="input" size="3"/> ''kg'' is equal to:
<$macrocall $name=rpn a={{$:/temp/british-weight-converter}} b=2.20462 operation=* locale=en-GB/> pounds
<$macrocall $name=rpn a={{$:/temp/british-weight-converter}} b=<<rpn 2.20462 14 />> operation=* decimals=3 locale=en-GB/> stones\define stones() <$macrocall $name=rpn a={{$:/temp/british-weight-converter}} b=<<rpn 2.20462 14 / >> operation=*/>
The metric weight of
<$edit-text tiddler="$:/temp/british-weight-converter" field="text" tag="input" size="3"/> ''kg'' is equal to:
<$wikify name=toPounds text="<$macrocall $name=rpn a=<<stones>> b=1 operation=%/>">
<$macrocall $name=rpn a=<<stones>> b=0 precision=f/> stones <$macrocall $name=rpn a=<<toPounds>> b=14 operation=* decimals=1/> pounds
</$wikify>Thomas Elmiger asked:
Is your test case pointing to the fact, that instead of fractions of stones, the calculator should show pounds?
Thomas Elmiger wrote:
.... Josiah, it would be a great success for me personally if you would build a converter for the other direction ...
Easy things first. Here’s a unidirectional converter for two units in three lines:The metric weight of <$edit-text tiddler="$:/temp/british-weight-converter" field="text" tag="input" size="3"/> ''kg'' is equal to:
<$macrocall $name=rpn a={{$:/temp/british-weight-converter}} b=2.20462 operation=* locale=en-GB/> pounds
<$macrocall $name=rpn a={{$:/temp/british-weight-converter}} b=<<rpn 2.20462 14 />> operation=* decimals=3 locale=en-GB/> stones
Thomas Elmiger wrote:
.... Josiah, it would be a great success for me personally if you would build a converter for the other direction ...
Eeek! Give me a week.
''Stones''
<$edit-text tiddler="$:/temp/stone-converter" field="text" tag="input" size="4"/> stones = <$macrocall $name=rpn a={{$:/temp/stone-converter}} b=6.35029 operation=* decimals=2 /> kilograms = <$macrocall $name=rpn a={{$:/temp/stone-converter}} b=14 operation=* decimals=2 locale=en-GB/> pounds
Then I attempted a Celsius to Fahrenheit conversion and failed. It needs two steps and I couldn't work out how to do that.
The °C -> °F is: T(°F) = T(°C) × 1.8 + 32
\define charLimit() 140
\define charCount(tid) <$editor-counter tiddler="""$tid$"""/>
''Text''
<$edit-text tiddler="$:/temp/text-to-count-down" field="text" rows="3" minHeight="3em" autoHeight="yes" class="te-vt-note-field"/>
<$reveal type=nomatch state="$:/temp/text-to-count-down" text="">
<$wikify name=count text='<$macrocall $name="charCount" tid="$:/temp/text-to-count-down"/>'>
<$macrocall $name=rpn a=<<charLimit>> b=<<count>> operation=-/>
</$wikify>
</$reveal>
<style>
.te-vt-note-field {
width: 100%;
}
</style>