Fix overflow using lcm with big integers. 96/10396/2
Bruno JOFRET [Tue, 5 Feb 2013 08:30:43 +0000 (09:30 +0100)]
Change-Id: Ie9d6febea53e5b061ff8ca649dc404bcfb29060b

scilab/modules/overloading/macros/%i_lcm.sci
scilab/modules/polynomials/tests/unit_tests/lcm.dia.ref [new file with mode: 0644]
scilab/modules/polynomials/tests/unit_tests/lcm.tst [new file with mode: 0644]

index 7022190..064a08c 100644 (file)
@@ -18,7 +18,7 @@ function [q,fact]=%i_lcm(p)
 
   q=p(1);
   for k=2:size(p,'*')
-    q=q*p(k)/%i_gcd([q,p(k)]);
+    q=q/%i_gcd([q,p(k)])*p(k);
   end
   fact=q./p
 endfunction
diff --git a/scilab/modules/polynomials/tests/unit_tests/lcm.dia.ref b/scilab/modules/polynomials/tests/unit_tests/lcm.dia.ref
new file mode 100644 (file)
index 0000000..6a7b4bc
--- /dev/null
@@ -0,0 +1,19 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Bruno JOFRET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+// <-- CLI SHELL MODE -->
+s=poly(0,'s');
+p=[s,s*(s+1)^2,s^2*(s+2)];
+[pp,fact]=lcm(p);
+assert_checkequal(pp, s^5+4*s^4+5*s^3+2*s^2);
+assert_checkequal(fact, [2*s+5*s^2+4*s^3+s^4, 2*s+s^2, 1+2*s+s^2]);
+V=int32([2^2*3^5, 2^3*3^2,2^2*3^4*5]);
+assert_checkequal(lcm(V), int32(9720));
+// Interger Overflow
+V = int32([59356 44517]);
+assert_checkequal(lcm(V), int32(178068));
+V = int32([50000 50000]);
+assert_checkequal(lcm(V), int32(50000));
diff --git a/scilab/modules/polynomials/tests/unit_tests/lcm.tst b/scilab/modules/polynomials/tests/unit_tests/lcm.tst
new file mode 100644 (file)
index 0000000..9c696ae
--- /dev/null
@@ -0,0 +1,26 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2013 - Scilab Enterprises - Bruno JOFRET
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- CLI SHELL MODE -->
+
+
+s=poly(0,'s');
+p=[s,s*(s+1)^2,s^2*(s+2)];
+[pp,fact]=lcm(p);
+
+assert_checkequal(pp, s^5+4*s^4+5*s^3+2*s^2);
+assert_checkequal(fact, [2*s+5*s^2+4*s^3+s^4, 2*s+s^2, 1+2*s+s^2]);
+
+V=int32([2^2*3^5, 2^3*3^2,2^2*3^4*5]);
+assert_checkequal(lcm(V), int32(9720));
+
+// Interger Overflow
+V = int32([59356 44517]);
+assert_checkequal(lcm(V), int32(178068));
+
+V = int32([50000 50000]);
+assert_checkequal(lcm(V), int32(50000));
\ No newline at end of file