Golang : Accurate and reliable decimal calculations
If you have been programming for a long time, chances are you will be aware of a well-known issue with the floating-point numbers. The "feature" or inaccuracy is that the floating-point numbers cannot be accurately represented by all base-10 decimals. This effects all programming languages and not isolated to Golang.
Let's consider the following code:
package main
import (
"fmt"
)
func main() {
// not accurate
a := 5.2
b := 4.1
fmt.Println(a + b)
fmt.Println((a + b) == 9.3) // will return TRUE
c := 5.2
d := 2.1
fmt.Println(c + d)
fmt.Println((c + d) == 7.3) // will return FALSE
}
and running the program will produce this output:
9.3
true
7.300000000000001
false
As you can see that the calculations are not reliable and inaccurate for the certain decimal values. The inaccuracy is caused by the underlying CPU and the native representation used by Golang (which is faster and optimized for performing a large number of calculations).
To get accuracy in performing decimal calculations, you can use the github.com/shopspring/decimal package.
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
// accurate
aDec, _ := decimal.NewFromString("5.2")
bDec, _ := decimal.NewFromString("4.1")
fmt.Println(aDec.Add(bDec))
cDec, _ := decimal.NewFromString("9.3")
// wrong way to compare
fmt.Println((aDec.Add(bDec)) == cDec) // will still return FALSE
dDec := aDec.Add(bDec)
fmt.Println(dDec)
// still wrong way to compare
fmt.Println(dDec == cDec) // will still return FALSE
// proper way to compare
// see https://godoc.org/github.com/shopspring/decimal#Decimal.Cmp
// -1 if cDec < dDec
// 0 if cDec == dDec
// +1 if cDec > dDec
result := cDec.Cmp(dDec)
fmt.Println(cDec.Cmp(dDec)) // should return 0
if result == 0 {
fmt.Println("true")
}
}
Output:
9.3
false
9.3
false
0
true
For Golang developers that are more comfortable with the standard package such as math/big package.
package main
import (
"fmt"
"math/big"
)
func main() {
f1 := big.NewFloat(5.2)
f2 := big.NewFloat(2.1)
var f3, f4 big.Float
f3.Add(f1, f2)
fmt.Println(f3.String())
f4.Add(f1, f2)
fmt.Println(f3.Cmp(&f4)) // should return 0 and zero means equal
}
Output:
7.3
0
Depending on your application domain, most developers will use the standard float
..... however, if you are programming applications for science and engineering domain that demand accuracy. Please do consider testing your application for the decimal "feature" or inaccuracy and use the appropriate package to address the inaccuracy.
References:
https://github.com/golang/go/issues/12127
https://godoc.org/github.com/shopspring/decimal#Decimal.Cmp
https://www.socketloop.com/tutorials/golang-compare-floating-point-numbers
See also : Golang : Compare floating-point numbers
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+24.9k Golang : Storing cookies in http.CookieJar example
+8.5k Golang : Get final balance from bit coin address example
+13.6k Golang : How to check if a file is hidden?
+40.7k Golang : How to count duplicate items in slice/array?
+6.1k Golang : Break string into a slice of characters example
+21.1k Golang : Encrypt and decrypt data with TripleDES
+9.6k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+6.5k Golang : Get expvar(export variables) to work with multiplexer
+9.4k Golang : Detect number of active displays and the display's resolution
+44.3k Golang : Use wildcard patterns with filepath.Glob() example
+22.4k Golang : Round float to precision example
+11.4k Golang : How to detect a server/machine network interface capabilities?