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

Error handling (panics, nan, inf) #4

Closed
1 of 2 tasks
kostya-sh opened this issue Jan 14, 2016 · 6 comments
Closed
1 of 2 tasks

Error handling (panics, nan, inf) #4

kostya-sh opened this issue Jan 14, 2016 · 6 comments

Comments

@kostya-sh
Copy link

kostya-sh commented Jan 14, 2016

I think that a library like decimal should never panic. Generally inputs can come from users (e.g. from an HTML form) and it is much nicer to have an ability to handle errors without using recover or pre-validating decimals before each operation.

Have a look at math/big.Accuracy, I think it is better to use it instead of bool or error where it makes sense. E.g. https://golang.org/pkg/math/big/#Float.Uint64

It might be good idea to introduce +Inf and -Inf values for Decimal objects. This gives natural way to report errors for many operations, such as multiplying two large numbers, division by 0, parsing something like 1e+908940390904920390109201902909201901, etc.

  • Trade panics for +/- Inf
  • Return something similar to big.Accuracy
@ericlagergren
Copy link
Owner

I think that a library like decimal should never panic.

I never felt good about the panics myself. They were in there primarily because I had not worked on incorporating +/- Inf, which leads me to...

It might be good idea to introduce +Inf and -Inf values for Decimal objects.

Yes. I've been working on this tonight and am planning on following the IEEE 754 Inf rules much like the math package does.

@kostya-sh
Copy link
Author

BTW, math/big.Float doesn't support NaN. There is https://golang.org/pkg/math/big/#ErrNaN instead. Probably your library should use the same approach.

@ericlagergren
Copy link
Owner

Yeah. I'm curious how I'd handle cases like -Inf + -Inf where (to my
knowledge) IEEE dictates the result should be NaN.

I'd like to get away from NaN, though, since +/- Inf should be good enough
(I think).
On Thu, Jan 14, 2016 at 6:47 PM kostya-sh notifications@github.com wrote:

BTW, math/big.Float doesn't support NaN. There is
https://golang.org/pkg/math/big/#ErrNaN instead. Probably your library
should use the same approach.


Reply to this email directly or view it on GitHub
#4 (comment)
.

@kostya-sh
Copy link
Author

From https://golang.org/pkg/math/big/#Float.Add:

Add panics with ErrNaN if x and y are infinities with opposite signs. The value of z is undefined in that case.

@ericlagergren
Copy link
Owner

I wonder if it's worth adding traps to the Context so the user can decide
whether they want to have quiet or signaling NaN.

There could be a flag (defaults to signaling?) that determines the behavior
of operations that result in NaN.
On Thu, Jan 14, 2016 at 8:27 PM kostya-sh notifications@github.com wrote:

From https://golang.org/pkg/math/big/#Float.Add:

Add panics with ErrNaN if x and y are infinities with opposite signs. The
value of z is undefined in that case.


Reply to this email directly or view it on GitHub
#4 (comment)
.

@ericlagergren
Copy link
Owner

I do not think I want to implement Accuracy. I'm not quite sure what benefit it adds, tbh.

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

No branches or pull requests

2 participants