-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always add decimal when printing float #47502
base: master
Are you sure you want to change the base?
Conversation
Does this change apply automatically to floats within Vectors/Colors/Transforms/Matrices as well? |
Hmm, it doesn't. Changing it might be not as easy :I |
Ok, so I extended this to these types (in addition to floats): Vector2, Rect2, Vector3. |
Also need to solve this problem:
|
The precision problem doesn't look related. |
The problem is not with the precision of the numbers themselves, but that |
Ah, right. I don't know how to fix this tbh. I can't use Well maybe I could search for |
Ok changed it. Here's the FLOAT_STRING as it was originally:
The new one is slightly less efficient, but maybe it doesn't matter that much. |
We approved the change in a PR review meeting today, though we were concerned about the performance cost of the A better option might be to use |
Rebased and reworked. I removed the macro and just changed usages of
->
|
So I just noticed that changing |
modules/gdscript/tests/scripts/parser/features/nested_arithmetic.out
Outdated
Show resolved
Hide resolved
This seems to break the generated Mono glue:
CC @godotengine/mono |
@akien-mga It's likely because the bindings generator now generates code using literal The generator uses godot/modules/mono/editor/bindings_generator.cpp Lines 3188 to 3207 in 44516d1
godot/modules/mono/editor/bindings_generator.cpp Lines 3251 to 3287 in 44516d1
For example, we use it to generate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the Mono bindings generator, we'll probably need to manually generate each piece of the constructor.
640fb5d
to
ae092ad
Compare
@@ -1360,6 +1360,9 @@ static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_ | |||
Dictionary elem = new_api[i]; | |||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks JSON 😬
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested locally, it works as expected with float
but not Vector2
(tested on single-precision build).
func _ready() -> void:
print(-0.0 * Vector2.ONE)
print(-0.0000000001 * Vector2.ONE)
print(-1.0 * Vector2.ONE)
print(-1.0000000001 * Vector2.ONE)
print(0.0 * Vector2.ONE)
print(0.0000000001 * Vector2.ONE)
print(1.0 * Vector2.ONE)
print(1.0000000001 * Vector2.ONE)
Results in:
(0.0, 0.0)
(-0, -0)
(-1.0, -1.0)
(-1.0, -1.0)
(0.0, 0.0)
(0, 0)
(1.0, 1.0)
(1.0, 1.0)
In master
, this is the output:
(0, 0)
(-0, -0)
(-1, -1)
(-1, -1)
(0, 0)
(0, 0)
(1, 1)
(1, 1)
Notice the 0
and -0
which shouldn't be here, as Vector2 is always floating-point.
The same occurs with Vector3
.
While this issue is not introduced by this PR, I think people will have more confidence in the displayed type if we merge this PR. This means that we need to get the displayed type right, as showing a misleading type will be a lot more problematic once this is merged.
Fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested locally, it works as expected with vector types now:
(0.0, 0.0)
(-0.0, -0.0)
(-1.0, -1.0)
(-1.0, -1.0)
(0.0, 0.0)
(0.0, 0.0)
(1.0, 1.0)
(1.0, 1.0)
It still says -0.0
on the second line, but this is technically more correct given the number is being rounded from a very low negative value. (This rounding should be addressed in a separate PR.)
The PR also affects the inspector correctly as well now:
c414ac0
to
65a0b61
Compare
Since this PR ended up improving the output of String::num() I noticed that this same code also reports back if a float/double is "nan" or "inf". For "inf" it also checks, using signbit(), if it's "+inf" or "-inf". I was wondering if it makes sense to also do this for zero values and nan values. I don't think this is just a theoretical improvement either. Let me give an example I bumped into recently. When you calculate the "arc tangent" of a number, an often used way to do that is using some implementation (see for example https://en.cppreference.com/w/cpp/numeric/math/atan2) of atan2(y, x). There are edge cases where this function will return a different value based on if one of the inputs was +0 or -0, therefore I think it makes sense if printing such a value would show the sign bit correctly. I'm not saying this should be part of this PR since it addresses a different issue, it's just that me reading this triggered the thought. |
This PR breaks line numbers in the script editor. Line numbers are integers, they should not have I tested this on GodSVG, a project that heavily relies on float stringification, and here are the results: The resulting SVG text looks quite different, but it is actually completely valid, just not the most minimal. This is a good sign that even in UI-heavy applications, this PR will not break things. GodSVG already has code to minify floats, which is why |
I changed EDIT: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this on The Mirror and the app still works fine, and in fact it highlighted a pre-existing bug which I've fixed here the-mirror-gdp/the-mirror#115
I am clicking approve because I support this change, the code looks good from a quick review, and I have tested it on 2 large projects. However, I am approving it on the condition that this is merged immediately after the release of Godot 4.3, so that it can get lots of testing in master before being included in Godot 4.4, since this PR changes a fundamental part of the engine and is inherently risky it needs testing. I also removed the cherrypick:3.x
label for the same reason.
So that
prints
Makes working with numbers less confusing. I remember someone mentioned it around godotengine/godot-proposals#1866
Closes godotengine/godot-proposals#7894