[TW5] Updated: rpn – Plugin for calculations, random numbers and text concatenation

286 views
Skip to first unread message

Thomas Elmiger

unread,
Aug 14, 2017, 11:50:32 AM8/14/17
to TiddlyWiki
Hi all

After seeing some interest and other applications (Mat’s GRAPHS) using my little plugin for calculations, I showed my little pony some new tricks.

Besides much better structured code in JS I am especially content with wikification of input (operands) and output (concatenated text) as well as number formatting using locale standard (see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString for more info).

Here is in short what rpn can do for you in version 0.6.2:

===

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

  • takes two operands (a and b) as input – this may be macros or transclusions
    • operands are sent through a simplified wikify function before calculation starts
    • use pi to get π (Math.PI)
  • basic operations: +, -, /, *
    • advanced operations: modulus (%) and rounding (optional: Math.floor or Math.ceil)
  • output formatting
    • limit digits after comma (decimals), force precision display with precision:"p" (.toPrecision)
    • locale formatting (.toLocaleString)
  • string concatenation (&)
    • the resulting string is sent through the internal wikify function
  • random numbers in a range from a to b with precision:"r" (including both a and b)

Installation


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


@TiddlyTweeter

unread,
Aug 14, 2017, 12:25:24 PM8/14/17
to TiddlyWiki
Ciao Thomas

Did you know that a "Pony" is £25 British Pounds in Cockney rhyming slang (also a "Macaroni" is £25), whilst an "Apple Core" (a score) is only £20 ? Well worth it for your doings.

London East End
Josiah

Thomas Elmiger wrote:
... I showed my little pony some new tricks.

@TiddlyTweeter

unread,
Aug 16, 2017, 11:16:57 AM8/16/17
to tiddl...@googlegroups.com
Ciao Thomas

Originally I wasn't really interested in RPN because I'm still looking for something simple that can add up a column in a table without having to have a Tiddler per row.

However RPN its growing on me as a way to do simple calculations. Obvious applications are, for instance, a measures convertor (°F -> °C; Euros to Dollars etc)

Could it, for instance, calculate the weight of your pony? :-)

Just for fun here is the formula for estimating the weight of a pony ...

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". **

Best wishes
Josiah

P.S. ** I'm not making this up. The horse weight calculation thing is here: http://www.thehorse.com/articles/27936/estimating-horse-weight-accurately

@TiddlyTweeter

unread,
Aug 16, 2017, 11:47:57 AM8/16/17
to TiddlyWiki
Ciao Thomas

A lot of British, Australians & Americans & others still think in Imperial Measures.

One of the most mutually confusing things between Brits & mainland Europeans is many British still weigh themselves in "Stones" and pounds, not Kilos.

Can RPN provide a converter back and forth between British Stones and Euro Kilos? What would the solution look like?

Here is a test case...

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

Love to the pony. Happy weighty calculations :-)
Josiah

@TiddlyTweeter

unread,
Aug 16, 2017, 12:00:06 PM8/16/17
to TiddlyWiki
Ciao Thomas

A small footnote & I'll shut up ... This example I understand but its not illuminating ...

Most readers would think, well "why not just write the sentence?" ... I think you need a more complex concatenation.

Best wishes
Josiah
Auto Generated Inline Image 1

Mark S.

unread,
Aug 16, 2017, 12:21:32 PM8/16/17
to TiddlyWiki
I'm not sure if this actually works, but it seems to give the right answer:

\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>>

Only the wikify widget made this straight-forward.

I guessed at the pony measurements. Suspect the girth is too small. I notice that horse calculations tack on 50lbs.

Mark

Mark S.

unread,
Aug 16, 2017, 12:22:26 PM8/16/17
to TiddlyWiki
I think it's demonstrating that "Pi" is handled differently in concatenation mode.

@TiddlyTweeter

unread,
Aug 16, 2017, 12:47:10 PM8/16/17
to TiddlyWiki
Ciao Mark S. & Thomas

Great stuff. I changed the test values to match slightly better actual horses I know to check. Its accurate enough for what is a guesstimate in any case. The formulae differ between sites slightly.

What surprised me was the extent of the code you had to write to do that. It was for more complex than I ever imagined it would need be. But then I don't really understand coding.

Best wishes
Josiah

@TiddlyTweeter

unread,
Aug 16, 2017, 1:05:01 PM8/16/17
to TiddlyWiki
Ciao Mark S.

Small side footnote ...


I notice that horse calculations tack on 50lbs.

 Its true that in guesstimating horse weight from tape measures in actual stables there is a kind of rule-of-thumb to "add on a few pounds" the larger and older the horse is. The downside is that if you make that rule-of-thumb into a codification in computer calculations its starts drifting off from guesstimate to stab-in-the-dark. I don't think that formula would well for tiny Shetland Ponies, for instance. But mostly its in the ballpark.

