The operator "<" cannot be applied to operands of type "bool" and "double"

Wow ... I've never seen this before. Any way around this?

foreach( double r in portfolioReturns)
        {
            if (-8.0 < r < -7.0)
            {
                n8++;
            }}
+3
source share
5 answers

You do it effectively

if ((-8.0 < r) < -7.0)

Since (-8.0 <r) evaluates to logical, you cannot compare it to a float. Do this instead:

if (-8.0 < r && r < -7.0) {
  //code here
}
+11
source
if (-8.0 < r && r < -7.0)
+5
source

, python? , , : -)

(-8.0 < r < -7.0), -, -8.0 < r , . true < -7.0 barfs.

+3
source

You can also use LINQ, in addition to the fix that is all so quickly provided

n8 += portfolioReturns.Count(r => -8.0 < r && r < -7.0);
+3
source
foreach(double r in portfolioReturns)
{
    if(-8.0 < r && r < -7.0)
            n8++;
}
+1
source

Source: https://habr.com/ru/post/1761435/


All Articles