Avoiding Precision Errors in Float-to-Int Conversion in Go
When converting float64 values to int in Go, programmers often encounter precision errors. These errors arise due to the inherent limitations of binary number representation on computers.
Understanding Binary Representation
A float64 represents a number using the IEEE-754 standard, allocating 53 bits for digits and 11 bits for the exponent. However, certain numbers, like 100.55, cannot be exactly represented within this finite binary structure.
The Code:
The following code demonstrates the issue:
package main import "fmt" func main() { x := 100.55 fmt.Println(x - float64(int(x))) }
This code outputs 0.5499999999999972 instead of the expected 0.55. The reason is that binary representation cannot precisely store 100.55, and the resulting number stored in x is slightly different.
Solutions:
Round for Display: To avoid misleading outputs, round floating-point numbers before displaying them. For example:
fmt.Printf("%.2f\n", x) // Outputs: 0.55
Conclusion:
Understanding the limitations of binary representation and employing rounding or integer-based calculations can mitigate precision errors when converting float64 to int in Go.
The above is the detailed content of How Can I Avoid Precision Errors When Converting `float64` to `int` in Go?. For more information, please follow other related articles on the PHP Chinese website!