Wow, you have a tough mission if you want to replicate the bit level answers for double (btw, hi Jacob).<br>Libraries differ for transcendental function, and even worse, CPUs differ. You may get different answers on an Intel and and AMD.<br>
That said, I think your best bet is to import log2 and log10 from C and use those.<br><br>Haskell sadly lacks an efficient way to go from a Double to its bit pattern. :(<br><br> -- Lennart<br><br><div class="gmail_quote">
On Thu, Mar 13, 2008 at 12:35 AM, Jacob Schwartz <<a href="mailto:quark@bluespec.com">quark@bluespec.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have two questions about using the Double data type and the<br>
operations in the Floating typeclass on a computer that uses IEEE<br>
floating point numbers.<br>
<br>
I notice that the Floating class only provides "log" (presumably log<br>
base 'e') and "logBase" (which, in the latest source that I see for<br>
GHC is defined as "log y / log x"). However, in C, the "math.h"<br>
library provides specific "log2" and "log10" functions, for extra<br>
precision. A test on IEEE computers (x86 and x86-64), shows that for<br>
a range of 64-bit "double" values, the answers in C do differ (in the<br>
last bit) if you use "log2(x)" and "log10(x)" versus "log (x) /<br>
log(2)" and "log(x) / log(10)".<br>
<br>
I am under the restriction that I need to write Haskell programs using<br>
Double which mimic existing C/C++ programs or generated data sets, and<br>
get the same answers. (It's silly, but take it as a given<br>
requirement.) If the C programs are using "log2", then I need "log2"<br>
in the Haskell, or else I run the risk of not producing the same<br>
answers. My first thought is to import "log2" and "log10" through the<br>
FFI. I was wondering if anyone on Haskell-Cafe has already done this<br>
and/or has a better suggestion about how to get specialized "log2" and<br>
"log10" (among the many specialized functions that the "math.h"<br>
library provides, for better precision -- for now, I'm just concerned<br>
with "log2" and "log10").<br>
<br>
My second question is how to get at the IEEE bit representation for a<br>
Double. I am already checking "isIEEE n" in my source code (and<br>
"floatRadix n == 2"). So I know that I am operating on hardware that<br>
implements floating point numbers by the IEEE standard. I would like<br>
to get at the 64 bits of a Double. Again, I can convert to a CDouble<br>
and use the FFI to wrap a C function which casts the "double" to a<br>
64-bit number and returns it. But I'm wondering if there's not a<br>
better way to do this natively in Haskell/GHC (perhaps some crazy use<br>
of the Storable typeclass?). Or if someone has already tackled this<br>
problem with FFI, that would be interesting to know.<br>
<br>
Thanks.<br>
<br>
Jacob<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>