Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7024 > unrolled thread

(simple?) problem with multiplication

Started byTtodir <sanmrtn96@gmail.com>
First post2011-06-21 11:04 -0700
Last post2011-06-25 10:29 +0200
Articles 20 on this page of 48 — 16 participants

Back to article view | Back to comp.lang.c++


Contents

  (simple?) problem with multiplication Ttodir <sanmrtn96@gmail.com> - 2011-06-21 11:04 -0700
    Re: (simple?) problem with multiplication Ttodir <sanmrtn96@gmail.com> - 2011-06-21 11:11 -0700
    Re: (simple?) problem with multiplication "Balog Pal" <pasa@lib.hu> - 2011-06-21 20:35 +0200
      Re: (simple?) problem with multiplication Anonymous <anonymous@no.net> - 2011-06-21 20:45 +0200
      Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-21 14:46 -0400
        Re: (simple?) problem with multiplication Ttodir <sanmrtn96@gmail.com> - 2011-06-21 12:03 -0700
          Re: (simple?) problem with multiplication Gido <gido0001@gmail.com> - 2011-06-21 14:30 -0700
            Re: (simple?) problem with multiplication Anonymous <anonymous@no.net> - 2011-06-21 23:32 +0200
            Re: (simple?) problem with multiplication Juha Nieminen <nospam@thanks.invalid> - 2011-06-23 06:33 +0000
    Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-21 14:40 -0400
    Re: (simple?) problem with multiplication Christopher <cpisz@austin.rr.com> - 2011-06-21 11:39 -0700
    Re: (simple?) problem with multiplication Anonymous <anonymous@no.net> - 2011-06-21 23:32 +0200
      Re: (simple?) problem with multiplication Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-21 23:41 +0200
    Re: (simple?) problem with multiplication Rune Allnor <allnor@tele.ntnu.no> - 2011-06-21 21:33 -0700
      Re: (simple?) problem with multiplication Ttodir <sanmrtn96@gmail.com> - 2011-06-22 04:59 -0700
    Re: (simple?) problem with multiplication gwowen <gwowen@gmail.com> - 2011-06-22 06:55 -0700
      Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-22 10:10 -0400
        Re: (simple?) problem with multiplication Leigh Johnston <leigh@i42.co.uk> - 2011-06-22 15:19 +0100
          Re: (simple?) problem with multiplication Dilip <rdilipk@gmail.com> - 2011-06-22 07:33 -0700
            Re: (simple?) problem with multiplication Leigh Johnston <leigh@i42.co.uk> - 2011-06-22 15:39 +0100
              Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-22 11:02 -0400
                Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-23 23:45 +0100
            Re: (simple?) problem with multiplication gwowen <gwowen@gmail.com> - 2011-06-22 23:59 -0700
        Re: (simple?) problem with multiplication gwowen <gwowen@gmail.com> - 2011-06-22 23:50 -0700
          Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-23 09:26 +0100
    Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-22 21:24 +0100
      Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-24 00:00 +0100
    Re: (simple?) problem with multiplication gwowen <gwowen@gmail.com> - 2011-06-24 01:42 -0700
      Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-24 10:47 +0100
    Re: (simple?) problem with multiplication SG <s.gesemann@gmail.com> - 2011-06-24 10:09 -0700
      Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-24 13:32 -0400
        Re: (simple?) problem with multiplication Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-24 20:07 +0200
          Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-24 14:14 -0400
            Re: (simple?) problem with multiplication Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-24 20:28 +0200
              Re: (simple?) problem with multiplication Pete Becker <pete@versatilecoding.com> - 2011-06-24 16:26 -0400
                Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 03:01 +0100
                  Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 18:20 +0100
              Re: (simple?) problem with multiplication "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-25 04:21 +0200
              Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 10:12 +0100
                Re: (simple?) problem with multiplication SG <s.gesemann@gmail.com> - 2011-06-25 02:51 -0700
                  Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 11:19 +0100
                  Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 11:19 +0100
                  Re: (simple?) problem with multiplication Gareth Owen <gwowen@gmail.com> - 2011-06-26 07:59 +0100
                    Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-26 12:34 +0100
            Re: (simple?) problem with multiplication SG <s.gesemann@gmail.com> - 2011-06-25 02:37 -0700
          Re: (simple?) problem with multiplication Gareth Owen <gwowen@gmail.com> - 2011-06-25 08:58 +0100
            Re: (simple?) problem with multiplication "Paul" <pchristor@yahoo.co.uk> - 2011-06-25 10:00 +0100
            Re: (simple?) problem with multiplication Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-25 10:29 +0200

Page 1 of 3  [1] 2 3  Next page →


#7024 — (simple?) problem with multiplication

FromTtodir <sanmrtn96@gmail.com>
Date2011-06-21 11:04 -0700
Subject(simple?) problem with multiplication
Message-ID<589f8b74-d065-44a5-ab9e-81e6a6fcd88b@v12g2000vby.googlegroups.com>
Hello,

I need a fast way to calculate (a * b) % B, with the following
constraints:

- a, b, B are int
- B = 10^N , N>0 arbitrary (B always fits in an int)
- the result must be valid even if the multiplication overflows
- portable code, no assumptions on the sizeof(int) and no types larger
than int can be used.

Any help will be much appreciated.

[toc] | [next] | [standalone]


#7025

FromTtodir <sanmrtn96@gmail.com>
Date2011-06-21 11:11 -0700
Message-ID<ca838a99-8111-4ac3-8c1f-702173cc4751@l6g2000vbn.googlegroups.com>
In reply to#7024
On Jun 21, 8:04 pm, Ttodir <sanmrt...@gmail.com> wrote:
> Hello,
>
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
>
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
>
> Any help will be much appreciated.

Sorry, just one more note: consider a >=0 and b >=0 (but int)

[toc] | [prev] | [next] | [standalone]


#7026

From"Balog Pal" <pasa@lib.hu>
Date2011-06-21 20:35 +0200
Message-ID<itqo9u$1ahc$1@news.ett.com.ua>
In reply to#7024
"Ttodir" <sanmrtn96@gmail.com>
>  no types larger than int can be used.

why?

[toc] | [prev] | [next] | [standalone]


#7030

FromAnonymous <anonymous@no.net>
Date2011-06-21 20:45 +0200
Message-ID<itqot3$mqc$1@speranza.aioe.org>
In reply to#7026
Balog Pal ha scritto:
> "Ttodir" <sanmrtn96@gmail.com>
>>  no types larger than int can be used.
> 
> why?
> 
because a possible extension in the future will be to declare the 
variables long long instead of int

[toc] | [prev] | [next] | [standalone]


#7031

FromPete Becker <pete@versatilecoding.com>
Date2011-06-21 14:46 -0400
Message-ID<2011062114464963905-pete@versatilecodingcom>
In reply to#7026
On 2011-06-21 14:35:43 -0400, Balog Pal said:

> "Ttodir" <sanmrtn96@gmail.com>
>> no types larger than int can be used.
> 
> why?

Because it's a homework problem, with artificial constraints to force 
students to think.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#7033

FromTtodir <sanmrtn96@gmail.com>
Date2011-06-21 12:03 -0700
Message-ID<26340e76-4821-4561-bcef-9606f06a2c06@d14g2000yqb.googlegroups.com>
In reply to#7031
On Jun 21, 8:46 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2011-06-21 14:35:43 -0400, Balog Pal said:
>
> > "Ttodir" <sanmrt...@gmail.com>
> >> no types larger than int can be used.
>
> > why?
>
> Because it's a homework problem, with artificial constraints to force
> students to think.
>
> --
>   Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)

No, it's a problem coming from a real necessity. I made it simple. For
now I have found an algorithm, but is slow, which consists in
splitting a and b in smaller componenents, then calculating the higher
bits of the multiplication with simple math and the lower bits with an
grade-school multiplication algorithm

[toc] | [prev] | [next] | [standalone]


#7035

FromGido <gido0001@gmail.com>
Date2011-06-21 14:30 -0700
Message-ID<cdc14d66-ef08-4387-a9f7-e87f4e9a996d@d1g2000yqm.googlegroups.com>
In reply to#7033
> No, it's a problem coming from a real necessity. I made it simple. For
> now I have found an algorithm, but is slow, which consists in
> splitting a and b in smaller componenents, then calculating the higher
> bits of the multiplication with simple math and the lower bits with an
> grade-school multiplication algorithm

Well, if it's not homework... Wouldn't the following do the trick?

int res = (a % B) * (b % B);
while (res >= B)
    res -= B;

[toc] | [prev] | [next] | [standalone]


#7037

FromAnonymous <anonymous@no.net>
Date2011-06-21 23:32 +0200
Message-ID<itr2ma$g53$2@speranza.aioe.org>
In reply to#7035
Gido ha scritto:
>> No, it's a problem coming from a real necessity. I made it simple. For
>> now I have found an algorithm, but is slow, which consists in
>> splitting a and b in smaller componenents, then calculating the higher
>> bits of the multiplication with simple math and the lower bits with an
>> grade-school multiplication algorithm
> 
> Well, if it's not homework... Wouldn't the following do the trick?
> 
> int res = (a % B) * (b % B);

i think res might overflow there

> while (res >= B)
>     res -= B;

[toc] | [prev] | [next] | [standalone]


#7072

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-06-23 06:33 +0000
Message-ID<4e02de3c$0$2809$7b1e8fa0@news.nbl.fi>
In reply to#7035
Gido <gido0001@gmail.com> wrote:
> while (res >= B)
>    res -= B;

  Isn't that the exact same thing as "res %= B;" (but less efficient)?
What's the point?

[toc] | [prev] | [next] | [standalone]


#7028

FromPete Becker <pete@versatilecoding.com>
Date2011-06-21 14:40 -0400
Message-ID<2011062114400582456-pete@versatilecodingcom>
In reply to#7024
On 2011-06-21 14:04:37 -0400, Ttodir said:

> Hello,
> 
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
> 
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
> 
> Any help will be much appreciated.

Read about greatest common divisors.

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#7032

FromChristopher <cpisz@austin.rr.com>
Date2011-06-21 11:39 -0700
Message-ID<9bb70a01-1b29-44bd-9e61-2607df37453f@34g2000pru.googlegroups.com>
In reply to#7024
On Jun 21, 1:04 pm, Ttodir <sanmrt...@gmail.com> wrote:
> Hello,
>
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
>
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
>
> Any help will be much appreciated.

Smells like homework to me.

[toc] | [prev] | [next] | [standalone]


#7036

FromAnonymous <anonymous@no.net>
Date2011-06-21 23:32 +0200
Message-ID<itr2ke$g53$1@speranza.aioe.org>
In reply to#7024
Ttodir ha scritto:
> Hello,
> 
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
> 
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
> 
> Any help will be much appreciated.

replace c with B in the below code, and long long with int, then test it

/* this function calculates (a*b)%c taking into account that a*b might 
overflow */
long long mulmod(long long a,long long b,long long c){
     long long x = 0,y=a%c;
     while(b > 0){
         if(b%2 == 1){
             x = (x+y)%c;
         }
         y = (y*2)%c;
         b /= 2;
     }
     return x%c;
}

[toc] | [prev] | [next] | [standalone]


#7038

FromKai-Uwe Bux <jkherciueh@gmx.net>
Date2011-06-21 23:41 +0200
Message-ID<itr36h$21c$1@hoshi.visyn.net>
In reply to#7036
Anonymous wrote:

> Ttodir ha scritto:
>> Hello,
>> 
>> I need a fast way to calculate (a * b) % B, with the following
>> constraints:
>> 
>> - a, b, B are int
>> - B = 10^N , N>0 arbitrary (B always fits in an int)
>> - the result must be valid even if the multiplication overflows
>> - portable code, no assumptions on the sizeof(int) and no types larger
>> than int can be used.
>> 
>> Any help will be much appreciated.
> 
> replace c with B in the below code, and long long with int, then test it
> 
> /* this function calculates (a*b)%c taking into account that a*b might
> overflow */
> long long mulmod(long long a,long long b,long long c){
>      long long x = 0,y=a%c;
>      while(b > 0){
>          if(b%2 == 1){
>              x = (x+y)%c;
>          }
>          y = (y*2)%c;
>          b /= 2;
>      }
>      return x%c;
> }

I think, the proposed code could still overflow when c is, say, one short of 
the maximum possible value for long long: the code assumes that neither x+y 
nor 2*y overflow.


Best,

Kai-Uwe Bux

[toc] | [prev] | [next] | [standalone]


#7047

FromRune Allnor <allnor@tele.ntnu.no>
Date2011-06-21 21:33 -0700
Message-ID<d95a9f61-83ce-4499-bdfa-9536f306d253@q15g2000yqk.googlegroups.com>
In reply to#7024
On Jun 21, 8:04 pm, Ttodir <sanmrt...@gmail.com> wrote:
> Hello,
>
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
>
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
>
> Any help will be much appreciated.

Fixed-point arithmetics with overflow issues is common
in embedded devices. You might want to ask parts this
question in comp.dsp.

Having said that - your other constraints seem contradictory:
Arbitrary N but no assumptions about the size of int?
Portable code but no types 'larger than int' to be used?

Either this is homework in disguise or you will have to
relax on some of these constraints. Or get a slow solution.

Rune

[toc] | [prev] | [next] | [standalone]


#7055

FromTtodir <sanmrtn96@gmail.com>
Date2011-06-22 04:59 -0700
Message-ID<e0544ab8-1303-40dd-b291-d23f10a5eb7e@s17g2000yqs.googlegroups.com>
In reply to#7047
On 22 Giu, 06:33, Rune Allnor <all...@tele.ntnu.no> wrote:
> On Jun 21, 8:04 pm, Ttodir <sanmrt...@gmail.com> wrote:
>
> > Hello,
>
> > I need a fast way to calculate (a * b) % B, with the following
> > constraints:
>
> > - a, b, B are int
> > - B = 10^N , N>0 arbitrary (B always fits in an int)
> > - the result must be valid even if the multiplication overflows
> > - portable code, no assumptions on the sizeof(int) and no types larger
> > than int can be used.
>
> > Any help will be much appreciated.
>
> Fixed-point arithmetics with overflow issues is common
> in embedded devices. You might want to ask parts this
> question in comp.dsp.
>
> Having said that - your other constraints seem contradictory:
> Arbitrary N but no assumptions about the size of int?
> Portable code but no types 'larger than int' to be used?

I did say B will always be contained in an int, so N is arbitray but
limited.
long long is not standard , and long might have the same size as int.

> Rune

[toc] | [prev] | [next] | [standalone]


#7057

Fromgwowen <gwowen@gmail.com>
Date2011-06-22 06:55 -0700
Message-ID<836756d3-a423-4353-ad39-0ef17280a461@x38g2000pri.googlegroups.com>
In reply to#7024
On Jun 21, 7:04 pm, Ttodir <sanmrt...@gmail.com> wrote:
> Hello,
>
> I need a fast way to calculate (a * b) % B, with the following
> constraints:
>
> - a, b, B are int
> - B = 10^N , N>0 arbitrary (B always fits in an int)
> - the result must be valid even if the multiplication overflows
> - portable code, no assumptions on the sizeof(int) and no types larger
> than int can be used.
>
> Any help will be much appreciated.


You know how to get the overflowing part of the sum.
You know how to get the non-overflowing part of the sum.
You know that modulo is distributive over multiplication ...

Just beware that calculations involving the overflow part may
themselves overflow ... but not forever.

[toc] | [prev] | [next] | [standalone]


#7058

FromPete Becker <pete@versatilecoding.com>
Date2011-06-22 10:10 -0400
Message-ID<2011062210102947914-pete@versatilecodingcom>
In reply to#7057
On 2011-06-22 09:55:49 -0400, gwowen said:

> You know that modulo is distributive over multiplication ...

But it's not. :-(

(5 * 2) % 10 != (5%10) * (2%10)

-- 
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The 
Standard C++ Library Extensions: a Tutorial and Reference 
(www.petebecker.com/tr1book)

[toc] | [prev] | [next] | [standalone]


#7059

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-06-22 15:19 +0100
Message-ID<r6Cdnc4udqZ5ZJzTnZ2dnUVZ8tKdnZ2d@giganews.com>
In reply to#7058
On 22/06/2011 15:10, Pete Becker wrote:
> On 2011-06-22 09:55:49 -0400, gwowen said:
>
>> You know that modulo is distributive over multiplication ...
>
> But it's not. :-(
>
> (5 * 2) % 10 != (5%10) * (2%10)

Yes it is:

(5 * 2) % 10 == ( (5 % 10) * (2 % 10) ) % 10

/Leigh

[toc] | [prev] | [next] | [standalone]


#7060

FromDilip <rdilipk@gmail.com>
Date2011-06-22 07:33 -0700
Message-ID<1eca825a-8d6f-4939-92e3-2f1fcdc553cb@fr19g2000vbb.googlegroups.com>
In reply to#7059
On Jun 22, 10:19 am, Leigh Johnston <le...@i42.co.uk> wrote:
> On 22/06/2011 15:10, Pete Becker wrote:
>
> > On 2011-06-22 09:55:49 -0400, gwowen said:
>
> >> You know that modulo is distributive over multiplication ...
>
> > But it's not. :-(
>
> > (5 * 2) % 10 != (5%10) * (2%10)
>
> Yes it is:
>
> (5 * 2) % 10 == ( (5 % 10) * (2 % 10) ) % 10
>
> /Leigh

Huh? For an operation to be called distributive it has to be both left
& right distributive. Obviously modulo over multiplication is not
right distributive as Pete Showed.

[toc] | [prev] | [next] | [standalone]


#7061

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-06-22 15:39 +0100
Message-ID<ir-dnTwS6c81Y5zTnZ2dnUVZ8vednZ2d@giganews.com>
In reply to#7060
On 22/06/2011 15:33, Dilip wrote:
> On Jun 22, 10:19 am, Leigh Johnston<le...@i42.co.uk>  wrote:
>> On 22/06/2011 15:10, Pete Becker wrote:
>>
>>> On 2011-06-22 09:55:49 -0400, gwowen said:
>>
>>>> You know that modulo is distributive over multiplication ...
>>
>>> But it's not. :-(
>>
>>> (5 * 2) % 10 != (5%10) * (2%10)
>>
>> Yes it is:
>>
>> (5 * 2) % 10 == ( (5 % 10) * (2 % 10) ) % 10
>>
>> /Leigh
>
> Huh? For an operation to be called distributive it has to be both left
> &  right distributive. Obviously modulo over multiplication is not
> right distributive as Pete Showed.

I guess, however what I showed is probably what gwowen actually meant.

/Leigh

[toc] | [prev] | [next] | [standalone]


Page 1 of 3  [1] 2 3  Next page →

Back to top | Article view | comp.lang.c++


csiph-web