I never said it was, he specifically said as an aside for a book he was writing. He said he hadn't heard of code using it, so I provided an example

You are talking about an *internal implementation detail* in the C code
of the Javascript engine, not a Javascript language feature. There is no
Javascript API for the JS programmer to perform NAN boxing or to encode
pointers inside the 51 payload bits of NANs.

I never claimed there was, just that that was a use case of tagged nans, was common for JS engine implementers to use, and indeed was a use case in which inspecting the payload bits and having unique nan values was relevant.

Imagine that a Python program would use a `libc` binding (for example, C-types) to interface with such an engine. It would have to be able to differentiate.
Obviously, there are good reasons Python doesn't support it, and people shouldn't grow to expect it, and this isn't really relevant to the discussion (insofar as the PEP proposed doesn't specify that 'inf' and 'nan' be treated as singletones, just as default builtins).

This would become relevant, if, say Python 4.0 migrated 'inf' and 'nan' to builtin names (like True and False). If that happened, a 'nan' singleton wouldn't make sense unless you had 2**53 of them, so code like:

  >>> x is nan

Would be a flawed formulation

Thanks,
----
Cade Brown
Research Assistant @ ICL (Innovative Computing Laboratory)
Personal Email: brown.cade@gmail.com
ICL/College Email: cade@utk.edu




On Sun, Sep 13, 2020 at 8:57 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Sat, Sep 12, 2020 at 08:16:36PM -0400, Cade Brown wrote:
> As per tagged nans, check for JavaScript tagged NaN optimization.
> Essentially, the tag of the NaN (i.e. the mantissa) is interpreted as a
> pointer. Obviously, this is a very advanced use case, probably not worth
> Python guaranteeing such behavior.
> Here is one article:
> https://brionv.com/log/2018/05/17/javascript-engine-internals-nan-boxing/

You are talking about an *internal implementation detail* in the C code
of the Javascript engine, not a Javascript language feature. There is no
Javascript API for the JS programmer to perform NAN boxing or to encode
pointers inside the 51 payload bits of NANs.

Aside from the extra complexity, which may or may not pay off in speed
improvements, the downside of NAN boxing is the serious security hole
that if you can introduce an arbitrary NAN value into a JS primitive
value, you get a pointer to arbitrary memory and can use that to get
up to all sorts of shenanigans.

To avoid that security hole, JS has to normalise all incoming NANs to a
single canonical NAN (thus, losing any possibility of user code making
use of NAN payloads).

In CPython's case, the interpreter uses pointers as object references,
not the payload bits of a NAN. Jython and IronPython use whatever the
JVM and .Net CLR use, which probably isn't NANs either.

So while NAN boxing is a clever use of NAN payloads, it's not really
relevant here. Python code doesn't have a notion of pointers to
arbitrary addresses, but if it did, user code probably wouldn't have to
manipulate the payload bits of a NAN float object to get one.



--
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/54RN6HEE2LKANJ2Z7INQKLSULOL4J6WJ/
Code of Conduct: http://python.org/psf/codeofconduct/