[Cbor] nan'' application-extension literal

Carsten Bormann <cabo@tzi.org> Sun, 04 August 2024 20:39 UTC

Return-Path: <cabo@tzi.org>
X-Original-To: cbor@ietfa.amsl.com
Delivered-To: cbor@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 991ABC14F5EB for <cbor@ietfa.amsl.com>; Sun, 4 Aug 2024 13:39:18 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -6.907
X-Spam-Level:
X-Spam-Status: No, score=-6.907 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, RCVD_IN_ZEN_BLOCKED_OPENDNS=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01, URIBL_DBL_BLOCKED_OPENDNS=0.001, URIBL_ZEN_BLOCKED_OPENDNS=0.001] autolearn=ham autolearn_force=no
Received: from mail.ietf.org ([50.223.129.194]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bz5EIHKAVwlZ for <cbor@ietfa.amsl.com>; Sun, 4 Aug 2024 13:39:14 -0700 (PDT)
Received: from smtp.zfn.uni-bremen.de (smtp.zfn.uni-bremen.de [134.102.50.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id 93187C14EB19 for <cbor@ietf.org>; Sun, 4 Aug 2024 13:39:13 -0700 (PDT)
Received: from smtpclient.apple (p5dc5d809.dip0.t-ipconnect.de [93.197.216.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.zfn.uni-bremen.de (Postfix) with ESMTPSA id 4WcWdB5DzpzDCf9; Sun, 4 Aug 2024 22:39:10 +0200 (CEST)
From: Carsten Bormann <cabo@tzi.org>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\))
Date: Sun, 04 Aug 2024 22:38:59 +0200
Message-Id: <65F1B2EE-DC84-438A-B3C6-943B153F3B08@tzi.org>
To: CBOR <cbor@ietf.org>
X-Mailer: Apple Mail (2.3776.700.51)
Message-ID-Hash: VLCGXN3XHR5C72E5SNEB6NLEXSPXPVTV
X-Message-ID-Hash: VLCGXN3XHR5C72E5SNEB6NLEXSPXPVTV
X-MailFrom: cabo@tzi.org
X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-cbor.ietf.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header
X-Mailman-Version: 3.3.9rc4
Precedence: list
Subject: [Cbor] nan'' application-extension literal
List-Id: "Concise Binary Object Representation (CBOR)" <cbor.ietf.org>
Archived-At: <https://mailarchive.ietf.org/arch/msg/cbor/_cwBnypBbISrGobbTo_5s2qyhXo>
List-Archive: <https://mailarchive.ietf.org/arch/browse/cbor>
List-Help: <mailto:cbor-request@ietf.org?subject=help>
List-Owner: <mailto:cbor-owner@ietf.org>
List-Post: <mailto:cbor@ietf.org>
List-Subscribe: <mailto:cbor-join@ietf.org>
List-Unsubscribe: <mailto:cbor-leave@ietf.org>

Now that we have a more detailed understanding of NaNs in IEEE 754 and thus CBOR, I went ahead and designed an EDN syntax for representing diverse NaN values in diagnostic input and output.  
It seemed to me that a nan’’ application—extension literal will be the best way to integrate such a syntax without a need for a change in the main EDN syntax (which will rarely actually need to express NaN values outside the default for 0.0/0.0).

The syntax is: nan’payload-rep’, where payload-rep is the EDN representation of a floating point number with an absolute value between 1.0 and 2.0 (both sides exclusive).
The payload-rep number is obtained by flipping the topmost bit of the exponent of the NaN.
The payload is usually expressed as a hexadecimal floating point literal; 0x1.0p0 (exclusive) to 0x1.8p0 (exclusive) is a signaling NaN, and 0x1.8p0 (inclusive) to 0x2.0p0 (exclusive) is a quiet NaN.
(Decimal floating point literals are accepted, but they usually lead to periodic fractions that don’t make a lot of sense for NaNs, except for specifically chosen values such as 1.5, 1.25, 1.875, ....
The printer as implemented below always emits hex floats.)

Implemented in cbor-diag 0.9.1 (using the cbor-pure library coming with that) — `gem update cbor-diag` to get that.

In Ruby code, the nan'' syntax is available in the cbor-diag parser after requiring cbor-diag-app/nan, and in the printer by adding the :nan option (otherwise the printer maps everything to NaN, the backwards-compatible behavior).
E.g.,

>> puts (0.0/0.0).cbor_diagnostic(nan: true)
nan'0x1.8p0’

For the tools, the printer option for nnnn2diag.rb is -n (NaN), the parser option for diag2nnnn.rb is (as usual) -anan.  
(Combine to -nanan for playing around.)

Comments welcome.

Next: (1) write it up; (2) implement the necessary flags in cbor.me <http://cbor.me/> as well.

Grüße, Carsten