All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] lib/lcm.c: Ensure correct result whenever it fits
@ 2013-12-17 14:46 Rasmus Villemoes
  2013-12-17 14:46 ` [PATCH 2/2] lib/lcm.c: lcm(n,0)=lcm(0,n) is 0, not n Rasmus Villemoes
  2014-01-14 11:43 ` [PATCH 1/2] lib/lcm.c: Ensure correct result whenever it fits Rasmus Villemoes
  0 siblings, 2 replies; 7+ messages in thread
From: Rasmus Villemoes @ 2013-12-17 14:46 UTC (permalink / raw)
  To: linux-kernel; +Cc: Rasmus Villemoes

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>
---
There are of course still plenty of cases where the return value is
wrong.

 lib/lcm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/lcm.c b/lib/lcm.c
index b9c8de4..01b3aa9 100644
--- a/lib/lcm.c
+++ b/lib/lcm.c
@@ -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;
 
-- 
1.8.4.rc3.2.g61bff3f


^ permalink raw reply related	[flat|nested] 7+ messages in thread
* [PATCH 1/2] lib/lcm.c: Ensure correct result whenever it fits
@ 2014-12-09 21:03 Rasmus Villemoes
  2014-12-09 21:03 ` [PATCH 2/2] lib/lcm.c: lcm(n,0)=lcm(0,n) is 0, not n Rasmus Villemoes
  0 siblings, 1 reply; 7+ messages in thread
From: Rasmus Villemoes @ 2014-12-09 21:03 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Rasmus Villemoes, linux-kernel

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>
---
 lib/lcm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/lcm.c b/lib/lcm.c
index b9c8de461e9e..01b3aa922dda 100644
--- a/lib/lcm.c
+++ b/lib/lcm.c
@@ -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;
 
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-03-30 19:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-17 14:46 [PATCH 1/2] lib/lcm.c: Ensure correct result whenever it fits Rasmus Villemoes
2013-12-17 14:46 ` [PATCH 2/2] lib/lcm.c: lcm(n,0)=lcm(0,n) is 0, not n Rasmus Villemoes
2014-01-14 11:43 ` [PATCH 1/2] lib/lcm.c: Ensure correct result whenever it fits Rasmus Villemoes
2014-12-09 21:03 Rasmus Villemoes
2014-12-09 21:03 ` [PATCH 2/2] lib/lcm.c: lcm(n,0)=lcm(0,n) is 0, not n Rasmus Villemoes
2015-03-29  2:44   ` Mike Snitzer
2015-03-30 19:38     ` Rasmus Villemoes
2015-03-30 19:38       ` Rasmus Villemoes

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.