Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   comparison is always false due to limited range of data type (http://www.velocityreviews.com/forums/t437099-comparison-is-always-false-due-to-limited-range-of-data-type.html)

Dave 02-26-2005 02:23 AM

comparison is always false due to limited range of data type
 
hello...
I wrote a marco for saturation.
#define clip(x) (char)(x)<0?0:((x)>255?255:(x));
and use this marco in the program like this...
char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2));

which unsigned_int_16 will cast the (tmp1+tmp2) to 16 bit
unsigned int.

Each time when I compile this program, the gcc warning me the
"comparison is always false due to limited range of data type"

Where leads this problem?
Please tell me! Thanx~~~

Dave.



Ben Pfaff 02-26-2005 02:47 AM

Re: comparison is always false due to limited range of data type
 
"Dave" <wcn93@cs.ccu.edu.tw> writes:

> I wrote a marco for saturation.
> #define clip(x) (char)(x)<0?0:((x)>255?255:(x));


[...]

> Each time when I compile this program, the gcc warning me the
> "comparison is always false due to limited range of data type"


Presumably `char' is unsigned, so the comparison (char)(x) < 0
is always false.

By the way, you seem to be over-fond of casts. You might want to
consider which casts are actually necessary and get rid of the
others.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt

Randy Howard 02-26-2005 02:51 AM

Re: comparison is always false due to limited range of data type
 
In article <cvomil$t0m$1@reader.ccu.edu.tw>, wcn93@cs.ccu.edu.tw
says...
> hello...
> I wrote a marco for saturation.
> #define clip(x) (char)(x)<0?0:((x)>255?255:(x));
> and use this marco in the program like this...
> char tmp=(char)clip((unsigned_int_16)(tmp1+tmp2));
>
> which unsigned_int_16 will cast the (tmp1+tmp2) to 16 bit
> unsigned int.
>
> Each time when I compile this program, the gcc warning me the
> "comparison is always false due to limited range of data type"


What do you think x < 0 does on an unsigned data type?


--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig


All times are GMT. The time now is 02:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.