home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   sci.math.symbolic      Symbolic algebra discussion      10,432 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 8,766 of 10,432   
   Richard Heathfield to jacob navia   
   Re: Test for overflow   
   30 Mar 15 13:03:10   
   
   XPost: comp.lang.c   
   From: rjh@cpax.org.uk   
      
   On 30/03/15 12:17, jacob navia wrote:   
   > Hi the math experts!   
   >   
   > When parsing a digit sequence I test for overflow not by calculating the   
   > maximum number of digits allowed but just with the test   
   >   
   > tvalue = value * base;   
   >   
   > if (tvalue < value) {   
   >      // overflow detected   
   > }   
   >   
   > I am assuming then that   
   >   
   > if value * base > 2^64 then   
   >      value * base mod 2^64 < value   
   >   
   > for base >= 2 and base <= 36.   
   >   
   > Is this correct?   
      
   Consider simpler cases. For example, if we have a four-bit type, then   
   for overflow we are concerned by the following cases (we start off by   
   ignoring 0 * anything and 1 * anything and 2 * stuff that won't overflow):   
      
   2 * 8 = 16 = 0 mod 16 - fine   
   3 * 8 = 24 = 8 mod 16 - this would fail your test, but it's right on the   
   edge   
   4 * 8 = 32 = 0 mod 16 - fine   
   5 * 8 = 40 = 8 mod 16 - fail, and we have a pattern for 8s   
      
   2 * 9 = 18 = 2 mod 16 - fine   
   3 * 9 = 27 = 11 mod 16 - fail, and not really a borderline fail either   
      
   and the theory seems to be running into problems, even with four-bit   
   quantities.   
      
   Okay, I grant that 64 bits != 4 bits. But if it doesn't work for 4, I   
   see no particular reason to be optimistic that it'll work for 64.   
      
   --   
   Richard Heathfield   
   Email: rjh at cpax dot org dot uk   
   "Usenet is a strange place" - dmr 29 July 1999   
   Sig line 4 vacant - apply within   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca