caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Is this a bug in Num.float_of_num ?
@ 2016-05-26  8:24 Mohamed Iguernlala
  2016-05-26  8:57 ` Xavier Leroy
  0 siblings, 1 reply; 3+ messages in thread
From: Mohamed Iguernlala @ 2016-05-26  8:24 UTC (permalink / raw)
  To: caml-list

Hi,

I came to the following "stange" behavior when using the function
float_of_num. I don't know if this should be considered as a bug,
since this function has no specification (in num.mli *)

The float "f = 6004799503160661. /. 18014398509481984." has an
exact representation on 64 bits. Its value is

0.333333333333333314829616256247390992939472198486328125

However, when using float_of_num to compute the float corresponding to
the number above represented as a num, the result is slightly different.
In fact, the value returned for the expression

fnum = Num.float_of_num (Num.num_of_string 
"6004799503160661/18014398509481984")

is

0.333333333333000025877623784253955818712711334228515625

I tried to investigate the reason of this difference. It seems
that it is due to the (default) value of the constant
"floating_precision"[1] in file arith_flags.ml of num library: it
is equal to 12 instead of 54 (I think) for 64bits standard floats
representation.

How should this situation be interpreted ?

(a) Should one explicitly modify the value of the
reference "floating_precision" before calling function
float_of_num ?

(b) Should the function be parametrized by a precision (which is
  better than modifying global references like in (a) ) ?

(c) Other suggestion / solution ?

I attach a standalone file with relevant parts of Nums, Ratio,
Arith_flags, and Int_misc to reproduce this behavior and to
easily play with the value of "floating_precision".

Regards,
Mohamed Iguernlala.

[1] 
https://github.com/ocaml/ocaml/blob/4.03.0/otherlibs/num/arith_flags.ml#L22

PS. I have not seen any constant in arith_flags.ml about the min/max 
value of
the exponent ...

-- 
Senior R&D Engineer, OCamlPro
Research Associate, VALS team, LRI.
http://www.iguer.info
https://fr.linkedin.com/in/mohamed-iguernlala-71515979


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Is this a bug in Num.float_of_num ?
  2016-05-26  8:24 [Caml-list] Is this a bug in Num.float_of_num ? Mohamed Iguernlala
@ 2016-05-26  8:57 ` Xavier Leroy
  2016-05-26  9:16   ` Mohamed Iguernlala
  0 siblings, 1 reply; 3+ messages in thread
From: Xavier Leroy @ 2016-05-26  8:57 UTC (permalink / raw)
  To: caml-list

On 26/05/2016 10:24, Mohamed Iguernlala wrote:
> Hi,
> 
> I came to the following "stange" behavior when using the function
> float_of_num. I don't know if this should be considered as a bug,
> since this function has no specification (in num.mli *)
> 
> The float "f = 6004799503160661. /. 18014398509481984." has an
> exact representation on 64 bits. Its value is
> 
> 0.333333333333333314829616256247390992939472198486328125
> 
> However, when using float_of_num to compute the float corresponding to
> the number above represented as a num, the result is slightly different.

float_of_ratio and float_of_num are very imprecise in OCaml 4.02 and
earlier.  They were reimplemented in OCaml 4.03 in a way that should
give correct roundings.  So, make sure you're using 4.03, and if the
bug is still there, please file a report on Mantis.

Best,

- Xavier Leroy

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Is this a bug in Num.float_of_num ?
  2016-05-26  8:57 ` Xavier Leroy
@ 2016-05-26  9:16   ` Mohamed Iguernlala
  0 siblings, 0 replies; 3+ messages in thread
From: Mohamed Iguernlala @ 2016-05-26  9:16 UTC (permalink / raw)
  To: Xavier Leroy, caml-list

Le 26/05/2016 10:57, Xavier Leroy a écrit :
> On 26/05/2016 10:24, Mohamed Iguernlala wrote:
>> Hi,
>>
>> I came to the following "stange" behavior when using the function
>> float_of_num. I don't know if this should be considered as a bug,
>> since this function has no specification (in num.mli *)
>>
>> The float "f = 6004799503160661. /. 18014398509481984." has an
>> exact representation on 64 bits. Its value is
>>
>> 0.333333333333333314829616256247390992939472198486328125
>>
>> However, when using float_of_num to compute the float corresponding to
>> the number above represented as a num, the result is slightly different.
> float_of_ratio and float_of_num are very imprecise in OCaml 4.02 and
> earlier.  They were reimplemented in OCaml 4.03 in a way that should
> give correct roundings.  So, make sure you're using 4.03, and if the
> bug is still there, please file a report on Mantis.
Thanks, the result is correct with OCaml 4.03.0.

I hadn't tried before because the constant "let floating_precision = ref 12"
is still there in OCaml 4.03.0 ... Sorry.

Regards,

Mohamed.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-05-26  9:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-26  8:24 [Caml-list] Is this a bug in Num.float_of_num ? Mohamed Iguernlala
2016-05-26  8:57 ` Xavier Leroy
2016-05-26  9:16   ` Mohamed Iguernlala

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).