Re: truncate/floor with logBase

Hans Aberg <[email protected]>
Newsgroups gmane.comp.lang.haskell.hugs.bugs
Message-ID <v03110701b9bdc22cd2a8@[195.100.226.142]>
At 08:08 +0200 2002/09/30, Thorsten Stocksmeier wrote:
>Main> floor (logBase 2 (fromInteger 32768)) -- should be == floor 15.0
>> > 14
>>
>>Rounding error I suspect.
>>
>>Prelude> logBase 2 (fromInteger 32768) - 15.0
>>-9.53674e-07
>
>Okay, but is that my fault or hugs'?

Any code that treats floating point numbers as though they are exact
numbers is faulty.

>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 need to compare floating point numbers x, y, introduce a small
epsilon > 0, and write abs(x - y) < epsilon instead of x == y. If you need
them to be exact integers, make a roundoff. Etc.

  Hans Aberg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.