Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Incorrect encoding of complex numbers #995

Closed
hemantjadon opened this issue Aug 31, 2021 · 1 comment · Fixed by #1001
Closed

Bug: Incorrect encoding of complex numbers #995

hemantjadon opened this issue Aug 31, 2021 · 1 comment · Fixed by #1001
Labels

Comments

@hemantjadon
Copy link
Contributor

zap.Complex128 and zap.Complex64 field types incorrectly formats complex numbers with negative imaginary component. Extra + sign is added before the actual - sign
Eg. 2-3i is formatted as 2+-3i

package main

import (
	"go.uber.org/zap"
)

func main() {
    l, _ := zap.NewProduction()

    l.Info("test", zap.Complex128("key", complex(2, -3)))
}

Got output

{"level":"info","ts":1257894000,"caller":"sandbox516008598/prog.go:10","msg":"test","key":"2+-3i"}

Expected output

{"level":"info","ts":1257894000,"caller":"sandbox516008598/prog.go:10","msg":"test","key":"2-3i"}

Reference

https://play.golang.org/p/xz9b8kz_hCR

@abhinav abhinav added the bug label Sep 7, 2021
@abhinav
Copy link
Collaborator

abhinav commented Sep 7, 2021

Thanks for reporting @hemantjadon. This appears to be a legitimate bug.
We'll fix it shortly unless you already have a fix inbound.

(Internal issue: GO-857)

hemantjadon added a commit to hemantjadon/zap that referenced this issue Sep 8, 2021
abhinav pushed a commit that referenced this issue Sep 8, 2021
Fix encoding of complex numbers with negative imaginary components.
Previously, `2 - 3i` was encoded as the following:

    2+-3i

Instead of the following:

    2-3i

Fix this by handling negative imaginary components correctly.

Fixes #995
abhinav added a commit that referenced this issue Sep 10, 2021
Per #1010, #1002, and #995, some of the corner cases where precision is
changed or lost aren't fully tested.

Add test cases for corner cases for a number of these:

- complex{64, 128}:  Test incorrect precision and negatives
- float{32, 64}: Test incorrect precision
- int{8, 16, 32, 64}: Test minimum and maximum values
- uint{8, 16, 32, 64}: Test maximum values

Per #1010, the test for complex64 incorrect precision is currently
failing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants