3

I had a problem in a program where the result of product is not what it's supposed to be. For example: 775*775 gives 10801 instead of 6006025.

To try to isolate and resolve the problem, I made a simple sketch to test the result of different products. What I found is that if I do the same product in this way: 775/100.00*775/100.00 then multiply the result by 10000 I get the right value: 600625.00

The attached pictures illustrate what I said.

While I can use this solution in my initial program by I still want to do the multiplication directly as more operations means more time.

I would appreciate any help. Thank you.enter image description here

enter image description here

6

Integer overflow. You're multiplying an int by an int then assigning it to a long. Instead if you multiply a long by a long, or an int by a long, or a long by an int, it should work.

unsigned long res = 775L * 775;

The L suffix tells the compiler to treat it as a long, not an int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.