Skip to content

fix(ar2): compute the random template index in double precision (#40) - #77

Merged
kalwalt merged 1 commit into
devfrom
fix/issue-40-selecttemplate-warning
Oct 6, 2026
Merged

kalwalt merged 1 commit into
devfrom
fix/issue-40-selecttemplate-warning

Conversation

@kalwalt

@kalwalt kalwalt commented Oct 6, 2026

Copy link
Copy Markdown
Member

Closes #40.

Summary

lib/SRC/AR2/selectTemplate.c:279 picks a random candidate index:

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

RAND_MAX + 1.0F converted 2147483647 to a float, which cannot hold it exactly (-Wimplicit-const-int-float-conversion). A double represents RAND_MAX + 1 exactly, and j * rand() no longer loses precision in single-precision float. rand() / (RAND_MAX + 1.0) stays below 1, so k is still in [0, j-1].

⚠️ This touches the vendored ARToolKit5 sources (lib/SRC/**): one line, as proposed in #40.

Test plan

  • emcc -Wall on selectTemplate.c (emsdk 4.0.17): the warning is gone
  • WebARKitNFT builds with Emscripten 4.0.17
  • Native (Ubuntu 24.04, GCC 13): webarkit_nft_test 12/12, webarkit_test 28/28

🤖 Generated with Claude Code

RAND_MAX + 1.0F converted 2147483647 to float (-Wimplicit-const-int-float-conversion). A double holds RAND_MAX + 1 exactly, and j * rand() no longer loses precision. k stays in [0, j-1].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Tip of the day
💡 Did you know, you can add REVIEW.md to your repo root and Qodo follows it on every PR

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Compute AR2 random template indices in double precision

🐞 Bug fix 🕐 Less than 5 minutes

Grey Divider

AI Description

• Use double precision when choosing a random AR2 template candidate.
• Eliminate the float-conversion warning while preserving the intended index range.
Diagram

graph TD
  C["Count candidates"] --> R["Draw random value"] --> I["Compute double index"] --> S["Select template"]
Loading
High-Level Assessment

The targeted double-precision conversion is appropriate for the reported warning and avoids a broader change to vendored selection logic. Replacing the sampling algorithm would add scope without addressing a demonstrated need.

Files changed (1) +1 / -1

Bug fix (1) +1 / -1
selectTemplate.cCalculate random candidate index in double precision +1/-1

Calculate random candidate index in double precision

• Replaces float arithmetic in the fallback template-selection index calculation with double arithmetic. This avoids converting RAND_MAX to an imprecise float and removes the reported compiler warning.

lib/SRC/AR2/selectTemplate.c

@kalwalt kalwalt self-assigned this Oct 6, 2026
@kalwalt kalwalt added enhancement New feature or request C/C++ code concerning the C/C++ code design and improvements Emscripten labels Oct 6, 2026
@kalwalt
kalwalt merged commit b2d6166 into dev Oct 6, 2026
1 check passed
kalwalt added a commit to webarkit/jsartoolkitNFT that referenced this pull request Oct 6, 2026
- emscripten/WebARKitLib: b2d6166 on WebARKitLib dev, which adds
  webarkit/WebARKitLib#77 (#40: random template index computed in double
  precision in AR2/selectTemplate.c, fixing the
  -Wimplicit-const-int-float-conversion warning)
- build/ and dist/ rebuilt with emsdk 4.0.17 (npm run build-docker,
  npm run build-ts); vitest 182 passed / 6 skipped, node 7/7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kalwalt added a commit to webarkit/jsartoolkitNFT that referenced this pull request Oct 7, 2026
* refactor: use the NFT helpers from WebARKitLib (#453)

trackingMod, trackingMod2d, trackingSub, markerDecompress and
NFTMarkerState moved to WebARKitLib (webarkit/WebARKitLib#75, #76) under
WebARKit/WebARKitTrackers/WebARKitNFT.

- tools/makem.js: take those sources from WebARKitLib and add its include
  directory
- bindings (JS and Python): include <WebARKitTrackers/WebARKitNFT/...>
- python-bindings/setup.py: new source and include paths
- remove the copies from emscripten/
- bump the WebARKitLib submodule to feat/issue-75-webarkit-nft

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: report a failed .zft decompression through onError

decompressMarkers() now returns -1 for a missing or malformed archive instead of exiting. The decompressZFT bindings returned 1 regardless, so loadZFT read temporary files that were never written and threw ENOENT before any callback ran. The bindings now return -1 on failure and loadZFT calls onError(prefix + ".zft"). Reported by Qodo on #687.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf: free the .zft archive from MEMFS once decompressed

loadZFT kept /markerNFT_N.zft in the Emscripten filesystem for the whole session; the old C-side remove() built the name without the dot, so it never deleted it either. Suggested by Qodo on #687.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: bump WebARKitLib to dev and rebuild build/ and dist/ (#453)

- emscripten/WebARKitLib: dfe8570 on WebARKitLib dev, the squash merge of
  webarkit/WebARKitLib#76 (same tree as the 480fdb9 used so far)
- build/ and dist/ rebuilt with emsdk 4.0.17 (npm run build-docker,
  npm run build-ts)

The earlier commits of this PR (0cff312, 3543c45, 00cf36e) changed
C/C++, JS and TS sources without committing the rebuilt build/ and dist/;
this commit brings them up to date with those changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: bump WebARKitLib to b2d6166 and rebuild build/ and dist/

- emscripten/WebARKitLib: b2d6166 on WebARKitLib dev, which adds
  webarkit/WebARKitLib#77 (#40: random template index computed in double
  precision in AR2/selectTemplate.c, fixing the
  -Wimplicit-const-int-float-conversion warning)
- build/ and dist/ rebuilt with emsdk 4.0.17 (npm run build-docker,
  npm run build-ts); vitest 182 passed / 6 skipped, node 7/7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: bump WebARKitLib to b5e4770 and rebuild build/ and dist/

- emscripten/WebARKitLib: b5e4770 on WebARKitLib dev, which adds
  webarkit/WebARKitLib#80 (review fixes before 0.10.0: capped .zft
  decompression, trackingInitMain null check, explicit #error for
  AR2_CAPABLE_ADAPTIVE_TEMPLATE)
- build/ and dist/ rebuilt with emsdk 4.0.17 (npm run build-docker,
  npm run build-ts); vitest 182 passed / 6 skipped, node 7/7

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore: pin WebARKitLib to the 0.10.0 release tag

Move emscripten/WebARKitLib from b5e4770 (dev) to 4fd3034, the
merge commit on master tagged 0.10.0, as the 0.9.0 pin pointed at
master. Both commits have the same tree, so build/ and dist/ are
unchanged and need no rebuild.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C/C++ code concerning the C/C++ code design and improvements Emscripten enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant