How test a function with float return


I’m trying to test a function that returns a float, the problem is that I want to put the number manually and then compare it with the return of the function, but the test always fails since the two floats are never the same. For example, the result of the function is 0.8, but if I test:
Result: = floatFun () // result = 0.8
If result == 0.8 {

The test fails, since the 0.8 I put manual becomes a 0.7999 …
How to handle the floats in order to test them?

The test suite for math uses some helper functions that help with the inexact nature of floats.

These aren’t exported, but give a good idea of what you can do.

In general you cannot compare two floating point numbers for equality.

How will this function be used in real code? The test should do the same.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.