Skip to content

Commit

Permalink
Capitalize INF, -INF, and NAN in serializing and allow in Range
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Dec 14, 2024
1 parent b9437c3 commit 5b15a8d
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 41 deletions.
12 changes: 6 additions & 6 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1638,14 +1638,14 @@ String String::chr(char32_t p_char) {

String String::num(double p_num, int p_decimals) {
if (Math::is_nan(p_num)) {
return "nan";
return "NAN";
}

if (Math::is_inf(p_num)) {
if (signbit(p_num)) {
return "-inf";
return "-INF";
} else {
return "inf";
return "INF";
}
}

Expand Down Expand Up @@ -1841,14 +1841,14 @@ String String::num_real(float p_num, bool p_trailing) {

String String::num_scientific(double p_num) {
if (Math::is_nan(p_num)) {
return "nan";
return "NAN";
}

if (Math::is_inf(p_num)) {
if (signbit(p_num)) {
return "-inf";
return "-INF";
} else {
return "inf";
return "INF";
}
}

Expand Down
48 changes: 28 additions & 20 deletions core/variant/variant_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ const char *VariantParser::tk_name[TK_MAX] = {
};

static double stor_fix(const String &p_str) {
if (p_str == "inf") {
if (p_str == "INF") {
return INFINITY;
} else if (p_str == "-INF") {
return -INFINITY;
} else if (p_str == "NAN") {
return NAN;
} else if (p_str == "inf") {
return INFINITY;
} else if (p_str == "inf_neg") {
return -INFINITY;
Expand Down Expand Up @@ -412,23 +418,20 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
if (cchar <= 32) {
break;
}

if (cchar == '-' || (cchar >= '0' && cchar <= '9')) {
StringBuffer<> token_text;
if (cchar == '-') {
token_text += '-';
cchar = p_stream->get_char();
}
if (cchar >= '0' && cchar <= '9') {
//a number

StringBuffer<> num;
#define READING_SIGN 0
#define READING_INT 1
#define READING_DEC 2
#define READING_EXP 3
#define READING_DONE 4
int reading = READING_INT;

if (cchar == '-') {
num += '-';
cchar = p_stream->get_char();
}

char32_t c = cchar;
bool exp_sign = false;
bool exp_beg = false;
Expand Down Expand Up @@ -475,7 +478,7 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
if (reading == READING_DONE) {
break;
}
num += c;
token_text += c;
c = p_stream->get_char();
}

Expand All @@ -484,25 +487,24 @@ Error VariantParser::get_token(Stream *p_stream, Token &r_token, int &line, Stri
r_token.type = TK_NUMBER;

if (is_float) {
r_token.value = num.as_double();
r_token.value = token_text.as_double();
} else {
r_token.value = num.as_int();
r_token.value = token_text.as_int();
}
return OK;
} else if (is_ascii_alphabet_char(cchar) || is_underscore(cchar)) {
StringBuffer<> id;
bool first = true;

while (is_ascii_alphabet_char(cchar) || is_underscore(cchar) || (!first && is_digit(cchar))) {
id += cchar;
token_text += cchar;
cchar = p_stream->get_char();
first = false;
}

p_stream->saved = cchar;

r_token.type = TK_IDENTIFIER;
r_token.value = id.as_string();
r_token.value = token_text.as_string();
return OK;
} else {
r_err_str = "Unexpected character.";
Expand Down Expand Up @@ -697,6 +699,12 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
value = false;
} else if (id == "null" || id == "nil") {
value = Variant();
} else if (id == "INF") {
value = INFINITY;
} else if (id == "-INF") {
value = -INFINITY;
} else if (id == "NAN") {
value = NAN;
} else if (id == "inf") {
value = INFINITY;
} else if (id == "inf_neg") {
Expand Down Expand Up @@ -1935,12 +1943,12 @@ static String rtos_fix(double p_value) {
if (p_value == 0.0) {
return "0"; //avoid negative zero (-0) being written, which may annoy git, svn, etc. for changes when they don't exist.
} else if (isnan(p_value)) {
return "nan";
return "NAN";
} else if (isinf(p_value)) {
if (p_value > 0) {
return "inf";
return "INF";
} else {
return "inf_neg";
return "-INF";
}
} else {
return rtoss(p_value);
Expand All @@ -1960,7 +1968,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str
} break;
case Variant::FLOAT: {
String s = rtos_fix(p_variant.operator double());
if (s != "inf" && s != "inf_neg" && s != "nan") {
if (s != "INF" && s != "-INF" && s != "NAN") {
if (!s.contains_char('.') && !s.contains_char('e')) {
s += ".0";
}
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Vector2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
<constant name="ONE" value="Vector2(1, 1)">
One vector, a vector with all components set to [code]1[/code].
</constant>
<constant name="INF" value="Vector2(inf, inf)">
<constant name="INF" value="Vector2(INF, INF)">
Infinity vector, a vector with all components set to [constant @GDScript.INF].
</constant>
<constant name="LEFT" value="Vector2(-1, 0)">
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Vector3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
<constant name="ONE" value="Vector3(1, 1, 1)">
One vector, a vector with all components set to [code]1[/code].
</constant>
<constant name="INF" value="Vector3(inf, inf, inf)">
<constant name="INF" value="Vector3(INF, INF, INF)">
Infinity vector, a vector with all components set to [constant @GDScript.INF].
</constant>
<constant name="LEFT" value="Vector3(-1, 0, 0)">
Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Vector4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
<constant name="ONE" value="Vector4(1, 1, 1, 1)">
One vector, a vector with all components set to [code]1[/code].
</constant>
<constant name="INF" value="Vector4(inf, inf, inf, inf)">
<constant name="INF" value="Vector4(INF, INF, INF, INF)">
Infinity vector, a vector with all components set to [constant @GDScript.INF].
</constant>
</constants>
Expand Down
7 changes: 7 additions & 0 deletions misc/extension_api_validation/4.3-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,10 @@ Validate extension JSON: Error: Field 'classes/EditorInterface/methods/open_scen

Added optional argument to open_scene_from_path to create a new inherited scene.
Compatibility method registered.


GH-
---------
Validate extension JSON: Error: Field 'builtin_classes/Vector2/constants/INF': value changed value in new API, from "Vector2(inf, inf)" to "Vector2(INF, INF)".
Validate extension JSON: Error: Field 'builtin_classes/Vector3/constants/INF': value changed value in new API, from "Vector3(inf, inf, inf)" to "Vector3(INF, INF, INF)".
Validate extension JSON: Error: Field 'builtin_classes/Vector4/constants/INF': value changed value in new API, from "Vector4(inf, inf, inf, inf)" to "Vector4(INF, INF, INF, INF)".
4 changes: 2 additions & 2 deletions modules/gdscript/doc_classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@
<constant name="TAU" value="6.28318530717959">
The circle constant, the circumference of the unit circle in radians. This is equivalent to [code]PI * 2[/code], or 360 degrees in rotations.
</constant>
<constant name="INF" value="inf">
<constant name="INF" value="INF">
Positive floating-point infinity. This is the result of floating-point division when the divisor is [code]0.0[/code]. For negative infinity, use [code]-INF[/code]. Dividing by [code]-0.0[/code] will result in negative infinity if the numerator is positive, so dividing by [code]0.0[/code] is not the same as dividing by [code]-0.0[/code] (despite [code]0.0 == -0.0[/code] returning [code]true[/code]).
[b]Warning:[/b] Numeric infinity is only a concept with floating-point numbers, and has no equivalent for integers. Dividing an integer number by [code]0[/code] will not result in [constant INF] and will result in a run-time error instead.
</constant>
<constant name="NAN" value="nan">
<constant name="NAN" value="NAN">
"Not a Number", an invalid floating-point value. [constant NAN] has special properties, including that [code]!=[/code] always returns [code]true[/code], while other comparison operators always return [code]false[/code]. This is true even when comparing with itself ([code]NAN == NAN[/code] returns [code]false[/code] and [code]NAN != NAN[/code] returns [code]true[/code]). It is returned by some invalid operations, such as dividing floating-point [code]0.0[/code] by [code]0.0[/code].
[b]Warning:[/b] "Not a Number" is only a concept with floating-point numbers, and has no equivalent for integers. Dividing an integer [code]0[/code] by [code]0[/code] will not result in [constant NAN] and will result in a run-time error instead.
</constant>
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/doc_classes/GLTFLight.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
The outer angle of the cone in a spotlight. Must be greater than or equal to the inner angle.
At this angle, the light drops off to zero brightness. Between the inner and outer cone angles, there is a transition from full brightness to zero brightness. If this angle is a half turn, then the spotlight emits in all directions. When creating a Godot [SpotLight3D], the outer cone angle is used as the angle of the spotlight.
</member>
<member name="range" type="float" setter="set_range" getter="get_range" default="inf">
<member name="range" type="float" setter="set_range" getter="get_range" default="INF">
The range of the light, beyond which the light has no effect. glTF lights with no range defined behave like physical lights (which have infinite range). When creating a Godot light, the range is clamped to 4096.
</member>
</members>
Expand Down
4 changes: 0 additions & 4 deletions scene/gui/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ void Range::set_value(double p_val) {
}

void Range::_set_value_no_signal(double p_val) {
if (!Math::is_finite(p_val)) {
return;
}

if (shared->step > 0) {
p_val = Math::round((p_val - shared->min) / shared->step) * shared->step + shared->min;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/core/string/test_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ TEST_CASE("[String] sprintf") {
args.push_back(INFINITY);
output = format.sprintf(args, &error);
REQUIRE(error == false);
CHECK(output == String("fish inf frog"));
CHECK(output == String("fish INF frog"));

// Real right-padded.
format = "fish %-11f frog";
Expand Down Expand Up @@ -1041,13 +1041,13 @@ TEST_CASE("[String] sprintf") {
REQUIRE(error == false);
CHECK(output == String("fish ( 19.990000, 1.000000, -2.050000) frog"));

// Vector left-padded with inf/nan
// Vector left-padded with INF/NAN
format = "fish %11v frog";
args.clear();
args.push_back(Variant(Vector2(INFINITY, NAN)));
output = format.sprintf(args, &error);
REQUIRE(error == false);
CHECK(output == String("fish ( inf, nan) frog"));
CHECK(output == String("fish ( INF, NAN) frog"));

// Vector right-padded.
format = "fish %-11v frog";
Expand Down
4 changes: 2 additions & 2 deletions tests/core/variant/test_variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ TEST_CASE("[Variant] Writer and parser Variant::FLOAT") {
VariantWriter::write_to_string(a64, a64_str);

CHECK_MESSAGE(a64_str == "1.79769e+308", "Writes in scientific notation.");
CHECK_MESSAGE(a64_str != "inf", "Should not overflow.");
CHECK_MESSAGE(a64_str != "nan", "The result should be defined.");
CHECK_MESSAGE(a64_str != "INF", "Should not overflow.");
CHECK_MESSAGE(a64_str != "NAN", "The result should be defined.");

String errs;
int line;
Expand Down

0 comments on commit 5b15a8d

Please sign in to comment.