C++/STL Algorithms Helper/numeric limits

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Use numeric_limits to get max value for float, double and long double

<source lang="cpp">


/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <limits>
  3. include <string>

using namespace std; int main() {

  // use textual representation for bool
  cout << boolalpha;
  // print maximum of floating-point types
  cout << "max(float):       "
       << numeric_limits<float>::max() << endl;
  cout << "max(double):      "
       << numeric_limits<double>::max() << endl;
  cout << "max(long double): "
       << numeric_limits<long double>::max() << endl;
  cout << endl;

}

/* 

max(float): 3.40282e+038 max(double): 1.79769e+308 max(long double): 1.#INF

*/       
   
 </source>


Use numeric_limits to get max value for short, int and long

<source lang="cpp">


/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <limits>
  3. include <string>

using namespace std; int main() {

  // use textual representation for bool
  cout << boolalpha;
  // print maximum of integral types
  cout << "max(short): " << numeric_limits<short>::max() << endl;
  cout << "max(int):   " << numeric_limits<int>::max() << endl;
  cout << "max(long):  " << numeric_limits<long>::max() << endl;
  cout << endl;

}

/* 

max(short): 32767 max(int): 2147483647 max(long): 2147483647

*/       
   
 </source>


Using numeric_limits Template Class to Calculate Type Ranges

<source lang="cpp">

  1. include <iostream>
  2. include <limits>

using namespace std; int main(){

    numeric_limits<int> _i;
    cout<<"Integer Range:  "<<_i.min()<<"  "<<_i.max()<<endl;
    return 0;

}


 </source>


Whether char is signed

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <limits>
  3. include <string>

using namespace std; int main() {

  // use textual representation for bool
  cout << boolalpha;
  // print whether char is signed
  cout << "is_signed(char): "
       << numeric_limits<char>::is_signed << endl;
  cout << endl;

}

/* 

is_signed(char): true

*/       
   
 </source>


Whether numeric limits for type string exist

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <limits>
  3. include <string>

using namespace std; int main() {

  // use textual representation for bool
  cout << boolalpha;
  // print whether numeric limits for type string exist
  cout << "is_specialized(string): "
       << numeric_limits<string>::is_specialized << endl;

}

/* 

is_specialized(string): false

*/       
   
 </source>