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

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