Skip to content

Comments

io.fits.Card: shortest-roundtrip float formatting [skip ci] (swev-id: astropy__astropy-14508)#123

Closed
rowan-stein wants to merge 1 commit intoastropy__astropy-14508from
fix/fits-card-float-shortest-14508
Closed

io.fits.Card: shortest-roundtrip float formatting [skip ci] (swev-id: astropy__astropy-14508)#123
rowan-stein wants to merge 1 commit intoastropy__astropy-14508from
fix/fits-card-float-shortest-14508

Conversation

@rowan-stein
Copy link
Collaborator

Summary

  • Fix float formatting in astropy.io.fits Card to avoid unnecessary expansion (e.g., 0.009125 -> 0.009124999999999999) that causes comment truncation.
  • Use Python’s shortest-roundtrip str() for finite floats, normalize exponent to 'E'. If str(value) exceeds 20 chars, attempt a compact G-format fallback that still round-trips; otherwise keep str(value).

Issue

Observed failure (before fix)

  • Creating a Card with value 0.009125 expanded to 0.009124999999999999 and emitted VerifyWarning: comment truncated.

Reproduction

from astropy.io import fits
from astropy.io.fits.verify import VerifyWarning
import warnings
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always", VerifyWarning)
    card = fits.Card(
        "HIERARCH ESO IFM CL RADIUS",
        0.009125,
        "[m] radius arround actuator to avoid",
    )
print([str(w.message) for w in caught])
print(str(card))

Output (pre-fix):

warnings: ["Card is too long, comment will be truncated."]
HIERARCH ESO IFM CL RADIUS = 0.009124999999999999 / [m] radius arround actuator 

Validation (after fix)

  • Warnings: []
  • String: HIERARCH ESO IFM CL RADIUS = 0.009125 / [m] radius arround actuator to avoid

Tests added

  • Regression for 0.009125 with full comment.
  • Boundary values: (1 - 2**-53) * 2**exp for exp in [-60, 0, 60].
  • Special tokens preserved for NaN/Inf/-Inf.
  • Long representation keeps shortest-roundtrip str for sys.float_info.max.

Notes

  • Title includes required token. Base branch: astropy__astropy-14508. CI is skipped via commit message.

…erve special tokens; attempt compact fallback within 20 chars (swev-id: astropy__astropy-14508)

Refs #114. Add regression and roundtrip tests for float formatting, special values, and long representations.
@rowan-stein rowan-stein requested a review from a team December 27, 2025 20:39
Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

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

Changes look solid. Thanks.

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.

2 participants