Skip to content

std.abs(0) incorrectly returns -0 (negative zero) #886

Description

@He-Pin

Bug Description

std.abs(0) returns -0 instead of 0. The absolute value of zero should always be positive zero.

Reproduction

std.abs(0)

Expected Behavior

0

Actual Behavior

-0

Notably, std.abs(-0) correctly returns 0:

std.abs(-0)  // returns 0 (correct)
std.abs(0)   // returns -0 (incorrect)

Version

$ jsonnet --version
Jsonnet commandline interpreter (Go implementation) v0.22.0

Analysis

This is likely caused by the implementation using math.Copysign or similar, which copies the sign bit incorrectly. The Go math.Abs function should handle this correctly:

// math.Abs(0) in Go returns 0 (positive zero)
// math.Abs(-0) in Go returns 0 (positive zero)

sjsonnet correctly returns 0 for both std.abs(0) and std.abs(-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