Skip to content

FIX: game_converters: Number formats in the .gam reader and writer - #970

Merged
mmcky merged 6 commits into
mainfrom
gam-number-format
Sep 23, 2026
Merged

mmcky merged 6 commits into
mainfrom
gam-number-format

Conversation

@oyamad

@oyamad oyamad commented Sep 21, 2026

Copy link
Copy Markdown
Member

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

  • Numbers with an exponent and no decimal point. _str2num chose between int and float by whether the token contains a decimal point, so that a token such as 1e3 was passed to int and rejected with a ValueError. It now tries int first and falls back to float. As a side effect, inf and nan become readable.
  • Rational tokens n/d. _str2num converts a token of the form n/d to the nearest float (via fractions.Fraction, with a zero denominator reported as a ValueError). 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 _str2num directly, not through from_gam_string.

Writer

  • Loss of precision. to_gam formatted the payoffs with np.array2string at its 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. Each float is now formatted with np.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 reached 1e8. This is also much faster: 0.27 s instead of 18.5 s for a game with 648,000 payoffs.
  • bool payoffs. A game with payoffs of dtype bool was written with True and False, which from_gam_string rejects. They are now written as 0 and 1.

With payoffs of other dtypes formatted with str, Fraction payoffs are now written as 1/3 instead of Fraction(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 (float64 and float32; checks that no token has an exponent and that reading back is exact), and test_gam_writer_bool.

🤖 Generated with Claude Code (Claude Fable 5.1)

oyamad and others added 4 commits September 20, 2026 21:39
`_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]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 0 and 1.
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.

@coveralls

coveralls commented Sep 21, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 93.981% (+0.01%) from 93.967% — gam-number-format into main

oyamad and others added 2 commits September 21, 2026 12:28
* 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]>
@oyamad oyamad added the ready label Sep 21, 2026
@mmcky

mmcky commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

thanks @oyamad

@mmcky
mmcky merged commit b5a05ab into main Sep 23, 2026
15 checks passed
@mmcky
mmcky deleted the gam-number-format branch September 23, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants