[Cbor] Dealing with -0.0, NaN, and Infinity

Anders Rundgren <anders.rundgren.net@gmail.com> Wed, 21 August 2024 09:42 UTC

Return-Path: <anders.rundgren.net@gmail.com>
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 28B40C14F6E4 for <cbor@ietfa.amsl.com>; Wed, 21 Aug 2024 02:42:56 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -7.105
X-Spam-Level:
X-Spam-Status: No, score=-7.105 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, 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_BLOCKED=0.001, URIBL_DBL_BLOCKED_OPENDNS=0.001, URIBL_ZEN_BLOCKED_OPENDNS=0.001] autolearn=ham autolearn_force=no
Authentication-Results: ietfa.amsl.com (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com
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 EwoD-fGsYYbR for <cbor@ietfa.amsl.com>; Wed, 21 Aug 2024 02:42:52 -0700 (PDT)
Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id 83A8DC14F694 for <cbor@ietf.org>; Wed, 21 Aug 2024 02:42:52 -0700 (PDT)
Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-428e3129851so53351305e9.3 for <cbor@ietf.org>; Wed, 21 Aug 2024 02:42:52 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724233371; x=1724838171; darn=ietf.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=hgKRmCQXhpq0+q5MA9aO46IbbnQWJCaF9KV8BnhQUYw=; b=i/ETYdzFMQck21m5rIJvIWJIKFBukeivvqHM0XCcfPtfY9/coH3zs4mhkxDx+feNLj SdWPYmU04Esj25mwadlngVyxo4jMh7G3aQKU9mn23mKBFyMWABzKm/bvjOX2wO6cs+sd 3Higmw5kmfqJydDHpTs8zFQUE2RcaOQE68yKPiQuiDnp8mRJ6HRCJQCgQrY0SLWDJUnO V/SU+vX/Pziyuo0ufXzD8ZTSPkz6rJ2lGZlbPwk5ihDnAw5IAgOoy2QWQZCg5hitGpvW qAymW6rnyygOooVy6xr0MhKkx44yeTJQeOj/BPW0hU/F/ZKnBJTOCe+x2jcOlrRBoz1U MyzA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724233371; x=1724838171; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=hgKRmCQXhpq0+q5MA9aO46IbbnQWJCaF9KV8BnhQUYw=; b=IfR3o4e+S9EBBbLKXDMClGvp91s3g8rO3G3mS0j8u9lHBlOGrydalx5loL1SRpYixO 1gs7vxrY237YKwHLWe4Bfq7UKz5bjATwFqtvfe1Ky6qafLMiaA9WT/GxguO0IbvW//Gn oboviLOy9GQ6sa6NsaXoyrF+dO+ov71iwl/OngIXkNr4Zg6932g7+jDg/HNtOwsF4lgi nkIPCKcqSVpFlW70YKWOWhK+51HwjvUep8spj8B6feFJfetJqe+C2R4Sgoe1mHFwVxhN snWXO1+80aKrCKqn4RXPWtq6xyA3yomxq07EQefS9aRP6v3XZHQ+t92EhPbB40zsZAQv Xghg==
X-Gm-Message-State: AOJu0YzT1pyqgV4nmkppNhlev3pK+JhA304RkItJ+v2m53zEXs3660sV VstX1VrgIWOIyLXkdRa8br/waT+b4QCY3qcNo85adj+U5hNoiZKwz2ZQ+g==
X-Google-Smtp-Source: AGHT+IFXdYKAq50St9xHhqAcaOLRGHEp/K4rSOkyaZ9O4mU0zWfvt9kFLWIMhY8b61mDZUKFp7U57g==
X-Received: by 2002:a05:6000:e45:b0:36d:341d:270 with SMTP id ffacd0b85a97d-372fd732496mr935885f8f.59.1724233370354; Wed, 21 Aug 2024 02:42:50 -0700 (PDT)
Received: from ?IPV6:2a01:e0a:e1b:64b0:8563:213:eece:478? ([2a01:e0a:e1b:64b0:8563:213:eece:478]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-37189839f51sm15218613f8f.8.2024.08.21.02.42.49 for <cbor@ietf.org> (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Aug 2024 02:42:49 -0700 (PDT)
Message-ID: <17c394aa-095b-4a6d-95ee-0741b6b166f8@gmail.com>
Date: Wed, 21 Aug 2024 11:42:48 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Content-Language: en-US
To: "cbor@ietf.org" <cbor@ietf.org>
From: Anders Rundgren <anders.rundgren.net@gmail.com>
Content-Type: text/plain; charset="UTF-8"; format="flowed"
Content-Transfer-Encoding: 8bit
Message-ID-Hash: EVZXVZMAIDJWDIDQMOPUFGDAVWLXLNTB
X-Message-ID-Hash: EVZXVZMAIDJWDIDQMOPUFGDAVWLXLNTB
X-MailFrom: anders.rundgren.net@gmail.com
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] Dealing with -0.0, NaN, and Infinity
List-Id: "Concise Binary Object Representation (CBOR)" <cbor.ietf.org>
Archived-At: <https://mailarchive.ietf.org/arch/msg/cbor/5wn-QEWeZK1qMqRhQO3coMZCOBw>
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>

When trying to define a subset of CBOR CDE suitable as a JSON replacement
https://github.com/wintercg/admin/issues/69
I ran into a few issues with floating point numbers.

Although -0.0, NaN, and Infinity probably have very limited relevance in the intended target market, they must still be dealt with in some way.  The easy way is of course to just ban them during decoding.  FWIW, these are my current suggestions:

Following IEEE 754 rules it seems that -0.0 does not interfere with normal operations including comparisons; it is still treated as 0.0. So it is currently supported "as is".

However, since mathematical operations using NaN and Infinity are somewhat less nice, and typically indicate that something is fundamentally wrong, their support is currently made optional: https://cyberphone.github.io/CBOR.js/doc/#decoder.cbor.initextended

NaN with payloads are always rejected.  It is still a "strict subset" :)

Related: since the JavaScript "JSON" object never became compatible with Open API number requirements, this became a core priority for "CBOR": https://cyberphone.github.io/CBOR.js/doc/#cbor.int.getint64


WDYT?

Anders