Just like This question, I need to a positive number that is as close to zero as possible, without it being zero, but in Go.
Asked
Active
Viewed 561 times
0
-
3Answered here: https://stackoverflow.com/a/22185792/1174966 – jdaz Aug 03 '20 at 16:28
-
Wow! Thanks big time – Kisinga Aug 03 '20 at 16:42
-
1Hmm, I don't think this is a duplicate of https://stackoverflow.com/a/22185792/1174966. epsilon is something else. – Henry Aug 03 '20 at 16:48
-
@Henry Well I guess it answers my question, although I'm still trying to understand the overlap – Kisinga Aug 03 '20 at 16:54
-
2epsilon is related to the accuracy of the floating point data type (how many bits are in the mantissa) while your question is related to the number range (what is the minimum exponent + gradual underflow). – Henry Aug 03 '20 at 17:09
-
I guess the logic is that the minimum exponent is directly related to the number of bits, and maybe this is a rule of the thumb for attaining this value that I was not aware of @Henry either way soemone already closed it as duplicate so... Thanks anyway – Kisinga Aug 03 '20 at 17:21
-
3The so-called “epsilon” is the distance from 1 to the next greater representable number. For IEEE-754 binary64, it is 2^−52. The minimum positive representable value is the next greater representable number after 0. For binary64, it is 2^−1074. These are very different numbers with very different purposes, and either this question’s title and body should be changed or it should be marked as a duplicate of a different question, not one that answers about epsilon. – Eric Postpischil Aug 03 '20 at 18:03
-
@EricPostpischil, are you aware of an SO answer which can be used to mark a duplicate? Or care to post your comment as an answer? Thanks! (It would be ideal to add some references to the relevant documentation material, if possible.) – kostix Aug 03 '20 at 18:07
-
2@kostix: I have a thorough answer on the various “minimums” for C++ types and IEEE-754 binary64 [here](https://stackoverflow.com/a/48635448/298225), but this question asks about Go. The numerical values are of course the same, but a correct answer/duplicate has to include the assertion that Go’s float64 uses IEEE-754 binary64. There is a cursory question and answer [here](https://stackoverflow.com/questions/50143585/how-to-get-the-smallest-non-zero-float64). – Eric Postpischil Aug 03 '20 at 18:53
1 Answers
3
The math
package has a constant for this and similar values, just use that: math.SmallestNonzeroFloat64
:
const (
MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
Printing its value:
fmt.Println(math.SmallestNonzeroFloat64)
Outputs (try it on the Go Playground):
5e-324
(Note: it's greater than the constant due to rounding in the fmt
package.)

icza
- 389,944
- 63
- 907
- 827