Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Approximations when converting from string to float
Date: Tue, 25 Oct 2016 20:07:02 +0200	[thread overview]
Message-ID: <6ed7708d-1e70-7072-f537-dbe305da3b09@inria.fr> (raw)
In-Reply-To: <CAFxmOcQwFS-_46nEv6btOYdK82Pj=LtP3BNTvR4zxkTkjJE0SQ@mail.gmail.com>

On 10/25/2016 06:58 PM, Tung Vu Xuan wrote:

> Thanks for suggestions. Since I am using a library for interval arithmetic
> [1], your idea becomes easier for me.
> let intv_of_big_int bi = 
>     let n = Big_int.num_bits_big_int bi in
>     if n <= 53 then 
>       let bi_float = Big_int.float_of_big_int bi in
>       {low=bi_float;high=bi_float}
>     else begin
>       let n = n - 53 in
>       (* Extract top 53 bits of x *)
>       let top = Big_int.shift_right_big_int bi n in

Yes, that's how I would go about it, too.  But probably you don't need
the interval arithmetic in the end: both "top" and "top + 1" convert
to floats without rounding (*), and the scaling by 2^n can be achieved
with "ldexp".

(*) If "top" is 2^53 - 1, "top + 1" is 2^53, which converts exactly.
Otherwise, "top + 1" has at most 53 digits and converts exactly as well.

Jacques-Henri Jourdan adds:

> But really, you should use ZArith, which is much more modern, faster
> and well-maintained than Big_int.

Agreed.  And if you must use Big_int.float_of_big_int, make sure
you're using OCaml 4.03.0 or later, because older versions were flaky.

- Xavier Leroy

  parent reply	other threads:[~2016-10-25 18:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-24 14:01 Tung Vu Xuan
2016-10-25 10:21 ` Jacques-Henri Jourdan
2016-10-25 11:20 ` Soegtrop, Michael
2016-10-25 11:24   ` Soegtrop, Michael
2016-10-25 14:19   ` Jacques-Henri Jourdan
2016-10-25 15:28     ` Soegtrop, Michael
2016-10-25 16:58       ` Tung Vu Xuan
2016-10-25 17:04         ` Jacques-Henri Jourdan
2016-10-25 18:07         ` Xavier Leroy [this message]
2016-10-26  9:38           ` Tung Vu Xuan
2016-10-26  8:08         ` Soegtrop, Michael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ed7708d-1e70-7072-f537-dbe305da3b09@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox