# How to use BigFloats with ccall?

**URL:** <https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895>\
**Category:** General Usage\
**Tags:** ccall, bigfloat\
**Created:** [October 12, 2023, 12:36pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895 "2023-10-12T12:36:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [October 12, 2023, 12:36pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/1 "2023-10-12T12:36:51Z")

</div>

I want to call a C library that uses MPFR in my Julia code. The issue is, I can’t make it work: if I manipulate a BigFloat declared in Julia within the C code, it becomes NaN. Here is an example:

```cpp
#include <gmp.h>
#include <mpfr.h>

void add_val(mpfr_t a)
{
  mpfr_t tmp;
  mpfr_init2(tmp, 42);
  mpfr_add(a, a, tmp, MPFR_RNDD);
}

```

which I compile with

```bash
gcc -O2 -fPIC -Wno-unused-result -c file.c -o file.o
gcc -O2 -fPIC -Wno-unused-result -shared file.o -o lib.so -lmpfr -lgmp

```

and call in:

```julia
using Libdl

lib = Libdl.dlopen("./lib.so")
symb = Libdl.dlsym(lib, :add_val)

a = BigFloat(2.)
ccall(symb, Cvoid, (Ref{BigFloat},), a)

```

After this snippet of code, `a` is `NaN`. Is there something I am doing wrong?  
(I am a very new user of MPFR, and don’t know it very well)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 12, 2023, 12:46pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/2 "2023-10-12T12:46:47Z")

</div>

I don’t see an obvious problem with your code — that’s how [`base/mpfr.jl`](https://github.com/JuliaLang/julia/blob/e949236f29347f3e57a1a3a7d5ec656872ad8347/base/mpfr.jl) does it.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [October 12, 2023, 1:31pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/3 "2023-10-12T13:31:21Z")

</div>

`mpfr_init2(tmp, 42)` initializes a mpfr number with 42bits precision, and sets its value to NaN.  
You need to assign a value to it first, e.g. set it to `10` with

```julia
mpfr_set_si(tmp, 10, MPFR_RNDN)

```

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [October 12, 2023, 1:54pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/4 "2023-10-12T13:54:20Z")

</div>

This worked, thanks! So the problem was a lack of doc reading for me.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 12, 2023, 1:59pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/5 "2023-10-12T13:59:32Z")

</div>

I don’t know, and you seem to have an answer already, I just wanted to propose look into ArbNumerics.jl It’s ArbFloat type is faster then Julia’s BigFloat/MPFR.

At least for many things and not just because the default is 106 bits, also faster at any precision, e.g. Julia’s default 256 bits. Why I found it intriguing that you chose 42 bits. Why do different libraries choose their defaults, e.g. Julia 256, and why you 42?

Note, you could use the underlying C library of ArbNumerics.jl if you need to.

Also if you’re calling to C, from Julia, does it make sense to send down the Julia type or get Julia types back?

I would like to know why you prefer MPFR, since I’ve suggested it be dropped from Julia.

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [October 12, 2023, 7:35pm UTC](https://discourse.julialang.org/t/how-to-use-bigfloats-with-ccall/104895/6 "2023-10-12T19:35:05Z")

</div>

Thanks for the suggestion! I am however limited, as the library I want to use only supports MPFR, therefore I need to do this with BigFloats. The `42` bits precision was completely arbitrary, and since I’m new to all of this I can’t tell why certain people make certain choices…

> [@Palli](#):
>
> Also if you’re calling to C, from Julia, does it make sense to send down the Julia type or get Julia types back?

I’m not sure I understood this part… From what I understand, `mpfr_t` corresponds exactly to `Ref{BigFloat}`, so I don’t see the problem.
