Skip to content

Widen a Go double remainder's int and float32 operands [patch] - #458

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/376-go-double-remainder
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/376-go-double-remainder

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #376

What changed

GoGenerator.WriteFloatingRemainder wrote both operands of a double remainder unchanged. math.Mod takes only float64, and Go converts nothing implicitly, so any int or float32 operand broke the build. The double branch now passes each operand through GenerateWidenedTo(operand, double), which writes float64(...) only around an operand narrower than float64. The float32 branch already converted both operands.

double rem(float x, double d, int i) { double r = x % d; r %= i; return r + (i % d); } now generates:

var r float64 = math.Mod(float64(x), d)
r = math.Mod(r, float64(i))
return (r + math.Mod(float64(i), d))

Tests

  • New Go_MixedOperandsOfADoubleRemainderAreWidened checks the spelling of all three remainders, including the compound %=.
  • RemaindersCompile now also compiles the mixed-operand file for C, C++ and Go (go vet).
  • With the fix reverted, both tests fail. go vet reports cannot use x (variable of type float32) as float64 value in argument to math.Mod.
  • Full suite: 1665 passed, 0 failed.

Generated by Claude Code

math.Mod takes float64 alone and Go converts nothing implicitly, so the
double branch of WriteFloatingRemainder now writes each operand through
GenerateWidenedTo, as the float32 branch already converts both. A mixed
remainder and %= are added to the Go spelling and compile tests.

Fixes #376

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4VpqLXsf6UDQnX46h
@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

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.

Go: % between a double and an int or float emits math.Mod(x, i) with unconverted operands, so it doesn't compile

2 participants