diff options
author | Rasmus Villemoes <linux@rasmusvillemoes.dk> | 2014-12-10 18:51:27 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-10 20:41:11 -0500 |
commit | 74a5fef7cb0839c6b595f90c7914a62ac9d0bcf9 (patch) | |
tree | 82e34b9120005a8a764d25f9c91b17ada4445b05 /lib | |
parent | 7b212edf86e28f4d0198f2152b2f0553f51a59a5 (diff) |
lib/lcm.c: ensure correct result whenever it fits
Ensure that lcm(a,b) returns the mathematically correct result, provided
it fits in an unsigned long. The current version returns garbage if a*b
overflows, even if the final result would fit.
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lcm.c | 2 |
1 files changed, 1 insertions, 1 deletions
@@ -7,7 +7,7 @@ | |||
7 | unsigned long lcm(unsigned long a, unsigned long b) | 7 | unsigned long lcm(unsigned long a, unsigned long b) |
8 | { | 8 | { |
9 | if (a && b) | 9 | if (a && b) |
10 | return (a * b) / gcd(a, b); | 10 | return (a / gcd(a, b)) * b; |
11 | else if (b) | 11 | else if (b) |
12 | return b; | 12 | return b; |
13 | 13 | ||