On Sun, Feb 14, 2021 at 1:19 PM Santhosh Kumar T <santhosh.tek...@gmail.com> wrote:
> When I print both values, they print exactly same. so I am assuming no > precision lost for this specific example 123.4. > but still Cmp returns non-zero. > As Dan pointed out, that is an invalid assumption. The code that formats a float (whether big or float64) as a string is aware that the IEEE 754 binary format for floats cannot represent most values exactly and compensates accordingly. That way you get "3.5" rather than "3.499999999999999" for example. > On Monday, February 15, 2021 at 2:03:24 AM UTC+5:30 Kurtis Rader wrote: > >> The value 123.4 cannot be represented exactly as a float64. See >> https://golang.org/pkg/math/big/#NewFloat for the caveats involved in >> using NewFloat() or SetFloat64(). Passing a string representation allows >> the implementation to preserve the exact value without rounding. >> >> On Sun, Feb 14, 2021 at 12:21 PM Santhosh Kumar T <santhos...@gmail.com> >> wrote: >> >>> I created: >>> one instance using big.NewFloat function >>> another instance using big.Float.SetString method >>> >>> when compared both these instances using big.Float.Cmp, it return >>> non-zero >>> >>> code: https://play.golang.org/p/NhQ-klcph2z >>> >>> am I doing something wrong ? >>> can some one explain why Cmp returns non-zero ? >>> >> -- Kurtis Rader Caretaker of the exceptional canines Junior and Hank -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD8eK8XRfbKmN2oKi2RCv2FkLc2ML8BdqCzC3DxbL7cm-A%40mail.gmail.com.