float Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/float/ Mon, 23 Dec 2019 16:42:05 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 float Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/float/ 32 32 159787465 Comparing floating point numbers in Golang https://vikasboss.github.io/comparing-floating-point-numbers-go/ https://vikasboss.github.io/comparing-floating-point-numbers-go/#respond Mon, 23 Dec 2019 16:41:58 +0000 https://vikasboss.github.io/?p=859 Introduction Go Lang has two types of floats float32 float64 Comparing Same Float Types Two floating point numbers can be compared usingĀ  Go’s == operator assuming they are the same float types....

The post Comparing floating point numbers in Golang appeared first on Welcome To Golang By Example.

]]>
Introduction

Go Lang has two types of floats

  • float32
  • float64

Comparing Same Float Types

Two floating point numbers can be compared usingĀ  Go’s == operator assuming they are the same float types. See below example

package main

import "fmt"

func main() {
    a := 3.14
    b := 3.14
    if a == b {
        fmt.Println("Same")
    } else {
        fmt.Println("Not Same")
    }


    a = 3.142
    b = 3.14
    if a == b {
        fmt.Println("Same")
    } else {
        fmt.Println("Not Same")
    }
}

Output:

Same
Not Same

Comparing Different Float Types

Comparison of float32 with float64 will give a compilation error

package main

import "fmt"

func main() {
   
    var a float32
    var b float64
    if a == b {
        fmt.Println("Same")
    } else {
        fmt.Println("Not Same")
    }
}

Output:

invalid operation: a == b (mismatched types float32 and float64)

Comparing Same Float Types With Tolerance

If some kind of tolerance is acceptable while comparing float types, then below approach can be used

package main

import (
    "fmt"
    "math"
)

func main() {
    withTolerane(3.14, 3.141)
    withTolerane(3.14, 3.142)
}

func withTolerane(a, b float64) {
    tolerance := 0.001
    if diff := math.Abs(a - b); diff < tolerance {
        fmt.Printf("When a=%f and b =%f => Nearly same by tolerance\n", a, b)
    } else {
        fmt.Printf("When a=%f and b=%f => Not same Even by Tolerance\n", a, b)
    }
}

Output:

When a=3.140000 and b =3.141000 => Nearly same by tolerance
When a=3.140000 and b=3.142000 => Not same Even by Tolerance

Comparing Using math.big package

The big package supports big numbers and it supports Int, Rational Number and Float. It has a compare method which can be used to compare two floats

package main

import (
    "fmt"
    "math/big"
)

func main() {
    a := 3.1432
    b := 3.1456
    // compare a to b
    result := big.NewFloat(a).Cmp(big.NewFloat(b))
    
    // -1 if a < b
    if result < 0 {
        fmt.Println("a less than b")
    }
    
    // 0 if a == b
    if result == 0 {
        fmt.Println("a  equals to b")
    }
    
    // +1 if a > b
    if result > 0 {
        fmt.Println("a 1 more than b")
    }
}

Output:

a less than b

The post Comparing floating point numbers in Golang appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/comparing-floating-point-numbers-go/feed/ 0 859