Arithmetic addition with strange results

Discussion in 'C++' started by dizzy, Sep 10, 2008.

  1. dizzy

    dizzy Guest

    Hello

    I have noticed a strange behavior (tho for many of you this is to be
    expected I'm sure :) ) when I have an expression that adds a long
    value with an unsigned one (like in "10l + 10u").

    On 32bit platforms (where unsigned can represent more than long) the
    result is of unsigned long type while on 64bit platforms the result is
    of long time.

    Usually in my coding experience I've learned that when writing bool
    expressions like "a - 10 < b" I should better write them "a < b + 10"
    because somehow I thought this keeps the expressions to their original
    types (I thought "b + 10" will have the type of "b") and then the case
    of comparing signed to unsigned values depends if a and b were mixed
    signed types. It seems that was false.

    Can someone point me to the relevant standard section that
    allows/requires that behavior described above?

    Thank you!

    --
    Dizzy
    dizzy, Sep 10, 2008
    #1
    1. Advertising

  2. dizzy

    James Kanze Guest

    On Sep 10, 5:16 pm, "Alf P. Steinbach" <> wrote:
    > * dizzy:
    > > I have noticed a strange behavior (tho for many of you this
    > > is to be expected I'm sure :) ) when I have an expression
    > > that adds a long value with an unsigned one (like in "10l +
    > > 10u").


    > > On 32bit platforms (where unsigned can represent more than
    > > long) the result is of unsigned long type while on 64bit
    > > platforms the result is of long time.


    > > Usually in my coding experience I've learned that when
    > > writing bool expressions like "a - 10 < b" I should better
    > > write them "a < b + 10" because somehow I thought this keeps
    > > the expressions to their original types (I thought "b + 10"
    > > will have the type of "b") and then the case of comparing
    > > signed to unsigned values depends if a and b were mixed
    > > signed types. It seems that was false.


    > > Can someone point me to the relevant standard section that
    > > allows/requires that behavior described above?


    > Just search (the standard) for "usual arithmetic conversions"
    > and "promotion".


    > And yes, the type of an expression depends on the ranges of
    > types with the particular C++ compiler, and so does the type
    > of a literal! :)


    Right. In particular, don't mix signedness. Ever. If you're
    stuck with a value which is signed, and another which isn't,
    cast one of them to ensure that both have the same signedness.
    (The exception is small non-negative values. You can safely use
    integral literals like 0 and 1 with unsigned types, even though
    they are formally signed. But that's about it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 10, 2008
    #2
    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. joshc
    Replies:
    5
    Views:
    544
    Keith Thompson
    Mar 31, 2005
  2. some strange for me (addition)

    , Dec 22, 2007, in forum: C Programming
    Replies:
    5
    Views:
    304
    David Thompson
    Jan 7, 2008
  3. Terrence Cole

    Bizarre arithmetic results

    Terrence Cole, Feb 11, 2010, in forum: Python
    Replies:
    10
    Views:
    409
  4. Robert Kern

    Re: Bizarre arithmetic results

    Robert Kern, Feb 11, 2010, in forum: Python
    Replies:
    5
    Views:
    267
    Jussi Piitulainen
    Feb 12, 2010
  5. FAQ server
    Replies:
    2
    Views:
    126
    John Hosking
    Aug 17, 2007
Loading...

Share This Page