Re: Computer time -> Developer time -> User time?

Discussion in 'C Programming' started by Ro, Jan 25, 2005.

  1. Ro

    Ro Guest

    On Mon, 24 Jan 2005 23:30:17 +0100, Herbert Kleebauer <>
    wrote:
    >Ro wrote:
    >
    >> I think that a complex language (like C)
    >> where is possible to write

    >
    >> z= x*y+x+y;

    >
    >> has the change to cause error on overflow in the last expression
    >> but asm not

    >
    >There can't be an overflow in a modulo 2**n arithmetic.


    So how do you call (unsigned char)64 * (unsigned char 4)?

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {unsigned char i=64,j=4,k;
    unsigned int a1,a2,m1,m2, m3;

    a1=k=i+j; a2=i+j;
    m1=k=i*j; m2=i*j;
    m3=(unsigned char)(i*j);

    printf("a1=%d a2=%d m1=%d m2=%d m3=%d\n",
    a1, a2, m1, m2, m3);
    return 0;
    }

    with output here:
    a1=68 a2=68 m1=0 m2=256 m3=0

    (it seems in m1 the result of overflow)
    but what I don't understand is why m2=256
    if i=64 is uchar and j=4 is uchar => i*j=256=0 in uchar
    so why here m2=256 ? Why m2!=m3?
    [So m2=((unsigned)i)*j; and not m2=(unsigned)(unsigned char)(i*j);]
    Thank you
     
    Ro, Jan 25, 2005
    #1
    1. Advertising

  2. Ro

    pete Guest

    Ro wrote:
    >
    > On Mon, 24 Jan 2005 23:30:17 +0100, Herbert Kleebauer <>
    > wrote:
    > >Ro wrote:
    > >
    > >> I think that a complex language (like C)
    > >> where is possible to write

    > >
    > >> z= x*y+x+y;

    > >
    > >> has the change to cause error on overflow in the last expression
    > >> but asm not

    > >
    > >There can't be an overflow in a modulo 2**n arithmetic.

    >
    > So how do you call (unsigned char)64 * (unsigned char 4)?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {unsigned char i=64,j=4,k;
    > unsigned int a1,a2,m1,m2, m3;
    >
    > a1=k=i+j; a2=i+j;
    > m1=k=i*j; m2=i*j;
    > m3=(unsigned char)(i*j);
    >
    > printf("a1=%d a2=%d m1=%d m2=%d m3=%d\n",
    > a1, a2, m1, m2, m3);
    > return 0;
    > }
    >
    > with output here:
    > a1=68 a2=68 m1=0 m2=256 m3=0
    >
    > (it seems in m1 the result of overflow)
    > but what I don't understand is why m2=256
    > if i=64 is uchar and j=4 is uchar => i*j=256=0 in uchar
    > so why here m2=256 ? Why m2!=m3?
    > [So m2=((unsigned)i)*j; and not m2=(unsigned)(unsigned char)(i*j);]
    > Thank you


    "Overflow" is where the result of an
    operation can't be represented by the type.
    Signed integer overflow is like that.
    That's undefined behavior.

    With unsigned types, when the correct result can't be
    represented by the type, then an adjusted result can be.
    In C, the word "overflow" is not used to
    describe what happens to unsigned types.

    --
    pete
     
    pete, Jan 25, 2005
    #2
    1. Advertising

  3. Ro

    Phil Carmody Guest

    Ro <> writes:
    > On Mon, 24 Jan 2005 23:30:17 +0100, Herbert Kleebauer <>
    > >There can't be an overflow in a modulo 2**n arithmetic.

    >
    > So how do you call (unsigned char)64 * (unsigned char 4)?


    If you view unsigned char as a model of the natural
    numbers, then yes, it's an overflow. However, unsigned
    char is a model of Z/(2^b)Z for some b>=8, and not
    the natural numbers. It's not an overflow, it's wrapping,
    and as such is perfectly acceptible. However, like Eminem,
    it can be annoying if you didn't want it.

    Phil
    --
    Excerpt from Geoff Bulter's Proscriptive Dictionary:
    aaa Don't use this, there's no such word
    aaaa Don't use this, there's no such word
    aaaaa Don't use this, there's no such word
     
    Phil Carmody, Jan 25, 2005
    #3
  4. On Tue, 25 Jan 2005 09:27:52 +0000, Ro wrote:

    > On Mon, 24 Jan 2005 23:30:17 +0100, Herbert Kleebauer <>
    > wrote:
    >>Ro wrote:
    >>
    >>> I think that a complex language (like C)
    >>> where is possible to write

    >>
    >>> z= x*y+x+y;

    >>
    >>> has the change to cause error on overflow in the last expression
    >>> but asm not

    >>
    >>There can't be an overflow in a modulo 2**n arithmetic.

    >
    > So how do you call (unsigned char)64 * (unsigned char 4)?


    If you interpret (unsigned char)64 * (unsigned char)4 you will get the
    result 256 because unsigned char operands would (typically) be pronoted to
    int before the operation is performed and the result of the operation
    would then also have type int.

    But speaking in mathematical terms in a modulo 256 number system the
    result of 64 * 4 is 0. This is not because of overflow, it is simply the
    natural result in that mathematical system. Consider the simpler case of
    255+1 In modulo 256 arithmetic the number after 255 is 0, there is no
    boundary as such to "overflow".

    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {unsigned char i=64,j=4,k;
    > unsigned int a1,a2,m1,m2, m3;
    >
    > a1=k=i+j; a2=i+j;
    > m1=k=i*j; m2=i*j;
    > m3=(unsigned char)(i*j);


    In this case the code is converting an int value of 256 back to unsigned
    char. To do that we apply C's rules for converting out of range values.
    This happens to be consistent, also producing a zero result where
    CHAR_BIT is 8.

    > printf("a1=%d a2=%d m1=%d m2=%d m3=%d\n",
    > a1, a2, m1, m2, m3);
    > return 0;
    > }
    >
    > with output here:
    > a1=68 a2=68 m1=0 m2=256 m3=0
    >
    > (it seems in m1 the result of overflow)
    > but what I don't understand is why m2=256
    >
    > if i=64 is uchar and j=4 is uchar => i*j=256=0 in uchar so why here
    > m2=256 ? Why m2!=m3?
    > [So m2=((unsigned)i)*j; and not m2=(unsigned)(unsigned char)(i*j);]


    The multiplications are performed in int arithmetic. Without the
    assignment to k as in the case of m1, or the case in m3, the result isn't
    shorted back to character width.

    Lawrence
     
    Lawrence Kirby, Jan 26, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A. M. G. Solo
    Replies:
    0
    Views:
    557
    A. M. G. Solo
    Jan 13, 2006
  2. A. M. G. Solo
    Replies:
    0
    Views:
    795
    A. M. G. Solo
    Jun 8, 2006
  3. Replies:
    0
    Views:
    353
  4. A. M. G. Solo (do not reply to this email address)
    Replies:
    0
    Views:
    733
    A. M. G. Solo (do not reply to this email address)
    Nov 8, 2006
  5. A. M. G. Solo
    Replies:
    0
    Views:
    743
    A. M. G. Solo
    Dec 5, 2006
Loading...

Share This Page