question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List/Array/Seq.max/min/maxBy/minBy may return nan if the sequence starts with nan

See original GitHub issue

The position where NaN appears changes what Seq.max returns.

Repro steps

printfn $"{Seq.max [nan; 2.; 1.; 4.]}"
printfn $"{Seq.max [3.; nan; 1.; 4.]}"
printfn $"{Seq.max [3.; 2.; nan; 4.]}"
printfn $"{Seq.max [3.; 2.; 1.; nan]}"

Expected behavior

4
4
4
3

Actual behavior

NaN
4
4
3

Known workarounds

Filter away all NaNs first.

Related information

SharpLab https://sharplab.io/#v2:DYLgZgzgNALiBOBXAdgHwA7wJbJmZABACQBEA3gMoCmAjgHQC2AhgB4EDayTyA3AQEx0+ARiEEALHQC6AXxIBYAFCYceQqUq1GrDgGYxXXgVF9Jshcuy58xctXrM27fX0F9Dp6XKUrr6u1qOemJuxgbc5kA=

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
tannergoodingcommented, Jul 25, 2022

seemingly making NaN the lowest number, so it only affects Min, not Max.

Missed this bit. This does seem like a bug and an undesirable one at that. If someone is willing to log an issue, then we should probably look at this and make a determination.

2reactions
abelbraaksmacommented, Jul 25, 2022

@edgarfgp, I’ve taken that on, see #13558. So far I’ve only added tests, but that by and of itself exhibited some behavior that may need addressing.

Min is weird one, seems NaN is always lowest.

@dsyme/@vzarytovskii, considering that .NET as a whole is moving to more conformance towards IEEE 754:2019, I think it’s probably best not to mimic the bug from LINQ. Besides, if that one in particular relies on Math.Min, there’s https://github.com/dotnet/runtime/pull/70865 by @tannergooding, which fixes that behavior.

My understanding is min and max should return NaN in all four cases.

That’s also my understanding. It would otherwise be quite surprising w.r.t. the basic rule with NaN: if either operand is NaN, return NaN.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Why does max() sometimes return nan and ...
The reason is that max works by taking the first value as the "max seen so far", and then checking each other value...
Read more >
NaN - JavaScript - MDN Web Docs
The NaN global property is a value representing Not-A-Number. ... There are five different types of operations that return NaN :.
Read more >
How to Fix Pandas Standard Deviation Returning NaN
Another reason why pandas standard deviation may return NaN is if there is insufficient data to calculate the standard deviation.
Read more >
Watch out for NaNs - The Rust Programming Language Forum
That's because due to rounding errors it ends up taking a square root of a tiny negative number, and sqrt is defined to...
Read more >
Cost function turning into nan after a certain number of iterations
Well, if you get NaN values in your cost function, it means that the input is outside of the function domain. E.g. the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found