Josiah

Mark S.

unread,
Aug 16, 2017, 1:31:14 PM8/16/17
to TiddlyWiki
The existing formula assumes that the horse is a cylinder of flesh. To make it more accurate, you would want to do the estimates for the head and legs separately. I'm pretty sure you could make a separate calculation based on girth for the additive head and legs (the head and legs would have a higher percentage bone mass) and get within 10%.

The only use for these measurements that comes to mind is possibly loading a horse trailer and you need to make sure you're within limits but don't need to be overly precise.

Ok, now what about the load capacity of an African swallow ...

Mark

@TiddlyTweeter

unread,
Aug 16, 2017, 1:47:12 PM8/16/17
to TiddlyWiki
The only use for these measurements that comes to mind is possibly loading a horse trailer

There is that. But calculating the rough main body weight bulk is important in racing. TALL horses that are also lean race faster. Its a but more complicated than just weight as "hands" (height) come into it. Hands are a proxy measure of stride. Its the stride to bulk/weight ratio that is likely the Holy Grail in all this.

On to the African Swallow ...

Josiah

Mark S.

unread,
Aug 16, 2017, 3:09:38 PM8/16/17
to TiddlyWiki
Anachronistic weight conversion

\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>





Thomas Elmiger

unread,
Aug 16, 2017, 3:14:11 PM8/16/17
to TiddlyWiki
Ciao Josiah and Mark

Yeehaw! – Your feedback’s worth in stones would be enough to build a house! Thank you very much, this will at least improve my docs by several miles ;–)


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

Josiah, it would be a great success for me personnally if you would build a converter for the other direction or even a pony weight estimator based on this example.
Is your test case pointing to the fact, that instead of fractions of stones, the calculater should show pounds? That would require some additional lines of code …

Happy conversions,
Thomas

---

''Important!'' Shortcut macro calls in <<syntax>> are not dynamic and get updated only through re-rendering e.g. when a tiddler is saved or loaded:

<<rpn {{$:/temp/british-weight-converter}} 2.20462 * locale:en-GB>> pounds

Thomas Elmiger

unread,
Aug 16, 2017, 5:21:13 PM8/16/17
to TiddlyWiki
Pretty cool, Mark!

The other direction:

\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>

… the shortest solution that came to my mind.

Now I need a rest (i fear dreaming about African Swallows swallowing ponies).

Good night!
Thomas

@TiddlyTweeter

unread,
Aug 17, 2017, 5:31:13 AM8/17/17
to TiddlyWiki
Ciao Thomas (& Mark S.) ...

Thomas Elmiger asked:
Is your test case pointing to the fact, that instead of fractions of stones, the calculator should show pounds?

In actual verbal usage Brits are more likely to say "I'm 10 stone and a-half" or "I'm 10 stone and a-quarter"" than "I'm 10 stone and 7 pounds" or "I'm 10 stone and 3 and a-half pounds". Either with pounds, or without, would work. The user could figure it out.

The decimal places for weight, both straight pounds & stones practically would be better rounded to one place on this. Verbal expression of weight wouldn't bother with fractional values beyond that.

For North Americans having weight, as you do, in pounds is spot-on. They don't use stones at all.

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.


Thomas Elmiger wrote:
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

Tchüss
Josiah

@TiddlyTweeter

unread,
Aug 19, 2017, 6:47:59 AM8/19/17
to TiddlyWiki
Ciao Thomas

Surprisingly, I found it not too difficult. Result below ...

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

Love to the pony. Best wishes,
Josiah


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

 

Mat

unread,
Aug 19, 2017, 9:52:50 AM8/19/17
to TiddlyWiki
@TiddlyTweeter wrote:

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

Maybe you guys are doing some kind of challenge with special restriction rules, but otherwise I think it can be done like so:


\define CtoF(C) <$macrocall $name=rpn a=<<rpn $C$ 1.8 *>> b=32 operation=+ />


This should work (didn't test it) because Thomas made the rpn macro conveniently wikifies the output. Otherwise, one would have to split out that inner macro call and use a surrounding setwidget or wikifywidget to assignt a variable with the value of the split out macro... and only then use that variable in the macrocallwidget.


<:-)

@TiddlyTweeter

unread,
Aug 19, 2017, 9:55:09 AM8/19/17
to TiddlyWiki
Ciao Thomas

Did you know there are 33.814022558919 PONY to the liter? https://www.convertunits.com/from/pony/to/liter

Josiah

Thomas Elmiger

unread,
Aug 19, 2017, 5:51:56 PM8/19/17
to TiddlyWiki
Ciao Josiah and Mat

Both your solutions work as expected. Great!

Here’s a reward for Josiah for doing his homework (requires my EditorCounter):

\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>


Have a nice sunday everyone!
Thomas

I think I’ll get a pony of Grappa now.
Reply all
Reply to author
Forward
0 new messages