FAQ
When I convert a float32 into a float64 the precision changes.

http://play.golang.org/p/sWen40j2IP

I'm storing the result of ping between two hosts and measuring it over a
long period of time. I have 10+ million ping replies, in order to save
space I'm converting the them from float64 (8 bytes) to float32 (4 bytes)
before I send them off to persistence. When I pull them back out I noticed
some slight differences in average and std deviation, which I traced back
to my conversion of a float32 (storage format) to float64 (calculation
format).

This might not be Go specific but it's the first time I've noticed this
behavior. Maybe I'm just not aware of how floats convert!

Is my assumption wrong that up-converting a float from 32 to 64 *shouldn't*
change precision?

Greg


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Lance Taylor at Feb 8, 2015 at 5:34 pm

    On Sun, Feb 8, 2015 at 9:10 AM, Greg Ingelmo wrote:
    When I convert a float32 into a float64 the precision changes.

    http://play.golang.org/p/sWen40j2IP

    I'm storing the result of ping between two hosts and measuring it over a
    long period of time. I have 10+ million ping replies, in order to save space
    I'm converting the them from float64 (8 bytes) to float32 (4 bytes) before I
    send them off to persistence. When I pull them back out I noticed some
    slight differences in average and std deviation, which I traced back to my
    conversion of a float32 (storage format) to float64 (calculation format).

    This might not be Go specific but it's the first time I've noticed this
    behavior. Maybe I'm just not aware of how floats convert!

    Is my assumption wrong that up-converting a float from 32 to 64 shouldn't
    change precision?
    This is normal behaviour.

    It's not really accurate to say that the precision is changing. The
    string printed by fmt.Fprintln is the string required to recreate the
    exact bits in the floating point representation. The choices it makes
    for float64 are, of course, different than the choices it makes for
    f32.

    Compare your code to http://play.golang.org/p/ODfWABu3Sp .

    And please read
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Greg Ingelmo at Feb 8, 2015 at 5:41 pm
    Thanks Ian, I will read up.
    On Sunday, February 8, 2015 at 12:34:56 PM UTC-5, Ian Lance Taylor wrote:

    On Sun, Feb 8, 2015 at 9:10 AM, Greg Ingelmo <ing...@gmail.com
    <javascript:>> wrote:
    When I convert a float32 into a float64 the precision changes.

    http://play.golang.org/p/sWen40j2IP

    I'm storing the result of ping between two hosts and measuring it over a
    long period of time. I have 10+ million ping replies, in order to save space
    I'm converting the them from float64 (8 bytes) to float32 (4 bytes) before I
    send them off to persistence. When I pull them back out I noticed some
    slight differences in average and std deviation, which I traced back to my
    conversion of a float32 (storage format) to float64 (calculation format).
    This might not be Go specific but it's the first time I've noticed this
    behavior. Maybe I'm just not aware of how floats convert!

    Is my assumption wrong that up-converting a float from 32 to 64 shouldn't
    change precision?
    This is normal behaviour.

    It's not really accurate to say that the precision is changing. The
    string printed by fmt.Fprintln is the string required to recreate the
    exact bits in the floating point representation. The choices it makes
    for float64 are, of course, different than the choices it makes for
    f32.

    Compare your code to http://play.golang.org/p/ODfWABu3Sp .

    And please read
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 8, '15 at 5:10p
activeFeb 8, '15 at 5:41p
posts3
users2
websitegolang.org

2 users in discussion

Greg Ingelmo: 2 posts Ian Lance Taylor: 1 post

People

Translate

site design / logo © 2022 Grokbase