Navigation Menu

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

proposal: math: Add Euler-Mascheroni constant #24759

Closed
jxdp opened this issue Apr 7, 2018 · 14 comments
Closed

proposal: math: Add Euler-Mascheroni constant #24759

jxdp opened this issue Apr 7, 2018 · 14 comments

Comments

@jxdp
Copy link

jxdp commented Apr 7, 2018

The Euler-Macheroni, or Euler, constant is used internally in Gamma (to a lesser precision than other constants like Pi, Phi, E) and would be a reasonable addition, without adding any bloat or maintenance, to the standard library. This constant is important and I believe it would be a useful addition, and would also ensure that external libraries using the constant to implement more niche functions have a standard value to depend on.

I would simply add to consts.go:

Euler = 0.57721566490153286060651209008240243104215933593992359880576723 // https://oeis.org/A001620
@gopherbot gopherbot added this to the Proposal milestone Apr 7, 2018
@jxdp
Copy link
Author

jxdp commented Apr 7, 2018

I've submitted a change here: https://go-review.googlesource.com/#/c/go/+/105396

@gopherbot
Copy link

Change https://golang.org/cl/105396 mentions this issue: math: add the Euler-Mascheroni constant

@ALTree
Copy link
Member

ALTree commented Apr 8, 2018

cc @rsc @griesemer for decision.

@robpike
Copy link
Contributor

robpike commented Apr 8, 2018

When someone writes math.Euler, I wouldn't immediately think of γ. I know for shorthand it's called Euler's constant but it appears rarely. I'm not opposed to adding it (although I'm not enthusiastic either) but it needs a more evocative name.

@robpike
Copy link
Contributor

robpike commented Apr 8, 2018

Also, please note that it's bad form to send a change before the proposal has been discussed and approved. In this case it's a minor change, but in general it complicates matters and - worse - can split the discussion into two.

@jxdp
Copy link
Author

jxdp commented Apr 9, 2018

@robpike Sorry for jumping the gun with the change - it was not my intention to complicate things.

If not Euler, what name would you suggest? Would EulerGamma work?

@griesemer
Copy link
Contributor

FWIW. I'm not opposed per se to having another constant here (they are cheap), but I want to raise two points:

  1. Why is this particular constant more important than others that we don't have here? I note that at the moment the number of constants is entirely reduced to some very elementary ones (e, pi, phi, their square roots, and then constants for logarithm conversions). Adding almost anything will open the flood gates to adding more. Where do we stop? Is there some specific set of constants that we should have?

  2. If we add this, we need to have a better name than what has been proposed so far (but see below).

In summary, I think we should either leave things as they are (it's trivial to define whatever constant in your code); or, alternatively, have a clear plan of what set of constants we will accept, and then perhaps add them all at once, so we have a chance to look at their names collectively.

@rsc
Copy link
Contributor

rsc commented Apr 9, 2018

The ones that are there are there basically because they're defined in C (and we've done the same for the functions, mostly). There are many other constants we could possibly add. It doesn't really seem worth adding them one by one. Do you have a different criteria you are suggesting for what should be in Go's package math's exported constants?

@jxdp
Copy link
Author

jxdp commented Apr 9, 2018

This seems to suggest that the constants in Go are not identical to those available in C. In particular,

In C, not in Go:

  • Pi/2
  • Pi/4
  • 1/Pi
  • 2/Pi
  • 2/SqrtPi
  • 1/Sqrt2

In Go, not in C:

  • Phi
  • SqrtPhi

Notably, the EulerGamma constant is absent from both. IMHO Phi is a nice addition; it's an interesting number with significance.

My original reason for suggesting EulerGamma was that it is actually already used in the standard library (in Gamma) and therefore may as well be exported. It is also not easily calculate using standard library functions and constants.

Is there much value to be gained from adding Pi/4 etc? Probably not. Maybe we could add LnPi, LnPhi (and LnEulerGamma if we decide to add EulerGamma) which might save an odd bit of precision over lnPi := math.Log(math.Pi), though I haven't tested this. That these numbers are already easily calculated from standard library constants and functions to high precision is probably sufficient IMO.

I believe a more useful addition would be to add complex128 versions of the package math constants to package cmplx, to save having to define them, e.g. pi := complex(math.Pi, 0), whenever one is using complex numbers.

@griesemer
Copy link
Contributor

In Go it's easy to write Pi/2 and get a compile-time fully accurate representation useable for any runtime floating point precision, which is not necessarily the case in C. This is because we have "precise" (at least 256 bits of precision, and 512 bits in the existing compiler) constants and corresponding accurate constant arithmetic in Go. Thus it makes sense to leave away any constants that can be easily expression in form of fractions.

There's no compile time constant expression evaluation of square roots, so it makes sense to add the respective constant's square roots.

The current set of constants in Go is pretty minimal, and perhaps Phi could have been left away as well.

The question remains: Where do we set the boundaries of what should be added?

@agnivade
Copy link
Contributor

There's no compile time constant expression evaluation of square roots, so it makes sense to add the respective constant's square roots.

We do have SqrtPi and Sqrt2. Or maybe I am missing something ?

To have a fixed criteria of matching with C, we should probably remove Phi as well. But that will be a breaking change. At the same time, if we are to add EulerGamma, this will lead to a slippery slope of people requesting for more constants to be added.

FWIW, I took a look at what constants are there in other programming languages:

Going into more mathematics focused languages, we get some more esoteric constants added.

So as of now, we maintain parity with general purpose programming languages. But, if we are to add more, I think Euler and Catalan are 2 top contenders.

My original reason for suggesting EulerGamma was that it is actually already used in the standard library (in Gamma) and therefore may as well be exported.

Fair enough. In the same vein, then I think we should only have exported constants which are actually used in the standard library.

@jxdp
Copy link
Author

jxdp commented Apr 13, 2018

@agnivade I believe the comment about it making sense to have Sqrt2 and SqrtPi was justifying their inclusion in the std lib (whereas Pi/4 etc aren't necessary due to constants having (essentially) arbitrary precision), not a statement that we don't already have them.

I completely understand the slippery slope argument and I agree that anything in the standard library should be there justifiably.

In the same vein, then I think we should only have exported constants which are actually used in the standard library.

IMO this presents a useful standard for what could (should?) be exported. However, I would probably seek to avoid making any breaking changes such as removing Phi and SqrtPhi.

@agnivade
Copy link
Contributor

I believe the comment about it making sense to have Sqrt2 and SqrtPi was justifying their inclusion in the std lib (whereas Pi/4 etc aren't necessary due to constants having (essentially) arbitrary precision), not a statement that we don't already have them.

Ah, you may be right. I misread that.

@rsc
Copy link
Contributor

rsc commented Apr 16, 2018

I added Phi in Jan 2009. I have no idea why. If a proposal came in today to add it, we probably wouldn't.

I think we should just declare the set of constants finished at this point. The discussion here seems to agree that the Euler gamma is below the bar anyway and that we don't want to have a discussion about each new value.

@rsc rsc closed this as completed Apr 16, 2018
@golang golang locked and limited conversation to collaborators Apr 16, 2019
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

7 participants