Skip to content

false positive in unittests due to not accounting for limited floating point accuracy #352

Closed

Description

@maribu

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.

Activity

  1. thiagomacieira commented on Nov 9, 2026

    @thiagomacieira
    Member

    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.

  2. maribu commented on Nov 9, 2026

    @maribu
    Author

    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.

  3. thiagomacieira commented on Nov 9, 2026

    @thiagomacieira
    Member

    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.

  4. Anmol-In-Verse commented on Nov 13, 2026

    @Anmol-In-Verse
    Contributor

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions