Skip to content

Format operator does not detect negative zero (-0.0) sign #889

Description

@He-Pin

Summary

The format operator (%) does not correctly detect the sign of negative zero (-0.0). All format specifiers (%f, %e, %g) produce output without a minus sign for -0.0, while Python's equivalent produces -0.000000.

Reproduction

$ go-jsonnet -e '"%f" % (-0.0)'
"0.000000"

$ go-jsonnet -e '"%+f" % (-0.0)'
"+0.000000"

Expected (Python reference)

>>> "%f" % (-0.0)
'-0.000000'
>>> "%+f" % (-0.0)
'-0.000000'

Analysis

The issue is that the sign detection uses s < 0, which returns false for -0.0 in IEEE 754 floating point (-0.0 < 0 is false). The fix is to check the sign bit directly, e.g. using math.Signbit(s) in Go.

This affects %f, %e, %g, and their uppercase variants, as well as the + and (space) sign flags.

Version

go-jsonnet v0.22.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions