home bbs files messages ]

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

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

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

   Message 32,303 of 33,346   
   Ulrich Eckhardt to Jeremy Todd   
   Re: taking abs() of min signed int value   
   16 May 12 17:28:22   
   
   c21e7739   
   From: doomster@knuut.de   
      
   Jeremy Todd wrote:   
   > I need to compute the absolute value of the minimum signed int value   
   > for each signed integral type. This value isn't representable as a   
   > signed int, but it is representable as an unsigned int.   
   >   
   > This is easy when a larger signed type is available:   
   >   
   > int32_t min_int32 = std::numeric_limits::min();   
   > int64_t abs_min_int32= -int64_t(min_int32);   
   > uint32_t answer = uint32_t(abs_min_int32);   
   >   
   > I couldn't come up with an elegant solution when attempting this for   
   > the 64-bit type.   
      
      
   You could use the fact that "min == min/2 + (min - min/2)" and that the   
   absolute of the two summands can be represented as the according *int_t.   
      
   Uli   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- 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