Skip to content
Snippets Groups Projects
Commit f8c22c3e authored by vonopr's avatar vonopr
Browse files

Fix mul() to work with negative arguments

parent c345859b
Branches
Tags
1 merge request!1Draft: Fix mul() to work with negative arguments
#include "exple-mul.h" #include "exple-mul.h"
int sgn(int val) {
return (0 < val) - (val < 0);
}
int abs(int a) {
return (a >= 0)? a : -a;
}
int exple::mul(int a, int b) int exple::mul(int a, int b)
{ {
int result = 0; int result = 0;
for (int n = 0; n<b; n++) for (int n = 0; n<abs(b); n++)
{ {
result += a; result += a;
} }
return result; return result * sgn(b);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment