A simple macro for basic arithmetic

57 views
Skip to first unread message

Charlie Veniot

unread,
Mar 12, 2022, 1:51:47 PMMar 12
to TiddlyWiki
After a bit of discussion over on TiddlyTalk, I've resolved a security issue and feel good enough about the little solution for lengthy/complex (but primitive only) arithmetic expressions.

In this sample TiddlyWiki instance, you'll find the following two tiddlers needed:

  • Eval.js (the javascript eval function, locked to only allow the characters (excluding the double quotes) "0123456789 ()+-*/%"
  • EvalThis tiddler with the EvalThis macro.

Although I can't stand javascript, I am not fussy about TiddlyWiki filter operations for lengthy/complex basic arithmetic, and not really fond of bringing in any of the powerful plugins out there when I don't need that much oomph.

Some sample macro references:

`
<<EvalThis "(100 + 2) / (9 - 1)">>

<<EvalThis "59 % 3">>

<<EvalThis "5**3">>

<<EvalThis "( {{!!a}} * {{!!b}} ) / 2">>

<$list filter="[range[25]]"> {{!!title}} / 2 = <<EvalThis "{{!!title}} / 2">><br> </$list>
`



Charlie Veniot

unread,
Mar 14, 2022, 9:44:08 PMMar 14
to TiddlyWiki
I just updated the TiddlyWiki.  The related javascript macro, I had locked it down to make sure nothing other than `"0123456789 ()+-*/%"` would be accepted, but I had totally forgotten about decimals.

So now the javascript is locked down to make sure nothing other than `"0123456789 .()+-*/%"` is accepted.

So now this works:

`<<EvalThis "{{{ [[25.9]] }}} * ( 1 + ( {{{ [[15%]removesuffix[%]] }}} / 100 ) )">>`
i.e.
`<<EvalThis "{{ item_price }} * ( 1 + ( {{( [[tax_rate]removesuffix[%]] })} / 100 ) )">>`

Charlie Veniot

unread,
Jun 25, 2022, 4:44:56 PMJun 25
to TiddlyWiki
I've never been in love with the approach of using a javascript macro to do basic arithmetic.

Since it is on my mind, I'm turfing what I did before and redoing into something that can do way more complex math, but still in a way that feels natural.

If you have suggestions, lob them over.  Eventually, I'll lob a TiddlyWiki instance out on the web for folk to play with.

Eventually, this will be one macro (TiddlyWiki au natural) to handle equation solving (i.e. show me the results of an equation) and equation building (a little bit like a calculator.)

Screenshot 2022-06-25 5.38.42 PM.png

ma...@mklauber.com

unread,
Jul 7, 2022, 10:43:03 AMJul 7
to TiddlyWiki
Not a macro, but I wrote a simple plugin to do essentially this.

https://mklauber.github.io/tw5-plugins/#%24%3A%2Fplugins%2Fmklauber%2Fmath.js which you can install from here https://mklauber.github.io/tw5-plugins/  It works like this

<$calc> {{some collection of wikitext that renders to an expression </$calc>
<$calc> 2 * 5 + {{SomeTiddler!!value1}}</$calc>

You can even do something with lists.
Total = <$calc> 0 <$list filter="[tag[Purchase]]"> + {{!!cost}}</$list></$calc>  This will add up the total of all cost fields on all tiddlers tagged Purchase.  The leading 0 is to help with the initial plus sign that's part of the list output.

Charlie Veniot

unread,
Jul 7, 2022, 5:22:24 PMJul 7
to TiddlyWiki
For anybody interested in my on-going development work on this, follow the TW “Calculator” prototype ready to play with discussion over at Talk TiddlyWiki.
Reply all
Reply to author
Forward
0 new messages