Skip to content

Fix: -Wfloat-conversion error in print_number on MinGW - #1061

Open
coveyjorjet wants to merge 1 commit into
DaveGamble:masterfrom
coveyjorjet:fix/float-conversion-warning-mingw
Open

Fix: -Wfloat-conversion error in print_number on MinGW#1061
coveyjorjet wants to merge 1 commit into
DaveGamble:masterfrom
coveyjorjet:fix/float-conversion-warning-mingw

Conversation

@coveyjorjet

Copy link
Copy Markdown

Closes #963

Problem

On MinGW-w64, isnan/isinf in math.h are macros that dispatch to float-typed helpers (__isnanf, __fpclassifyf, ...) via __builtin_choose_expr. Passing a double then triggers:

error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]

when building with -Wconversion/-Wfloat-conversion + -Werror (as cJSON's own CMake custom flags do).

Fix

Replace the isnan(d) || isinf(d) call in print_number with a pure C89 IEEE-754 check that only operates on double:

if ((d != d) || (d > DBL_MAX) || (d < -DBL_MAX))
  • d != d is true exactly for NaN
  • d > DBL_MAX / d < -DBL_MAX are true exactly for +/-Inf (DBL_MAX is the largest finite double)

This avoids the platform isnan/isinf macros entirely, so no double-to-float conversion warning is generated. It is also fully ANSI C (C89), so it works on old compilers such as GCC 2.95 that do not provide __builtin_isnan/__builtin_isinf, and on non-GCC compilers that define __GNUC__.

The now-unused ANSI C fallback isnan/isinf macros were also removed (they would otherwise trigger -Wunused-macros on platforms where they get defined).

Verification

  • Compiles clean with cJSON's strict flags: -std=c89 -pedantic -Wall -Wextra -Werror -Wconversion -Wfloat-conversion -Wunused-macros ...
  • make test passes
  • All 20 standalone test binaries pass
  • NaN, +Inf, -Inf print as null; normal numbers unchanged

isnan/isinf in MinGW-w64's math.h dispatch to float-typed helpers,
so passing a double triggers -Wconversion/-Wfloat-conversion errors
under -Werror.

Replace the call with a pure C89 IEEE-754 check on double only:
(d != d) detects NaN, and comparisons against DBL_MAX detect infinity.
This avoids the platform isnan/isinf macros entirely, so it builds on
older compilers (e.g. GCC 2.95) that lack __builtin_isnan/isinf and
on other compilers that define __GNUC__.

Also remove the now-unused ANSI C isnan/isinf fallback macros.
@wissem01chiha

wissem01chiha commented Aug 1, 2026

Copy link
Copy Markdown

Thanks very much @coveyjorjet , for the PR , do you have any idea why it failed with GNU 15.2.0 and worked with GNU 15.3.0 ? or maybe i have something in my local setup that overlap like includes from other compilers (i MSVC and clang installed) ? thanks anyway

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]

2 participants