Skip to content

Commit

Permalink
lib/lcm.c: ensure correct result whenever it fits
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Rasmus Villemoes authored and Linus Torvalds committed Dec 11, 2014
1 parent 7b212ed commit 74a5fef
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/lcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
unsigned long lcm(unsigned long a, unsigned long b)
{
if (a && b)
return (a * b) / gcd(a, b);
return (a / gcd(a, b)) * b;
else if (b)
return b;

Expand Down

0 comments on commit 74a5fef

Please sign in to comment.