124 questions
1
vote
1
answer
113
views
How to generalize concept with templated function?
Given something like the following:
struct numeric_limits_max_t {
template<class T>
constexpr operator T() const noexcept {
return std::numeric_limits<T>::max();
}
};
...
1
vote
1
answer
308
views
C++-23 numeric_limits<long double>::max() use case
Following the discussion in c++ long double (128-bit) precision, what's the use case of numeric_limits<long double>::max()? How can I trust that constant, for example in checking for range / ...
4
votes
2
answers
212
views
std::numeric_limits::epsilon() for double-double arithmetic
I am designing some double-double and quad-double arithmetic libraries for C++11. Part of the code is based off of the QD and DDFUN library from https://www.davidhbailey.com/dhbsoftware/.
struct ...
2
votes
0
answers
80
views
Better way of clearing an input buffer?
I am learning how to clear an input buffer. So far I have come across two statements that both work and seem to do the same thing:
while(getchar() != '\n');
cin.ignore(numberic_limits<streamsize&...
0
votes
0
answers
160
views
Why does initializing class variables using std::numeric_limits take so long?
I have a C++ class which gets instantiated a very large number of times. It has an unsigned short private member. I am getting very different runtime performance depending on whether I initialize it ...
-1
votes
2
answers
127
views
Problem finding limits for each numeric data type [closed]
I'm studying C++ using The C++ Programming Language from Bjarne Stroustrup, and while doing exercise 4.5 (What, on your system, are the largest and the smallest values of the following types: char, ...
7
votes
2
answers
373
views
What does std::numeric_limits do for types that don't specialize it?
This code compiles and runs
#include <limits>
#include <iostream>
struct Foo {
int x;
};
static_assert(!std::numeric_limits<Foo>::is_specialized);
int main() {
std::cout &...
-3
votes
2
answers
90
views
(C++) What exactly does this expression do? numeric_limits<double>::is_signed [closed]
I came accross the following in a C++ textbook:
bool result3 = numeric_limits<double>::is_signed; //result3 holds a value of true
bool result4 = numeric_limits<short>::is_integer; ...
0
votes
0
answers
113
views
Use QString::arg(double a) with std::numeric_limits<double>::infinity()
I am trying to convert floating point values to their string representation, using the QString class from the Qt framework. To do this, I am using the following function:
QString QString::arg(double a)...
0
votes
2
answers
164
views
Float to Double adding many 0s at the end of the new double number during conversion
I'm facing a little problem on a personal project:
When I'm converting a float number to a double to make operations (+-*/) easy, it adds a lot of 0s behind the default float number.
For example: ...
2
votes
1
answer
3k
views
How does std::numeric_limits<unsigned int>::max() work
I recently learnt that:
The correct way to get the largest possible value of a numeric type on your system is to use std::numeric_limits. In particular, we can use std::numeric_limits<unsigned int&...
3
votes
0
answers
31
views
Why is numeric_limits::infinity() misbehaving logically for integral types [duplicate]
std::numeric_limits<T>::infinity() function is logically misbehaving for integral types but for double its working fine. Is there some reason for defining such behavior in standard library, ...
-2
votes
2
answers
802
views
In C++, how to express the largest possible value of a type via its variable name?
Assuming there is a declaration in a header file you don't control that states something like:
static const uint16 MaxValue = 0xffff; // the type could be anything, static or not
Inside a file that ...
-2
votes
1
answer
1k
views
Why is std::integral defined with a type_trait and not with std::numeric_limits?
Why is the concept std::integral defined as
template < class T > concept integral = std::is_integral_v<T>;
and not as
template < class T > concept integral = std::numeric_limits<...
1
vote
1
answer
701
views
What is wrong with my numeric_limits function C++ [duplicate]
I have code like this:
// .h file
#include <iostream>
#include <limits>
class MyClass
{
public:
myInput();
int inputValue;
}
// .cpp file
#include "MyClass.h"
void ...
2
votes
2
answers
114
views
C++: Can a + or - tell the compiler a value should be an int?
I'm a beginner with C++ so this may seem like a silly question. Please humour me!
I was using static_cast<int>(numeric_limits<unsigned_char>::min()) and ::max() (according to this post), ...
1
vote
1
answer
45
views
Numeric limits in stanza
In stanza, I would like to get the numeric limits (min and max) of Int and Double types.
In C++, there are INT_MIN, INT_MAX, DBL_MIN and DBL_MAX.
1
vote
3
answers
283
views
std::uniform_real_distribution always returns infinity
When I run this code:
double getRandomDouble() {
static std::mt19937 entropy_ = std::mt19937();
std::uniform_real_distribution<double> distribution;
distribution.param(typename ...
0
votes
2
answers
343
views
std::cin failure leading to looped if statement in while loop
So I figure I'll put this here since I had to traverse a lot of docs and forums to find the definitive answer. I was trying to get input from the user and check if the input was an integer using ...
2
votes
1
answer
209
views
Largest floating point value that can be squared
I have a function that takes in an optional distance parameter dist, but my algorithm works with squared distances. If the user does not specify any value, I want dist to be as large a number as ...
0
votes
1
answer
472
views
c++ numeric_limits<double>::epsilon() for values other than 1
numeric_limits<double>::epsilon() is defined to be the difference between 1.0 and the next value representable by double, but I want to find/calculate the numeric_limits<double>::epsilon() ...
3
votes
3
answers
12k
views
C++ Why numeric limits doesn't work for uint8_t and int8_t? [duplicate]
I recently noticed numeric_limits::max() and numeric_limits::min() don't seem to work for uint8_t and int8_t. Is there a reason for this or could it be a bug? I tried on my own computer using gcc ...
10
votes
3
answers
957
views
How do I detect if anything but an integer is passed to my class constructor?
Fair and simple: How do I check if anything else but an integer is passed to my class in c++?
If I pass f.e. a char 'a' my class gets number 97 in ascii.
I tried std::numeric_limits but I don't get ...
1
vote
1
answer
121
views
What maximal integer, capable of adding 1 a float type can hold in c++?
Suppose I am using float to hold integer values and adding small shifts to it, approximately 1s or 2s. At which value float will stop to change? What is the name of this value?
0
votes
1
answer
199
views
How to find the next greater value generically in C++ for integers and floats?
I naively tried
template<typename T>
void foo(T a, T b){
if(min==max){
max += std::numeric_limits<T>::epsilon();
}
// Do some other stuff
}
However I found out that epsilon ...
4
votes
2
answers
195
views
Converting number to a padded string
I am trying to convert numeric types to fixed length string representations with leading '0's (used in a GUI that can only deal with strings - and lexicographic sorting of numbers is really awkward).
...
4
votes
1
answer
301
views
SFINAE with numeric_limits<T>::max() on MSVC2017
The following code:
template <typename T, typename U>
typename std::enable_if<
std::numeric_limits<T>::max() == std::numeric_limits<U>::max(),
bool>::type
same_max() {
...
1
vote
2
answers
2k
views
Wrong C++ long double min/max values using numeric_limits in Visual Studio 2019 [duplicate]
Using Visual Studio Community 2019 v16.4.2 with the latest stuff it comes with on 64bit Win10.
While testing various datatype limits ran into a weird bug, numeric_limits can't distinguish between ...
4
votes
3
answers
104
views
Smallest expressible value above given value
Given a variable defined as
T x;
where T is a generic arithmetic type (i.e. such that std::is_arithmetic<T>::value), is there a simple expression (e.g something from std::numeric_limits) that ...
2
votes
3
answers
286
views
numeric_limits is wrong on a BOOST_STRONG_TYPEDEF
I was supposing that numeric_limits::max() on a BOOST_STRONG_TYPEDEF defined type would give the same result than on the underlying type. But the following program shows that it is not the case (...
2
votes
1
answer
269
views
Check if numeric type is subset of another
I try to implement a meta function which checks if an integer type is a subset of another integer type. It should be platform independent and work at least with all numeric types defined by the C++ ...
2
votes
0
answers
189
views
Inserting and extracting numeric_limits<T>::infinity in a stream
The following program apparently shows an inconsistency when writing and reading on a std::stringstream a double set to "infinity", using std::numeric_limits<double>::infinity.
#include <...
12
votes
2
answers
1k
views
Why can integer type int64_t not hold this legal value? [duplicate]
I'm trying to write a test case for some corner case. For input of type int64_t, the following line won't compile:
int64_t a = -9223372036854775808LL;
The error/warning is:
error: integer constant ...
1
vote
1
answer
2k
views
__FLT_MAX__ and __DBL_MAX__ to 0?
With GCC 9.1, when calling std::numeric_limits's functions with floating-point types, they return 0 in most cases.
This happens in a project I'm working on, and there is no issue with MSVC, GCC 8.3 ...
3
votes
2
answers
227
views
What is the benefit of max_digits10 being 0 on references to floating point types?
In C++11, what is the advantage of std::numeric_limits<Type>::max_digits10 returning 0 for a Type that is a reference to floating point number?
For example:
constexpr int precisionPositive(...
1
vote
2
answers
5k
views
Value of numeric_limits<streamsize>::max() in c++
While using cin.ignore() in c++, it takes an argument of number of characters to consume until the delimiter occurs.
Most often I have observed the following to be used cin.ignore(numeric_limits<...
6
votes
1
answer
1k
views
Is there a reason why numeric_limits do not work on reference types?
If you mistakenly do something like:
#include<limits>
int arr[3];
auto x = std::numeric_limits<decltype(arr[0])>::max();
You will get unhelpful error message from the file in the STL ...
5
votes
2
answers
579
views
Is there a specification for a floating point’s exponent bias?
IEEE floating point exponents are stored as unsigned integers, using a pre-defined exponent bias to offset the exponent.
The exponent bias seems to be consistently equal to numeric_limits<T>::...
1
vote
1
answer
5k
views
definition of UINT_MAX macro
I would like to know if there is a particular reason to define the macro UINT_MAX as (2147483647 * 2U + 1U) and not directly its true value (4294967295U) in the climits header file.
Thank you all.
0
votes
1
answer
4k
views
How to cin only integers in C++ without disrupting remaining code?
I would like my code to only input integers. The code below does it's job correctly and asks the user for input if an integer was not used. However, after adding the code:
while ( ! ( cin >> x )...
-4
votes
1
answer
555
views
INT_MAX divided by twice of itself (INT_MAX * 2)
in C++, I wonder how to get the correct answer 0.5 for "INT_MAX / (INT_MAX + INT_MAX)"? I tried cast the divisor/both divisor and dividend to long, and also cast divisor to double, all return -...
-1
votes
2
answers
224
views
(~INT_MAX && INT_MAX) returns 1?
I am under the impression that INT_MAX will turn on all 32 bits of an int. If I negate that and 'and' it with itself, I should be comparing all 0s with all 1s and get back false. What am I missing?
...
6
votes
1
answer
243
views
Why does the standard provide both is_integer and is_exact?
std::numeric_limits provides 2 constants that are mutually exclusive:
is_integer : "true for all integer arithmetic types T"
is_exact: "true for all arithmetic types T that use exact representation"
...
10
votes
2
answers
21k
views
What can std::numeric_limits<double>::epsilon() be used for?
unsigned int updateStandardStopping(unsigned int numInliers, unsigned int totPoints, unsigned int sampleSize)
{
double max_hypotheses_=85000;
double n_inliers = 1.0;
double ...
20
votes
1
answer
4k
views
C++: Why does numeric_limits work on types it does not know?
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++ ...
0
votes
2
answers
320
views
numeric_limits of atomic types
Suppose someAtomic is an std::atomic with an integral underlying type, such as atomic_uint16_t. I don't want to assume WHICH integral type, however, in particular code, so I want something to ...
0
votes
0
answers
747
views
INT_MAX vs numeric_limits<int>::max() [duplicate]
Is numeric_limits::max() preferred over INT_MAX in C++ ? If so, what is the reason for this ?
What should be preferred in simple cases like finding minimum and maximum element in an array ?
5
votes
1
answer
5k
views
What is the difference between <climits> and <limits> header files in c++?
Limits is the header file in c++ which consists of numeric_limits class whereas climits is the header file consisting of the min and max values of various data types only.
Limits can be used ...
-1
votes
3
answers
582
views
How to make user input numbers only in C++?
So far, this is my code:
while(bet > remaining_money || bet < 100)
{
cout << "You may not bet lower than 100 or more than your current money. Characters are not accepted." <...
0
votes
1
answer
2k
views
Nice way to truncate an integer
I have a function which is given a buffer which accepts to be filled up to a size_t length; however, the actual call which fills it wants an int as max length.
So, in case the parameter cannot fit ...