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

math: floating point epsilon constant #966

Closed
rsc opened this issue Jul 26, 2010 · 5 comments
Closed

math: floating point epsilon constant #966

rsc opened this issue Jul 26, 2010 · 5 comments

Comments

@rsc
Copy link
Contributor

rsc commented Jul 26, 2010

---------- Forwarded message ----------
From: Sanbo Qin <qsb300@gmail.com>
Date: Mon, Jul 26, 2010 at 13:29
Subject: [go-nuts] Where is the similar of FLT_EPSILON / DBL_EPSILON in go
To: golang-nuts <golang-nuts@googlegroups.com>


I can't find it. I think it is a very useful constant in float
comparison, which is defined in <float.h> in C.
I find some relative constant, such as MaxFloat32, MaxFloat64, MaxInt8
in pkg/math/const.go. But I can't find EPSILON that defined the
smallest number x such that 1.0 + x != 1.0.
Is it good to add it into the go? How about to add it into pkg/math/const.go?
Or if I just miss it, where is it?
Thanks.
Sanbo
@gopherbot
Copy link

Comment 1 by bcfabien:

Russ,
DBL_EPSILON is used frequently in numerical computations.
Here are two examples.
1) If f1 and f2 are O(1) then consider the following test:
 if math.Fabs(f2-f1) <= DBL_EPSILON*(1.0 + math.Fabs(f1))
if this test is true then relative error between f1 and f2 is negligible.
2) math.Sqrt(DBL_EPSILON) is related to the "optimal" step size used in forward
difference approximation of derivatives.

@rsc
Copy link
Contributor Author

rsc commented Jul 27, 2010

Comment 2:

I'm happy to defer to the real numericists.  I can see how it would be useful
though I'm skeptical that most people use it correctly.  
EpsilonFloat32 and EpsilonFloat64?

@gopherbot
Copy link

Comment 3 by bcfabien:

I only use float32 when I want to illustrate the deficiencies of single precision.
So, I would vote for EpsilonFloat64.
However, some programmers may wish for EpsilonFloat32 as well.
Thanks.

@gopherbot
Copy link

Comment 4 by joerg.sonnenberger:

ISO C99 has nextafter and co for this purpose. Epsilon itself doesn't make much sense as
it depends on the value (think denormalized numbers).

@rsc
Copy link
Contributor Author

rsc commented Jul 25, 2011

Comment 5:

Go has Nextafter too.  Good enough.

Status changed to WorkingAsIntended.

@rsc rsc self-assigned this Jul 25, 2011
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants