Skip to content

Commit

Permalink
Always add decimal when printing float
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Apr 19, 2024
1 parent 4a01602 commit 73451d9
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 113 deletions.
6 changes: 6 additions & 0 deletions core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
Dictionary elem = var;
ERR_FAIL_COND_V_MSG(!elem.has(p_name_field), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", base_array, p_name_field));
String name = elem[p_name_field];
if (name.is_valid_float()) {
name = name.trim_suffix(".0"); // Make "integers" stringified as integers.
}
if (p_compare_operators && elem.has("right_type")) {
name += " " + String(elem["right_type"]);
}
Expand All @@ -1371,6 +1374,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_
continue;
}
String name = old_elem[p_name_field];
if (name.is_valid_float()) {
name = name.trim_suffix(".0"); // Make "integers" stringified as integers.
}
if (p_compare_operators && old_elem.has("right_type")) {
name += " " + String(old_elem["right_type"]);
}
Expand Down
2 changes: 1 addition & 1 deletion core/math/rect2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_re
}

Rect2::operator String() const {
return "[P: " + position.operator String() + ", S: " + size + "]";
return "[P: " + position.operator String() + ", S: " + size.operator String() + "]";
}

Rect2::operator Rect2i() const {
Expand Down
2 changes: 1 addition & 1 deletion core/math/vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ bool Vector2::is_finite() const {
}

Vector2::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")";
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ")";
}

Vector2::operator Vector2i() const {
Expand Down
2 changes: 1 addition & 1 deletion core/math/vector3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool Vector3::is_finite() const {
}

Vector3::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ")";
return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ")";
}

Vector3::operator Vector3i() const {
Expand Down
28 changes: 21 additions & 7 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ String String::num(double p_num, int p_decimals) {
#endif

buf[324] = 0;
//destroy trailing zeroes
// Destroy trailing zeroes, except one after period.
{
bool period = false;
int z = 0;
Expand All @@ -1620,7 +1620,7 @@ String String::num(double p_num, int p_decimals) {
if (buf[z] == '0') {
buf[z] = 0;
} else if (buf[z] == '.') {
buf[z] = 0;
buf[z + 1] = '0';
break;
} else {
break;
Expand Down Expand Up @@ -1709,14 +1709,28 @@ String String::num_real(double p_num, bool p_trailing) {
return num_int64((int64_t)p_num);
}
}
#ifdef REAL_T_IS_DOUBLE
int decimals = 14;
#else
// We want to align the digits to the above sane default, so we only need
// to subtract log10 for numbers with a positive power of ten magnitude.
const double abs_num = Math::abs(p_num);
if (abs_num > 10) {
decimals -= (int)floor(log10(abs_num));
}
return num(p_num, decimals);
}

String String::num_real(float p_num, bool p_trailing) {
if (p_num == (float)(int64_t)p_num) {
if (p_trailing) {
return num_int64((int64_t)p_num) + ".0";
} else {
return num_int64((int64_t)p_num);
}
}
int decimals = 6;
#endif
// We want to align the digits to the above sane default, so we only need
// to subtract log10 for numbers with a positive power of ten magnitude.
double abs_num = Math::abs(p_num);
const float abs_num = Math::abs(p_num);
if (abs_num > 10) {
decimals -= (int)floor(log10(abs_num));
}
Expand Down Expand Up @@ -4025,7 +4039,7 @@ String String::humanize_size(uint64_t p_size) {
}

if (magnitude == 0) {
return String::num(p_size) + " " + RTR("B");
return String::num_uint64(p_size) + " " + RTR("B");
} else {
String suffix;
switch (magnitude) {
Expand Down
1 change: 1 addition & 0 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ class String {
static String num(double p_num, int p_decimals = -1);
static String num_scientific(double p_num);
static String num_real(double p_num, bool p_trailing = true);
static String num_real(float p_num, bool p_trailing = true);
static String num_int64(int64_t p_num, int base = 10, bool capitalize_hex = false);
static String num_uint64(uint64_t p_num, int base = 10, bool capitalize_hex = false);
static String chr(char32_t p_char);
Expand Down
2 changes: 1 addition & 1 deletion core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ String Variant::stringify(int recursion_count) const {
case INT:
return itos(_data._int);
case FLOAT:
return rtos(_data._float);
return String::num_real(_data._float, true);
case STRING:
return *reinterpret_cast<const String *>(_data._mem);
case VECTOR2:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GDTEST_OK
0
0.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const const_packed_ints: PackedFloat64Array = [52]
@warning_ignore("assert_always_true")
func test():
assert(typeof(const_float_int) == TYPE_FLOAT)
assert(str(const_float_int) == '19')
assert(str(const_float_int) == '19.0')
assert(typeof(const_float_plus) == TYPE_FLOAT)
assert(str(const_float_plus) == '34')
assert(str(const_float_plus) == '34.0')
assert(typeof(const_float_cast) == TYPE_FLOAT)
assert(str(const_float_cast) == '76')
assert(str(const_float_cast) == '76.0')

assert(typeof(const_packed_empty) == TYPE_PACKED_FLOAT64_ARRAY)
assert(str(const_packed_empty) == '[]')
assert(typeof(const_packed_ints) == TYPE_PACKED_FLOAT64_ARRAY)
assert(str(const_packed_ints) == '[52]')
assert(str(const_packed_ints) == '[52.0]')
assert(typeof(const_packed_ints[0]) == TYPE_FLOAT)
assert(str(const_packed_ints[0]) == '52')
assert(str(const_packed_ints[0]) == '52.0')

print('ok')
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GDTEST_OK
4
4.0
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,39 @@ func test():
untyped_basic.push_back(430.0)
inferred_basic.push_back(263.0)
typed_basic.push_back(518.0)
assert(str(empty_floats) == '[705, 430, 263, 518]')
assert(str(untyped_basic) == '[705, 430, 263, 518]')
assert(str(inferred_basic) == '[705, 430, 263, 518]')
assert(str(typed_basic) == '[705, 430, 263, 518]')
assert(str(empty_floats) == '[705.0, 430.0, 263.0, 518.0]')
assert(str(untyped_basic) == '[705.0, 430.0, 263.0, 518.0]')
assert(str(inferred_basic) == '[705.0, 430.0, 263.0, 518.0]')
assert(str(typed_basic) == '[705.0, 430.0, 263.0, 518.0]')


const constant_float := 950.0
const constant_int := 170
var typed_float := 954.0
var filled_floats: Array[float] = [constant_float, constant_int, typed_float, empty_floats[1] + empty_floats[2]]
assert(str(filled_floats) == '[950, 170, 954, 693]')
assert(str(filled_floats) == '[950.0, 170.0, 954.0, 693.0]')
assert(filled_floats.get_typed_builtin() == TYPE_FLOAT)

var casted_floats := [empty_floats[2] * 2] as Array[float]
assert(str(casted_floats) == '[526]')
assert(str(casted_floats) == '[526.0]')
assert(casted_floats.get_typed_builtin() == TYPE_FLOAT)

var returned_floats = (func () -> Array[float]: return [554]).call()
assert(str(returned_floats) == '[554]')
assert(str(returned_floats) == '[554.0]')
assert(returned_floats.get_typed_builtin() == TYPE_FLOAT)

var passed_floats = floats_identity([663.0 if randf() > 0.5 else 663.0])
assert(str(passed_floats) == '[663]')
assert(str(passed_floats) == '[663.0]')
assert(passed_floats.get_typed_builtin() == TYPE_FLOAT)

var default_floats = (func (floats: Array[float] = [364.0]): return floats).call()
assert(str(default_floats) == '[364]')
assert(str(default_floats) == '[364.0]')
assert(default_floats.get_typed_builtin() == TYPE_FLOAT)

var typed_int := 556
var converted_floats: Array[float] = [typed_int]
converted_floats.push_back(498)
assert(str(converted_floats) == '[556, 498]')
assert(str(converted_floats) == '[556.0, 498.0]')
assert(converted_floats.get_typed_builtin() == TYPE_FLOAT)


Expand All @@ -96,23 +96,23 @@ func test():
assert(constant_basic.get_typed_builtin() == TYPE_NIL)

const constant_floats: Array[float] = [constant_float - constant_basic[0] - constant_int]
assert(str(constant_floats) == '[552]')
assert(str(constant_floats) == '[552.0]')
assert(constant_floats.get_typed_builtin() == TYPE_FLOAT)


var source_floats: Array[float] = [999.74]
untyped_basic = source_floats
var destination_floats: Array[float] = untyped_basic
destination_floats[0] -= 0.74
assert(str(source_floats) == '[999]')
assert(str(untyped_basic) == '[999]')
assert(str(destination_floats) == '[999]')
assert(str(source_floats) == '[999.0]')
assert(str(untyped_basic) == '[999.0]')
assert(str(destination_floats) == '[999.0]')
assert(destination_floats.get_typed_builtin() == TYPE_FLOAT)


var duplicated_floats := empty_floats.duplicate().slice(2, 3)
duplicated_floats[0] *= 3
assert(str(duplicated_floats) == '[789]')
assert(str(duplicated_floats) == '[789.0]')
assert(duplicated_floats.get_typed_builtin() == TYPE_FLOAT)


Expand Down
26 changes: 13 additions & 13 deletions modules/gdscript/tests/scripts/parser/features/export_arrays.out
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ var test_placeholder: Array
var test_placeholder_packed: PackedStringArray
hint=TYPE_STRING hint_string="String/PLACEHOLDER_TEXT:Placeholder" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_int: Array
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_int_packed_byte: PackedByteArray
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_int_packed32: PackedInt32Array
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_int_packed64: PackedInt64Array
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_int_float_step: Array
hint=TYPE_STRING hint_string="int/RANGE:1,10,0.01" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0,0.01" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_float: Array
hint=TYPE_STRING hint_string="float/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="float/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_float_packed32: PackedFloat32Array
hint=TYPE_STRING hint_string="float/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="float/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_float_packed64: PackedFloat64Array
hint=TYPE_STRING hint_string="float/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="float/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_exp_easing: Array
hint=TYPE_STRING hint_string="float/EXP_EASING:" usage=DEFAULT|SCRIPT_VARIABLE
var test_exp_easing_packed32: PackedFloat32Array
Expand Down Expand Up @@ -124,14 +124,14 @@ var test_weak_packed_vector2_array: PackedVector2Array
var test_weak_packed_vector3_array: PackedVector3Array
hint=TYPE_STRING hint_string="Vector3:Vector3" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_weak_packed_byte_array: PackedByteArray
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_weak_packed_int32_array: PackedInt32Array
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_weak_packed_int64_array: PackedInt64Array
hint=TYPE_STRING hint_string="int/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="int/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_weak_packed_float32_array: PackedFloat32Array
hint=TYPE_STRING hint_string="float/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="float/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_weak_packed_float64_array: PackedFloat64Array
hint=TYPE_STRING hint_string="float/RANGE:1,10" usage=DEFAULT|SCRIPT_VARIABLE
hint=TYPE_STRING hint_string="float/RANGE:1.0,10.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_noalpha_weak_packed_color_array: PackedColorArray
hint=TYPE_STRING hint_string="Color/COLOR_NO_ALPHA:" usage=DEFAULT|SCRIPT_VARIABLE
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ var test_storage_weak_int: Variant = 3
var test_storage_hard_int: int = 4
hint=NONE hint_string="" usage=STORAGE|SCRIPT_VARIABLE
var test_range: int = 100
hint=RANGE hint_string="0,100" usage=DEFAULT|SCRIPT_VARIABLE
hint=RANGE hint_string="0.0,100.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_step: int = 101
hint=RANGE hint_string="0,100,1" usage=DEFAULT|SCRIPT_VARIABLE
hint=RANGE hint_string="0.0,100.0,1.0" usage=DEFAULT|SCRIPT_VARIABLE
var test_range_step_or_greater: int = 102
hint=RANGE hint_string="0,100,1,or_greater" usage=DEFAULT|SCRIPT_VARIABLE
hint=RANGE hint_string="0.0,100.0,1.0,or_greater" usage=DEFAULT|SCRIPT_VARIABLE
var test_color: Color = Color(0, 0, 0, 1)
hint=NONE hint_string="Color" usage=DEFAULT|SCRIPT_VARIABLE
var test_color_no_alpha: Color = Color(0, 0, 0, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ true
0
-255
256
2
2.0
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ GDTEST_OK
---
-1234.4567
-1234.4567
-1234
-1234
-1234.0
-1234.0
0.4567
0.4567
---
-1234500
-1234500
-1234500
-1234500
-1234500.0
-1234500.0
-1234500.0
-1234500.0
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GDTEST_OK
8
8.0
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GDTEST_OK
{ 1: (2, 0) }
{ 3: (4, 0) }
[[(5, 0)]]
[[(6, 0)]]
[[(7, 0)]]
[X: (8, 9, 7), Y: (0, 1, 0), Z: (0, 0, 1), O: (0, 0, 0)]
{ 1: (2.0, 0.0) }
{ 3: (4.0, 0.0) }
[[(5.0, 0.0)]]
[[(6.0, 0.0)]]
[[(7.0, 0.0)]]
[X: (8.0, 9.0, 7.0), Y: (0.0, 1.0, 0.0), Z: (0.0, 0.0, 1.0), O: (0.0, 0.0, 0.0)]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GDTEST_OK
x is 1
x is 1.0
typeof x is 3
x is 2
x is 2.0
typeof x is 3
x is 3
x is 3.0
typeof x is 3
ok
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ a
1
b
1
(1, 1)
(0, 0)
(6, 1)
(0, 0)
(1.0, 1.0)
(0.0, 0.0)
(6.0, 1.0)
(0.0, 0.0)
Loading

0 comments on commit 73451d9

Please sign in to comment.