Re: ULL multiply overflow

Discussion in 'C++' started by James Kanze, Oct 21, 2009.

  1. James Kanze

    James Kanze Guest

    On Oct 20, 7:21 pm, "Michael D. Berger" <>
    wrote:
    > In there a way to detect overflow in multiplying
    > two unsigned long long (other than dividing to see
    > if I get the original value)?


    Check beforehand: for a*b,
    if ( std::numeric_limits< unsigned long long >::max() / a < b )
    overflow...

    Other than that, implement your own extended precision types,
    multiply using them, then check the results.

    --
    James Kanze
     
    James Kanze, Oct 21, 2009
    #1
    1. Advertising

  2. James Kanze

    Bill Davy Guest

    "James Kanze" <> wrote in message
    news:...
    > On Oct 20, 7:21 pm, "Michael D. Berger" <>
    > wrote:
    >> In there a way to detect overflow in multiplying
    >> two unsigned long long (other than dividing to see
    >> if I get the original value)?

    >
    > Check beforehand: for a*b,
    > if ( std::numeric_limits< unsigned long long >::max() / a < b )
    > overflow...
    >
    > Other than that, implement your own extended precision types,
    > multiply using them, then check the results.
    >
    > --
    > James Kanze



    if ( a && std::numeric_limits< unsigned long long >::max() / a < b )
    ....
     
    Bill Davy, Oct 21, 2009
    #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. Jam
    Replies:
    3
    Views:
    2,797
    Martin Thompson
    Sep 15, 2004
  2. Jam
    Replies:
    2
    Views:
    740
  3. Pascal Peyremorte

    logic_std and multiply and array index

    Pascal Peyremorte, Aug 19, 2005, in forum: VHDL
    Replies:
    6
    Views:
    2,055
    Pascal Peyremorte
    Aug 29, 2005
  4. Replies:
    2
    Views:
    2,941
    Hubble
    Sep 13, 2005
  5. Replies:
    2
    Views:
    19,164
    Thomas Stanka
    Dec 15, 2005
Loading...

Share This Page