Re: [Json] I-JSON Topic #5: Numbers

"Joe Hildebrand (jhildebr)" <jhildebr@cisco.com> Mon, 19 May 2014 22:01 UTC

Return-Path: <jhildebr@cisco.com>
X-Original-To: json@ietfa.amsl.com
Delivered-To: json@ietfa.amsl.com
Received: from localhost (ietfa.amsl.com [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 6F7121A03DA for <json@ietfa.amsl.com>; Mon, 19 May 2014 15:01:19 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -15.152
X-Spam-Level:
X-Spam-Status: No, score=-15.152 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_HI=-5, RP_MATCHES_RCVD=-0.651, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5] autolearn=ham
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2yWw_puTjlg1 for <json@ietfa.amsl.com>; Mon, 19 May 2014 15:01:18 -0700 (PDT)
Received: from rcdn-iport-2.cisco.com (rcdn-iport-2.cisco.com [173.37.86.73]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id 1C9561A03DE for <json@ietf.org>; Mon, 19 May 2014 15:01:18 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=2892; q=dns/txt; s=iport; t=1400536878; x=1401746478; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-id:content-transfer-encoding: mime-version; bh=blm3sgZaoV6igaE7k1Vi1nVudN199Rj94PgYTApJpPk=; b=S+m0bQWsSSOv+aOsvMXHN+0rUDlPAoGjUeGbjzoDIF175DCQJjiib3Eu 7jwvi+HgPm7P6dznQV5Iw32IizkxCI5UdzkvPphnC+Z6nPdAwuBUZcTKI F+ciMhJ7Pbvxf07zusiIssR5MrNhYr6HH9DPUa1ji5m+UIV105WL81vCU M=;
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AucIAOp+elOtJV2d/2dsb2JhbABZgwaBHA2CaaZqAQEBAQEBBQGaKQEZgQIWdIIlAQEBAwEjEUUFCwIBCBoCJgICAjAVEAIEDgWIOQitd6RiF4EqhCuIRxgbB4J1gUsElV6DfJMagzeCMA
X-IronPort-AV: E=Sophos;i="4.98,869,1392163200"; d="scan'208";a="326128005"
Received: from rcdn-core-6.cisco.com ([173.37.93.157]) by rcdn-iport-2.cisco.com with ESMTP; 19 May 2014 22:00:59 +0000
Received: from xhc-aln-x06.cisco.com (xhc-aln-x06.cisco.com [173.36.12.80]) by rcdn-core-6.cisco.com (8.14.5/8.14.5) with ESMTP id s4JM0x21025641 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL) for <json@ietf.org>; Mon, 19 May 2014 22:00:59 GMT
Received: from xmb-rcd-x10.cisco.com ([169.254.15.76]) by xhc-aln-x06.cisco.com ([173.36.12.80]) with mapi id 14.03.0123.003; Mon, 19 May 2014 17:00:59 -0500
From: "Joe Hildebrand (jhildebr)" <jhildebr@cisco.com>
To: "Matt Miller (mamille2)" <mamille2@cisco.com>
Thread-Topic: [Json] I-JSON Topic #5: Numbers
Thread-Index: AQHPYxzPc1qOL3zAFk6QxZ1T0YrJO5snzN0A//+jtwCAAGnvgIAEImEAgBzCVgD//8VpAA==
Date: Mon, 19 May 2014 22:00:59 +0000
Message-ID: <CF9FCEC9.4A4E7%jhildebr@cisco.com>
References: <535EB3BF.8080606@cisco.com> <CAHBU6ivjF9ULW0yGSVdJi2D6QgUThuhym_ZhpgLM=cvLu=mAiQ@mail.gmail.com> <CF841AAE.47D86%jhildebr@cisco.com> <CAHBU6itK5HtSTPWSsHsHUPja90emqU86LsgjrBorkqcUDivS2A@mail.gmail.com> <CF87EB9C.48BB0%jhildebr@cisco.com> <537A5BE0.3020406@cisco.com>
In-Reply-To: <537A5BE0.3020406@cisco.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
user-agent: Microsoft-MacOutlook/14.4.1.140326
x-originating-ip: [10.129.24.156]
Content-Type: text/plain; charset="utf-8"
Content-ID: <4792ED3EDEFA824A9181245A96FC128B@emea.cisco.com>
Content-Transfer-Encoding: base64
MIME-Version: 1.0
Archived-At: http://mailarchive.ietf.org/arch/msg/json/zVfO-zGSU1QqNs1zWfSwq2ipi_8
Cc: IETF JSON WG <json@ietf.org>
Subject: Re: [Json] I-JSON Topic #5: Numbers
X-BeenThere: json@ietf.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: "JavaScript Object Notation \(JSON\) WG mailing list" <json.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/options/json>, <mailto:json-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/json/>
List-Post: <mailto:json@ietf.org>
List-Help: <mailto:json-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/json>, <mailto:json-request@ietf.org?subject=subscribe>
X-List-Received-Date: Mon, 19 May 2014 22:01:19 -0000

On 5/19/14, 1:30 PM, "Matt Miller (mamille2)" <mamille2@cisco.com> wrote:

>could you provide more detail of what the concern here is?  I suspect
>you have been too terse so far.

You're likely correct.

I said:

> Subnormals are allowed in JSON, I believe.  They probably don't
> interop quite as expected if you're using C and have -O1 or higher
> turned on (for example).


Here's an example of a number that is allowed in JSON, but in some
implementations would lead to a subnormal number:

4e-324

You'll note that (at least in a couple of the browsers that I checked, as
well as node.js):

JSON.parse("4e-324") == 5e-324


The reason for this is that the minimum "normal" IEEE754 double-precision
floating point number is represented in hex as: 0010 0000 0000 0000, which
corresponds to roughly 2.2250738585072014e−308.  For numbers smaller than
this, precision gets thrown away intentionally, in order to represent
numbers closer to zero.  For example, the smallest number you can
represent this way is 0000 0000 0000 0001 (hex), which corresponds to
roughly 4.94e-324 ... which lets you understand why 5e-324 might be a
reasonable way to render this number as a string.

For some computations, the loss of precision is nice.  For others, you'd
just as soon have zero than a really tiny number.  IEEE-compliant hardware
will set an underflow flag when you get a subnormal result as the output
of a calculation.  However many languages (including C) don't give you
access to this flag.

This problem is exacerbated by the fact that different C compilers at
different optimization settings, at different usages of SSE processor
extensions will either "flush to zero" (FTZ) or not.  If FTZ is enabled,
your JSON parser is likely going to return 0 for JSON.parse("4e-324").

As such, there will be a loss of interoperability for very small numbers
(either positive or negative), and I think we should mention what the
smallest safely-interoperable numbers are.  10^-308 and -10^-308 seem like
reasonable limits in decimal-land.

-- 
Joe Hildebrand