equalWithin on DU types of float
See original GitHub issueDescription
I am building test on discriminated union types that can take on a case =of float=. However the =equalWithin= operator is not able to compare two numbers within a range for such types, making it hard to test things as I need to drill down to perform explicit float conversion. See example:
Repro steps
type myType = | Number of float | Name of string
// inside test:
1.1 * (1. + 2.) |> should (equalWithin 1E-10) 3.3 //this test passes
(Number (1.1 * (1. + 2.))) |> should (equalWithin 1E-10) (Number 3.3) //this test fails
Expected behavior
I would like both tests to pass or for there to be a simple way to write the test. Consider example:
let a = //code that produces a record containing myType instances.
a |> should (equalWithin 1E-10) {g="string"; h=Number 4;....}
Currently I have to test field by field so I can perform explicit conversion to floats for the number fields. Maybe the equalWithin operator could be expanded to cover types with declared double fields or similar?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Comparing Floating Point Numbers - Mod the Machine
Below is a more elaborate version of the IsEqual function that supports various types of comparisons. ' Compares two numbers to see if...
Read more >Triggering googletest error on EXPECT_FLOAT_EQ even ...
I'm creating unit tests for a program that performs floating point computations in C++. Throughout the unit test file, I've been using ...
Read more >Floats
Floats. A Go library with utilities for floats. For now, there are only functions to compare floats according to floating-point-gui.de.
Read more >Verifying the Floating-Point Computation Equivalence of ...
All sub-expressions must be of the same floating-point type, float, double, or long double. Table 1: Rewrite Sequence Statistics. Rules. (1) (2) ...
Read more >ISEQUALFP: Check equality within floating point precision
This function accepts two float values (single or double) or arrays of floats, and returns a logical value indicating whether they are equal...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@CaptnCodr yes, I agree.
That how we write tests in .NET (C#, F#) - multiple assertions for primitive types. If you need more, you can implement helper functions for your project or override equality for record type.
Hey @johanvts
What simply FsUnit does is:
Assert.AreEqual(expected, actual)
=>actual |> should equal expected
equal
andequalWithin
are very different:equal
is a simple check:x = y
equalWithin
checks if theactual
,expected
andtolerance
are convertible tofloat
and if the difference betweenactual
andexpected
is less than thetolerance
then this test passesequal
handles all types.equalWithin
handles numeral types with a given range (tolerance
).This feature does not fit in FsUnit, the reasons are described above.
I would like to close this issue. Do you agree with it, @sergey-tihon?
Thanks!