caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Float precision in OCaml
@ 2017-08-01 10:47 Viet Le
  2017-08-01 11:18 ` Nicolás Ojeda Bär
  2017-08-01 12:12 ` Daniel Bünzli
  0 siblings, 2 replies; 18+ messages in thread
From: Viet Le @ 2017-08-01 10:47 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 889 bytes --]

Hi all,

I'm writing a JSON parser in pure OCaml and have encountered an issue with
small float values:

10 zeros in between
# 1.00000000001e-312 ;;
- : float = 1.00000000001e-312

11 zeros in between
# 1.000000000001e-312 ;;
- : float = 1.00000000000341e-312

# 5e-324 ;;
- : float = 4.94065645841e-324


I haven't found precise limit, but as a rule of thumb (*not precise*), for
a positive float value to keep its precision, it *should* not be smaller
than 1.00000000001e-312. To use JSON as precise serializer, it would be
necessary to preserve accuracy.

I checked https://github.com/ocaml/Zarith and it supports only big int &
quotients, not floating point.

For values smaller than the limit above, should I just treat as 2 values:
(normalized: float, exponent: float), so we will have:

5e-324 -> (5, -324)

Comments and suggestions are appreciated. Thanks.

-- 
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 1757 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 10:47 [Caml-list] Float precision in OCaml Viet Le
@ 2017-08-01 11:18 ` Nicolás Ojeda Bär
  2017-08-01 11:25   ` Frédéric Bour
                     ` (2 more replies)
  2017-08-01 12:12 ` Daniel Bünzli
  1 sibling, 3 replies; 18+ messages in thread
From: Nicolás Ojeda Bär @ 2017-08-01 11:18 UTC (permalink / raw)
  To: Viet Le; +Cc: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1388 bytes --]

Dear Viet,

I am not sure this is an issue with OCaml (as you can verify using your
favourite C compiler).  Rather, I think IEEE 754 double-precision binary
floating-point numbers can only represent numbers between 10^{-308} and
10^308 with full decimal digits precision. Numbers smaller than that can
only be represented with reduced precision.

Best wishes,
Nicolas

On Tue, Aug 1, 2017 at 12:47 PM, Viet Le <vietlq85@gmail.com> wrote:

> Hi all,
>
> I'm writing a JSON parser in pure OCaml and have encountered an issue with
> small float values:
>
> 10 zeros in between
> # 1.00000000001e-312 ;;
> - : float = 1.00000000001e-312
>
> 11 zeros in between
> # 1.000000000001e-312 ;;
> - : float = 1.00000000000341e-312
>
> # 5e-324 ;;
> - : float = 4.94065645841e-324
>
>
> I haven't found precise limit, but as a rule of thumb (*not precise*),
> for a positive float value to keep its precision, it *should* not be
> smaller than 1.00000000001e-312. To use JSON as precise serializer, it
> would be necessary to preserve accuracy.
>
> I checked https://github.com/ocaml/Zarith and it supports only big int &
> quotients, not floating point.
>
> For values smaller than the limit above, should I just treat as 2 values:
> (normalized: float, exponent: float), so we will have:
>
> 5e-324 -> (5, -324)
>
> Comments and suggestions are appreciated. Thanks.
>
> --
> Kind regards,
> Viet
>

[-- Attachment #2: Type: text/html, Size: 2633 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 11:18 ` Nicolás Ojeda Bär
@ 2017-08-01 11:25   ` Frédéric Bour
  2017-08-01 11:45     ` Viet Le
  2017-08-01 11:40   ` François Bobot
  2017-08-01 11:42   ` Viet Le
  2 siblings, 1 reply; 18+ messages in thread
From: Frédéric Bour @ 2017-08-01 11:25 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2162 bytes --]

The Doubledouble[1] module from my grenier library implements 106-bit 
precision floating point. See 
https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic 
for more information.

But this is not a serious solution, if you want to preserve json numbers 
then you should represent them more or less literally (up to what you 
consider "worth" preserving), not by encoding them as IEEE 754 double.

