floatcompare

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 9 Imported by: 0

README

Go Report Card Build Status Coverage Status License

floatcompare

Go linter to search for float comparison.

Why are float comparisons problematic?

The Floating-Point Guide has a good description why comparison of floats is problematic.

In short, since floating-point numbers do have some issues with rounding, a comparison of float might not return the expected result. Therefore, I wrote this linter which helps to find possible bugs with floating point number comparison.

Example

package main

import "fmt"

func main() {
    a := 0.15
    b := 0.15

    c := 0.2
    d := 0.1

    // comparison of this two calculation, we would expect them to be true
    if (a + b) == (c + d) {
        fmt.Println("I would expect this is true")
    } else {
        fmt.Println("But it is not.")
    }

    // Or we would expect this to be true
    if (a + b) >= (c + d) {
        fmt.Println("I would expect this is true")
    } else {
        fmt.Println("But it is not.")
    }

    // But I would not expect this to be true
    if (a + b) < (c + d) {
        fmt.Println("I would not expect this is true")
    }
}

This example would have the following output:

But it is not.
But it is not.
I would not expect this is true

What it detected?

This linter detects all comparisons of floats within the checked code. This includes switch case statements.

It provides flags to reduce the test to equal and not equal comparison and to omit test files.

How to use the linter

Install
go install github.com/mweb/floatcompare/cmd/floatcompare@latest
Usage

Call this within your project.

floatcompare ./...

There are two parameters available to disable certain checks:

--skipTests (to skip all *_test.go files)
--equalOnly (only warn for == and != comparison)

Credits

This tool is built on top of the excellent go/analysis package that makes it easy to write custom analyzers in Go. And the blog post "Using go/analsysis to write a custom linter" helped me to get started with this linter.

Thanks to Fatih Arslan

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAnalyzer

func NewAnalyzer() *analysis.Analyzer

NewAnalyzer create a new analyzer for float compare

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL