If the scale is other than zero, calculations with %, such as 3%2 and 46%4, tend to output 0. How is the algorithm designed with the scale other than 0?
bc
scale=10
print 4%3 // output 0
If the scale is other than zero, calculations with %, such as 3%2 and 46%4, tend to output 0. How is the algorithm designed with the scale other than 0?


The command manual says this about how BC calculates the modulo:
EDIT: I looked at the source code for GNU BC and found that the mod operator extends the division operator. In other words, the modulo is calculated as a byproduct of the division. It relies on integer division to calculate the modulo. When scale is set, however integer division does not take place.
Try this in BC:
you should get:
Now let's plug in these figures the way BC does. The manual says it uses a(a/b)*b to calculate. Let's plug in our two results, the one resulting from integer division and the one with a
Without integer division:
This is why scale must be set to 0 for the modulo to work properly. There are other much more advanced tools that are free and open source for this purpose, and I recommend you use them. 


user272970's answer is great. Here's a tweak to it:
This (using I added these functions to p.s. 


I solved it this way: integerdefine int(x) { oldscale=scale; scale=0; x=x/1; scale=oldscale; return( x ); } modulodefine mod(x,y) { oldscale=scale; scale=1000; x = x  y * int(x/y); scale=oldscale; return( x ); } HTH 


define mod(x,base){oldscale=scale; scale=0; result=x%base; scale=oldscale; return result }
– Hello World Oct 12 '14 at 12:16