w3c / json-ld-syntax

JSON-LD 1.1 Specification
https://w3c.github.io/json-ld-syntax/
Other
112 stars 22 forks source link

Why xsd:double? Why not xsd:decimal? #318

Closed dr-shorthair closed 4 years ago

dr-shorthair commented 4 years ago

I note that in the section on conversion of native data-types, numbers with fractions are interpreted as xsd:double rather than xsd:decimal.

That is counter-intuitive to me when dealing with interpretation of a serialization, since conversion of a literal to a double is not exact in general.

iherman commented 4 years ago

I would expect that JSON conversions to various languages convert to float/double by default. It may lead to serious complications if the JSON-LD interpretation would be radically different: the idea is to provide an easy path from vanilla JSON usage to JSON expressing Linked Data.

It is perfectly possible to set the conversion to xsd:decimal explicitly in the @context, though:

{
  "@context" : {
    "xsd" : "http://www.w3.org/2001/XMLSchema#",
    "example": {
      "@id": "http://www.example.org/example",
      "@type": "xsd:decimal"
    }
  },
  "example" : 1.2345
}

The section you refer to only define the default conversion to Turtle.

dr-shorthair commented 4 years ago

The standard Turtle interpretation of a bare number-with-fraction is xsd:decimal, so there may be a complication here. Perhaps little more explanation in the text of why the default here is different? (it surprised me)

pchampin commented 4 years ago

I agree with @iherman's interpretation, and made a PR where this rationale is explained. This has to checked by @gkellogg or other members of the JSON-LD 1.0 team, though...

gkellogg commented 4 years ago

Text updated in #319.

@dr-shorthair, please indicate if this update satisfactorily addresses your concern.

dr-shorthair commented 4 years ago

Thank you. This does provide an explanation and alerts users to the solution selected.

(I remain suspicious about the general principle of reflexively casting numbers transported as strings into something different. But probably a battle to be fought elsewhere, sometime, maybe ...).

iherman commented 4 years ago

This issue was discussed in a meeting.

dr-shorthair commented 4 years ago

Interesting. The proposed solution is to leave the type for numbers with fractions as xsd:double, yet the comment from Gregg Kellogg in the meeting minutes suggests that even that may be incorrect since JSON uses IEEE float internally. So is this issue resolved correctly?

gkellogg commented 4 years ago

It dos use float internally, but integers, at least within a certain range, can be unambiguously identified. We covered this in depth in 1.0.

dr-shorthair commented 4 years ago

But is float the same as double these days? If JSON-LD number+fraction is cast to xsd:double, but JSON uses float internally, isn't there still an inconsistency?

gkellogg commented 4 years ago

@dr-shorthair See RFC8259 section 6 on numbers. interoperability is generally the same as IEEE double precision floating point values, which is the same as xsd:double.

Note that the JSON Canonicalization Scheme, which we use when transforming JSON literals to RDF, does show a decimal-like representation (333333333.3333333) it could potentially be used to carve out a subset of xsd:decimal that could be generated, but without hints in the context, there would be no way to distinguish between what should be a decimal, float or double, so we're pretty might back where we were.

Of course, by this logic, how to distinguish double 1e0, or 1.0 from integer 1; this is where we took license in how to consider when transforming, which seems to have good acceptance.