Version française
Home     About     Download     Resources     Contact us    
Browse thread
CAMLreturn does not work for floats between 0 and 1 ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] CAMLreturn does not work for floats between 0 and 1 ?
From: Matthieu Dubuget <matthieu.dubuget@laposte.net>
> 
> I'm sure I'm doing something bad. But I can't find out where?
> 
> The problem is that when I CAMLreturn float : r with   0. < r < 1.,
> I get 0.00000000.
> 
> double f (int i){
>   CAMLparam0 ();
>   double r; 
>   static value *cr = NULL;
>   if (cr == NULL) cr = caml_named_value("f");
>   r = Double_val(caml_callback(*cr, Val_int(i)));
>   printf("f: r(%d)=%.5f\n", i, r);
>   CAMLreturn(r);
> }

As pointed by others, CAMLreturn can only be used for values of type
"value" (i.e. long int).
In this specific case, you need register no root with the GC, so there
is no point in using CAMLparam0/CAMLreturn.

If you need to register roots with the GC in such a function
(because you're calling two caml callbacks with an allocation in
between, for instance) then you must use different macros.
In 3.10, there is a new macro CAMLreturnT for this kind of
situations.
Before that, you can also use the older interface Begin_roots/End_roots.

   value v1 = 0, v2 = 0; // v1 and v2 must be initialized
   Begin_roots2(v1, v2);
   ... v1 and v2 are registered ...
   End_roots(); // pop v1 and v2
   return r;

Jacques Garrigue