Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# accuracy when checking float in List<float> with Contains method

I have a list of floats and want to check if it already contains a particular value with the List.Contains() method. I know that for float equality tests you often can't use == but something like myFloat - value < 0.001.

My question is, does the Contains method account for this or I do I need to use a method that accounts for float precision errors for testing if the float is in the list?

like image 788
ogilberry Avatar asked Mar 08 '17 00:03

ogilberry


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

From the docs for List(T).Contains:

This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable<T>.Equals method for T (the type of values in the list).

So you will need to handle comparison with a threshold yourself. For example, you can use your own custom equality comparer. Something like this:

public class FloatThresholdComparer : IEqualityComparer<float>
{
    private readonly float _threshold;
    public FloatThresholdComparer(float threshold)
    {
        _threshold = threshold;
    }

    public bool Equals(float x, float y)
    {
        return Math.Abs(x-y) < _threshold;
    }

    public int GetHashCode(float f)
    {
        throw new NotImplementedException("Unable to generate a hash code for thresholds, do not use this for grouping");
    }
}

And use it:

var result = floatList.Contains(100f, new FloatThresholdComparer(0.01f))
like image 156
DavidG Avatar answered Oct 23 '22 03:10

DavidG


It just uses the default equality comparison for objects contained in the list. It will be the equivalent of calling object.Equals() when performing the comparisons.

If you need a different equality implementation, you could use the linq Contains() overload that accepts an equality comparer. Then you'd just have to implement that comparison and pass it in. This should perform roughly the same but ultimately slower.

like image 2
Jeff Mercado Avatar answered Oct 23 '22 04:10

Jeff Mercado


The other answers are correct, but if you want an alternative quick-and-dirty solution without writing a new equality comparer, you could use the List.Exists Method:

bool found = list.Exists(num => Math.Abs(num - valueToFind) < 0.001);

Edit: My original answer said the above was Linq, however the Exists method is part of the List class. The same concept using Linq is below, using IEnumerable.Any:

bool found = list.Any(num => Math.Abs(num - valueToFind) < 0.001);
like image 1
Corwin62 Avatar answered Oct 23 '22 05:10

Corwin62