locoboy
locoboy

Reputation: 38940

Operator '<' cannot be applied to operands of type 'bool' and 'double'

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

foreach( double r in portfolioReturns)
        {
            if (-8.0 < r < -7.0)
            {
                n8++;
            }}

Upvotes: 4

Views: 7489

Answers (5)

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

You could also use LINQ, in addition to the fix everyone so quickly provided

n8 += portfolioReturns.Count(r => -8.0 < r && r < -7.0);

Upvotes: 3

Mahmoud
Mahmoud

Reputation: 720

You are effectively doing this

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

Since (-8.0 < r) evaluates to a boolean, you can't compare it to a float. Do this instead:

if (-8.0 < r && r < -7.0) {
  //code here
}

Upvotes: 13

Matt Briggs
Matt Briggs

Reputation: 42208

let me guess, you are coming from python? It is the only language I know of where that works :-)

looking at (-8.0 < r < -7.0), first, -8.0 < r gets evaluated, which is true. true < -7.0 barfs.

Upvotes: 3

Justin Niessner
Justin Niessner

Reputation: 245429

foreach(double r in portfolioReturns)
{
    if(-8.0 < r && r < -7.0)
            n8++;
}

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

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

Upvotes: 5

Related Questions