Dear Apple

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Dear Apple

Postby Light-Dark on Mon Dec 29, 2014 3:54 am

WHAT THE FUCK:
unsigned int a = 1;
int b = -2;
if(b < a)
NSLog(@"Less than!");
else
NSLog(@"Greater than!");

Output:
Greater than!




Why is b casted to a uint and not a to a 64-bit signed int or even a 40-bit signed int?
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step

Image
User avatar
Light-Dark
Dreamcast Developer
Dreamcast Developer
 
Posts: 315
Joined: Sun Mar 13, 2011 8:57 pm
Location: Canada

Re: Dear Apple

Postby gamenovice on Mon Dec 29, 2014 6:03 am

How is this behaviour normally resolved using C++? Would negative numbers when compared to uints default to some logic?
without code, we wouldnt have life as we know it...
User avatar
gamenovice
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Fri Nov 12, 2010 9:49 pm
Location: Tampa,FL

Re: Dear Apple

Postby Light-Dark on Mon Dec 29, 2014 1:58 pm

gamenovice wrote:How is this behaviour normally resolved using C++? Would negative numbers when compared to uints default to some logic?


Apparently C++ and C do the same thing: http://ideone.com/b4ii49

That's really Interesting, I guess this sort of case would carry to much of performance penalty considering there appears to be no cpu compare instruction for this type of situation in the x86 lexicon. Upon further inspection the way to get this case to work is to explicitly convert the uint to a long int during the compare. I wonder why C/C++ compilers don't do this implicitly? Is it too much of a performance penalty or will implementing it at this point in the life of the language break too many things?
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step

Image
User avatar
Light-Dark
Dreamcast Developer
Dreamcast Developer
 
Posts: 315
Joined: Sun Mar 13, 2011 8:57 pm
Location: Canada

Re: Dear Apple

Postby gamenovice on Mon Dec 29, 2014 7:40 pm

huh, that is really interesting, But i could see the reasoning from a nomenclature sense. an unsigned int is by nature restricted to only positive numbers. in this case, it would probably be better to convert it to a long integer, so as to deal with signed integers.

but that still brings up a problem doesnt it? since signed integers have half the range the unsigned int does, the moment a uint exceeds the half range value, how do you go about comparing it? maybe bitwise operators could be used but... ehh... yea this is a tough one..
without code, we wouldnt have life as we know it...
User avatar
gamenovice
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Fri Nov 12, 2010 9:49 pm
Location: Tampa,FL

Re: Dear Apple

Postby wtetzner on Sat Jan 31, 2015 1:01 pm

Honestly, I think the right thing here would be for the compiler to give a type error, since they're different types. If you want to compare, then cast them so they're the same type. I know people feel like that would be annoying, but personally I think it's less annoying than having to remember the arbitrary behavior that compiler implements.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
 
Posts: 155
Joined: Wed Feb 18, 2009 8:43 pm
Location: Boston, MA

Re: Dear Apple

Postby bbguimaraes on Sat Jan 31, 2015 6:51 pm

I don't know about obj-c, but c rules are that types get promoted when two different types are compared. I don't have a reference right now, but one of the rules says a signed int gets promoted to an unsigned int.

Code: Select all
$ cat > test.c
#include <stdio.h>

int main() {
    printf("%d\n", (unsigned int) -2);
    printf("%u\n", (unsigned int) -2);
    printf("%x\n", (unsigned int) -2);
}
$ gcc -o test test.c && ./test
-2
4294967294
fffffffe
User avatar
bbguimaraes
Chaos Rift Junior
Chaos Rift Junior
 
Posts: 324
Joined: Wed Apr 11, 2012 5:34 pm
Location: Brazil

Re: Dear Apple

Postby gamenovice on Wed Apr 22, 2015 3:02 am

following the output provided by bbguimaraes, it does make sense when you remember the property of 2's complement (more importantly how it maps a signed number to unsigned).
without code, we wouldnt have life as we know it...
User avatar
gamenovice
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
 
Posts: 83
Joined: Fri Nov 12, 2010 9:49 pm
Location: Tampa,FL


Return to Programming Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron