Questions tagged [multiplication]

Multiplication is the mathematical operation of scaling one number by another. It is an elementary operation of most programming languages and is typically denoted by the symbol *.

Multiplication is the mathematical operation of scaling one number by another.

It is an elementary arithmetic operation in most programming languages along with addition, subtraction, division and sometimes modulo.

It is typically denoted by * (asterisk symbol) in programming, and × (cross symbol) in math.

Example (math): multiplication of numbers 3 and 4

3 × 4 = 12

See also: Multiplication on Wikipedia

2530 questions
25
votes
5 answers

Curious arithmetic error- 255x256x256x256=18446744073692774400

I encountered a strange thing when I was programming under c++. It's about a simple multiplication. Code: unsigned __int64 a1 = 255*256*256*256; unsigned __int64 a2= 255 << 24; // same as the above cerr()<<"a1 is:"<
Hamid Bazargani
  • 847
  • 1
  • 15
  • 30
24
votes
2 answers

Why is imul used for multiplying unsigned numbers?

I compiled the following program: #include uint64_t usquare(uint32_t x) { return (uint64_t)x * (uint64_t)x; } This disassembles to: 0: 89 f8 mov eax,edi 2: 48 0f af c0 imul rax,rax 6: c3 …
marmistrz
  • 5,974
  • 10
  • 42
  • 94
23
votes
2 answers

Moving out before brackets with XOR

If I had the sum of products like z*a + z*b + z*c + ... + z*y, it would be possible to move the z factor, which is the same, out before brackets: z(a + b + c + ... y). I'd like to know how it is possible (if it is) to do the same trick if bitwise…
23
votes
2 answers

Fastest way to multiply an array of int64_t?

I want to vectorize the multiplication of two memory aligned arrays. I didn't find any way to multiply 64*64 bit in AVX/AVX2, so I just did loop-unroll and AVX2 loads/stores. Is there a faster way to do this? Note: I don't want to save the…
23
votes
1 answer

Perform integer division using multiplication

Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) /…
22
votes
4 answers

Division result is always zero

I got this C code. #include int main(void) { int n, d, i; double t=0, k; scanf("%d %d", &n, &d); t = (1/100) * d; k = n / 3; printf("%.2lf\t%.2lf\n", t, k); return 0; } I want to…
VaioIsBorn
  • 7,683
  • 9
  • 31
  • 28
22
votes
5 answers

Is floating-point addition and multiplication associative?

I had a problem when I was adding three floating point values and comparing them to 1. cout << ((0.7 + 0.2 + 0.1)==1)<
Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30
21
votes
6 answers

JavaScript multiply not precise

I came accross a weird problem, I want to do some basic math checks. I have read to avoid floating numbers so I decided to multiply my math values with 10000, because my value can be between 0.9 and 0.0025. Everything works correct except for two…
adis
  • 5,901
  • 7
  • 51
  • 71
21
votes
3 answers

shift bits vs multiply in PHP

I have the following code:
aki
  • 1,241
  • 2
  • 13
  • 43
21
votes
4 answers

Optimizing x64 assembler MUL loop

I'm writing math code which needs to multiply large numbers fast. It breaks down to multiplications of an array of integers with a single integer. In C++ this looks like this (on unsigned's): void muladd(unsigned* r, const unsigned* a, unsigned len,…
cxxl
  • 4,939
  • 3
  • 31
  • 52
21
votes
2 answers

how to do two complement multiplication and division of integers?

I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg:…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
21
votes
5 answers

Measuring time in C

I'm trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this: clock_t start = clock(); sleep(3); clock_t end = clock(); double elapsed_time = (end - start)/(double)CLOCKS_PER_SEC; printf("Elapsed…
21
votes
2 answers

Timedelta multiply with float in python

I have two dates and can calculate timedelta as usual. But I want to calculate some percent with resulting timedelta: full_time = (100/percentage) * timdelta But it seems that it can only multiplying with interegs. How can I use float instead of…
Крайст
  • 776
  • 1
  • 9
  • 22
21
votes
10 answers

Multiplying a string by an int in C++

What do I have to do so that when I string s = "."; If I do cout << s * 2; Will it be the same as cout << ".."; ?
skittles sour
  • 479
  • 1
  • 4
  • 7
20
votes
4 answers

Why is Strassen matrix multiplication so much slower than standard matrix multiplication?

I have written programs in C++, Python and Java for matrix multiplication and tested their speed for multiplying two 2000 x 2000 matrices (see post). The standard ikj-implentation - which is in - took: C++: 15 seconds (Source) Python: 6 minutes 13…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958