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
No related branches found
No related tags found
1 merge request!1Draft: Fix mul() to work with negative arguments
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
#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 result = 0;
for (int n = 0; n<b; n++)
for (int n = 0; n<abs(b); n++)
{
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