J Fabian Meier
J Fabian Meier

Reputation: 35785

LINQ: Finding maximal element (not maximal value)

Assume you have a list of edges with given length (as double). Now you want to find the edge with maximal length. Is there an easy way to do this in LINQ?

Of course, I can first compute the maximal value with Max and then do comparison, but firstly, this would be a two computations and secondly comparing doubles for equality is a bad thing.

Any suggestions?

Upvotes: 2

Views: 140

Answers (2)

sehe
sehe

Reputation: 392833

Upvotes: 6

NPSF3000
NPSF3000

Reputation: 2451

sehe beat me to the correct answer, so I'll use this to note a possibly flawed assumption:

...secondly comparing doubles for equality is a bad thing.

IIRC there shouldn't be any problem comparing a copied double for equality. After all it's just 8 bytes. The problem occurs in computation - or using two similar values from different sources.

That said, it's good to be scared of comparing floating point numbers :P

Upvotes: 0

Related Questions