Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143010
| From | John Forkosh <forkosh@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Application Exercises |
| Date | 2019-04-11 13:19 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <q8nes5$elp$2@reader2.panix.com> (permalink) |
| References | (1 earlier) <861s2emhi2.fsf@mailhub.linuxsc.com> <71d9629f-03be-40e1-bb23-3a9314e745df@googlegroups.com> <86tvf7ju49.fsf@mailhub.linuxsc.com> <q8ne7e$elp$1@reader2.panix.com> <bab924a2-46f0-4206-8b2f-0ef179ae5a8a@googlegroups.com> |
Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:
> On Thursday, 11 April 2019 14:08:07 UTC+1, John Forkosh wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>> > mark.bluemel@gmail.com writes:
>> >
>> >> On Sunday, 7 April 2019 07:55:40 UTC+1, Tim Rentsch wrote:
>> >>
>> >>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> >>>
>> >>>> |Write a function to detect whether two integral numbers are
>> >>>> |equal without using any comparison operators.
>> >>>
>> >>> I have written a solution that I believe works correctly
>> >>> on any conforming implementation.
>> >>
>> >> But it's too large to fit in this margin?
>> >
>> > Actually I wrote several solutions. But I wasn't really
>> > happy with any of them, they were all a bit clunky in one
>> > way or another. The point of my comment is just to say
>> > a fully portable solution is possible.
>>
>> Didn't anybody just write...
>>
>> /* --- test whether or not two ints are equal,
>> without comparisons --- */
>> #include <stdio.h>
>> #include <stdlib.h>
>> int main ( int argc, char *argv[] ) {
>> int i = (argc>1? atoi(argv[1]) : 1 ),
>> j = (argc>2? atoi(argv[2]) : 2 );
>> int isequal();
>> fprintf( stdout," %d %s %d? \n",
>> i, (isequal(i,j)?"==":"=/="), j);
>> } /* --- end-of-function main() --- */
>>
>> int isequal ( int i, int j ) {
>> return ( !( abs(i-j) ) );
>> } /* --- end-of-function isequal() --- */
>>
>> Works fine on my box.
>>
> If both i is negative and j positive and large, this can generate an
> arithmetical overflow error
Sheesh! I didn't think that the -extremely-bleeping-pedantic compiler
switch was the point of the exercise:) So how about
return ( !( abs(abs(i)-abs(j)) ) ); ???
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Application Exercises Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-04-06 23:55 -0700
Re: Application Exercises mark.bluemel@gmail.com - 2019-04-08 00:33 -0700
Re: Application Exercises Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-04-09 04:28 -0700
Re: Application Exercises John Forkosh <forkosh@panix.com> - 2019-04-11 13:07 +0000
Re: Application Exercises Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-04-11 06:11 -0700
Re: Application Exercises John Forkosh <forkosh@panix.com> - 2019-04-11 13:19 +0000
Re: Application Exercises James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-11 06:49 -0700
Re: Application Exercises Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-12 01:20 +0100
Re: Application Exercises Richard Damon <Richard@Damon-Family.org> - 2019-04-11 23:36 -0400
Re: Application Exercises Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-04-12 19:08 -0700
csiph-web