Another question is how do I get 470, not 469 if I really have to let 4.7 *
100?
I mean is there a best practice to handle it?
在 2015年8月27日星期四 UTC+8上午12:56:23,bradfitz写道:
[-golang-dev, +golang-nuts]
You can read more about floating point numbers here:
https://en.wikipedia.org/wiki/Floating_point
and
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
On Wed, Aug 26, 2015 at 7:35 AM, Timothy Ye <yexiaoz...@gmail.com
<javascript:>> wrote:
--You can read more about floating point numbers here:
https://en.wikipedia.org/wiki/Floating_point
and
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
On Wed, Aug 26, 2015 at 7:35 AM, Timothy Ye <yexiaoz...@gmail.com
<javascript:>> wrote:
Hey guys,
I met a weird problem about the float32 type in Go.
This is my demo program: (You can run it via:
http://play.golang.org/p/dn4V4TcFtr)
package main
import "fmt"
func main() {
compute(4.8)
compute(4.7)
compute(4.6)
compute(4.5)
}
func compute(input float32) {
result := int64(input * 100)
fmt.Printf("Result is:%d\r\n", result)
result = int64(int(input *10)*10)
fmt.Printf("Result is:%d\r\n", result)
}
==================
The output is:
Result is:480
Result is:480
Result is:469
Result is:470
Result is:460
Result is:460
Result is:450
Result is:450
You may notice that all the results are as expected except when input is 4.7
I am confused about the float 4.7 * 100, why the result is 469?
But if I let 4.7 * 10 first, then convert it to int type and multiply by 10 again, the result is 470, not 469.
I don't know why, can anybody help to explain it? Thanks a lot!
--
You received this message because you are subscribed to the Google Groups
"golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-dev+...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/d/optout.
I met a weird problem about the float32 type in Go.
This is my demo program: (You can run it via:
http://play.golang.org/p/dn4V4TcFtr)
package main
import "fmt"
func main() {
compute(4.8)
compute(4.7)
compute(4.6)
compute(4.5)
}
func compute(input float32) {
result := int64(input * 100)
fmt.Printf("Result is:%d\r\n", result)
result = int64(int(input *10)*10)
fmt.Printf("Result is:%d\r\n", result)
}
==================
The output is:
Result is:480
Result is:480
Result is:469
Result is:470
Result is:460
Result is:460
Result is:450
Result is:450
You may notice that all the results are as expected except when input is 4.7
I am confused about the float 4.7 * 100, why the result is 469?
But if I let 4.7 * 10 first, then convert it to int type and multiply by 10 again, the result is 470, not 469.
I don't know why, can anybody help to explain it? Thanks a lot!
--
You received this message because you are subscribed to the Google Groups
"golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to golang-dev+...@googlegroups.com <javascript:>.
For more options, visit https://groups.google.com/d/optout.
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.