chibi-scheme icon indicating copy to clipboard operation
chibi-scheme copied to clipboard

Unable to read complex numeric literals with NaN as one of the components

Open Oxyd opened this issue 2 years ago • 4 comments

> 0+nan.0i
ERROR on line 23: invalid numeric syntax: #\n
> ERROR: undefined variable: an.0i
> +nan.0i
ERROR: undefined variable: |+nan.0i|
> +nan.0+1i
ERROR: undefined variable: |+nan.0+1i|
> +nan.0+i
ERROR: undefined variable: |+nan.0+i|
> +nan.0+nan.0i
ERROR: undefined variable: |+nan.0+nan.0i|

Even though the analogous syntax with infinites works:

> 0+inf.0i
0+inf.0i
> +inf.0i
0+inf.0i
> +inf.0+1i
+inf.0+i
> +inf.0+i
+inf.0+i
> +inf.0+inf.0i
+inf.0+inf.0i

Oxyd avatar Nov 03 '21 20:11 Oxyd

Additional observation: Chbi can't write complex numbers with NaN's in them either:

> (make-rectangular 0 +nan.0)
0++nan.0i
> (make-rectangular 0 +inf.0)
0+inf.0i

Should be just a single + for the first output, like it is for the second number.

And, for completeness, Chibi can't read its own write output in this case:

> 0++nan.0i
ERROR on line 10: invalid numeric syntax: #\n
> ERROR: undefined variable: an.0i

Oxyd avatar Nov 03 '21 21:11 Oxyd

The standard is not strict on this - most of the numeric tower and any NaN support at all is optional.

From a mathematical perspective I am opposed to the notion of any number that is somehow "partially" not a number. From a practical perspective, everyone else does it, and it makes certain things easier.

I have to decide whether to make those make-rectangular calls return a single real +nan.0, or break down and fix read/write.

ashinn avatar Nov 04 '21 14:11 ashinn

Well the standard says that implementations must “implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language” – I'm not sure if supporting NaN's but not supporting the read syntax for complex NaN's counts as a coherent subset.

Even if Chibi were to collapse complex numbers with any component being NaN to just a single real NaN, I'd expect it to be able to read such complex numbers. I.e. 1+nan.0i should evaluate to +nan.0 instead of giving a read error.

From a practical perspective I don't see any gains from doing such collapsing. If anything, not collapsing complex numbers in that way should be slightly easier to implement. From a mathematical perspective, I find numbers that are not numbers already objectionable, so if one accepts those, one might as well accept them as components of complex numbers.

Oxyd avatar Nov 04 '21 17:11 Oxyd

If we take the view that there are no partial NaN's, the logic would be to collapse intermediate computations resulting this to a single real NaN. Explicitly trying to create such invalid values with make-rectangular or read syntax would then logically signal an error.

However, I realize the ship has sailed with SRFI 160. Even if I try to patch up the homogeneous complex vectors to respect this behavior, other libraries such the usage of BLAS in (chibi math linalg) exposes these values.

ashinn avatar Nov 05 '21 02:11 ashinn