simple math macro

293 views
Skip to first unread message

A Gloom

unread,
Oct 21, 2019, 9:05:33 PM10/21/19
to TiddlyWiki
\define ragscalculator2(cnumb1,copera,cnumb2)
<$list filter=[[$cnumb1$]$copera$[$cnumb2$]]><<currentTiddler>></$list>
\end

<<ragscalculator2 (1st number) (math operator) (2nd number)>>


ragscalculator2 : : macro name
(1st number) : first number of your math equation
(math operator) : the math operation you want preformed
for + : use "add"
for - : use "subtract"
for x : use "multiply"
for ÷ : use "divide"
(2nd number) : second number of your math equation


<<ragscalculator2 2 subtract 2>>


produces just the solution to the equation/a number


0


Can be expanded for more numbers/equation steps and using the other math filter operators Tiddlywiki provides (abs, add, ceil, divide, exponential, fixed, floor, max, maxall, min, minall, multiply, negate, precision, product, remainder, round, sign, subtract, sum, trunc, untrunc)

As with the calculator tiddler, the macro can be used with variables for input from other sources.


<ragscalculator $(num1variable)$ $(operatorvariable)$ $(num2variable)$>>

math macro.tid

Mohammad

unread,
Oct 22, 2019, 1:29:41 AM10/22/19
to TiddlyWiki
Hi A Gloom

This is a variation of your simple macro. People with regex knowledge can write simpler and better parser to use TW math operators for a wikitext calculator.

\define calc(ops, operation)
<$list filter="[<__input__>splitbefore[$ops$]removesuffix[$ops$]]" variable=a>
<$list filter="[<__input__>removeprefix<a>removeprefix[$ops$]]" variable=b> 
<$list filter="[<a>$operation$<b>]" variable=result>
<<result>>
</$list>
</$list>
</$list>
\end

\define calculator(input)
<$vars pattern="^\d+\s?[\+\-\*\/]\s?\d+$">
<$list filter="[<__input__>regexp<pattern>]" variable=ignore emptyMessage="Wrong input"><!--check input -->
<$list filter="[<__input__>search[+]]">
<<calc ops:+ operation:add>>
</$list>

<$list filter="[<__input__>search[-]]">
<<calc ops:- operation:subtract>>
</$list>

<$list filter="[<__input__>search[*]]">
<<calc ops:* operation:multiply>>
</$list>

<$list filter="[<__input__>search[/]]">
<<calc ops:"/" operation:divide>>
</$list>
</$list>
</$vars>
\end

!! Examples

```
<<calculator 123+365>>
<<calculator 123-365>>
<<calculator 123*365>>
<<calculator 123/365>>
<<calculator 4+3>>
```

will produce

<<calculator 123+365>>
<<calculator 123-365>>
<<calculator 123*365>>
<<calculator 123/365>>
<<calculator -4>>


488

-242

44895

0.336986301369863

Wrong input 

Mohammad

unread,
Oct 22, 2019, 1:39:09 AM10/22/19
to TiddlyWiki
Considering the above post (simple calculator), then in the sidebar you can try this

<$edit-text tiddler="temp/calculation" tag="input"/><$macrocall $name=calculator input={{temp/calculation}}/>

It will give you a simple binary operations calculator!
If a parser using regexp is developed to parse (expression like 12 + 36*56 /36 +96) then you have a simple sidebar calculator!

Mohammad

unread,
Oct 22, 2019, 3:15:41 AM10/22/19
to TiddlyWiki


On Tuesday, October 22, 2019 at 8:59:41 AM UTC+3:30, Mohammad wrote:

A Gloom

unread,
Oct 22, 2019, 3:33:22 AM10/22/19
to TiddlyWiki
Mohammad,

Nice work! I shall be studying this, definitely something for your TWScripts-- feel free to borrow anything from mine (like the calculator graphic design) for yours : )

A Gloom

unread,
Oct 22, 2019, 3:37:12 AM10/22/19
to TiddlyWiki
Mohammad,
lol you posted that as I was posting you should add it to TWScripts

Mohammad

unread,
Oct 22, 2019, 5:19:17 AM10/22/19
to tiddl...@googlegroups.com
Hi again,

 Sure the idea is yours! :-)

No worry at all, your ragsculator was added see

Your idea and work is also acknowledged

TW-Scripts is belong to the whole community!

Cheers
Mohammad

A Gloom

