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,769 of 10,432   
   James Kuyper to James Kuyper   
   Re: Test for overflow   
   30 Mar 15 19:43:38   
   
   XPost: comp.lang.c   
   From: jameskuyper@verizon.net   
      
   On 03/30/2015 01:20 PM, James Kuyper wrote:   
   ...   
   > Consider a plot of y=x*base as a C expression in unsigned long long,   
   > (which therefore obeys modular arithmetic) as a function of x. It   
   > consists of parallel lines, each with a slope equal to the base. Each of   
   > those lines starts near y==0, and ends near y==ULLONG_MAX, and can be   
   > labelled with the value of x/base, which ranged from 0 to base-1. The   
   > total number of such linesfor 0 <= x && x <= ULLONG_MAX equals 'base'.   
   >   
   > Compare that plot with a plot of y=x. The line for x%base==0 starts at   
   > x=0, y=0, but then stays entire above y=x. The line for x%base ==   
   > (base-1) stays entirely below y=x unless ULLONG_MAX%base == 0, in which   
   > case they meet at x=ULLONG_MAX, y=ULONG_MAX. So for base==2, your test   
   > works perfectly.   
   > However, for every line where 0 < x%base && x%base < base - 1 crosses   
   > y=x. Therefore, your test will misclassify some of the values of tvalue   
   > in that range. It will also mishandle ULLONG_MAX*base if ULLONG_MAX%base==0.   
      
   All occurrences of x%base above should have been   
   x*base/(ULLONG_MAX+1), where the left and right operands of '/' are   
   evaluated mathematically without overflow, but where the division   
   results in an integer value rounded towards zero, per C rules. My   
   description should make slightly more sense that way :-}. I can't quite   
   figure out the train of thought that led me to use the simpler   
   expression, but it had something to do with the fact that the range of   
   values is the same for both expressions.   
      
   The correct expression has precisely the behavior I intended - I just   
   gave the wrong expression.   
      
   --- 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