octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #32924] lcm does not warn when losing precisio


From: Arun Giridhar
Subject: [Octave-bug-tracker] [bug #32924] lcm does not warn when losing precision
Date: Thu, 15 Sep 2022 16:12:43 -0400 (EDT)

Update of bug #32924 (project octave):

                  Status:             In Progress => Patch Submitted        

    _______________________________________________________

Follow-up Comment #25:

Going through old bug reports and this looks like a simple change analogous to
what we already do in nchoosek.m, so I copied and adapted these warnings from
nchoosek:


diff -r 0dec459a4064 scripts/specfun/lcm.m
--- a/scripts/specfun/lcm.m     Wed Sep 14 09:59:31 2022 -0400
+++ b/scripts/specfun/lcm.m     Thu Sep 15 16:00:16 2022 -0400
@@ -53,6 +53,13 @@ function l = lcm (varargin)
     l(msk) = 0;
   endfor
 
+  if (isfloat (l) && l > flintmax (l))
+    warning ("Octave:lcm:large-output-float", ...
+             "lcm: possible loss of precision");
+  elseif (isinteger (l) && l == intmax (l))
+    warning ("Octave:lcm:large-output-integer", ...
+             "lcm: result may have saturated at intmax");
+  end
 endfunction


For floating point inputs:

octave:15> lcm (num2cell(1:40){:})
ans = 5342931457063200

octave:16> lcm (num2cell(1:41){:})
warning: lcm: possible loss of precision
warning: called from
    lcm at line 57 column 5

ans = 2.190601897395912e+17


For integer inputs:

octave:27> lcm (num2cell(uint64(1:46)){:})
ans = 9419588158802421600

octave:28> lcm (num2cell(uint64(1:47)){:})
warning: lcm: result may have saturated at intmax
warning: called from
    lcm at line 60 column 5

ans = 18446744073709551615


If there are no objection to this change in the next few days, I'll push it.





@nrjank: If you're trying to recreate in Matlab, this sequence should work:

l = 26; for i = 27:52, l = lcm(l, i), end             % forward, double

l = 26; for i = 52:-1:27, l = lcm(l, i), end          % reverse, double

l = uint64(26); for i = 27:52, l = lcm(l, i), end     % forward, integer

l = uint64(26); for i = 52:-1:27, l = lcm(l, i), end  % reverse, integer




    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?32924>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]