What are current options for plain arithmetic?

I challenge thee!

Here’s my version for the problem further below:

<<EvalThis "( ( 2 - (-3) )**2 + ( 3 - (-1) )**2 )**(1/2)">>

I think we are missing the square root maths operator.

But here is an untested example of the above; with a fictional sqr-root macro

<$let X1=4 X2=6 Y1=2 Y2=10
delta-x={{{ [<X2>subtract<X1>] }}}
delta-y={{{ [<Y2>subtract<Y1>] }}}
delta-x-squared={{{ [<delta-x>power[2]] }}}
delta-y-squared={{{ [<delta-y>power[2]] }}}
square-of-d={{{ [<delta-x-squared>add<delta-y-squared>] }}}
>

*square-of-d=<<square-of-d>>
*Distance=`<$macrocall $name=sqr-root input=<<square-of-d>>/>`
</$let>

PS I am not at all a mathematician so prone to error.

[Edit]

  • Using the filter or subfilter operators could help combining more than one “computation” into one.
  • Creating macros could also help
    • Perhaps where we pass a variable name rather than a value.
\define square(varname) {{{ [[$varname$]getvariable[]power[2]] }}}
<$let input=2>
example=<<square input>>
</$let>

Although it is an Interesting challenge.

  • In my approach there could be some useful supporting macros.
  • Thanks for reminding me about Square Root is just to the power of 0.5

So lets update the above

  • One less macro, one additional $let
  • Prefix our variables in use “P-”, so we can autolist them and their values.
  • Notice how we only display the result to a fixed number of places, the variable distance retains its precision.
<$let P-X1=4 P-X2=6 P-Y1=2 P-Y2=10
delta-x={{{ [<P-X2>subtract<P-X1>] }}}
delta-y={{{ [<P-Y2>subtract<P-Y1>] }}}
delta-x-squared={{{ [<delta-x>power[2]] }}}
delta-y-squared={{{ [<delta-y>power[2]] }}}
square-of-d={{{ [<delta-x-squared>add<delta-y-squared>] }}}
distance={{{ [<square-of-d>power[0.5]] }}}
>
<$list filter="[variables[]prefix[P-]]">
   <<currentTiddler>> = {{{ [<currentTiddler>getvariable[]] }}}<br>
</$list>
Distance = {{{ [<distance>fixed[3]] }}}
</$let>

As the example above shows with the prefix[P-]] where our variables are named with a prefix we can make use of later (suffix also simultaneously possible) demonstrates how easy it is to then leverage TiddlyWiki’s title and string handling in filters to open a whole new way of doing maths, or logic by manipulating numbers based on names. We could even lookup a tiddler/field based on a title/variable name.

  • This is all before passing parameters to macros that read like variables

Square Root is just to the power of 0.5 (aka 1/2). I wouldn’t bother going through the trouble of setting up something for square root.

If that gives the same final number as mine, then it is A-1.

Absolutely nothing wrong with what you have there. Quite nice, just way too much overhead for this kid.

In a TiddlyWiki involving a lot of equations, something meant for scientists/mathematicians/engineers, I’d much prefer stick to the short and sweet.

The ability to take a formula and apply it as is, I can’t pass that up.

But if I were a student working on a thesis or book report, I might want to break it down like you just did as part of explaining the equation. Maybe. More likely I’d stick to Distance in the Coordinate Plane | College Algebra.

Definetly something which looks more maths like works for me!

I had a spell as a cobol programer and we had auditors and business analysts that may wish to review the calculations in the code which influenced the above approach. This also is a form of self documentation and easier to maintain. By spelling out and naming intermediate results the formulae is more adaptable and the output can present the input or meaninful intermediate results.

As I hinted at there may be some innovative posibilities we are yet to explore since tiddlywiki has powerful title manipulation and the titles. Can be variable names and a matching tiddler for each variable could carry more information such as display format, relavant units, tooltips and much more.

Please understand you do what suits you. What I am trying to do is promote innovative extencible alternatives.

BASIC. My favourite right after Gupta Team Developer (aka Gupta SQLWindows.)
COBOL. Excellent for what it was made for.
Modula II. That was good stuff.
Pascal was pretty good, but annoyed me for some reason.
FORTRAN was pretty good.

Not a particular fan of any other programming languages. Hated APL, yet love TiddlyWiki filtering.

SQL is the bomb.

Sure, there are times in which heavy code is needed, but I should have been more clear about my interest here, which is all about minimalism: complex arithmetic that can be coded simply and plainly and that looks like the actual equations.

A discussion of minimalism vs voluminous and everything in between, and when any one or the other or a middlin’ one make the most sense, that’s worth of a full thread, maybe many threads.

I got what I needed out of this discussion. i.e. there was nothing out there that offered what I wanted, so I built it. And it does exactly what I want, and nothing more.

<<EvalThis "( (x2-x1)**2+(y2-y1)**2 )**(1/2)">>

That’s perfect for the things I have in mind. But just for the things I have in mind, which match formulas from text books.

The exact same mechanism with well-worded tiddler/field titles still looks good when the arithmetic isn’t too complex:

total cost = <<EvalThis "{{ item_price }} * ( 1 + ( {{( [[tax_rate]removesuffix[%]] })} / 100 ) )">>

Rock’n roll !

1 Like

BTW, with just about everything, I have a disciple of Agile Documentation (there’s some good reading in there), and I apply agile (and agile documentation, agile project management, etc. etc.) practices and principles to almost anything, right down to something as dinky as expressing an arithmetic equation.

As little as possible, only as much as needed (if it requires much, then we do much.)

ALSO: New version of that Tiddler (to handle decimal values), as per details over in this GG thread.

2 Likes

Can someone point me in the right direction?

I am trying to produce a sum of a number field from a set of tiddlers having the same tag, ie. ‘IT Items’. The field is named ‘asset-approximate-value’.

I can see two alternatives but I am sure there are many others.

  1. create a $:/ tiddler with a field to hold to sum.
  2. on my asset register tiddler, create a field ‘inventory-sum’ and store the value in there.

Either way, display the automatically calculated sum underneath the inventory list on the asset register tiddler.

I have been reviewing various math operators, eg. Add and Sum, and I can see how to add a list of numbers by specifying them as =1 =2 =3 etc and suffixing with the Add operator. However, I can not work out how to calculate the number operator of the Add statement, eg. add[4] which I presume is like the index of a one dimensional array when I do not know how many tiddlers there will be before I do a filter operation.

Also, how do I get the contents of the variable field from relevant tiddlers into the ADD or SUM statement?

I am missing something elemental here, hope someone can help.

bobj

You can use the alternative sources of numbers other than literal numbers in filters such as;

  • add<variablename>
  • add{!!fieldname}

However if you wish to retrieve and sum (or other calculations) values from multiple tiddlers have a look at the reduce operator;

The reduce operator is used to flatten a list of items down to a single item by repeatedly applying a formula. A typical use is to add up the values in a given field of a list of tiddlers.

There is also a Reduce Filter Run Prefix

Keep in mind the reduce results in a single value after applying a calculation and accumulating the value.

If for example you want to say get price and quantity and multiply them to get price and accumulate that you may also want the Map Filter Run Prefix

Filter runs used with the :map prefix should return the same number of items that they are passed.

So these calculations are all done “on the run”. You could capture the result and store it for “as at records” however what if one item changes?. You could also add each desired result to a history, like a running balance.

I have a special snapshot tool if you want to capture the result (as html in a tiddler) and only update when a refresh is desired, just ask.

The add[] filter operator expects a numeric value as its operand, and adds that value to each of the items in the current filter input. For example, a filter of {{{ 1 2 3 +[add[1]] }}} produces 2 3 4 as a result.

In contrast, the sum[] filter operator doesn’t use any operand at all. Rather, it adds together all the current filter inputs to produce a single result. For example, {{{ 1 2 3 +[sum[]] }}} produces 6 as a result.

For your use-case, where you want to total the asset-approximate-value fields from all tiddlers tagged with IT Items, you could write:

{{{ [tag[IT Items]get[asset-approximate-value]sum[]] }}}

-e

2 Likes

Thanks Eric, got it to work a treat. Simple really, like most things TW once you understand the philosophy.

bobj