• Challenge/exercise problem - signum() function

    From Tim Rentsch@21:1/5 to Vir Campestris on Mon Aug 12 08:17:11 2024
    Vir Campestris <vir.campestris@invalid.invalid> 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 */
    }

    Of course such a function is trivial to write. The challenge
    part is to write one that observes the following restrictions:

    * must work on any conforming C90 or C99 implementation,
    including freestanding implementations. (Hence _Bool
    is out of bounds.)

    * statements must be limited to expression statements and
    one return statement at the end of the function (simple
    declarations are also okay).

    * no conditional compilation (#if, #ifdef, #ifndef, etc)

    * no pointers, casts, unions, or user-defined types

    * all operators that return "logical" values are off
    limits: must not use !, <, <=, >, >=, ==, !=, &&, ||, ?:

    To make things easier, you may assume 'long' has no padding
    bits, and no trap representations (there may be negative
    zeros though).

    That's it. Should provide some fun for those who want to
    try it.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Tim Rentsch on Mon Aug 12 16:07:35 2024
    On Mon, 12 Aug 2024 08:17:11 -0700, Tim Rentsch wrote:

    Vir Campestris <vir.campestris@invalid.invalid> 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?

    If I still have my "solution" around, I'll post it to followup
    the more current solutions.

    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Lew Pitcher on Mon Aug 12 09:57:28 2024
    Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

    On Mon, 12 Aug 2024 08:17:11 -0700, Tim Rentsch wrote:

    Vir Campestris <vir.campestris@invalid.invalid> 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)