O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter03/recipe02.
  2. Navigate to the directory.
  3. Create the tolerance.go file with the following content:
        package main        import (          "fmt"          "math"        )        const da = 0.29999999999999998889776975374843459576368331909180        const db = 0.3         func main() {          daStr := fmt.Sprintf("%.10f", da)          dbStr := fmt.Sprintf("%.10f", db)           fmt.Printf("Strings %s = %s equals: %v \n", daStr,                     dbStr, dbStr == daStr)          fmt.Printf("Number equals: %v \n", db == da)          // As the precision of float representation          // is limited. For the float comparison it is          // better to use comparison with some tolerance.          fmt.Printf("Number equals with TOLERANCE: %v \n",                      equals(da, db))        }        const TOLERANCE = 1e-8 // Equals compares the floating-point ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required