CryEngine 5.4 source code - float A != float B ? should it be absolute of float A - float B > Epsilon instead?

#1
For example, this code

Code: Select all

if (m_qRotation.v != qRotation.v || m_qRotation.w != qRotation.w) { nWhyFlags |= ENTITY_XFORM_ROT; changed++; m_qRotation = qRotation; }
where m_qRotation.w and qRotation.w are float values.
So imagine I'm debugging C++ 3rd person template project, I put the break point at this line. Because this line is called each milliseconds, so when I put breakpoint here, it constantly get hit, thus the mouse in game is not moved at all.
There is the case: m_qRotation.w = 0.999999940 and qRotation.w = 1.00000000, thus the condition m_qRotation.w != qRotation.w becomes true. Should it be crymath::abs(m_qRotation.w - qRotation.w) > EPSILON instead (where EPSILON = 0.00001)?
Last edited by HDN on Sun Jan 21, 2018 2:24 pm, edited 1 time in total.
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: CryEngine 5.4 source code - float A != float B ? should it be absolute of float A - float B > Epsilon instead?

#3
What if you tried this?

Code: Select all

if (!m_rotation.IsEquivalent(qRotation))
{
// do stuff if bother aren't equivalent
}
I just mean to ask if the code I mentioned (which is the code of the Engine) is a bug. And should we fix that code (and any similar one)?
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: CryEngine 5.4 source code - float A != float B ? should it be absolute of float A - float B > Epsilon instead?

#5
To be sure, you're worried about that condition in regards to performance?
Yes, but mostly I worry about the correctness of the code. Maybe you've already known, but according to this article
Floating point math is not exact. Simple values like 0.1 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations or the precision of intermediates can change the result. That means that comparing two floats to see if they are equal is usually not what you want. GCC even has a warning for this: “warning: comparing floating point with == or != is unsafe”.
What I'm talking about is in the "Epsilon comparisons" in the article above.
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: CryEngine 5.4 source code - float A != float B ? should it be absolute of float A - float B > Epsilon instead?

#7
I can't seem to find that expression in CE code base, can you kindly point me to the location?
At my first post, I pointed out this code in the file CRYENGINE/Code/CryEngine/CryEntitySystem/Entity.cpp
For example, this code

Code: Select all

if (m_qRotation.v != qRotation.v || m_qRotation.w != qRotation.w) { nWhyFlags |= ENTITY_XFORM_ROT; changed++; m_qRotation = qRotation; }
where m_qRotation.w and qRotation.w are float values.
The condition I'm talking about is if ( m_qRotation.w != qRotation.w)
Small tips
How to add an image to a forum post
[C++] How to smoothly turn your character

Re: CryEngine 5.4 source code - float A != float B ? should it be absolute of float A - float B > Epsilon instead?

#8
It is my understanding that since the values are stored from a previous update, these check if the value has changed and therefor they should be matched.
This tells the Entity system to propagate the fact that this entity has moved.

It is true that if you were to take a floating point and compare to certain values you may come across rounding issues which can cause comparison problems.
In the example you gave it should not be an issue since it is not checking for an exact number, it only checks if the value has changed in any way.
Uniflare
CRYENGINE Technical Community Manager
Here to help the community and social channels grow and thrive.

My personal belongings;
Beginner Guides | My GitHub | Splash Plugin

Who is online

Users browsing this forum: No registered users and 1 guest

cron