Skip to content

Fix -Wimplicit-const-int-float-conversion warning in selectTemplate.c #40

Description

@kalwalt

Description

During the compilation of WebARKitLib with Emscripten, the following warning is triggered in src/AR2/selectTemplate.c:

C:/path/to/webarkit-org/jsartoolkitNFT/emscripten/WebARKitLib/lib/SRC/AR2/selectTemplate.c:279:41: warning: implicit conversion from 'int' to 'float' changes value from 2147483647 to 2147483648 [-Wimplicit-const-int-float-conversion]
  279 |       k = (int)((float )j * rand() / (RAND_MAX + 1.0F));
      |                                      ^~~~~~~~ ~

Root Cause

The expression RAND_MAX + 1.0F forces the integer RAND_MAX (0x7fffffff) to be converted into a float. Since 2147483648 (2^31) cannot be represented exactly as a 32-bit single-precision float without potential loss of precision, the compiler issues this warning.

Proposed Solution

The calculation should be performed using double precision to ensure accuracy and resolve the implicit conversion warning.

Specifically, changing line 279:

-k = (int)((float )j * rand() / (RAND_MAX + 1.0F));
+k = (int)((double)j * rand() / ((double)RAND_MAX + 1.0));

Environment

Compiler: Emscripten (emcc)

File: lib/SRC/AR2/selectTemplate.c

Metadata

Metadata

Assignees

Labels

C/C++ codeconcerning the C/C++ code design and improvementsEmscriptenenhancementNew feature or request

Type

No fields configured for Task.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions