Repository navigation
FIX: game_converters: Number formats in the .gam reader and writer - #970
Merged
Merged
Conversation
`_str2num` chose between `int` and `float` by whether the token contains a decimal point, so that tokens such as `1e3` were passed to `int` and rejected. Try `int` first and fall back to `float` instead. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Convert a token of the form `n/d` to the nearest float. This is for the upcoming reader for the Gambit .nfg format, where rational payoffs are part of the format, and which will share this function. For the .gam format it is left unspecified. Co-Authored-By: Claude Fable 5.1 <[email protected]>
`np.array2string` was called with the default precision of 8 digits, so that a payoff such as 1/3 or 0.1 + 0.2 was not recovered by reading the output back. Format each float with `np.format_float_positional`, which gives the shortest representation that round-trips and never uses the exponent notation. This is also much faster: 0.27 s instead of 18.5 s for a game with 648,000 payoffs. Co-Authored-By: Claude Fable 5.1 <[email protected]>
A game with payoffs of dtype bool was written with `True` and `False`, which `from_gam_string` rejects. Write them as `0` and `1`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused changes are coherent and adequately covered by passing tests.
Review effort: Balanced
Findings: None
What changed in this PR
Improves .gam numeric parsing and serialization precision.
Changes:
- Parses exponent-form and rational numbers.
- Preserves floating-point round trips without exponent notation.
- Serializes Boolean payoffs as
0and1.
| File | Description |
|---|---|
quantecon/game_theory/game_converters.py |
Updates numeric parsing and payoff formatting. |
quantecon/game_theory/tests/test_game_converters.py |
Adds parsing, precision, and Boolean round-trip tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
* Test the writer with signed zeros, subnormal values, and the largest finite values, comparing signs as well as values * Test that the output does not depend on the print options of NumPy * State in `test_gam_writer_float_precision` what is tested for float32: a payoff is written with the shortest representation among float32 values and is read as float64, so that the original value is recovered when cast to the source dtype Co-Authored-By: Claude Fable 5.1 <[email protected]>
"Integer if `s` represents an integer" was ambiguous: `3.`, `1e3`, and `2/1` represent integers but are returned as floats. The distinction is by how the token is written, not by its value. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
|
thanks @oyamad |
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.
Four fixes to how numbers are read and written in
game_converters.py, found while working on the Julia counterpart (QuantEcon/GameTheory.jl#242).Reader
_str2numchose betweenintandfloatby whether the token contains a decimal point, so that a token such as1e3was passed tointand rejected with aValueError. It now triesintfirst and falls back tofloat. As a side effect,infandnanbecome readable.n/d._str2numconverts a token of the formn/dto the nearest float (viafractions.Fraction, with a zero denominator reported as aValueError). This is for the upcoming reader for the Gambit .nfg format, where rational payoffs are part of the format, and which will share this function. For the .gam format, which GameTracer defines for floating-point payoffs, it is left unspecified: it is tested on_str2numdirectly, not throughfrom_gam_string.Writer
to_gamformatted the payoffs withnp.array2stringat its default precision of 8 digits, so that a payoff such as1/3or0.1 + 0.2was not recovered by reading the output back. Each float is now formatted withnp.format_float_positional(x, trim='.'), which gives the shortest representation that round-trips, in the same style as before (1.), so that the existing expected strings are unchanged. It never uses the exponent notation; previously the whole array switched to it once a value reached1e8. This is also much faster: 0.27 s instead of 18.5 s for a game with 648,000 payoffs.boolpayoffs. A game with payoffs of dtypeboolwas written withTrueandFalse, whichfrom_gam_stringrejects. They are now written as0and1.With payoffs of other dtypes formatted with
str,Fractionpayoffs are now written as1/3instead ofFraction(1, 3); like reading them, this is left unspecified for .gam.Tests
test_from_gam_string_number_formats,test_str2num,test_gam_writer_float_precision(float64andfloat32; checks that no token has an exponent and that reading back is exact), andtest_gam_writer_bool.🤖 Generated with Claude Code (Claude Fable 5.1)