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 31,475 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: sqrt(a) with 'a' being an int   
   20 Sep 11 15:10:38   
   
   53f27c45   
   From: daniel.kruegler@googlemail.com   
      
   Am 20.09.2011 15:42, schrieb ptyxs:   
   > Hi,   
   > in C++ sqrt is supposed to be an overloaded function taking either a   
   > double,a  float or a long double argument.   
   > So I would guess that an int argument should give an error message at   
   > compile time.   
   > However, I observed that my gcc 4.6.1 (Mandriva Linux 2011) compiles   
   > and runs perfectly a program such as the following :   
   >   
   >   
   > #include   
   > #include   
   >   
   > int main()   
   > {   
   >      int a = 25;   
   >      std::cout<<  sqrt(a)<<  std::endl;   
   > }   
      
   It is unspecified, whether this program produces a diagnostic, but for   
   different reasons as you expect. The reason is the lack of the std:: in   
   front of sqrt after making it available via the cmath header (see   
   [using.linkage] p2). Once fixed this way, this program should be   
   well-formed in C++11 based on [c.math] p11:   
      
   "Moreover, there shall be additional overloads sufficient to ensure:   
   1. If any argument corresponding to a double parameter has type long   
   double, then all arguments corresponding to double parameters are   
   effectively cast to long double.   
   2. Otherwise, if any argument corresponding to a double parameter has   
   type double or an integer type, then all arguments corresponding to   
   double parameters are effectively cast to double.   
   3. Otherwise, all arguments corresponding to double parameters are   
   effectively cast to float."   
      
   Thus, the int argument has the same effect as calling std::sqrt(double).   
      
   C99 has added similar rules in regard to the type-generic math header   
    (see 7.25 p3).   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ 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