Search code examples
karate

Checking if numbers are approximately equal in Karate


I'm working with Karate, and I need to compare two numbers for approximate equality. Is there a built-in function or a recommended approach in Karate to check if two numbers are approximately equal within a certain tolerance?

For example, in other programming languages, there might be a function like assertAlmostEqual or a way to specify a delta for equality checks. Does Karate have a similar feature, or is there a standard practice for handling approximate equality in numeric comparisons?


Solution

  • Just use a re-usable function:

    * def isAlmostEqual = (actual, expected) => Math.abs(actual - expected) < 0.1
    * assert isAlmostEqual(1.0, 1.05)