\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)$>>
\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
<$edit-text tiddler="temp/calculation" tag="input"/><$macrocall $name=calculator input={{temp/calculation}}/>
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 : )
See the calculator in TW-Scripts
No worry at all, your ragsculator was added see
TW-Scripts is belong to the whole community!
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!
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
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 1 | field 2 | macro output | |
---|---|---|---|
zz test pg1 | 12 | 24 | 36 |
zz test pg2 | 27 | 18 | 45 |
zz test pg3 | 72 | 18 | 90 |
zz test pg4 | 11 | 11 | |
zz test pg5 | -5 | -25 | -30 |
zz test pg6 | 12 | -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.