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
+15.4k Golang : How to login and logout with JWT example
+21k Curl usage examples with Golang
+38.8k Golang : How to iterate over a []string(array)
+7.6k Golang : Get today's weekday name and calculate target day distance example
+15.4k Golang : Get digits from integer before and after given position example
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+4.1k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+7.9k Android Studio : Rating bar example
+4.8k Golang : Check if a word is countable or not
+25k Golang : Convert long hexadecimal with strconv.ParseUint example
+6.7k Golang : Pat multiplexer routing example
+12.7k Python : Convert IPv6 address to decimal and back to IPv6