1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #25351 from Kumataro:fix25073_format_g

core: persistence: output reals as human-friendly expression. #25351

Close #25073
Related https://github.com/opencv/opencv/pull/25087

This patch is need to merge same time with https://github.com/opencv/opencv_contrib/pull/3714

### Pull Request Readiness Checklist

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
This commit is contained in:
Kumataro
2024-04-10 21:17:15 +09:00
committed by GitHub
parent cf3a130be6
commit b14ea19466
9 changed files with 81 additions and 52 deletions
+17 -5
View File
@@ -76,9 +76,11 @@ char* doubleToString( char* buf, size_t bufSize, double value, bool explicitZero
}
else
{
static const char* fmt = "%.16e";
// binary64 has 52 bit fraction with hidden bit.
// 53 * log_10(2) is 15.955. So "%.16f" should be fine, but its test fails.
snprintf( buf, bufSize, "%.17g", value );
char* ptr = buf;
snprintf( buf, bufSize, fmt, value );
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )
@@ -118,11 +120,21 @@ char* floatToString( char* buf, size_t bufSize, float value, bool halfprecision,
}
else
{
char* ptr = buf;
if (halfprecision)
snprintf(buf, bufSize, "%.4e", value);
{
// bfloat16 has 7 bit fraction with hidden bit.
// binary16 has 10 bit fraction with hidden bit.
// 11 * log_10(2) is 3.311. So "%.4f" should be fine, but its test fails.
snprintf(buf, bufSize, "%.5g", value);
}
else
snprintf(buf, bufSize, "%.8e", value);
{
// binray32 has 23 bit fraction with hidden bit.
// 24 * log_10(2) is 7.225. So "%.8f" should be fine, but its test fails.
snprintf(buf, bufSize, "%.9g", value);
}
char* ptr = buf;
if( *ptr == '+' || *ptr == '-' )
ptr++;
for( ; cv_isdigit(*ptr); ptr++ )