unread,
Oct 22, 2019, 10:29:53 PM10/22/19
to TiddlyWiki
Mohammsd,
TYVM!
We here were pleasantly surprised to see it called the Rags Calculator : )  (rags is the prefix I use for macros and other system tidd's used in my personal wiki which is for a trilogy called Of Rags and Shadows, a Tolkien like story written for a child so it has sentimental value), Amusingly, it has modifired tags pills which ending up being called RagTag.

TW-Scripts is belong to the whole community!

Yep, thats why I attach tid's to posts-- for TWScripts and the community.  You have kept what Tobias started with TB5 (collecting tricks and solutions developed in the Group) alive and well : )

@TiddlyTweeter

unread,
Oct 23, 2019, 6:04:55 AM10/23/19
to tiddl...@googlegroups.com
Very nice stuff.

One thought. Is there a way that you could take the RESULT of a first operation and use that number for the INPUT number of a second step ... and so on?

Hope this is clear!

Best wishes
TT 

A Gloom

unread,
Oct 23, 2019, 11:37:43 PM10/23/19
to TiddlyWiki
One thought. Is there a way that you could take the RESULT of a first operation and use that number for the INPUT number of a second step ... and so on?

Hope this is clear!

I believe so, make a first calculaton, save result to a temp tidd, start a 2nd calculation drawing the first number from the temp tdd and just manually entering math function and second number-- could be repeated on and on...

A Gloom

unread,
Oct 29, 2019, 7:54:55 AM10/29/19
to TiddlyWiki
math macro for multiple add, subtract, etc as detailed in the official examples of the math operators

[=1 =2 =3 =4 +[add[4]]

x - <$list filter="=1 =2 =3 =4 +[add[4]]"><<currentTiddler>></$list>

z - <<ragscalculator3 "=1 =2 =3 =4" add 4>>

\define ragscalculator3(cnumbm1,coperam,cnumbm2)
<$list filter="$cnumbm1$ +[$coperam$[$cnumbm2$]]"><<currentTiddler>></$list>
\end

<<ragscalculator3 "cnumbm1" coperam cnumbm2 >>
<<ragscalculator3 "=1 =2 =3 =4" add 4>>

ragscalculator3 : macro name
cnumbm1 : a series of numbers to have the final number of the macro added, etc to each
--- must be in " ", each number should have an "=" before it
coperam : the math operation you want preformed

for + : use "add"
for - : use "subtract"
for x : use "multiply"
for ÷ : use "divide"
cnumbm2: single number to be added to each of the cnumbm1 numbers

problem : the output doesn't have spaces between numbers of the final result

working on chaining the different basic math operators

with the RagsCalculator, it now has a Mn button which takes the result of the first operation and places it at the start of second operateion where a new math function and new second number can be entered for a new math operation.  This can be repeated again with a thied (and more) using the result from the previous operation.

A Gloom

unread,
Oct 29, 2019, 9:35:29 PM10/29/19
to TiddlyWiki

BOTH THIS MACRO AND SIMPLE EQUATION MACRO CAN ACCEPT NEGATIVE NUMBERS
just type -number, ie: -1

FIX FOR DISPLAY ISSUE:

math macro for multiple add, subtract, etc as detailed in the official examples of the math operators

x - [=1 =2 =3 =4 +[add[4]]

x - 5678


z - <<ragscalculator3 "=1 =2 =3 =4" add 4>>


without delimiter marks:
ragscalculator3 - 5678


with delimiter marks:
ragscalculator3separate - 5 | 6 | 7 | 8 |


list:
ragscalculator3list -

5

6

7

8


\define ragscalculator3(cnumbm1,coperam,cnumbm2)
<$list filter="$cnumbm1$ +[$coperam$[$cnumbm2$]]"><<currentTiddler>></$list>
\end
\define ragscalculator3separate(cnumbm1,coperam,cnumbm2)
<$list filter="$cnumbm1$ +[$coperam$[$cnumbm2$]]"> <<currentTiddler>> |</$list>
\end
\define ragscalculator3list(cnumbm1,coperam,cnumbm2)
<$list filter="$cnumbm1$ +[$coperam$[$cnumbm2$]]">

<<currentTiddler>></$list>
\end

<<ragscalculator3 "cnumbm1" coperam cnumbm2 >>
<<ragscalculator3separate "cnumbm1" coperam cnumbm2 >>
<<ragscalculator3list "cnumbm1" coperam cnumbm2 >>

<<ragscalculator3 "=1 =2 =3 =4" add 4>>
<<ragscalculator3seperate "=1 =2 =3 =4" add 4>>
<<ragscalculator3list "=1 =2 =3 =4" add 4>>


ragscalculator3 : macro name cnumbm1 : a series of numbers to have the final number of the macro added, etc to each, use "-number" for negative numbers

— must be in " ", each number should have an "=" before it

coperam : the math operation you want preformed

for + : use "add"

for - : use "subtract"

for x : use "multiply"

for ÷ : use "divide"

cnumbm2: single number to be added to each of the cnumbm1 numbers


ATTACHMENT FOR TWSCRIPTS

math macro string of values.tid

A Gloom

unread,
Nov 3, 2019, 6:33:25 PM11/3/19
to TiddlyWiki
HOW TO USE MATH MACRO WITH FIELDS:

As with the calculator tiddler, the macro can be used with variables for input from other sources.


<ragscalculator $(num1variable)$ $(operatorvariable)$ $(num2variable)$>>


macro with values 32, 64 typed in:


<<ragscalculator2 32 add 64>>


= 96


values 32, 64 read from fields:


<$set name="n1var" value={{!!filter-field1}}> <$set name="n2var" value={{!!filter-field2}}> <<ragscalculator2 $(n1var)$ add $(n2var)$>> </$set></$set>


96


macro in a list generating a table, reading values from fields:


<table> <tr><th></th><th>field 1</th><th>field 2</th><th>macro output</th></tr> <$list filter="[prefix[zz test pg]]"> <$set name="n1var" value={{!!filter-field1}}> <$set name="n2var" value={{!!filter-field2}}> <tr><td>{{!!title}}</td><td>{{!!filter-field1}}</td><td><$view field="filter-field2"/></td> <td><<ragscalculator2 $(n1var)$ add $(n2var)$>></td></tr></$set></$set></$list></table>


field 1field 2macro output
zz test pg11224 36
zz test pg22718 45
zz test pg37218 90
zz test pg4
11 11
zz test pg5-5-25 -30
zz test pg612-12 0


To expand this for exponents, ie: 4 to the 4th power, a macro


<<tothepower basenumber multiply powernumber>>


could take "basenumber" (four) for both numbers in a multiply filter, save the result to a field/variable to insert into another multiply filter using the variable for both numbers of the filter, repeating "powernumber" number of steps.

Reply all
Reply to author
Forward
0 new messages