google / google/go-jsonnet

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

Open Beginner friendly
#886 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.8k
Forks
263
PR merge metrics
No merged PRs in 30d

Description

## Bug Description

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

## Reproduction

```jsonnet
std.abs(0)
```

## Expected Behavior

```json
0
```

## Actual Behavior

```json
-0
```

Notably, `std.abs(-0)` correctly returns `0`:
```jsonnet
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:

```go
// 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)`.

Contributor guide

Open the contributing guide

Research direction

Start at the Go implementation of the std.abs entry point and reproduce the behavior with std.abs(0) and std.abs(-0). Compare the result with Go's math.Abs and sjsonnet; done means both inputs produce positive zero in the Jsonnet output.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.