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
Vector2 angle inconsistency due to negative zero #43407
Comments
Does this cause any problems in practice? i.e. This is correct behaviour as determined by the CPU, afaik, which can store both positive and negative zero according to IEEE 754 standard. |
The problem was discovered because I was dividing a network of points and lines into polygons by sorting the edges from each point according to the angle. My code broke because I had assumed that if (p - q).angle() == (-(q - p)).angle() This obviously true statement is unexpectedly false when Just because it's in the IEEE standard doesn't mean we have to like it. I mean, I really hate radians which stops us from doing something simple like turning the playing area upside down with a 180 rotation (there's always a crappy 0.0000001 factor in there when you specify angles in this useless unit). GDscript seems to have kept negative zeros from showing up in normal calculations (eg you don't get one when you write real_t Vector2::angle() const {
return (x != 0 && y != 0 ? Math::atan2(y, x) : 0);
} This isn't going to add much overhead to the already heavily slow trig function. If we were after raw speed and efficiency, there is the alternative of the DiamondArg function which has many advantages to it: |
Your scalar arithmetic example uses integers, in which there are no signed zeros:
Try with floats:
As for
We could add a hack to work it around in |
This is a non-issue. Seeking the angle of a zero vector to the x-axis is meaningless; so the correct answer is "undefined". The fact that C++ provides a consistent result (either 0 or ±π depending on the ±0 of the components) doesn't change the fact that the answer is meaningless.
Yes, the same way one needs to check for zero before attempting to divide by zero, one should check for a zero length vector before trying to find its angle. |
If that is the case, we should safely assume that any time As there was no documentation about this condition I tested the function on If this function is not going to give an answer that is predictable enough to design against (and these |
I disagree, the answer is correct; so no warning is needed.
So, as I said before, the same way one needs to check for zero before attempting to divide by zero, one should check for a negligible distance between two points, before trying to find the angle between those two points. This is the user's responsibility, not the engine's, because the engine will return the right answer regardless of what the user is trying to do. |
I do |
@ivanvoid how does |
@AThousandShips way to reproduce:
will output |
Why? It's supposed to be somewhere between -3 and 0, it could only be 0 if delta is exactly 1, you are again dealing with precision errors, also are you certain it is exactly -0 or just rounded to -0, for example -0.0000001 But again this issue isn't about negative zero in general but angles, if you still think this is a bug please open a separate issue as this is unrelated |
But as pointed above it's not that important I guess. Just kinda unexpected behavior that you need to work around. |
I've explained to you? And said to open a separate report, why aren't you listening to what I'm explaining and suggesting, this is off topic to this issue which is about |
But I'll explain it again: And in fact as time goes on the value should eventually become stuck at the smallest number below -0 as when it reaches that it won't become 0 unless delta is large enough, at least 0.25 or 0.5, depending on various internals of floating point math. The reason being that v, being the smallest number that can be represented, is multiplied by some number smaller than 0.5, rounds down, and you never move away from v, as lerp in this case is I hope this has explained why it probably won't work as you expect |
Godot version:
3.2.3-stable
OS/device including version:
Windows 10
Issue description:
I don't know why Vector2 (and Vector3) are in particular allowing this problematic negative zero value to get into the system when it doesn't happen in scalar arithmetic, as seen here:
A different issue caused by Vector2 providing a gateway to negative zero pollution was patched over in a previous pull-request here: #6698
Steps to reproduce:
See above
Minimal reproduction project:
N/A
The text was updated successfully, but these errors were encountered: