Within Delta Checker for floats

Bug #1131537 reported by Nathan Youngman
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
gocheck
New
Undecided
Unassigned

Bug Description

How about including a Within checker, something like what follows?

Here's an example of it in use: https://github.com/nathany/go-poodr/blob/master/chapter9/gear1/gear1_check_test.go

type withinChecker struct {
  *CheckerInfo
}

var Within Checker = &withinChecker{
  &CheckerInfo{Name: "Within", Params: []string{"obtained", "delta", "expected"}},
}

func (c *withinChecker) Check(params []interface{}, names []string) (result bool, error string) {
  obtained, ok := params[0].(float64)
  if !ok {
    return false, "obtained must be a float64"
  }
  delta, ok := params[1].(float64)
  if !ok {
    return false, "delta must be a float64"
  }
  expected, ok := params[2].(float64)
  if !ok {
    return false, "expected must be a float64"
  }
  return math.Abs(obtained-expected) <= delta, ""
}

# https://gist.github.com/nathany/4238394

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.