numeric-limits

In c++, is it safe to use std::numeric_limits<double>::max() as a special “flag”?

风流意气都作罢 提交于 2019-12-12 15:36:55
问题 Given std::vector<double> a; std::vector<int> ind; where ind is 1 sorted in ascending order. I want to do the equivalent of the following: for (auto it=ind.rbegin();it!=ind.rend();it++) a.erase(a.begin() + *it); I came up with this: for (auto it=ind.begin();it!=ind.end();it++) a[*it] = std::numeric_limits<double>::max(); std::erase(std::remove(a.begin(),a.end(),std::numeric_limits<double>::max()),a.end()); This is very fast, but it doesn't feel right to use the std::numeric_limits::max() as a

std::numeric_limits as a Condition

此生再无相见时 提交于 2019-12-11 13:23:53
问题 Is there a way that I can use std::numeric_limits<T>::is_integer and std::numeric_limits<T>::is_specialized to change template behavior? For example can I do this: template < typename T > void foo( const T& bar ) { if( std::numeric_limits< T >::is_integer ) { isInt( bar ); } else if( std::numeric_limits< T >::is_specialized ) { isFloat( bar ); } else { isString( bar ); } } 回答1: The "obvious" answer is that you could use something like std::enable_if . For example: template<typename T>

boost::multiprecision::cpp_dec_float_50 overflow checking

℡╲_俬逩灬. 提交于 2019-12-11 04:36:32
问题 I am trying to use the boost::multiprecision library for floating (or in that case, fixed) point arithmetic. However, I am having trouble detecting potential overflow in the following way: typedef boost::multiprecision::number< boost::multiprecision::cpp_dec_float<50> > flp_type; typedef boost::multiprecision::number< boost::multiprecision::cpp_dec_float<100> > safe_flp_type; flp_type _1 = std::numeric_limits<flp_type>::max(); flp_type _2("1"); flp_type _3 = std::numeric_limits<flp_type>::max

why does std::numeric_limits<float>::min() differ in behavior when streamed to output with different functions?

时光怂恿深爱的人放手 提交于 2019-12-11 02:32:15
问题 I got a weird behaviour with std::numeric_limits<float>::min() when I call std::cout I get an output value of 1.17549e-38 in contrast when I use printf("%f", std::numeric_limits<float>::min()); I get a value of 0.000000. note that when I evaluate ( std::numeric_limits<float>::min() == std::numeric_limits<float>::min() ) I get true (which is intuitive and logical) so, can any one explain to me this difference in output? 回答1: cppreference for numeric_limits: std::numeric_limits<float>::min()

Is there a reason why numeric_limits do not work on reference types?

青春壹個敷衍的年華 提交于 2019-12-10 17:18:07
问题 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 implementation. Problem is that template argument is a reference, so the fix is to remove it: auto x = std::numeric_limits<std::remove_reference_t<decltype(arr[0])>>::max(); Now my question is why numeric_limits do not know to do this by themselves? I would understand that you do not want to remove pointerness

Templates and std::numeric_limits

淺唱寂寞╮ 提交于 2019-12-10 09:51:50
问题 I have a class called Atomic which is basically an _Atomic_word plus methods that call the gcc atomic builtins. class Atomic{ mutable volatile _Atomic_word value_; public: Atomic(int value = 0): value_(value) {} **** blah blah **** }; I would like std::numeric_limits<Atomic> to instantiate to std::numeric_limits<underlying integer type> (e.g. on my system _Atomic_word is just a typedef for int). Is there a way to do this? 回答1: std::numeric_limits<Atomic> will instantiate with Atomic as the

Difference in digits10 between GCC and MSVC

。_饼干妹妹 提交于 2019-12-06 03:47:11
问题 I have the following code: #include <iostream> #include <limits> int main() { std::cout << std::numeric_limits<unsigned long long>::digits10 << std::endl; return 0; } GCC 4.4 returns 19 MS VS 9.0 returns 18 Can someone please explain Why is there a difference between the two? I would have expected such a constant would be the same regardless of the compiler. 回答1: If Visual C++ 2008 returns 18 for std::numeric_limits<unsigned long long>::digits10 , it is a bug (I don't have Visual C++ 2008

Templates and std::numeric_limits

心已入冬 提交于 2019-12-05 21:31:42
I have a class called Atomic which is basically an _Atomic_word plus methods that call the gcc atomic builtins. class Atomic{ mutable volatile _Atomic_word value_; public: Atomic(int value = 0): value_(value) {} **** blah blah **** }; I would like std::numeric_limits<Atomic> to instantiate to std::numeric_limits<underlying integer type> (e.g. on my system _Atomic_word is just a typedef for int). Is there a way to do this? std::numeric_limits<Atomic> will instantiate with Atomic as the type, you can't subvert that. However you could specialise std::numeric_limits for Atomic like this template<>

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

怎甘沉沦 提交于 2019-12-05 02:41:33
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 mathematical result exactly . Given any numeric data type T , what is the correct way to define std:

Difference in digits10 between GCC and MSVC

谁说胖子不能爱 提交于 2019-12-04 07:45:42
I have the following code: #include <iostream> #include <limits> int main() { std::cout << std::numeric_limits<unsigned long long>::digits10 << std::endl; return 0; } GCC 4.4 returns 19 MS VS 9.0 returns 18 Can someone please explain Why is there a difference between the two? I would have expected such a constant would be the same regardless of the compiler. If Visual C++ 2008 returns 18 for std::numeric_limits<unsigned long long>::digits10 , it is a bug (I don't have Visual C++ 2008 installed to verify the described behavior). In Visual C++ (at least for 32-bit and 64-bit Windows), unsigned