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

Floating Point Precision #557

Closed
cipriantarta opened this issue Jun 19, 2023 · 5 comments · Fixed by #663
Closed

Floating Point Precision #557

cipriantarta opened this issue Jun 19, 2023 · 5 comments · Fixed by #663

Comments

@cipriantarta
Copy link

I can understand why -1 was used here, but it can cause some pain when loading the message, say in elasticsearch with the value 0 instead of 0.00 for example.

For me, it creates a field of type long instead of what I really want it to be, a float and I'm sure there may be other specific cases.

I propose adding a FloatingPointPrecission variable somewhere around here with a default value of -1, then everyone is happy :)

@mitar
Copy link
Contributor

mitar commented Apr 5, 2024

In my case I have the opposite problem, I am getting too precise numbers, like 0.00000013312693498452013. Do I really have to log at all those digits? Probably not. It could be rounded to 0.000 just fine.

mitar added a commit to mitar/zerolog that referenced this issue Apr 5, 2024
@mitar
Copy link
Contributor

mitar commented Apr 5, 2024

I made #663 to address this.

@rayjanoka
Copy link

I'm interested in this

mitar added a commit to mitar/zerolog that referenced this issue Apr 27, 2024
mitar added a commit to mitar/zerolog that referenced this issue Apr 27, 2024
mitar added a commit to mitar/zerolog that referenced this issue Apr 27, 2024
mitar added a commit to mitar/zerolog that referenced this issue Apr 28, 2024
@rs rs closed this as completed in #663 Apr 28, 2024
@rayjanoka
Copy link

rayjanoka commented Apr 29, 2024

Hey, nice work.

I tried this out, but it doesn't seem to apply to Floats sent via the .Interface() field func, which is where most of my Floats are coming from.

Also, it appears there is a downside where floats that happen to be whole numbers that do not contain any decimals end up displaying with padded zeros when they previously displayed without them.
ex. set precision to 3, previously: 0 -> now: 0.000, previously: 1000 -> now: 1000.000

@mitar
Copy link
Contributor

mitar commented Apr 29, 2024

I tried this out, but it doesn't seem to apply to Floats sent via the .Interface() field func, which is where most of my Floats are coming from.

This is governed by the InterfaceMarshalFunc. You will have to implement your own function which does what you want. Currently it just calls into json.Marshal more or less.

Also, it appears there is a downside where floats that happen to be whole numbers that do not contain any decimals end up displaying with padded zeros when they previously displayed without them.
ex. set precision to 3, previously: 0 -> now: 0.000, previously: 1000 -> now: 1000.000

Yes, this is how strconv.FormatFloat works. Currently this option is just an argument to it. I think this is fine as it is, but feel free to argue the case why should we do more than just use strconv.FormatFloat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants