On Sat, Feb 18, 2017 at 7:52 PM, <ngjun...@gmail.com> wrote: > I am trying to multiply an int16 and float64 in Golang 1.8 but the result it > returns is incorrect. I need this number to be exact for some hash > verification. Any ideas why this might be happening? > > int16(10) // 10 > float64(int16(10)) // 10 > > float64(3.99) // 3.99 > > float64(int16(10)) * float64(3.99) // 39.900000000000006
That is how floating point numbers work. The number 3.99 can not be represented precisely in the 64-bit floating point format, so the result of the multiplication has errors. If you store 3.99 in a float64 variable and print it however you are printing the result of the multiplication, so you will see that it too has extraneous every low precision bits. Please see https://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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.