Skip to content

Commit ebe5cb5

Browse files
committed
Clarify the rules for formatting floating-point values.
1 parent 2a8a8fc commit ebe5cb5

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/docs/spec-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ It is divided in three sections covering the three different components of the s
88
* the specification for the [serialisation formats](spec-formats.md), to read, write, and exchange SSSOM mapping sets;
99
* the specification for [supporting functions](spec-support.md) to help manipulating SSSOM mappings and mapping sets.
1010

11-
Both sections are _normative_.
11+
All three sections are _normative_.
1212

1313
## Conventions used in this document
1414

src/docs/spec-support-hashing.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The S-expression MUST be assembled as follows:
4646
slot.
4747
2. Append to the S-expression `(N:SLOTNAME`, where _SLOTNAME_ is the LinkML
4848
name for the slot and _N_ is the length of the slot name (so, for
49-
rxample, `(10:subject_id`, `(9:author_id`, `(10:confidence`, etc.).
49+
example, `(10:subject_id`, `(9:author_id`, `(10:confidence`, etc.).
5050
3. If the slot is defined as a multi-valued slot (and even if it has only
5151
one value in the mapping record to hash):
5252
1. Append `(`.
@@ -55,9 +55,10 @@ The S-expression MUST be assembled as follows:
5555
length of _V_.
5656
3. Append `)`.
5757
4. If the slot is typed as a floating point number (e.g. `confidence`),
58-
convert the value to a string _V_ by truncating the floating point number
59-
to up to 3 digits after the decimal point, rounding the last digit half
60-
away from zero. Then append `N:V`, where _N_ is the length of _V_.
58+
convert the value into a string _V_ according to the rules set forth in
59+
the section
60+
[Formatting floating-point values](#formatting-floating-point-values).
61+
Then append `N:V`, where _N_ is the length of _V_.
6162
5. If the slot is typed as an enumeration (e.g. `subject_type`), append
6263
`N:ENUMVALUE`, where _ENUMVALUE_ is the allowed value in the enumeration
6364
as specified in the LinkML model, and _N_ is the length of _ENUMVALUE_
@@ -90,7 +91,7 @@ Converting extension values to string:
9091
| ----------------------- | ---------------------------------------------------------------------------------------------------------- |
9192
| `xsd:string` | No conversion needed, use the value directly |
9293
| `xsd:integer` | Base 10 representation of the integer value |
93-
| `xsd:double` | Truncate the number to up to 3 digits after the decimal point, round the last digit half away from zero |
94+
| `xsd:double` | Apply the rules from the section [Formatting floating-point values](#formatting-floating-point-values) |
9495
| `xsd:boolean` | `true` or `false` |
9596
| `xsd:date` | ISO-8601 representation: `YYYY-MM-DD` |
9697
| `xsd:datetime` | ISO-8601 representation: `YYYY-MM-DDThh:mm:ssTZ` where `TZ` is the zone offset (e.g. `+01:00` or `-06:30`) |
@@ -111,6 +112,34 @@ Encode the hash computed in step 2 into uppercase hexadecimal, also known as
111112
Base16 encoding as defined in
112113
[RFC 4648](https://datatracker.ietf.org/doc/html/rfc4648#section-8).
113114

115+
<a id="formatting-floats"></a>
116+
117+
### Formatting floating-point values
118+
119+
Whenever a floating-point number needs to be appended to the canonical
120+
S-expression built in Step 2 above, the following specific rules apply:
121+
122+
1. The non-fractional part MUST NOT be omitted, even it is zero. For example,
123+
`0.7` MUST NOT be written as `.7`.
124+
2. The fractional part MUST be truncated to _up to_ 3 digits _as needed_. If the
125+
fractional part can be written in less than 3 digits, then it MUST NOT be
126+
right-padded with zeros. For example, `0.7` MUST NOT be written as `0.700`.
127+
3. If the fractional part needs to be truncated, the value MUST be rounded to
128+
the nearest value representable with 3 digits, rounding half away from zero.
129+
This corresponds to the _roundTiesToAway_ mode as defined by [IEEE 754-2019
130+
§4.3.1](https://doi.org/ 10.1109/IEEESTD.2019.8766229).
131+
132+
The following table gives some examples of rounding after truncation:
133+
134+
| Original value | Canonical representation |
135+
| -------------- | ------------------------ |
136+
| 0.7832... | 0.783 |
137+
| 0.7835... | 0.784 |
138+
| 0.7836... | 0.784 |
139+
| -0.7832... | -0.783 |
140+
| -0.7836... | -0.784 |
141+
| -0.7835... | -0.784 |
142+
114143
## Example
115144

116145
> This section is not normative. It provides a step-by-step example of how to

0 commit comments

Comments
 (0)