Why fmt.Println(math.Sqrt2) gives ...0951 not ...0950?

179 views
Skip to first unread message

Kamil Ziemian

unread,
Apr 30, 2021, 8:31:06 PM4/30/21
to golang-nuts

Hello.

Wikipedia says that sqrt(2) = 1.414213562373095048..., so I would round it to ...0950, while fmt.Println(math.Sqrt2) give me ...0951.
https://en.wikipedia.org/wiki/Square_root_of_2

According to https://golang.org/pkg/math/#pkg-constants Sqrt2 is defined with far more digits, so rounding precision shouldn't be an error.

Can anyone tell me why math.Sqrt2 have such printing representation? This little detail is just bogging me.

I use go version go1.16.3 linux/amd64.

Kamil

Ian Lance Taylor

unread,
Apr 30, 2021, 8:36:13 PM4/30/21
to Kamil Ziemian, golang-nuts
Computer floating point numbers are not ideal floating point numbers.

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Ian

Kamil Ziemian

unread,
May 1, 2021, 10:12:36 AM5/1/21
to golang-nuts
Thank you. I still need to learn more about floating point numbers, there is so many thing about them that can go wrong. At the same time, I believe this was in "A Tour of Go", I read that golang const are values with arbitrary precision. I hope that I don't mess this up. Since according to Go page math.Sqrt2 is very long const I was thinking that such things as printing is standard representation using fmt library is something that should work without problems.

Can you guide me to some materials about const in Go? Or maybe I should finally read Go spec, which I avoid for a long time?

Kamil

wagner riffel

unread,
May 1, 2021, 2:16:45 PM5/1/21
to Kamil Ziemian, golang-nuts
On Sat May 1, 2021 at 11:12 AM -03, Kamil Ziemian wrote:
> Can you guide me to some materials about const in Go? Or maybe I should
> finally read Go spec, which I avoid for a long time?
>

You shouldn't avoid to read the spec, it's not as complicated as others
languages, and it's a great source of answers to such questions, i think
this is a great material on Go constatns:
https://blog.golang.org/constants

Brian Candler

unread,
May 1, 2021, 2:20:13 PM5/1/21
to golang-nuts
Remember that while a constant may have unlimited precision, when you call fmt.Println() then it will get converted to a float64 value to be passed as an argument.

Kamil Ziemian

unread,
May 1, 2021, 4:34:57 PM5/1/21
to Brian Candler, golang-nuts
Thank you Brain Candler, this was probably thing that I missed: that math.Sqrt2 is converted according to the context and I didn't think what context fmt.Println gave it.

sob., 1 maj 2021 o 20:20 Brian Candler <b.ca...@pobox.com> napisał(a):
Remember that while a constant may have unlimited precision, when you call fmt.Println() then it will get converted to a float64 value to be passed as an argument.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/q7StlcjMiII/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/30631b42-eb7b-43b6-891a-e9fa58a48038n%40googlegroups.com.
Message has been deleted

Kamil Ziemian

unread,
May 4, 2021, 10:53:30 AM5/4/21
to golang-nuts
Thank you for this Go Blog post and even more for encourage me to read the Go spec. C++ give spaces a bad name and teach me to avoid them.
Reply all
Reply to author
Forward
0 new messages