Skip to content

Float value formatting in FITS header #5449

@saimn

Description

@saimn

I have a FITS header with a float value that gets changed from '8.95' to '8.949999999999999' when saving to a new file (and as a result the comment is truncated):

> /home/conseil/lib/astropy/astropy/io/fits/card.py(976)_format_image()
-> warnings.warn('Card is too long, comment will be truncated.',

(Pdb) self
('ESO TEL AMBI WINDSP', 8.95, '[m/s] Observatory ambient wind speed queri')
(Pdb) output
'HIERARCH ESO TEL AMBI WINDSP = 8.949999999999999 / [m/s] Observatory ambient wind speed queri'
(Pdb) output[:Card.length]
'HIERARCH ESO TEL AMBI WINDSP = 8.949999999999999 / [m/s] Observatory ambient win'
(Pdb) len(output)
93
(Pdb) Card.length
80
(Pdb) value
'8.949999999999999'
(Pdb) self.value
8.95
(Pdb) str(self.value)
'8.95'
(Pdb) self._format_value()
'8.949999999999999'

(Pdb) from astropy.io.fits.card import _format_float
(Pdb) _format_float(self.value)
'8.949999999999999'

This is not highly critical, but it would be better to get the output header same as the input one.
astropy.io.fits.card._format_float uses %.16G to convert the string, I don't know what is the best option here (is str(value) a valid option ?).
ping @embray

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions