[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates



On 2008-08-24 12:25:00 -0500, Ron Johnson wrote:
> I was thinking that integer division might result in the occasional  
> rounding error.

This is the contrary: AFAIK, you really want an integer division.
So, integer arithmetic is OK. But a floating-point division can
be a problem since when the floating-point result is displayed,
it may be rounded to the next integer if at most 4 digits are
displayed after the decimal point, the worst case being:

$ echo 86399/86400 | bc -l
.99998842592592592592

For instance, under a shell that supports FP arithmetic (e.g. zsh):

$ printf "%.4f\n" $((86399./86400))
1.0000

Now, assuming you want to use floating-point arithmetic, before
displaying the result, you may want to take the floor of the result:

vin% zmodload zsh/mathfunc
vin% printf "%.0f\n" $((floor(86399./86400)))
0
vin% printf "%.0f\n" $((floor(86400./86400)))
1

And I proved that under some conditions (which are satisfied here),
one gets the same result as the integer division:

  http://www.vinc17.org/research/papers/rr_intdiv.pdf

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)


Reply to: