Repository navigation
Take a floating-point remainder with fmod or math.Mod in C, C++ and Go [patch] - #210
Open
matt-edmondson wants to merge 3 commits into
Open
matt-edmondson wants to merge 3 commits into
matt-edmondson wants to merge 3 commits into
Conversation
…o [patch] C, C++ and Go define % only on integers, so a remainder or %= of double or float operands did not compile there. StandardLanguageGenerator now lowers a floating remainder through a WriteFloatingRemainder hook for a target that opts in: fmod/fmodf in C, std::fmod in C++, math.Mod in Go (converting through float64 for float32). x %= y becomes x = <call>(x, y). C and C++ now push a static-type scope and register locals, as Go already did, so the operands' types are known. The file includes <math.h>/<cmath> or imports "math" only when it takes such a remainder. Integer % is unchanged. Fixes #174 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NgDciLntSfQTDeqFaJ3BM1
Keep both import conditions: main renamed ContainsNonFiniteDouble to ContainsNonFiniteLiteral, and this branch adds ContainsFloatingRemainder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw
Keep both C++ include conditions: main added <vector>/<string> for an entry point that accepts arguments (#211), and this branch adds <cmath> for a floating-point remainder. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NgDciLntSfQTDeqFaJ3BM1
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #174
What changed
C, C++ and Go define
%on integers only. A remainder or%=ofdoubleorfloatoperands was written as%anyway, and none of the three compiled it.StandardLanguageGeneratorgainsFloatingRemainderType, which returns the floating type of a remainder when either operand's static type isfloatordouble. It also gains aSpellsFloatingRemainderAsCall/WriteFloatingRemainderhook.GenerateBinarylowers a floatingModulothrough that hook. A newGenerateAssignmentStatementoverride lowers a floatingx %= ytox = <call>(x, y). Go's own override still defers to it for any value that is not a conditional.What each target writes:
doublefloatfmod(a, b)fmodf(a, b)<math.h>std::fmod(a, b)std::fmod(a, b)<cmath>math.Mod(a, b)float32(math.Mod(float64(a), float64(b)))"math"Before this change, C and C++ never pushed a static-type scope.
GenerateFunctionnow opens one andGenerateVariableDeclarationregisters each local, the same way Go already did. That is the prerequisite the triage comment shares with Calling a method through a pointer or reference, or on another call's result, generates C, C++ and Rust that doesn't compile #170.Imports are written before the body, so
ContainsFloatingRemainder(SourceFile)asks the question of the AST. It puts each function's parameters and locals in scope and then looks for a floating%or%=. Go needs this to be exact, because an unused"math"import does not compile there.Integer
%and%=are unchanged, and C#, Rust, Python and JavaScript are untouched, since the hook defaults to off.Tests (
FloatingRemainderTests)AFloatingRemainderIsACall(C, C++, Go): checks the spelling ofdouble % double,float % floatand%=on adoublelocal.AnIntegerRemainderIsUnchanged(C, C++, Go):int%and%=are still written as%.OnlyAFloatingRemainderImportsItsCall(C, C++, Go): the header or package appears exactly once for floating remainders, and not at all for integer ones.RemaindersCompile(C, C++, Go): both files compile withgcc -std=c11 -Wall -Wextra -pedantic -Werror,g++ -std=c++20 … -Werrorandgo vet.With the generator changes reverted, 9 of the 12 fail. The 3 that still pass are the unchanged integer rows. With the changes, the full suite passes (1127/1127).
🤖 Generated with Claude Code
https://claude.ai/code/session_01NgDciLntSfQTDeqFaJ3BM1
Generated by Claude Code