Greetings all,
I am trying to improve my simple invoice system made with TiddlyWiki and came across this mathematical oddity and am trying to determine if this is just one of those “javascript math precision” oddities or a bug in the filter code. These are two examples of a filtered transclusion to get a bunch of billable entries associated with an invoice number, multiply some billable hours by some rate, then add them all up and calculate the tax using a <<tax_rate>> variable:
$<$text text={{{[tag[billable_entry]!has[draft.of]field:invoice_no{!!invoice_no}] :reduce[get[hours]multiply{!!rate}multiply<tax_rate>fixed[2]add<accumulator>]}}}/>
Sometimes gives a wonky number that is more than 2 decimal places … not what I would expect from adding a bunch of numbers with 2 decimal places.
$<$text text={{{[tag[billable_entry]!has[draft.of]field:invoice_no{!!invoice_no}] :reduce[get[hours]multiply{!!rate}multiply<tax_rate>add<accumulator>fixed[2]]}}}/>
Moving fixed to the end fixes the non-rounding but this one sometimes (for a tax_rate=0.1, for instance) gives a number off by a cent due to accumulated numeric cruft after the second decimal place that then gets rounded.
$<$text text={{{[tag[billable_entry]!has[draft.of]field:invoice_no{!!invoice_no}] :reduce[get[hours]multiply{!!rate}add<accumulator>] +[multiply<tax_rate>fixed[2]]}}}/>
It shouldn’t matter if I multiply each one by the tax rate and then add them up or add them up and then multiply the subtotal by the tax rate. However, This last one gives what I would expect the answer to be.
I’ve kind of solved my problem by using the last example but I was just throwing it out there for folks more intimately knowledgeable about how javascript handles precision to see if this was expected behaviour or something faulty going on inside the reduce run.
Thanks,
/Mike