opencv/modules/core/test
Maxim Smolskiy 023d14ecc4
Merge pull request #27347 from MaximSmolskiy:improve-solveCubic-accuracy
Improve solveCubic accuracy #27347

### Pull Request Readiness Checklist

Fix #27323 

```
2e-13 * x^3 + x^2 - 2 * x + 1 = 0 -> x^3 + 5e12 * x^2 - 1e13 * x + 5e12 = 0
```

The problem that coefficients have quite big magnitudes and current calculations are subject to round-off error

```
Q = (a1 * a1 - 3 * a2) * (1./9)
R = (2 * a1 * a1 * a1 - 9 * a1 * a2 + 27 * a3) * (1./54)
Qcubed = Q * Q * Q = a1^6/729 - (a1^4 a2)/81 + (a1^2 a2^2)/27 - a2^3/27
R * R = R^2 = a1^6/729 - (a1^4 a2)/81 + (a1^2 a2^2)/36 + (a1^3 a3)/27 - (a1 a2 a3)/6 + a3^2/4
d = Qcubed - R * R
```

Let `a1`, `a2`, `a3` have quite big same magnitudes, then we see that `Qcubed` and `R * R` have same terms `a1^6/729` and `-(a1^4 a2)/81` (which will be reduced in `d`), but they level out the other terms (these terms have `6`th and `5`th degree and other terms - less or equal than `4`th degree).
So, if these terms will participate in the calculation, this will lead to a huge round-off error.
But if we expand the expression, then round-off error should be less
```
d = Qcubed - R * R = 1/108 (a1^2 a2^2 - 4 a2^3 - 4 a1^3 a3 + 18 a1 a2 a3 - 27 a3^2)
```

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
2025-05-24 09:56:48 +03:00
..
ocl core(ocl): fix POWN OpenCL implementation 2025-05-01 20:57:23 +00:00
ref_reduce_arg.impl.hpp test: fix technical issue with min_element in reduce tests 2024-11-26 21:55:41 +03:00
test_allocator.cpp Set and check allocator pointer for all cv::Mat instances. 2024-08-05 10:07:14 +03:00
test_arithm.cpp fix exp, log | enable ui for log | strengthen test 2025-03-07 17:11:26 +00:00
test_async.cpp Merge pull request #23736 from seanm:c++11-simplifications 2024-01-19 16:53:08 +03:00
test_concatenation.cpp Added more strict checks for empty inputs to compare, meanStdDev and RNG::fill 2018-07-26 18:06:38 +03:00
test_conjugate_gradient.cpp ts: refactor OpenCV tests 2018-02-03 19:39:47 +00:00
test_countnonzero.cpp Add missing std namespace qualifiers 2023-09-06 13:46:39 +03:00
test_cuda.cpp add cuda::Stream constructor with cuda flags 2021-01-28 16:14:01 +01:00
test_downhill_simplex.cpp Misc. modules/ typos 2018-02-12 07:09:43 -05:00
test_ds.cpp Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2021-12-03 12:32:49 +00:00
test_dxt.cpp Partially back-port #25075 to 4.x 2024-03-05 12:15:39 +03:00
test_eigen.cpp Fix modules/ typos 2019-08-16 17:34:29 +03:00
test_hal_core.cpp fix exp, log | enable ui for log | strengthen test 2025-03-07 17:11:26 +00:00
test_hasnonzero.cpp Merge pull request #22947 from chacha21:hasNonZero 2023-06-09 13:37:20 +03:00
test_intrin128.simd.hpp Merge pull request #22179 from hanliutong:new-rvv 2022-07-19 20:02:00 +03:00
test_intrin256.simd.hpp core(test): intrinsic tests for all dispatched CPU optimizations 2018-08-01 13:50:42 +03:00
test_intrin512.simd.hpp Merge pull request #14210 from terfendail:wui_512 2019-06-03 18:05:35 +03:00
test_intrin_emulator.cpp Merge pull request #16236 from alalek:fix_core_simd_emulator 2020-01-10 21:31:02 +03:00
test_intrin_utils.hpp Merge pull request #27343 from fengyuentau:4x/build/fix_more_warnings 2025-05-21 16:12:09 +03:00
test_intrin.cpp core: fix F16C compilation check 2020-11-17 12:22:49 +00:00
test_io.cpp Refactor test for FileStorage Base64 2025-02-09 01:38:14 +03:00
test_logger_replace.cpp Merge pull request #27154 from kinchungwong:logging_callback_simple_c 2025-03-30 16:17:07 +03:00
test_logtagconfigparser.cpp Merge pull request #13909 from kinchungwong:logging_20190220 2019-04-22 00:01:10 +03:00
test_logtagmanager.cpp build: reduce usage of constexpr 2019-04-22 15:41:27 +03:00
test_lpsolver.cpp Merge remote-tracking branch 'origin/3.4' into merge-3.4 2023-06-20 09:56:57 +03:00
test_main.cpp Merge pull request #11897 from Jakub-Golinowski:hpx_backend 2018-08-31 16:23:26 +03:00
test_mat.cpp Add missing Mat_<_Tp>::zeros(int _ndims, const int* _sizes) 2025-05-16 10:50:15 +02:00
test_math.cpp Merge pull request #27347 from MaximSmolskiy:improve-solveCubic-accuracy 2025-05-24 09:56:48 +03:00
test_misc.cpp Merge pull request #26022 from Kumataro:fix26016 2024-08-23 12:35:13 +03:00
test_opencl.cpp UMat usageFlags fixes opencv/opencv#19807 2021-06-03 16:33:03 +02:00
test_operations.cpp Merge pull request #27184 from CodeLinaro:gemm_fastcv_hal 2025-04-25 11:07:26 +03:00
test_precomp.hpp Merge pull request #23736 from seanm:c++11-simplifications 2024-01-19 16:53:08 +03:00
test_ptr.cpp 3.4: backported changes from 'master' branch 2019-08-14 16:36:08 +03:00
test_quaternion.cpp Merge pull request #19026 from chargerKong:dualquat 2021-02-17 17:05:08 +00:00
test_rand.cpp Merge pull request #26259 from Kumataro:fix26258 2024-10-08 15:55:00 +03:00
test_rotatedrect.cpp ts: refactor OpenCV tests 2018-02-03 19:39:47 +00:00
test_umat.cpp fix legacy constants 2022-01-03 15:08:10 +03:00
test_utils_tls.impl.hpp Merge pull request #23736 from seanm:c++11-simplifications 2024-01-19 16:53:08 +03:00
test_utils.cpp replace lena.jpg in find-existing-file tests 2024-05-25 08:53:33 +01:00