Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Challenge/exercise problem - signum() function
Date: Mon, 12 Aug 2024 09:57:28 -0700
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <86jzgl65mv.fsf@linuxsc.com>
References: <86y152in9n.fsf@linuxsc.com> <868qx193ew.fsf_-_@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Mon, 12 Aug 2024 18:57:29 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f42e4005105099d89c60a754521770ce"; logging-data="3563735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FJx2WUnw2eoZKlSd/ajZf2NS2V9F03JU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Eq6EeM5+wfIfVgdMcRQ+KcAeOqw= sha1:xPwiETxUQRwlkRI4li/2TFDCeX0=
Xref: csiph.com comp.lang.c:387520
Lew Pitcher writes:
> On Mon, 12 Aug 2024 08:17:11 -0700, Tim Rentsch wrote:
>
>> Vir Campestris writes:
>>
>>> On 12/08/2024 01:43, Tim Rentsch wrote:
>>
>> [...]
>>
>>>> Also, it would be better for your understanding of C if you would
>>>> stop thinking about what is going on at the level of actual
>>>> hardware. Doing that serves to confuse a lot more than it helps.
>>>
>>> I think I feel my ears burning!
>>
>> I'm taking that as a compliment. :)
>>
>> Also as an impetus to post a small C exercise I've been meaning
>> to put up.
>>
>> The goal is to write a C function to compute a signum() value:
>>
>> long
>> signum( long k ){
>> /* should return
>> * -1 if k < 0
>> * +1 if k > 0
>> * 0 otherwise
>> **/
>> /* ... */
>> return 0; /* appropriate return value to be supplied */
>> }
>
> [snip]
>
> Didn't we do something like this a few years ago, when someone
> complained that strcmp() did /not/ return -1, 0, or +1 on
> lessthan, equalto, or greaterthan string comparisons?
Certainly there have been similar exercises, but I don't remember
seeing this exact question. The added restrictions make it more
difficult than it might appear.
> If I still have my "solution" around, I'll post it to followup
> the more current solutions.
Thank you, it would be good to see that.