Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell FFI / C MPFR library wrapper woes
    primarykey
    data
    text
    <p>In order to create an arbitrary precision floating point / drop in replacement for Double, I'm trying to wrap <a href="http://www.mpfr.org/" rel="nofollow noreferrer">MPFR</a> using the FFI but despite all my efforts the simplest bit of code doesn't work. It compiles, it runs, but it crashes mockingly after pretending to work for a while. A simple C version of the code happily prints the number "1" to (640 decimal places) a total of 10,000 times. The Haskell version, when asked to do the same, silently corrupts (?) the data after only 289 print outs of "1.0000...0000" and after 385 print outs, it causes an assertion failure and bombs. I'm at a loss for how to proceed in debugging this since it "should work".</p> <p>The code can be perused at <a href="http://hpaste.org/10923" rel="nofollow noreferrer">http://hpaste.org/10923</a> and downloaded at <a href="http://www.updike.org/mpfr-broken.tar.gz" rel="nofollow noreferrer">http://www.updike.org/mpfr-broken.tar.gz</a></p> <p>I'm using GHC 6.83 on FreeBSD 6 and GHC 6.8.2 on Mac OS X. Note you will need MPFR (tested with 2.3.2) installed with the correct paths (change the Makefile) for libs and header files (along with those from GMP) to successfully compile this.</p> <h2>Questions</h2> <ul> <li><p>Why does the C version work, but the Haskell version flake out? What else am I missing when approaching the FFI? I tried StablePtrs and had the exact same results.</p></li> <li><p>Can someone else verify if this is a Mac/BSD only problem by compiling and running my code? (Does the C code "works" work? Does the Haskell code "noworks" work?) Can anyone on Linux and Windows attempt to compile/run and see if you get the same results?</p></li> </ul> <p>C code: (works.c)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;gmp.h&gt; #include &lt;mpfr.h&gt; #include "mpfr_ffi.c" int main() { int i; mpfr_ptr one; mpf_set_default_prec_decimal(640); one = mpf_set_signed_int(1); for (i = 0; i &lt; 10000; i++) { printf("%d\n", i); mpf_show(one); } } </code></pre> <p>Haskell code: (Main.hs --- doesn't work)</p> <pre><code>module Main where import Foreign.Ptr ( Ptr, FunPtr ) import Foreign.C.Types ( CInt, CLong, CULong, CDouble ) import Foreign.StablePtr ( StablePtr ) data MPFR = MPFR foreign import ccall "mpf_set_default_prec_decimal" c_set_default_prec_decimal :: CInt -&gt; IO () setPrecisionDecimal :: Integer -&gt; IO () setPrecisionDecimal decimal_digits = do c_set_default_prec_decimal (fromInteger decimal_digits) foreign import ccall "mpf_show" c_show :: Ptr MPFR -&gt; IO () foreign import ccall "mpf_set_signed_int" c_set_signed_int :: CLong -&gt; IO (Ptr MPFR) showNums k n = do print n c_show k main = do setPrecisionDecimal 640 one &lt;- c_set_signed_int (fromInteger 1) mapM_ (showNums one) [1..10000] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload