Why Does This If Condition Fail for Comparison of Negative and Positive Integers

Why does this if condition fail for comparison of negative and positive integers

The problem is in your comparison:

    if ((-1) < SIZE)

sizeof typically returns an unsigned long, so SIZE will be unsigned long, whereas -1 is just an int. The rules for promotion in C and related languages mean that -1 will be converted to size_t before the comparison, so -1 will become a very large positive value (the maximum value of an unsigned long).

One way to fix this is to change the comparison to:

    if (-1 < (long long)SIZE)

although it's actually a pointless comparison, since an unsigned value will always be >= 0 by definition, and the compiler may well warn you about this.

As subsequently noted by @Nobilis, you should always enable compiler warnings and take notice of them: if you had compiled with e.g. gcc -Wall ... the compiler would have warned you of your bug.

problem with if-statement and negative integer

It should work because int here is Int32 range is -2,147,483,648 to 2,147,483,647. If you are entering any number within this range, it should work. For number outside this range you will get "value too small or too large".
Please share the number you are entering

why sizeof type compared with integer returns false

The sizeof operator yields not an int but size_t which is an unsigned integer type. When you compare a signed integer like -1 to an unsigned integer you will end up comparing the wrong values.

Do the following changes and the code will work as expected.

#include<stdio.h>

#include <stdbool.h>

int main()
{
int x = (int)sizeof(int) > -1;

bool z = sizeof(int);

printf("x is %d \t z is %d \n",x,z);
if((int)sizeof(int) > -1)
{
printf("true\n");
}
else
printf("false\n");
}

Output:

x is 1   z is 1
true

Negative Integer Comparisons

I suspect the issue is that the return of count is an unsigned integer, and subtracting more than it's magnitude, it underflows and becomes quite large. I have run some tests, and I get the same basic behavior as you (it looks like it's -1, and in certain contexts it appears to be working as expected... however it is clearly being underflowed in the context of the if() block.

Silly problem, but luckily there is a simple solution: Cast it in place in the if statement:

if([self.selectedSetpoint row] < ( (int)[self.theCategories count] -3 ))
{
//Do true stuff
}

Why does the below expression turn out to be true, asking for C++ specifically

Two things:

  1. sizeof(int) can be any positive integral value. (I've worked on a system where sizeof(char), sizeof(int) and sizeof(long) were all 1 and were all 64 bit types.)

  2. The type returned by sizeof is an unsigned type. When comparing with -1, -1 is converted to an unsigned value, with a high magnitude. Almost certainly sizeof(int) will be less than that.

Python number comparison for negative numbers

The main idea is to concentrate on numbers in the range -10 < i < 10
I propose a different solution, somewhat simpler than yours:

$ cat /tmp/tmp.py
from __future__ import print_function
import random

def max_single_digit(N):
cur_max = -10
for i in N:
if (i > cur_max) and (-10 < i < 10):
cur_max = i
if cur_max > -10:
return cur_max
else:
return "No single digit numbers found"

_range = 20

negatives = tuple([random.randint(-100,0) for x in range(_range)])
positives = tuple([random.randint(0,100) for x in range(_range)])
numbers = tuple([random.randint(-100,100) for x in range(_range)])

for nums in (negatives, positives, numbers):
print("For", nums, "the result is:", max_single_digit(nums),"\n")

Which gives:

$ python /tmp/tmp.py
For (0, -31, -87, -80, -47, -21, -21, -14, -37, -43, -71, -61, -47, -4, -36, -72, -78, -83, -14, -70) the result is: 0

For (17, 83, 80, 50, 35, 43, 9, 75, 23, 38, 45, 55, 46, 99, 80, 93, 36, 97, 88, 30) the result is: 9

For (-1, 30, -20, -68, 13, -66, -71, 92, 77, 85, -100, 8, 32, 92, -6, 97, 40, 21, 13, -48) the result is: 8

$ python /tmp/tmp.py
For (-94, -94, -28, -37, -50, -5, -29, -51, -6, -24, -18, -46, -32, -20, -89, -49, -55, -39, -50, -30) the result is: -5

For (91, 4, 16, 68, 6, 100, 61, 92, 81, 65, 63, 87, 67, 67, 97, 89, 98, 53, 40, 89) the result is: 6

For (11, 49, 63, -17, 71, 50, 28, 5, 31, -100, -35, -5, -8, 77, -87, 77, 3, 8, -39, -97) the result is: 8

$ python /tmp/tmp.py
For (-55, -48, -52, -75, -1, -89, -53, -66, -48, -17, -9, -96, -16, -40, -52, 0, -90, -97, -40, -85) the result is: 0

For (52, 19, 82, 45, 54, 47, 94, 54, 46, 8, 66, 22, 100, 25, 0, 81, 79, 39, 5, 20) the result is: 8

For (-43, -32, 92, -59, -91, 63, -95, 100, -85, -21, 35, -88, -38, 43, -25, 85, 76, 67, -82, 87) the result is: No single digit numbers found

How to check the value given is a positive or negative integer?

if (values > 0) {
// Do Something
}


Related Topics



Leave a reply



Submit