Skip to content

Capitalize INF, -INF, and NAN in serializing#100414

Open
aaronfranke wants to merge 1 commit into
godotengine:masterfrom
aaronfranke:range-inf-neg-nan-caps
Open

Capitalize INF, -INF, and NAN in serializing#100414
aaronfranke wants to merge 1 commit into
godotengine:masterfrom
aaronfranke:range-inf-neg-nan-caps

Conversation

@aaronfranke
Copy link
Copy Markdown
Member

@aaronfranke aaronfranke commented Dec 14, 2024

As explained by bug report #88006, the editor inspector is unable to display non-finite values such as infinity and NaN. This is due to this check in Range (range.cpp) added by #81076:

Screenshot 2024-12-13 at 10 57 05 PM

If we remove this check, then non-finite values become allowed. They have to be typed as INF, -INF, or NAN like GDScript, and then get displayed as inf, -inf, or nan:

Screenshot 2024-12-13 at 11 00 22 PM

Inside of the .tscn file the values get serialized as inf, inf_neg, or nan:

Screenshot 2024-12-13 at 10 59 48 PM

So, it's clear that simply removing this check is not enough. We need to fix these problems:

  • The text the user can type is not the same as the displayed text (vital for good UX).
  • The constants in GDScript are not the same as the displayed text (ideal for good UX).
  • The value serialized in the file is not the same as the displayed text in the case of -inf and inf_neg (less important, but we should still be consistent).

Allowing the user to type both inf and INF is just generally a good idea, and stands on its own, so I put this into a separate PR here: #100395. This fixes the first point, since users can type inf like they see inf.

However, I think there is value in fixing the other two points as well. Capitalizing the displayed and serialized text is pretty straightforward, it's just an arbitrary choice we make as engine developers, although we need to keep the code being able to read inf and such to keep reading old files (and if this is merged, we should backport a fix to older branches to allow INF etc to be read by older Godot versions).

As for inf_neg, the current reason for this is that VariantParser is not able to parse -INF as a single thing. It has this code in master which checks for - or a number as a start of a numeric literal:

if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
	StringBuffer<> num;
	if (cchar == '-') {
		num += '-';
		cchar = p_stream->get_char();
	}
	// and so on

However, if you look closely at the code, we can see that it's needlessly checking for '-' twice. A simple fix would be to rearrange this code slightly, so that it only needs to check for a leading '-' once:

StringBuffer<> num;
if (cchar == '-') {
	num += '-';
	cchar = p_stream->get_char();
}
if (cchar >= '0' && cchar <= '9') {
	// and so on

This way, we can handle reading -INF or really any negated identifier (-INF would be the only one at the moment, but I could imagine adding -PI or something, in any case making this code work for all cases has no downsides).


If desired, I can undo the change in range.cpp, un-undoing #81076, leaving #88354 to be the PR changing the exposed behavior of Range. However, as for the rest of this PR, I think it's a good improvement to have the serialization and display of INF, -INF, and NAN values match what users already expect from GDScript. Aside from capitalization, it's important that this PR fixes reading negated identifiers such as -inf and -INF, so we can be consistent and avoid inf_neg / INF_NEG.

Comment thread core/variant/variant_parser.cpp Outdated
@akien-mga
Copy link
Copy Markdown
Member

There's really a lot to unpack here, and it's hard to have a good overview. I'm not against changing the serialization to uppercase identifiers and specifically -INF instead of inf_neg if that solves actual issues. But I'm not in favor of allowing all kinds of spellings just for the sake of it.

We should pick one convention and enforce it (and if we're changing it, then we do still need to support the previous convention as compatibility code - but that's only for compatibility's sake, not a design decision that we should support any possible variant the user may come up with).

I would suggest focusing on fixing bugs in priority, and then assess the more "cosmetic" changes. If we can fix parsing -inf and allow it in range, then it's probably a better change IMO than breaking compat by renaming them all to upper case for some seemingly arbitrary reason.

@aaronfranke
Copy link
Copy Markdown
Member Author

aaronfranke commented Jan 14, 2025

I feel like this is a bit of a paradox, then. As you stated in #100395, the constants are supposed to be capitalized to match GDScript. But capitalizing the constants is a "cosmetic" change. So... it's non-cosmetic, yet cosmetic.

If we can fix parsing -inf and allow it in range

PR #100395 allows Expression to read -inf. Range uses Expression to read user input and turn it into a number.

I would suggest focusing on fixing bugs in priority, and then assess the more "cosmetic" changes.

I did both at once since this way we only need to handle negative infinity as -INF or inf_neg. If I separated the changes to make it -inf first, then we would have to handle 3 different names, 2 for compatibility.

@Mickeon
Copy link
Copy Markdown
Member

Mickeon commented May 7, 2025

In the meeting, it was brought up that it is critical for this PR to stay fully backwards compatible, because users would really be unhappy for us breaking their tscn files over this change.

In this PR, the relevant changes are done in core/variant/variant_parser.cpp. So this PR should in theory maintain compatibility already, not just for PackedScenes, but for other methods that convert strings into variants.

Comment thread core/variant/variant_parser.cpp Outdated
Comment thread core/variant/variant_parser.cpp Outdated
Comment thread core/variant/variant_parser.cpp Outdated
Comment thread tests/core/string/test_string.h Outdated
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 3 times, most recently from 48e4d52 to 94e91a0 Compare May 9, 2025 20:21
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 2 times, most recently from 89901fb to ef47ab1 Compare May 22, 2025 18:59
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from ef47ab1 to bc894b4 Compare May 24, 2025 12:34
Copy link
Copy Markdown
Member

@Mickeon Mickeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't hold you at gunpoint for this but there's actually a few more places in the @GlobalScope.xml that directly refer to these values, and they would need to be changed as well.

@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 3 times, most recently from 5e4aafa to 49f0b1d Compare June 9, 2025 01:36
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from 49f0b1d to 2195a32 Compare June 13, 2025 07:58
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from 2195a32 to 2c9132c Compare June 22, 2025 10:34
@Repiteo Repiteo modified the milestones: 4.5, 4.x Jun 23, 2025
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from 2c9132c to 9e3e0a8 Compare July 3, 2025 23:37
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 2 times, most recently from 322bb8a to bf6d3cb Compare August 24, 2025 17:00
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from bf6d3cb to cfb3d1a Compare September 16, 2025 03:45
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 2 times, most recently from 225307f to f167768 Compare September 28, 2025 20:09
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from f167768 to 070607b Compare October 1, 2025 02:54
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch from 070607b to b259888 Compare October 10, 2025 16:48
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 2 times, most recently from 841718b to 79a8567 Compare October 23, 2025 03:28
@aaronfranke aaronfranke force-pushed the range-inf-neg-nan-caps branch 2 times, most recently from acdb8b2 to a9b4803 Compare November 12, 2025 15:49
Copy link
Copy Markdown
Contributor

@Zehir Zehir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the PR and works as expected.

The value in JSON.from_native are now uppercase.

The value in the .tscn and in var_to_str are like before aka inf_neg for -INF
The tscn files does decode for both lowercase and upper case and -INF.

Looked at the code, seems OK for me.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inspector doesn't allow entering values of NAN, INF, etc. for float fields

8 participants