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
|
feedback
|
|
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 by-product 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. | |||||||||||||||
feedback
|