bash, bc modulo does not work with -l flag

Saad Attieh picture Saad Attieh · Dec 14, 2014 · Viewed 7.7k times · Source

So I am trying to use bc to calculate some logarithms but I also need to use it to calculate the modulus for something. Whilst making my script, I launched bc to test it.

Without any flags, bc <<< "3%5" of course returns 3.

But with bc -l (loads math library so I can compute logarithms) any calculation of a%b returns 0 where a and b can be any number but 0.

What's happening?

Answer

gniourf_gniourf picture gniourf_gniourf · Dec 14, 2014

That's because, from the manual:

   expr % expr
          The result of the expression is the "remainder" and it  is  com‐
          puted  in  the following way.  To compute a%b, first a/b is com‐
          puted to scale digits.  That result is used to compute a-(a/b)*b
          to  the scale of the maximum of scale+scale(b) and scale(a).  If
          scale is set to zero and  both  expressions  are  integers  this
          expression is the integer remainder function.

When you run bc with the -l flag, scale is set to 20. To fix this:

bc -l <<< "oldscale=scale; scale=0; 3%5; scale=oldscale; l(2)"

We first save scale in variable oldscale, then set scale to 0 to perform some arithmetic operations, and to compute a ln we set scale back to its old value. This will output:

3
.69314718055994530941

as wanted.