Re: Simple question regarding command line argument
Quote:
Originally Posted by Hermit
I'm a little curious about this. Aren't the non-zero return values of strcmp unspecified? I can see how if you used multiplication it would be a sort of logical and (edit: only sort of), but division seems like it could round off to zero (imagine a strcmp implementation that returns the difference of the unequal characters).
it is actually a VERY old teaser...
"Pretend" would could compare string values with "normal" operators. and use "regular" math....
Code:
if ((a<b<c) || (a>b>c)) {...}
A simple test to see if three strings are different and monotonically either increasing or decreasing.
All the division cares about is the sign. If they are poth the same it will be positive, if they are different they will be negative....
Re: Simple question regarding command line argument
Quote:
Originally Posted by TheCPUWizard
it is actually a VERY old teaser...
"Pretend" would could compare string values with "normal" operators. and use "regular" math....
Code:
if ((a<b<c) || (a>b>c)) {...}
A simple test to see if three strings are different and monotonically either increasing or decreasing.
All the division cares about is the sign. If they are poth the same it will be positive, if they are different they will be negative....
Okay, that's what it looked like. I'm still curious about the use of division as opposed to multiplication. My thoughts on this are:
a) strcmp is specified as returning either 0 or... something else. Suppose strcmp(a,b) returned 1 and strcmp(b,c) returned 2. In that case, strcmp(a,b)/strcmp(b,c) would equal 0.
b) Integer multiplication is faster than division (Which is to say that division is not faster than multiplication... not trying to say there's a significant speed difference).
Re: Simple question regarding command line argument
'strcmp' is supposed to return a negative number if "a < b", and a positive number if "a > b". In the comparison that TheCPUWizard posted, we only care if the result is negative or positive. Negative divided by negative results in a positive; negative divided by positive results in a negative. The actual value is irrelevant.
Viggy
Re: Simple question regarding command line argument
That's assuming it's always the *same* positive or negative value. If it isn't, Hermit is pointing out that integer division could cause the result to be 0.