cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

The default float epsilon is too small

Open dumganhar opened this issue 3 years ago • 2 comments

creator:

https://github.com/cocos/cocos-engine/blob/v3.6.0/native/cocos/math/Utils.h#L11

// in cocos/math/Utils.h, we define the default value
constexpr auto EPSILON = 0.000001; // 5 zeros after point

cpp code was ported from ts: https://github.com/cocos/cocos-engine/blob/v3.6.0/cocos/core/math/utils.ts#L33

But js uses double to represent float number, but cpp uses float instead. The precision is lower than which in ts.

godot:

https://github.com/godotengine/godot/blob/fe52458154c64fb1b741df4f7bd10106395f7cbd/core/math/math_defs.h#L34

#define CMP_EPSILON 0.00001 // // 4 zeros after point

I think godot's implementation is better.

dumganhar avatar Jul 25 '22 06:07 dumganhar

Currently, the unit test here https://github.com/cocos/cocos-engine/pull/12169 will fail. If change it to 4 zeros after point, it will succeed.

dumganhar avatar Jul 25 '22 06:07 dumganhar

May be ts can also change to 0.00001 too. Babylon.js just use 0.001.

minggo avatar Jul 25 '22 07:07 minggo

Math library is using 0.0001 for precision now: https://github.com/cocos/cocos-engine/pull/12610 Should not change the default precision in math::isEquals function since there're many places use it, for example in GFX module. Change the milestone to 3.7

dumganhar avatar Aug 30 '22 07:08 dumganhar

@hana-alice suggested not to modify the default precision of math::isEquals since GFX code depends on the it. If we change it, we need to do lots of tests.

Since all math classes have a approxEquals method with default precision of #define CC_FLOAT_CMP_PRECISION (0.00001F), I think this issue could be closed.

dumganhar avatar Nov 11 '22 04:11 dumganhar