[1]: 
https://github.com/let-def/grenier/blob/master/doubledouble/doubledouble.mli


On 01/08/2017 13:18, Nicolás Ojeda Bär wrote:
> Dear Viet,
>
> I am not sure this is an issue with OCaml (as you can verify using 
> your favourite C compiler).  Rather, I think IEEE 754 double-precision 
> binary floating-point numbers can only represent numbers between 
> 10^{-308} and 10^308 with full decimal digits precision. Numbers 
> smaller than that can only be represented with reduced precision.
>
> Best wishes,
> Nicolas
>
> On Tue, Aug 1, 2017 at 12:47 PM, Viet Le <vietlq85@gmail.com 
> <mailto:vietlq85@gmail.com>> wrote:
>
>     Hi all,
>
>     I'm writing a JSON parser in pure OCaml and have encountered an
>     issue with small float values:
>
>     10 zeros in between
>     # 1.00000000001e-312 ;;
>     - : float = 1.00000000001e-312
>
>     11 zeros in between
>     # 1.000000000001e-312 ;;
>     - : float = 1.00000000000341e-312
>
>     # 5e-324 ;;
>     - : float = 4.94065645841e-324
>
>
>     I haven't found precise limit, but as a rule of thumb (*/not
>     precise/*), for a positive float value to keep its precision, it
>     */should/* not be smaller than 1.00000000001e-312. To use JSON as
>     precise serializer, it would be necessary to preserve accuracy.
>
>     I checked https://github.com/ocaml/Zarith
>     <https://github.com/ocaml/Zarith> and it supports only big int &
>     quotients, not floating point.
>
>     For values smaller than the limit above, should I just treat as 2
>     values: (normalized: float, exponent: float), so we will have:
>
>     5e-324 -> (5, -324)
>
>     Comments and suggestions are appreciated. Thanks.
>
>     -- 
>     Kind regards,
>     Viet
>
>


[-- Attachment #2: Type: text/html, Size: 5377 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 11:18 ` Nicolás Ojeda Bär
  2017-08-01 11:25   ` Frédéric Bour
@ 2017-08-01 11:40   ` François Bobot
  2017-08-01 11:42   ` Viet Le
  2 siblings, 0 replies; 18+ messages in thread
From: François Bobot @ 2017-08-01 11:40 UTC (permalink / raw)
  To: caml-list

Le 01/08/2017 à 13:18, Nicolás Ojeda Bär a écrit :
> I am not sure this is an issue with OCaml (as you can verify using your favourite C compiler).

Indeed, float_of_string is just strtod in the decimal case.

> Rather, I think IEEE 754 double-precision binary floating-point numbers can only represent numbers
> between 10^{-308} and 10^308 with full decimal digits precision. Numbers smaller than that can only
> be represented with reduced precision.

I don't know what you mean by full decimal digits precision, but the reduced precision is 
everywhere. For example: 2.**150.+.1. = 2.**150.


Best,

-- 
François

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 11:18 ` Nicolás Ojeda Bär
  2017-08-01 11:25   ` Frédéric Bour
  2017-08-01 11:40   ` François Bobot
@ 2017-08-01 11:42   ` Viet Le
  2017-08-01 11:48     ` François Bobot
  2 siblings, 1 reply; 18+ messages in thread
From: Viet Le @ 2017-08-01 11:42 UTC (permalink / raw)
  To: Nicolás Ojeda Bär; +Cc: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 2014 bytes --]

Thanks Nicolas. I did check against C++ before writing the email and I can
see OCaml is consistent with C++. I'm aware about IEEE 754. I'm seeking
opinion for maintaining precision using 2 values as shown.

On the other hand I think I should relax this requirement and let my JSON
parser accept this case because realistically I don't see anyone would use
numbers at +/- E308.

Thanks


On Tue, 1 Aug 2017 at 12:18, Nicolás Ojeda Bär <nicolas.ojeda.bar@lexifi.com>
wrote:

> Dear Viet,
>
> I am not sure this is an issue with OCaml (as you can verify using your
> favourite C compiler).  Rather, I think IEEE 754 double-precision binary
> floating-point numbers can only represent numbers between 10^{-308} and
> 10^308 with full decimal digits precision. Numbers smaller than that can
> only be represented with reduced precision.
>
> Best wishes,
> Nicolas
>
> On Tue, Aug 1, 2017 at 12:47 PM, Viet Le <vietlq85@gmail.com> wrote:
>
>> Hi all,
>>
>> I'm writing a JSON parser in pure OCaml and have encountered an issue
>> with small float values:
>>
>> 10 zeros in between
>> # 1.00000000001e-312 ;;
>> - : float = 1.00000000001e-312
>>
>> 11 zeros in between
>> # 1.000000000001e-312 ;;
>> - : float = 1.00000000000341e-312
>>
>> # 5e-324 ;;
>> - : float = 4.94065645841e-324
>>
>>
>> I haven't found precise limit, but as a rule of thumb (*not precise*),
>> for a positive float value to keep its precision, it *should* not be
>> smaller than 1.00000000001e-312. To use JSON as precise serializer, it
>> would be necessary to preserve accuracy.
>>
>> I checked https://github.com/ocaml/Zarith and it supports only big int &
>> quotients, not floating point.
>>
>> For values smaller than the limit above, should I just treat as 2 values:
>> (normalized: float, exponent: float), so we will have:
>>
>> 5e-324 -> (5, -324)
>>
>> Comments and suggestions are appreciated. Thanks.
>>
>> --
>> Kind regards,
>> Viet
>>
>
> --
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 3648 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 11:25   ` Frédéric Bour
@ 2017-08-01 11:45     ` Viet Le
  0 siblings, 0 replies; 18+ messages in thread
From: Viet Le @ 2017-08-01 11:45 UTC (permalink / raw)
  To: Frédéric Bour, caml-list

[-- Attachment #1: Type: text/plain, Size: 2322 bytes --]

Thanks Frederic. I will relax the condition for now as I don't know
practical use for such need yet.

On Tue, 1 Aug 2017 at 12:25, Frédéric Bour <frederic.bour@lakaban.net>
wrote:

> The Doubledouble[1] module from my grenier library implements 106-bit
> precision floating point. See
> https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic
> for more information.
>
> But this is not a serious solution, if you want to preserve json numbers
> then you should represent them more or less literally (up to what you
> consider "worth" preserving), not by encoding them as IEEE 754 double.
>
> [1]:
> https://github.com/let-def/grenier/blob/master/doubledouble/doubledouble.mli
>
> On 01/08/2017 13:18, Nicolás Ojeda Bär wrote:
>
> Dear Viet,
>
> I am not sure this is an issue with OCaml (as you can verify using your
> favourite C compiler).  Rather, I think IEEE 754 double-precision binary
> floating-point numbers can only represent numbers between 10^{-308} and
> 10^308 with full decimal digits precision. Numbers smaller than that can
> only be represented with reduced precision.
>
> Best wishes,
> Nicolas
>
> On Tue, Aug 1, 2017 at 12:47 PM, Viet Le <vietlq85@gmail.com> wrote:
>
>> Hi all,
>>
>> I'm writing a JSON parser in pure OCaml and have encountered an issue
>> with small float values:
>>
>> 10 zeros in between
>> # 1.00000000001e-312 ;;
>> - : float = 1.00000000001e-312
>>
>> 11 zeros in between
>> # 1.000000000001e-312 ;;
>> - : float = 1.00000000000341e-312
>>
>> # 5e-324 ;;
>> - : float = 4.94065645841e-324
>>
>>
>> I haven't found precise limit, but as a rule of thumb (*not precise*),
>> for a positive float value to keep its precision, it *should* not be
>> smaller than 1.00000000001e-312. To use JSON as precise serializer, it
>> would be necessary to preserve accuracy.
>>
>> I checked https://github.com/ocaml/Zarith and it supports only big int &
>> quotients, not floating point.
>>
>> For values smaller than the limit above, should I just treat as 2 values:
>> (normalized: float, exponent: float), so we will have:
>>
>> 5e-324 -> (5, -324)
>>
>> Comments and suggestions are appreciated. Thanks.
>>
>> --
>> Kind regards,
>> Viet
>>
>
>
> --
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 5965 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 11:42   ` Viet Le
@ 2017-08-01 11:48     ` François Bobot
  0 siblings, 0 replies; 18+ messages in thread
From: François Bobot @ 2017-08-01 11:48 UTC (permalink / raw)
  To: caml-list

Le 01/08/2017 à 13:42, Viet Le a écrit :
> Thanks Nicolas. I did check against C++ before writing the email and I can see OCaml is consistent
> with C++. I'm aware about IEEE 754. I'm seeking opinion for maintaining precision using 2 values as
> shown.
>

Doesn't JSON specifically precise that number are doubles? So the meaning is the one of the rounded 
value.

 > 4.3.20 Number value
 > primitive value corresponding to a double‐precision 64‐bit binary format IEEE 754‐2008 value


-- 
François

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 10:47 [Caml-list] Float precision in OCaml Viet Le
  2017-08-01 11:18 ` Nicolás Ojeda Bär
@ 2017-08-01 12:12 ` Daniel Bünzli
  2017-08-01 12:56   ` Viet Le
                     ` (3 more replies)
  1 sibling, 4 replies; 18+ messages in thread
From: Daniel Bünzli @ 2017-08-01 12:12 UTC (permalink / raw)
  To: Viet Le, caml-list

On 1 August 2017 at 12:47:37, Viet Le (vietlq85@gmail.com) wrote:

> I haven't found precise limit, but as a rule of thumb (*not precise*), for
> a positive float value to keep its precision, it *should* not be smaller
> than 1.00000000001e-312. To use JSON as precise serializer, it would be
> necessary to preserve accuracy.

Here are a few tips: 

1. The latest RFC recommends [1] to support the range of IEEE 754 double precision numbers (those are OCaml's float) in JSON numbers. However since you are converting your floating point representation from binary to a decimal one you can't guarantee to round trip your floats with a JSON number.

2. If you want to serialize precise integers with good interop you can do so using JSON number if they are in the range [-2^53;2^53] (the range written in the mentioned RFC seems wrong to me). Those are the integers a double precision floating point numbers are able to represent precisely.

3. If you want to serialize precise floats you should do this as a JSON string using a lossless textual notation for your binary floating point number. Before OCaml 4.03 you can use this [2] function after 4.03 you can use float_of_string and the "%h" format string [3].

Best,

Daniel

[1] https://tools.ietf.org/html/rfc7159#section-6
[2] http://erratique.ch/software/gg/doc/Gg.Float.html#VALpp
[3] https://github.com/ocaml/ocaml/pull/268

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 12:12 ` Daniel Bünzli
@ 2017-08-01 12:56   ` Viet Le
       [not found]   ` <etPan.59807b8a.db32dee.123@AirmailxGenerated.am>
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 18+ messages in thread
From: Viet Le @ 2017-08-01 12:56 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 2452 bytes --]

Thanks Daniel and François for pointing out the RFC
https://tools.ietf.org/html/rfc7159 and mentioning IEEE 754-2008 binary64.
For my JSON parser I use Int64 to store integers and float to store
decimal/floating point values. The tweak gives nice feature and helps with
round-trip encoding/decoding.

Since you mentioned the RFC and it states:

"A JSON

   number such as 1E400 or 3.141592653589793238462643383279 may indicate
   potential interoperability problems, since it suggests that the
   software that created it expects receiving software to have greater
   capabilities for numeric magnitude and precision than is widely

   available."

I will relax the condition for now. I used the benchmark provided by Milo
Yip:
https://github.com/miloyip/nativejson-benchmark/blob/master/data/roundtrip/roundtrip24.json

Thanks,
Viet


On 1 August 2017 at 13:12, Daniel Bünzli <daniel.buenzli@erratique.ch>
wrote:

> On 1 August 2017 at 12:47:37, Viet Le (vietlq85@gmail.com) wrote:
>
> > I haven't found precise limit, but as a rule of thumb (*not precise*),
> for
> > a positive float value to keep its precision, it *should* not be smaller
> > than 1.00000000001e-312. To use JSON as precise serializer, it would be
> > necessary to preserve accuracy.
>
> Here are a few tips:
>
> 1. The latest RFC recommends [1] to support the range of IEEE 754 double
> precision numbers (those are OCaml's float) in JSON numbers. However since
> you are converting your floating point representation from binary to a
> decimal one you can't guarantee to round trip your floats with a JSON
> number.
>
> 2. If you want to serialize precise integers with good interop you can do
> so using JSON number if they are in the range [-2^53;2^53] (the range
> written in the mentioned RFC seems wrong to me). Those are the integers a
> double precision floating point numbers are able to represent precisely.
>
> 3. If you want to serialize precise floats you should do this as a JSON
> string using a lossless textual notation for your binary floating point
> number. Before OCaml 4.03 you can use this [2] function after 4.03 you can
> use float_of_string and the "%h" format string [3].
>
> Best,
>
> Daniel
>
> [1] https://tools.ietf.org/html/rfc7159#section-6
> [2] http://erratique.ch/software/gg/doc/Gg.Float.html#VALpp
> [3] https://github.com/ocaml/ocaml/pull/268
>



-- 
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 4029 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
       [not found]   ` <etPan.59807b8a.db32dee.123@AirmailxGenerated.am>
@ 2017-08-01 13:17     ` Daniel Bünzli
  0 siblings, 0 replies; 18+ messages in thread
From: Daniel Bünzli @ 2017-08-01 13:17 UTC (permalink / raw)
  To: Viet Le; +Cc: OCaml Mailing List

On 1 August 2017 at 14:56:23, Viet Le (vietlq85@gmail.com) wrote:
> Thanks Daniel and François for pointing out the RFC
> https://tools.ietf.org/html/rfc7159 and mentioning IEEE 754-2008 binary64.
> For my JSON parser I use Int64 to store integers and float to store
> decimal/floating point values. The tweak gives nice feature and helps with
> round-trip encoding/decoding.

I wouldn't do this; you'll get into interoperability problems, other parsers (e.g. JSON.parse) won't be able to read all the ints you are serialize via these Int64.t values, they will be represented unprecisely. Stick to [-2^53;2^53] for ints and use floats to represent them, this will force users of the codec to think about the problem. 

Here's what parsing 2^53+1 gives in my browser:

 JSON.parse('9007199254740993')
 9007199254740992

If you want to represent full Int64.t values in JSON choose a textual representation for it and serialize to a string.

Best, 

Daniel

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

* RE: [Caml-list] Float precision in OCaml
  2017-08-01 12:12 ` Daniel Bünzli
  2017-08-01 12:56   ` Viet Le
       [not found]   ` <etPan.59807b8a.db32dee.123@AirmailxGenerated.am>
@ 2017-08-01 13:57   ` Soegtrop, Michael
  2017-08-01 14:07     ` Soegtrop, Michael
  2017-08-01 15:48     ` Peter Thiemann
       [not found]   ` <etPan.5980c173.1234cb4.123@AirmailxGenerated.am>
  3 siblings, 2 replies; 18+ messages in thread
From: Soegtrop, Michael @ 2017-08-01 13:57 UTC (permalink / raw)
  To: Daniel Bünzli, Viet Le, caml-list

Dear Daniel,

> 1. The latest RFC recommends [1] to support the range of IEEE 754 double
> precision numbers (those are OCaml's float) in JSON numbers. However since
> you are converting your floating point representation from binary to a decimal
> one you can't guarantee to round trip your floats with a JSON number.

I don't agree with this. It is possible to have a decimal representation of double precision numbers, which guarantees identity (maybe up to NaN and Inf variants) when serialized and desrialized. The point is that the example

4.94065645841e-324

Is probably a correctly rounded decimal representation of a precise double precision floating point number. There has been a lengthy discussion on the Maxima list on what floating point numbers mean and the final consensus there was that each floating point number is precisely an integer times a power of 2, even a denormalized one. Just that you can't convert decimal presentations exactly to floating point numbers doesn't mean that there is something like an imprecise floating point number, there are just imprecise representations of decimals numbers in floating point numbers.

Said that, one can define a mapping from floating point numbers to usual decimal string representations and back such that each floating point number results in a unique string. It is just quite tricky to get it right without extended precision arithmetic, though.

As far as I know the Lisp standard requires that certain floating point formats have this property (I need to check). One might be able to learn from them how to do this properly.

Best regards,

Michael
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

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

* RE: [Caml-list] Float precision in OCaml
  2017-08-01 13:57   ` Soegtrop, Michael
@ 2017-08-01 14:07     ` Soegtrop, Michael
  2017-08-01 15:48     ` Peter Thiemann
  1 sibling, 0 replies; 18+ messages in thread
From: Soegtrop, Michael @ 2017-08-01 14:07 UTC (permalink / raw)
  To: Soegtrop, Michael, Daniel Bünzli, Viet Le, caml-list

P.S.:

the discussion on the Maxima list was about what sin(2^1000) should be. At 2^1000 a double LSB is zillions of Pi, so that one can argue that the proper answer should be "something between -1 and 1". The IEEE standard though requires that 2^1000 is treated as an exact number and not as 2^1000 +- 2^(1000-53) and that sin(2^1000) is computed to full precision. The same ideas hold at the small end of numbers.

Best regards,

Michael
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 13:57   ` Soegtrop, Michael
  2017-08-01 14:07     ` Soegtrop, Michael
@ 2017-08-01 15:48     ` Peter Thiemann
  2017-08-01 17:12       ` Soegtrop, Michael
  1 sibling, 1 reply; 18+ messages in thread
From: Peter Thiemann @ 2017-08-01 15:48 UTC (permalink / raw)
  To: Soegtrop, Michael; +Cc: Peter Thiemann, Daniel Bünzli, Viet Le, caml-list

BTW, there is some literature on this topic, the most recent of which is the paper
“Printing Floating Point Numbers - A faster, always correct method”
https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf

It cites all the previous efforts starting with Knuth’s TACP Vol II, seminumerical algorithms.

-Peter

> On 1. Aug 2017, at 15:57, Soegtrop, Michael <michael.soegtrop@intel.com> wrote:
> 
> Said that, one can define a mapping from floating point numbers to usual decimal string representations and back such that each floating point number results in a unique string. It is just quite tricky to get it right without extended precision arithmetic, though.
> 
> As far as I know the Lisp standard requires that certain floating point formats have this property (I need to check). One might be able to learn from them how to do this properly.


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

* RE: [Caml-list] Float precision in OCaml
  2017-08-01 15:48     ` Peter Thiemann
@ 2017-08-01 17:12       ` Soegtrop, Michael
  2017-08-02  7:41         ` Peter Thiemann
  0 siblings, 1 reply; 18+ messages in thread
From: Soegtrop, Michael @ 2017-08-01 17:12 UTC (permalink / raw)
  To: Peter Thiemann; +Cc: Daniel Bünzli, Viet Le, caml-list

Dear Peter,

> https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf

A very interesting paper, but it handles only the print direction, not the scan direction and not the problem of:

forall (x : double): scan(print(x)) = x

Best regards,

Michael

> -----Original Message-----
> From: Peter Thiemann [mailto:thiemann@informatik.uni-freiburg.de]
> Sent: Tuesday, August 1, 2017 5:49 PM
> To: Soegtrop, Michael <michael.soegtrop@intel.com>
> Cc: Peter Thiemann <thiemann@informatik.uni-freiburg.de>; Daniel Bünzli
> <daniel.buenzli@erratique.ch>; Viet Le <vietlq85@gmail.com>; caml-
> list@inria.fr
> Subject: Re: [Caml-list] Float precision in OCaml
> 
> BTW, there is some literature on this topic, the most recent of which is the
> paper “Printing Floating Point Numbers - A faster, always correct method”
> https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf
> 
> It cites all the previous efforts starting with Knuth’s TACP Vol II, seminumerical
> algorithms.
> 
> -Peter
> 
> > On 1. Aug 2017, at 15:57, Soegtrop, Michael <michael.soegtrop@intel.com>
> wrote:
> >
> > Said that, one can define a mapping from floating point numbers to usual
> decimal string representations and back such that each floating point number
> results in a unique string. It is just quite tricky to get it right without extended
> precision arithmetic, though.
> >
> > As far as I know the Lisp standard requires that certain floating point formats
> have this property (I need to check). One might be able to learn from them how
> to do this properly.

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

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

* RE: [Caml-list] Float precision in OCaml
       [not found]   ` <etPan.5980c173.1234cb4.123@AirmailxGenerated.am>
@ 2017-08-01 18:05     ` Daniel Bünzli
  2017-08-02  5:46       ` Viet Le
  2017-08-02  8:22       ` Soegtrop, Michael
  0 siblings, 2 replies; 18+ messages in thread
From: Daniel Bünzli @ 2017-08-01 18:05 UTC (permalink / raw)
  To: Soegtrop, Michael, Viet Le, caml-list

> Said that, one can define a mapping from floating point numbers to usual decimal string 
> representations and back such that each floating point number results in a unique string. 
> It is just quite tricky to get it right without extended precision arithmetic, though. 

And since you have no control on which binary to decimal codecs JSON parsers are going to use you have absolutely no interoperability story at that point.

Best, 

Daniel

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 18:05     ` Daniel Bünzli
@ 2017-08-02  5:46       ` Viet Le
  2017-08-02  8:22       ` Soegtrop, Michael
  1 sibling, 0 replies; 18+ messages in thread
From: Viet Le @ 2017-08-02  5:46 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: Soegtrop, Michael, caml-list

[-- Attachment #1: Type: text/plain, Size: 646 bytes --]

Thanks everyone for interesting discussion!

On 1 August 2017 at 19:05, Daniel Bünzli <daniel.buenzli@erratique.ch>
wrote:

> > Said that, one can define a mapping from floating point numbers to usual
> decimal string
> > representations and back such that each floating point number results in
> a unique string.
> > It is just quite tricky to get it right without extended precision
> arithmetic, though.
>
> And since you have no control on which binary to decimal codecs JSON
> parsers are going to use you have absolutely no interoperability story at
> that point.
>
> Best,
>
> Daniel
>



-- 
Kind regards,
Viet

[-- Attachment #2: Type: text/html, Size: 1182 bytes --]

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

* Re: [Caml-list] Float precision in OCaml
  2017-08-01 17:12       ` Soegtrop, Michael
@ 2017-08-02  7:41         ` Peter Thiemann
  0 siblings, 0 replies; 18+ messages in thread
From: Peter Thiemann @ 2017-08-02  7:41 UTC (permalink / raw)
  To: Soegtrop, Michael; +Cc: Peter Thiemann, Daniel Bünzli, Viet Le, caml-list

Hi Michael,

> On 1. Aug 2017, at 19:12, Soegtrop, Michael <michael.soegtrop@intel.com> wrote:
> 
> Dear Peter,
> 
>> https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf
> 
> A very interesting paper, but it handles only the print direction, not the scan direction and not the problem of:
> 
> forall (x : double): scan(print(x)) = x

That’s true. A bit further digging produces this report:
https://arxiv.org/abs/1310.8121
Which claims such round-tripping with a fairly simple algorithm. 
Disclaimer: I only skimmed this paper.

-Peter

> 
> Best regards,
> 
> Michael
> 
>> -----Original Message-----
>> From: Peter Thiemann [mailto:thiemann@informatik.uni-freiburg.de]
>> Sent: Tuesday, August 1, 2017 5:49 PM
>> To: Soegtrop, Michael <michael.soegtrop@intel.com>
>> Cc: Peter Thiemann <thiemann@informatik.uni-freiburg.de>; Daniel Bünzli
>> <daniel.buenzli@erratique.ch>; Viet Le <vietlq85@gmail.com>; caml-
>> list@inria.fr
>> Subject: Re: [Caml-list] Float precision in OCaml
>> 
>> BTW, there is some literature on this topic, the most recent of which is the
>> paper “Printing Floating Point Numbers - A faster, always correct method”
>> https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf
>> 
>> It cites all the previous efforts starting with Knuth’s TACP Vol II, seminumerical
>> algorithms.
>> 
>> -Peter
>> 
>>> On 1. Aug 2017, at 15:57, Soegtrop, Michael <michael.soegtrop@intel.com>
>> wrote:
>>> 
>>> Said that, one can define a mapping from floating point numbers to usual
>> decimal string representations and back such that each floating point number
>> results in a unique string. It is just quite tricky to get it right without extended
>> precision arithmetic, though.
>>> 
>>> As far as I know the Lisp standard requires that certain floating point formats
>> have this property (I need to check). One might be able to learn from them how
>> to do this properly.
> 
> Intel Deutschland GmbH
> Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
> Tel: +49 89 99 8853-0, www.intel.de
> Managing Directors: Christin Eisenschmid, Christian Lamprechter
> Chairperson of the Supervisory Board: Nicole Lau
> Registered Office: Munich
> Commercial Register: Amtsgericht Muenchen HRB 186928


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

* RE: [Caml-list] Float precision in OCaml
  2017-08-01 18:05     ` Daniel Bünzli
  2017-08-02  5:46       ` Viet Le
@ 2017-08-02  8:22       ` Soegtrop, Michael
  1 sibling, 0 replies; 18+ messages in thread
From: Soegtrop, Michael @ 2017-08-02  8:22 UTC (permalink / raw)
  To: Daniel Bünzli, Viet Le, caml-list

Dear Daniel,

> And since you have no control on which binary to decimal codecs JSON parsers
> are going to use you have absolutely no interoperability story at that point.

this is true, although if both ends use "round trip safe" codecs, the error should be at most 1 LSB and this also only in rare cases, which is at least a bit of a story. If one uses codecs which are not round trip safe, the story is much worse. So I would say that using round trip safe codecs still gives a substantial advantage. But you are definitely right - it is not the holy grail.

It would be an interesting exercise to test the interoperability of different round trip safe codecs.

Best regards,

Michael
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

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

end of thread, other threads:[~2017-08-02  8:22 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-01 10:47 [Caml-list] Float precision in OCaml Viet Le
2017-08-01 11:18 ` Nicolás Ojeda Bär
2017-08-01 11:25   ` Frédéric Bour
2017-08-01 11:45     ` Viet Le
2017-08-01 11:40   ` François Bobot
2017-08-01 11:42   ` Viet Le
2017-08-01 11:48     ` François Bobot
2017-08-01 12:12 ` Daniel Bünzli
2017-08-01 12:56   ` Viet Le
     [not found]   ` <etPan.59807b8a.db32dee.123@AirmailxGenerated.am>
2017-08-01 13:17     ` Daniel Bünzli
2017-08-01 13:57   ` Soegtrop, Michael
2017-08-01 14:07     ` Soegtrop, Michael
2017-08-01 15:48     ` Peter Thiemann
2017-08-01 17:12       ` Soegtrop, Michael
2017-08-02  7:41         ` Peter Thiemann
     [not found]   ` <etPan.5980c173.1234cb4.123@AirmailxGenerated.am>
2017-08-01 18:05     ` Daniel Bünzli
2017-08-02  5:46       ` Viet Le
2017-08-02  8:22       ` Soegtrop, Michael

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