Skip to content

Read back infinities and NaNs from text archives - #387

Open
gennaroprota wants to merge 1 commit into
developfrom
fix/read-back-infinities-and-nans-from-text-archives
Open

Read back infinities and NaNs from text archives#387
gennaroprota wants to merge 1 commit into
developfrom
fix/read-back-infinities-and-nans-from-text-archives

Conversation

@gennaroprota

Copy link
Copy Markdown
Collaborator

An infinity is written as "inf" and a NaN as "nan", because that is what the stream writes, and extraction of a floating point number then refuses both: it takes digits and little else, in libstdc++ and in the Microsoft library alike. So the library wrote text and XML archives which it could not read back.

Recognize the two forms when reading, rather than change what is written, so that archives already in existence start loading instead of staying unreadable.

Fixes #386.

An infinity is written as "inf" and a NaN as "nan", because that is what
the stream writes, and extraction of a floating point number then
refuses both: it takes digits and little else, in libstdc++ and in the
Microsoft library alike.  So the library wrote text and XML archives
which it could not read back.

Recognize the two forms when reading, rather than change what is
written, so that archives already in existence start loading instead of
staying unreadable.

Fixes #386.
@robertramey

Copy link
Copy Markdown
Member

consider creating as special tag for NaN etc not dependent on the compiler. This would make the serialization library independent of any differences between compilers and/or standard libraries. Of course then one would have to be be checking the old compiler dependent tags and/or perhaps the library version number so I wouldn't be more efficient, just reduce future surprises. FYI It's amazing to me that this hasn't come up before.

It's also interesting to consider the possibility of handling this out side of streams. As it stands now the binary archive would not be portable between compiles. Same for non-stream archives. Think about this.

Again, consider this a suggestion rather than a "fix"

@correaa

correaa commented Aug 31, 2026

Copy link
Copy Markdown

It is tricky to detect NaNs when writing; sometimes it depends on the optimization level. For example, std::isnan might always return false at some optimization levels. You can compare with bit patterns, but I am not sure how portable it is.

Same with infinity; I wonder if this is a limitation of the underlying stream that the archive type must accept.

In any case, don't forget about infinities and NaNs in std::complex. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

infinity

3 participants