numeric-limits

positive and negative infinity for integral types in c++

不打扰是莪最后的温柔 提交于 2019-12-24 00:09:25
问题 I am reading about positive and negative infinity in c++. I read that integral types dont have a infinite value ie. std::numeric_limits<int>::infinity(); wont work, but std::numeric_limits<int>::max(); will work and will represent the maximum possible value that can be represented by the integral type. so the std::numeric_limits<int>::max(); of the integral type could be taken as its positive infinite limit ? Or the integral type has only the max value and the infinity value is not true ? 回答1

Apple Clang and numeric_limits<unsigned __int128>::max() is 0?

£可爱£侵袭症+ 提交于 2019-12-23 16:23:00
问题 I'm trying to figure out the best way to work around an apparent bug where numeric_limits<T>::max() returns 0 rather than the maximum value. First, the test program: $ cat test.cxx #include <iostream> #include <limits> int main(int argc, char* argv[]) { #if (__SIZEOF_INT128__ >= 16) std::cout << "__int128 is available" << std::endl; #else std::cout << "__int128 is not available" << std::endl; #endif unsigned __int128 m = std::numeric_limits<unsigned __int128>::max(); if (m == 0) std::cout <<

Looping on a closed range

不想你离开。 提交于 2019-12-23 13:58:12
问题 How would you fix this code? template <typename T> void closed_range(T begin, T end) { for (T i = begin; i <= end; ++i) { // do something } } T is constrained to be an integer type, can be the wider of such types and can be signed or unsigned begin can be numeric_limits<T>::min() end can be numeric_limits<T>::max() (in which case ++i will overflow in the above code) I've several ways, but none I really like. 回答1: Maybe, template <typename T> void closed_range(T begin, const T end) if (begin <

std::numeric_limits::is_exact … what is a usable definition?

ぃ、小莉子 提交于 2019-12-22 03:46:14
问题 As I interpret it, MSDN's definition of numeric_limits::is_exact is almost always false: [all] calculations done on [this] type are free of rounding errors. And IBM's definition is almost always true: (Or a circular definition, depending on how you read it) a type that has exact representations for all its values What I'm certain of is that I could store a 2 in both a double and a long and they would both be represented exactly . I could then divide them both by 10 and neither would hold the

‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’

蹲街弑〆低调 提交于 2019-12-22 01:26:29
问题 I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors: :‘numeric_limits’ is not a member of std :expected primary-expression before ‘>’ token :no matching function for call to ‘max()’ #include <iostream> #include <cstdlib> using namespace std; int GetIntegerInput(int lower, int upper) { int integer = -1; do { cin >> integer; cin.clear(); cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); //errors here

C++: Why does numeric_limits work on types it does not know?

限于喜欢 提交于 2019-12-20 11:49:30
问题 I have created my own type, without any comparator, and without a specialization of std::numeric_limits . Despite that, for some reason, std::numeric_limits<MyType> compiles fine. Why did the c++ standards committee define the numeric_limits template such that it is valid for all types, including non-numeric types?? Example code below: #include <iostream> #include <limits> using namespace std; // This is an int wrapper that defaults to 666 instead of 0 class A { public: int x; public: A() : x

C++: Why does numeric_limits work on types it does not know?

删除回忆录丶 提交于 2019-12-20 11:49:30
问题 I have created my own type, without any comparator, and without a specialization of std::numeric_limits . Despite that, for some reason, std::numeric_limits<MyType> compiles fine. Why did the c++ standards committee define the numeric_limits template such that it is valid for all types, including non-numeric types?? Example code below: #include <iostream> #include <limits> using namespace std; // This is an int wrapper that defaults to 666 instead of 0 class A { public: int x; public: A() : x

Is it possible to read infinity or NaN values using input streams?

拜拜、爱过 提交于 2019-12-18 13:52:27
问题 I have some input to be read by a input filestream (for example): -365.269511 -0.356123 -Inf 0.000000 When I use std::ifstream mystream; to read from the file to some double d1 = -1, d2 = -1, d3 = -1, d4 = -1; (assume mystream has already been opened and the file is valid), mystream >> d1 >> d2 >> d3 >> d4; mystream is in the fail state. I would expect std::cout << d1 << " " << d2 << " " << d3 << " " << d4 << std::endl; to output -365.269511 -0.356123 -1 -1 . I would want it to output -365

Negative infinity

≡放荡痞女 提交于 2019-12-18 11:08:12
问题 I'm trying to figure out how to assign the value of negative infinity to a float or double variable. It seems that including the standard library limits, I can get the infinity representation, and I know (quite surely) that adding a minus in front of that (-infinity) might result in the value I'm looking for in the IEEE754 floating point standard (as 0x7FFFFFFF might result on 0xFFFFFFFF), but I'm not even sure about that, not to mention other standards that might be out there (If there are

What is the purpose of max_digits10 and how is it different from digits10?

落爺英雄遲暮 提交于 2019-12-17 12:17:16
问题 I am confused about what max_digits10 represents. According to its documentation, it is 0 for all integral types. The formula for floating-point types for max_digits10 looks similar to int 's digits10 's. 回答1: To put it simple, digits10 is the number of decimal digits guaranteed to survive text → float → text round-trip. max_digits10 is the number of decimal digits needed to guarantee correct float → text → float round-trip. There will be exceptions to both but these values give the minimum