numeric-limits

Where are the limits for Qt types?

喜欢而已 提交于 2019-12-01 15:07:08
Regularly, I could reference limits.h to see what the max is for a certain type, like an int or long. In Qt, there are types like qlonglong. Is there a header file and/or documentation that can be used in a similar way to manually or programmatically reference the limits of these types? There's a high likelihood the Qt types distill down to one of the basic types for which numeric_limits are defined. Have you tried calling e.g., std::numeric_limits<qlonglong>::max() ? As MSalters points out, too, if the types are not builtin numeric_limits can still be specialized for them. If that were the

Why is C++ numeric_limits<enum_type>::max() == 0?

浪尽此生 提交于 2019-11-30 17:47:16
Here's a bit of code that might seem like it would work: #include <cassert> #include <limits> enum test { A = 1 }; int main() { int max = std::numeric_limits<test>::max(); assert(max > 0); } But it fails under both GCC (4.6.2) and clang (2.9) on Linux: max() for enum types is in fact zero! And this remains true even if you use the C++11 enum type specifier to explcitly say what type you want your enum to have. Why is this? And as for the C++11 behavior, is it something explcitly called for? I could find no mention of it in N2347, the paper on Strongly Typed Enums. std::numeric_limits is

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

☆樱花仙子☆ 提交于 2019-11-30 10:42:47
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.269511 -0.356123 -Inf 0 instead. This set of data was output using C++ streams. Why can't I do the

Why does numeric_limits::min return a negative value for int but positive values for float/double?

筅森魡賤 提交于 2019-11-30 04:19:11
Why does numeric_limits::min return a negative value for int, but positive values for e.g. float and double? #include<iostream> #include<limits> using namespace std; int main() { cout << "int: " << numeric_limits<int>::min() << " " << "float: " << numeric_limits<float>::min() << " " << "double: " << numeric_limits<double>::min() << "\n"; return 0; } Output: int: -2147483648 float: 1.17549e-38 double: 2.22507e-308 From cppreference: Returns the minimum finite value representable by the numeric type T. For floating-point types with denormalization, min returns the minimum positive normalized

Why is C++ numeric_limits<enum_type>::max() == 0?

折月煮酒 提交于 2019-11-30 01:49:59
问题 Here's a bit of code that might seem like it would work: #include <cassert> #include <limits> enum test { A = 1 }; int main() { int max = std::numeric_limits<test>::max(); assert(max > 0); } But it fails under both GCC (4.6.2) and clang (2.9) on Linux: max() for enum types is in fact zero! And this remains true even if you use the C++11 enum type specifier to explcitly say what type you want your enum to have. Why is this? And as for the C++11 behavior, is it something explcitly called for? I

Why does numeric_limits::min return a negative value for int but positive values for float/double?

混江龙づ霸主 提交于 2019-11-29 01:38:47
问题 Why does numeric_limits::min return a negative value for int, but positive values for e.g. float and double? #include<iostream> #include<limits> using namespace std; int main() { cout << "int: " << numeric_limits<int>::min() << " " << "float: " << numeric_limits<float>::min() << " " << "double: " << numeric_limits<double>::min() << "\n"; return 0; } Output: int: -2147483648 float: 1.17549e-38 double: 2.22507e-308 From cppreference: Returns the minimum finite value representable by the numeric

Syntax error with std::numeric_limits::max

若如初见. 提交于 2019-11-28 17:48:44
I have class struct definition as follows: #include <limits> struct heapStatsFilters { heapStatsFilters(size_t minValue_ = 0, size_t maxValue_ = std::numeric_limits<size_t>::max()) { minMax[0] = minValue_; minMax[1] = maxValue_; } size_t minMax[2]; }; The problem is that I cannot use 'std::numeric_limits::max()' and the compiler says: Error 8 error C2059: syntax error : '::' Error 7 error C2589: '(' : illegal token on right side of '::' The compiler which I am using is Visual C++ 11 (2012) Your problem is caused by the <Windows.h> header file that includes macro definitions named max and min :

Why is std::numeric_limits<T>::max() a function?

这一生的挚爱 提交于 2019-11-28 10:42:31
In the C++ Standard Library the value std::numeric_limits<T>::max() is specified as a function. Further properties of a specific type are given as constants (like std::numeric_limits<T>::is_signed ). All constants that are of type T are given as functions, whereas all other constants are given as, well, constant values. What's the rationale behind that? Matthieu M. To expand on Neil's remark, std::numeric_limit<T> is available for any number type including floating point numbers, and if you dig through the comp.lang.c++ thread, you'll see the mention that it might not be possible to define the

Using numeric_limits::max() in constant expressions

a 夏天 提交于 2019-11-28 10:42:04
I would like to define inside a class a constant which value is the maximum possible int. Something like this: class A { ... static const int ERROR_VALUE = std::numeric_limits<int>::max(); ... } This declaration fails to compile with the following message: numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression numeric.cpp:8: error: a function call cannot appear in a constant-expression I understand why this doesn't work, but two things look weird to me: It seems to me a natural decision to use the value in constant expressions. Why did the language designers

warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();

我与影子孤独终老i 提交于 2019-11-27 18:40:48
This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints: #include <limits> x = std::numeric_limits<int>::max(); c:\...\x.cpp(192) : warning C4003: not enough actual parameters for macro 'max' c:\...\x.cpp(192) : error C2589: '(' : illegal token on right side of '::' c:\...\x.cpp(192) : error C2059: syntax error : '::' I get the same results with: #include <limits> using namespace std; x = numeric_limits<int>::max(); Why is it seeing max as the macro max(a,b); ? This commonly occurs when including a Windows header that