-1

I have a code which reads the temperature values constantly and temperature values will be always negative. How do I write a If condition in C# to check if this temperature falls with in a range of -44 and -46.

I tried with the below code and still the condition passes

             if (!Enumerable.Range(-44, 1).Contains(cameraTemp)
              || !Enumerable.Range(-45, 1).Contains(cameraTemp)
              || !Enumerable.Range(-46, 1).Contains(cameraTemp))

I also tried with the below code, but some how still the condition passes

             if (!(cameraTemp >= -44 && cameraTemp <= -46))

Though my temperature value is -45, still the condition passes.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Baba
  • 49
  • 1
  • 8

5 Answers5

3

You should try following if range is -44 to -46.

 if (!(cameraTemp <= -44 && cameraTemp >= -46))

when we fall to nagative number like 0 to -1. 0 is greater then -1. Same way -1 greater then -2. So it is bit reverse order.

If you get confuse with this then you have to convert value to absolute value and then apply condition.

To elaborate more.

Following condition

1. if (!(cameraTemp <= -44 && cameraTemp >= -46))
   {
        // Some print or logic
   }

If you write condition above way then your logic only execute if temperature value is not between -44 to -46 including -44 and -46. 

  2. if ((cameraTemp <= -44 && cameraTemp >= -46))
       {
            // Some print or logic
       }
    If you write this way then it only execute for value -44 , -45 and -46.
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • Explaining how negative numbers work might add more value to the response. My two cents. – danish May 11 '18 at 11:30
  • If you pass `-45` as `cameraTemp`, are you sure it will enter this condition? – SᴇM May 11 '18 at 11:37
  • @SeM it will not. It will not enter for -44,-45 and -46. – dotnetstep May 11 '18 at 11:41
  • @dotnetstep Am I getting something wrong or OP is asking _"How do I write a If condition in C# to check if this temperature falls with in a range of -44 and -46?"_, which means that it **should** enter the condition? – SᴇM May 11 '18 at 11:44
  • All, it was resolved by applying the condition in reverse order :). Thats my bad.This code worked if (!(cameraTemp <= -44 && cameraTemp >= -46)) – Baba May 11 '18 at 18:09
1

Or you should try following if you want to enter range -44 to -46:

if (cameraTemp >= -46 && cameraTemp <= -44)
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

You've inverted the operators to check temperature

if (!(cameraTemp <= -44 && cameraTemp >= -46))

-45 is inferior than -44 and superior than -46 :-)

Cid
  • 14,968
  • 4
  • 30
  • 45
0

You're mixing up your greater thans and less thans. In your second attempt, you can't simultaneously be less than -46 (e.g. that would be -47, -48, etc.) and greater than -44 (e.g. -43, -42, etc.)

if (!(cameraTemp <= -44 && cameraTemp >= -46))
rory.ap
  • 34,009
  • 10
  • 83
  • 174
0

-46 is smaller than -44 so you have to reverse your condition.

if (!(cameraTemp <= -44 && cameraTemp >= -46))
Krzyserious
  • 364
  • 4
  • 12