Trunc operator returning "0" on a "4.5" input

Hello all,

I have those two procedures:

\procedure calc.basicspeed.from.cp() <$let base={{{ [<DX>add<HT>divide[4]] }}}> <$text text={{{ [<currentTiddler>get[basic-speed-cp]divide[5]trunc[]multiply[0.25]add<base>] }}}/>

\procedure calc.basicmove.from.cp() <$let base={{{ [<calc.basicspeed.from.cp>trunc[]] }}}> <$text text={{{ [<currentTiddler>get[basic-move-cp]divide[5]trunc[]add<base>] }}}/>

The first one returns a value of 4.5 in a test example, working as intended.

The second should return 5, but returns 1. For some reason that completely evades me, the trunc[] operator is the culprit, since I get 0 for <base> with it, while I get 4.5 for <base> without it.

I’m flabbergasted :sweat_smile:

It’s really hard to confirm or deny here since there are so many values that we haven’t been given. Consider providing some sample tiddlers, variable values or even a separate site where we can look at what’s actually happen.

I think at least part of your problem is that you’re trying to use procedures (wikitext, not wikified or otherwise evaluated before substitution) as numeric values in a filter when you should really be using functions (filters that do get fully evaluated, designed to be used as variables). Try this instead and let us know if you’re still having issues:

\function calc.basicspeed.from.cp() [<currentTiddler>get[basic-speed-cp]divide[5]trunc[]multiply[0.25]] [<DX>add<HT>divide[4]] +[sum[]]

\function calc.basicmove.from.cp() [<currentTiddler>get[basic-move-cp]divide[5]trunc[]] [<calc.basicspeed.from.cp>trunc[]] +[sum[]]

Here I’ve just moved the values from your $let definitions into the primary filter and replaced add with +[sum[]]. Note that we can also eliminate the $text widgets, since functions display their first result as plain text by default.

Edit: Since multiply[0.25] and divide[4] are equivalent, you should be able to simplify calc.basicspeed.from.cp a bit by moving divide[4] into the final filter run, as follows, so each input value will get divided before they’re both summed:

\function calc.basicspeed.from.cp() [<currentTiddler>get[basic-speed-cp]divide[5]trunc[]] [<DX>add<HT>] +[divide[4]sum[]]

Similarly, we can simplify calc.basicmove.from.cp by moving the shared trunc[] into the final run:

\function calc.basicmove.from.cp() [<currentTiddler>get[basic-move-cp]divide[5]] [<calc.basicspeed.from.cp>] +[trunc[]sum[]]

4 Likes

Hey @Mark_S ! I should have mentioned, I’m usually syncing at https://rahan-testing.tiddlyhost.com/. The functions/procedures I was on in this case where in the Character macros tiddler, being tested in the Character tiddler.

This indeed works with the function refactor, but I suspect the use of the sum operator is also better than my obsession with the add one :slight_smile:

However, I think the trunc cannot be syndicated and shared in the final run of calc.basicmove.from.cp, because it could increment values if both runs fractional part would have cumulated to more than a fraction; but regardless, thanks for the help and working solution!

Unless I’m misunderstanding, this shouldn’t be an issue: trunc[] will be applied to each part individually before they’re summed, not after, so you shouldn’t end up with any rounding issues.