You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
false positive in unittests due to not accounting for limited floating point accuracy #352
The unit tests for tinycbor version 7.1 is failing on x86_64 on Alpine with:
FAIL! : tst_ToJson::metaDataForKeys(2.f^64) Compared values are not the same
Actual (decoded) : "{\"1.8446744073709551e+19f\":false,\"1.8446744073709551e+19f$keycbordump\":true}"
Expected (expected): "{\"1.8446744073709552e+19f\":false,\"1.8446744073709552e+19f$keycbordump\":true}"
Loc: [/home/maribu/Repos/software/aports/master/community/tinycbor/src/tinycbor-7.1/tests/tojson/tst_tojson.cpp(277)]
FAIL! : tst_ToJson::metaDataForKeys(2.^64) Compared values are not the same
Actual (decoded) : "{\"1.8446744073709551e+19\":false,\"1.8446744073709551e+19$keycbordump\":true}"
Expected (expected): "{\"1.8446744073709552e+19\":false,\"1.8446744073709552e+19$keycbordump\":true}"
Loc: [/home/maribu/Repos/software/aports/master/community/tinycbor/src/tinycbor-7.1/tests/tojson/tst_tojson.cpp(277)]
The issue here is that the unit tests is comparing two floats for being perfectly equal. This is a common source of failing unit tests in many software projects, as floating point arithmetic inherently is not perfectly accurate. Accuracy of floating point arithmetic depends on compiler options, hardware used, and the current alignment of stars. The unit tests should be relaxed to allow a small relative error between actual and expected value for all floating point comparisons.
No, they should not. The result is expected to be bit-exact and it is. There's no rounding in any floating-point number that is an exact power of two.
The problem I guess is that Alpine is using MUSL as its C library implementation and it is formatting the exact number 2^64 differently from glibc, Microsoft's Universal C Runtime, Apple's libc (I haven't tested FreeBSD in a while). The value is 18446744073709551616, so it should be rounded as the expected says.
Sorry, bug is in your libc. Feel free to submit a patch with an #if so the number is truncated instead of rounded to nearest at that place. I don't plan on working on this.
Note that the binary representation of floats uses an exponent to the power of two. The formatted scientific representation uses an exponent to the power of 10. The assumption that formatting a floating point number does not involve floating point arithmetic is not correct, at least not in the general case.
Hi, this implements the #if approach suggested in #352: on musl-based systems, the four affected test rows now expect the truncated 1.8446744073709551e+19 instead of the rounded 1.8446744073709552e+19.
Since musl defines no identification macro, it is detected by exclusion: Linux with neither glibc, uClibc nor Bionic.
Note: the CI workflow appears to be waiting for approval to run.
Activity
thiagomacieira commentedon Nov 9, 2026
No, they should not. The result is expected to be bit-exact and it is. There's no rounding in any floating-point number that is an exact power of two.
The problem I guess is that Alpine is using MUSL as its C library implementation and it is formatting the exact number 2^64 differently from glibc, Microsoft's Universal C Runtime, Apple's libc (I haven't tested FreeBSD in a while). The value is 18446744073709551616, so it should be rounded as the expected says.
Sorry, bug is in your libc. Feel free to submit a patch with an
#ifso the number is truncated instead of rounded to nearest at that place. I don't plan on working on this.maribu commentedon Nov 9, 2026
Note that the binary representation of floats uses an exponent to the power of two. The formatted scientific representation uses an exponent to the power of 10. The assumption that formatting a floating point number does not involve floating point arithmetic is not correct, at least not in the general case.
thiagomacieira commentedon Nov 9, 2026
That's fair, but it's only the formatting and one expects as close to exact as possible.
Anyway, it doesn't change that I don't plan on working on this myself.
Anmol-In-Verse commentedon Nov 13, 2026
Hi, this implements the #if approach suggested in #352: on musl-based systems, the four affected test rows now expect the truncated 1.8446744073709551e+19 instead of the rounded 1.8446744073709552e+19.
Since musl defines no identification macro, it is detected by exclusion: Linux with neither glibc, uClibc nor Bionic.
Note: the CI workflow appears to be waiting for approval to run.
Happy to make any changes if needed!