FAQ
Based on this question Unexpected results when summing numbers from 1 to
1,000,000,000 in interpreted languages<http://stackoverflow.com/q/18046347/1226894> i
gave go a try add floats



var sum , i float64for i = 0 ; i <= 1000000000; i++ {
     sum += i}
fmt.Println(sum) // 5.00000000067109e+17

Expected : 5.00000000050000e+17


Tested both on 32 and 64 bit system ???

--
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/groups/opt_out.

Search Discussions

  • Jan Mercl at Aug 12, 2013 at 6:19 pm

    On Mon, Aug 12, 2013 at 8:13 PM, Oleku Konko wrote:
    Based on this question Unexpected results when summing numbers from 1 to
    1,000,000,000 in interpreted languages i gave go a try add floats



    var sum , i float64
    for i = 0 ; i <= 1000000000; i++ {
    sum += i
    }
    fmt.Println(sum) // 5.00000000067109e+17

    Expected : 5.00000000050000e+17


    Tested both on 32 and 64 bit system ???
    What is your question and/or where do you see a problem? Everything is
    explained several times in the link that the very you have kindly
    provided.

    -j

    --
    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/groups/opt_out.
  • Oleku Konko at Aug 12, 2013 at 6:23 pm
    My Expectation id different from result

    *5.00000000050000e+17* got *5.00000000067109e+17*
    *
    *
    Has no information in this behavior in go ... in fact the OP assumes that
    go is the only lang out if the 3 with correct results

    --
    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/groups/opt_out.
  • Andrey mirtchovski at Aug 12, 2013 at 6:34 pm
    It would benefit the list if you took the time to compose your query
    more clearly. As Jan remarks, the original link you provided explains
    the issue very clearly: if you use integers with sufficient precision
    to hold the full result you'll get the correct value. everything that
    uses floats (including your example) gets the value deemed
    "incorrect".

    took me a few tries to sneak this under the "program took too long"
    axe but now it should be cached:
    http://play.golang.org/p/ajsglQCIjp

    --
    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/groups/opt_out.
  • Carlos Castillo at Aug 12, 2013 at 7:16 pm
    Both languages in question (PHP and Node.JS, AKA Javascript) silently do
    floating point arithmetic for you under certain conditions. The nature of
    floating point arithmetic results in bad decimal results after multiple
    successive operations due to rounding errors. In Javascript, all numbers
    are actually doubles (64-bit floating point numbers), so all arithmetic
    suffers this problem. In PHP, there is an integer type, but it's only 32
    bits in length. Once an operation produces a value that is too big for an
    integer, it is automatically converted into a 64-bit floating point value,
    and thus starts accumulating error. As a result of the different points in
    the operation that this happens, the magnitude of the error is different.

    In Go, if you use a significantly large enough integer type (int64 will do
    for this code), then all operations are done with integer math, and have no
    rounding errors (and the whole thing is faster too!). If you use a float64
    as the datatype, you get similar results to Javascript (which always uses
    64-bit floats).

    If you want to avoid the rounding, and all your operations are integer
    operations, you can do the following:

        1. Use an appropriately large enough fixed size integer.
        2. If it's too big for your fixed size integers, you can use an
        arbitrary precision integer (GO: math/big.Int, PHP: GMP, Javascript: None
        built-in).
        3. If that isn't an option then use a large floating point type (as
        large as possible), and consider the result with a grain of salt.

    Alternatively, if you use an algorithm that minimizes the number of
    operations involved, then the number of rounding errors will be small, and
    your result will be more accurate. In the case of the posted SO code, one
    comment suggests using the formula "sum(1 to N) = (N/2)*(N+1)" which avoids
    the loop, and only performs 3 floating point operations instead of 1
    billion.
    On Monday, August 12, 2013 11:34:04 AM UTC-7, andrey mirtchovski wrote:

    It would benefit the list if you took the time to compose your query
    more clearly. As Jan remarks, the original link you provided explains
    the issue very clearly: if you use integers with sufficient precision
    to hold the full result you'll get the correct value. everything that
    uses floats (including your example) gets the value deemed
    "incorrect".

    took me a few tries to sneak this under the "program took too long"
    axe but now it should be cached:
    http://play.golang.org/p/ajsglQCIjp
    --
    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/groups/opt_out.
  • Allwiki at Aug 12, 2013 at 11:54 pm
    A float64 number in IEEE754 only has 53bit significant.
    During 1+2+3..., when sum is large than 1.8014398509481983e+16(2^54-1),
    deviation will occur.

    --
    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/groups/opt_out.
  • Allwiki at Aug 12, 2013 at 11:54 pm
    A float64 number in IEEE754 only has 53bit significant.
    During 1+2+3..., when sum is large than 1.8014398509481983e+16(2^54-1),
    deviation will occur.


    在 2013年8月13日星期二UTC+8上午2时13分26秒,Oleku Konko写道:
    Based on this question Unexpected results when summing numbers from 1 to
    1,000,000,000 in interpreted languages<http://stackoverflow.com/q/18046347/1226894> i
    gave go a try add floats



    var sum , i float64for i = 0 ; i <= 1000000000; i++ {
    sum += i}
    fmt.Println(sum) // 5.00000000067109e+17

    Expected : 5.00000000050000e+17


    Tested both on 32 and 64 bit system ???
    --
    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/groups/opt_out.
  • Allwiki at Aug 12, 2013 at 11:54 pm
    According to IEEE754, a float64 number has 53bit significant.
    The sum 500000000500000000 (=0x 6F0 5B59 F17F 6500) has 59 bits, so it
    can't be exact in float64 format. For the same reason, deviations will
    occur during calculation.

    --
    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/groups/opt_out.
  • Kevin Gillette at Aug 13, 2013 at 5:35 pm

    On Monday, August 12, 2013 2:20:48 PM UTC-6, yxtiger wrote:

    According to IEEE754, a float64 number has 53bit significant.
    The sum 500000000500000000 (=0x 6F0 5B59 F17F 6500) has 59 bits, so it
    can't be exact in float64 format. For the same reason, deviations will
    occur during calculation.
    Some integers greater than 2^53 are directly representable (e.g. all even
    numbers between 2^53 and 2^54, and all multiples of 4 between 2^54 and
    2^55, etc). However, even if your expected integral result is directly
    representable as a float64 (it is), many of the intermediate sums greater
    than 2^53 will not be representable. In other words doing the sum in an
    int64 and converting to a float64 will, in *this* case happen to preserve
    the correct answer, but summing in a float64 in *any* language that forces
    use of floats (JavaScript and Lua [depending on compile]) or is weakly
    typed and implicitly converts on overflow (PHP) will be unable to natively
    arrive at the mathematically correct solution.

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 12, '13 at 6:13p
activeAug 13, '13 at 5:35p
posts9
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase