Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 51095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:41:46+00:00 2026-05-10T16:41:46+00:00

In order to create an arbitrary precision floating point / drop in replacement for

  • 0

In order to create an arbitrary precision floating point / drop in replacement for Double, I’m trying to wrap MPFR 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’.

The code can be perused at http://hpaste.org/10923 and downloaded at http://www.updike.org/mpfr-broken.tar.gz

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.

Questions

  • 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.

  • 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?

C code: (works.c)

#include <stdio.h>   #include <stdlib.h>   #include <string.h>    #include <gmp.h>   #include <mpfr.h> #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 < 10000; i++)     {         printf('%d\n', i);       mpf_show(one);     }   }   

Haskell code: (Main.hs — doesn’t work)

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 -> IO ()   setPrecisionDecimal                     :: Integer -> IO ()   setPrecisionDecimal decimal_digits = do       c_set_default_prec_decimal (fromInteger decimal_digits)    foreign import ccall 'mpf_show'      c_show                               :: Ptr MPFR -> IO ()    foreign import ccall 'mpf_set_signed_int'      c_set_signed_int                     :: CLong -> IO (Ptr MPFR)    showNums k n = do      print n      c_show k    main = do      setPrecisionDecimal 640      one <- c_set_signed_int (fromInteger 1)      mapM_ (showNums one) [1..10000]   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:41:47+00:00Added an answer on May 10, 2026 at 4:41 pm

    I see the problem too, on a

    $ uname -a Linux burnup 2.6.26-gentoo-r1 #1 SMP PREEMPT Tue Sep 9 00:05:54 EDT 2008 i686 Intel(R) Pentium(R) 4 CPU 2.80GHz GenuineIntel GNU/Linux $ gcc --version gcc (GCC) 4.2.4 (Gentoo 4.2.4 p1.0) $ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.3 

    I also see the output changing from 1.0000…000 to 1.0000…[garbage].

    Let’s see, the following does work:

    main = do     setPrecisionDecimal 640     mapM_ (const $ c_set_signed_int (fromInteger 1) >>= c_show) [1..10000] 

    which narrows down the problem to parts of one being somehow clobbered during runtime. Looking at the output of ghc -C and ghc -S, though, isn’t giving me any hints.

    Hmm, ./noworks +RTS -H1G also works, and ./noworks +RTS -k[n]k, for varying values of [n], demonstrate failures in different ways.

    I’ve got no solid leads, but there are two possibilities that jump to my mind:

    • GMP, which the GHC runtime uses, and MPFR having some weird interaction
    • stack space for C functions called within the GHC runtime is limited, and MPFR not dealing well

    That being said… is there a reason you’re rolling your own bindings rather than use HMPFR?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer More specifically in your case, document.body.scrollTop. However, that's pretty IE-specific,… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer You need to specify what it is you would specifically… May 11, 2026 at 11:55 pm
  • Editorial Team
    Editorial Team added an answer To clarify: you are referring to the args argument you… May 11, 2026 at 11:55 pm

Related Questions

I am working on a 'print preview' facility to show an overview of a
I have a requirement on my current project (a Flex app which will be
I'm developing a system (with Rails 2.3.2, Ruby 1.8.7-p72) that has a sizable reporting
I have an assortment of database objects (tables, functions, views, stored procedures) each scripted

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.