0

I am learning C at the moment and we got a pretty simple matrix multiplication exercise that seems to work well for most test cases. However, one case doesn't work and I already narrowed it down to this piece of code that doesn't produce the expected result:

#include <stdio.h>

int main(){
    int a1 = 1261373;
    int b1 = 1261373;
    int a2 = 1669717;
    int b2 = 1293027;
    long mult1 = a1*b1;
    long mult2 = a2*b2;
    printf("mult1=%ld , mult2=%ld", mult1, mult2);
}

The output I get is:

mult1=1923945609 , mult2=-1379386529

Instead of the expected:

mult1=1591061845129, mult2=2158989163359

Obviously, especially with the second result I thought of an overflow first. But that really shouldn't happen with such small numbers, should it? The input is definetly an integer (we can't change that) and the output should be a long so I can't change the data types and I have no idea how it gets a negative number for the second multiplication or why the first result is wrong.

Any help would be greatly appreciated!

Juliette
  • 966
  • 16
  • 35
  • `a1 * b1` is an `int`, which you’re then converting to a `long`. If you want to use `long` multiplication, cast one of them to a `long` first, as in `(long)a1 * b1`. (Also, don’t rely on `long` representing any particular number of bits. If you want an unsigned 64-bit integer, use `uint64_t`.) – Ry- May 13 '17 at 17:50
  • Possible duplicate of [How to multiply 32-bit integers in c](http://stackoverflow.com/questions/27203425/how-to-multiply-32-bit-integers-in-c) – vgru May 13 '17 at 17:55
  • Thank you!!! That solved the problem, I had no idea you had to cast it to a long first but now all testcases work correctly :) – Juliette May 13 '17 at 17:55
  • @Ryan, you should post that as an answer... – Erich Kitzmueller May 13 '17 at 17:56
  • pay attention please that `long` isn't 64 bit on all platforms, even on x64 platforms , see this : http://stackoverflow.com/questions/6155784/range-of-values-in-c-int-and-long-32-64-bits – niceman May 13 '17 at 17:57
  • "Such small numbers". Times change. The product you're trying to compute is 2158989163359, which is a 41-bit number. So it'll work on a 64-bit machine, if you cast to `long` first, as other comments/answers have advised. But it still won't work on a machine where longs are "only" 32 bits. – Steve Summit May 13 '17 at 17:57
  • 1
    If you think about the rule, "multiplying two `int`s creates an `int` in C", that's rather easy to remember actually. On the other hand, [integer promotion](http://www.idryman.org/blog/2012/11/21/integer-promotion/) is what most C programmers sadly don't know (in my opinion, out of 10 randomly chosen C programmers, 2 will know promotion rules), and *that* will produce strange results if you are not aware of it. – vgru May 13 '17 at 17:59
  • 1
    @Groo I'm aware of integer promotion, and I still think it produces strange results at times. ;-) – Andrew Henle May 13 '17 at 19:36

1 Answers1

1

Try this one

#include <stdio.h>

int main(){
    int a1 = 1261373;
    int b1 = 1261373;
    int a2 = 1669717;
    int b2 = 1293027;
    long mult1 = (long long)a1*b1; //Type cast one operand to long long
    long mult2 = (long long)a2*b2;
    printf("mult1=%ld , mult2=%ld", mult1, mult2);
}
Ajay
  • 2,483
  • 2
  • 16
  • 27