GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - mweb/floatcompare: go linter to search for float comparison

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
cmd/floatcompare
cmd/floatcompare
 
 
testdata/src
testdata/src
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
analyzer.go
analyzer.go
 
 
analyzer_test.go
analyzer_test.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 

Repository files navigation

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

About

go linter to search for float comparison

Resources

Readme
MIT license
Activity

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.