truncate/floor with logBase
Alastair Reid
alastair@reid-consulting-uk.ltd.uk
30 Sep 2002 10:06:23 +0100
Alastair:
>> Rounding error I suspect.
Thorsten Stocksmeier <thorsten.stocksmeier@t-online.de>
> Okay, but is that my fault or hugs'?
Rather than assign blame, I think I'll assign homework.
http://citeseer.nj.nec.com/goldberg91what.html
This computer surveys article which discusses issues of accuracy and
the like in floating point arithmetic. I's well worth reading if you
haven't read it before.
Another detail worth being aware of is that Hugs uses single precision
arithmetic for both Float and Double.
> Independent of that: Is there a workaround? Before logBase I used
> log n / log 2 as a simulation of logBase 2. Still have to find out
> whether this solves the problem. (well maybe logBase *is* in fact
> log/log - must stick my nose into the prelude this afternoon)
If you want a log_2 function of type Int->Int, I'd write the obvious
recursive function. (Or the less obvious one-liner using iterate,
takeWhile and length.) If speed is very important, I'd probably crack
open some volume of Knuth's Art of Computer Programming which almost
certainly has a more cunniung algorithm.
--
Alastair Reid alastair@reid-consulting-uk.ltd.uk
Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/