Skip to content

r.sunmask, d.linegraph: Compute trig products in double - #7712

Merged
cwhite911 merged 1 commit into
OSGeo:mainfrom
cwhite911:sunmask-linegraph-precision
Jul 14, 2026
Merged

cwhite911 merged 1 commit into
OSGeo:mainfrom
cwhite911:sunmask-linegraph-precision

Conversation

@cwhite911

Copy link
Copy Markdown
Contributor

Fixes the final 37 CodeQL cpp/integer-multiplication-cast-to-long alerts, the two clusters that are float-precision rather than integer-overflow (r.sunmask: 29, d.linegraph: 8). Together with #7704, #7705, #7706, and #7708 this closes out the entire 154-alert rule.

Unlike the sibling PRs, these are float * float products widened to double, so I fixed the root cause by widening the types rather than casting per site:

  • r.sunmask (solpos00.c, borrowed NREL SOLPOS code): degrad/raddeg and the five struct trigdata sine/cosine fields were float, so raddeg * angle and cl * cd were computed in float and only then widened to double for the trig calls. Widening these to double computes the products in double and lets the two conversion constants (57.295779513, 0.0174532925) keep their full declared precision instead of being truncated to float.
  • d.linegraph (main.c): xscale and yscale are computed as double (from (double) casts) and were then stored into float, losing precision before every plot coordinate multiplication. Widening them to double clears the alerts and removes the narrowing.

Output impact, verified on nc_spm:

  • r.sunmask shadow raster is bit-identical before and after (0 changed cells at three different sun angles, including low-angle winter mornings with thousands of shadow cells). Only the -s reported solar position changes, by about 1e-5 degrees, reflecting the now-full-precision constants.
  • d.linegraph PNG render is bit-identical (same md5).

Because this touches borrowed SOLPOS code and nominally changes a printed value, I kept it separate from the mechanical integer-cast PRs so it can be reviewed on its own.

This is part of a larger effort to work through the open code scanning alerts, grouped into small PRs by issue type.

Written with the assistance of Claude Code.

In the borrowed NREL SOLPOS code, the radian/degree conversion
constants and the local sine/cosine fields were float, so products
like raddeg * angle were computed in float and only then widened to
double for the trig calls. Widen degrad, raddeg, and the trigdata
fields to double; this also lets the constants keep their full
declared precision. In d.linegraph, widen xscale and yscale to
double, which they are already computed as before being stored.

Fixes 37 CodeQL cpp/integer-multiplication-cast-to-long alerts.

The r.sunmask shadow raster is unchanged (verified bit-identical at
several sun angles on nc_spm); only the reported solar position
shifts by about 1e-5 degrees. The d.linegraph rendering is
bit-identical.
@cwhite911
cwhite911 enabled auto-merge (squash) July 14, 2026 18:47
@github-actions github-actions Bot added raster Related to raster data processing C Related code is in C module display labels Jul 14, 2026
@cwhite911
cwhite911 merged commit 7a696db into OSGeo:main Jul 14, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C display module raster Related to raster data processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants