This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.
Related
For example 14.2 is not an integer but 14.0 is an integer in a mathematical perspective. What I've tried to do is the following, let n be a long double, so to check if it's an integer I compared it to its integer form:
if (n == (int) n)
{
// n is an integer
}
It all looks perfect but when I applied it, it didn't work, and when i debugged the program I discovered that the long double number is never a whole number; it's not 14.0 instead it's 14.0000000000000000000002, the last 2 is added by the compiler.
Does someone know how to fix it?
The cleanest approach is to use floor() and not concern yourself with casting to integer types which makes the false assumption that there will be no overflow in converting a floating-point value to integer.
For large floats that's obviously not true.
#include <iostream>
#include <cmath> // This is where the overloaded versions of floor() are.
bool is_whole(double d){
return d==floor(d);
}
int main() {
double x{14.0};
double y{14.2};
double z{-173.5};
std::cout << is_whole(14.0) << '\n';
std::cout << is_whole(14.2) << '\n';
std::cout << is_whole(-123.4) << '\n';
std::cout << is_whole(-120394794.0) << '\n';
std::cout << is_whole(-120394794.44) << '\n';
std::cout << is_whole(3681726.0) << '\n';
return 0;
}
Expected Output:
1
0
0
1
0
1
This question already has an answer here:
`std::bitset` with and without boundary checks
(1 answer)
Closed 1 year ago.
Some data are stored in a 64 bit integer. As you can see, inside the getDrvAns function I check if the bit in the position drvIdx-1 is 0 or 1. One cannot be sure if the drvIdx will have a value in the right range (1-64). However, I noticed that if we put a value higher that 64, we have a wrap-around effect as demonstrated in the code below.
My question is, is this a standard behavior? Is it safe to leave it as it is without boundary checks?
Note: If the bit is set to 0 the drive has answered.
uint64_t mDrvAns = 48822;
bool getDrvAns(int drvIdx) {
std::bitset<64> bitRepOfmDrvAns{mDrvAns};
return (bitRepOfmDrvAns[drvIdx - 1] != 0ull);
};
std::string yesOrNo(bool val) {
return ((val==false) ? "Yes" : "No");
}
int main()
{
int drvIdx = 1;
std::bitset<64> bitsOfDrvAns{mDrvAns};
std::cout << bitsOfDrvAns << std::endl;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
drvIdx = 65;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
return 0;
}
According to the documentation, out of bounds access using operator[] is Undefined Behaviour. Don't do it.
If you don't want to check the bounds yourself, call test() instead, and be prepared to handle the exception if necessary.
This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.
This question already has answers here:
maximum value of int
(7 answers)
Closed 9 years ago.
I thought that unsigned int max possible value is 65535 ( in C++ ) but I created a programm which can use an int which is equal with 100000 for example. Is it safe to use int with values up to 10000000 or the program may crash? In that case is long the only solution?
Thank you for your time!
Use std::numeric_limits<unsigned int>::max() to know for certain what this value is.
Please check the code below for limits:
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int main () {
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
return 0;
}
INT_MAX is implementation defined. That means it's up to your compiler vendor to decide, as long as it's no less than 32767, and greater or equal to a short. You can use the climits definitions to discover your implementation's limits:
#include <iostream>
#include <climits>
int main () {
std::cout << INT_MAX << std::endl;
return 0;
}
On my installation of gcc/g++ v4.8.1 targeting x86_64-linux-gnu, this snippet produces:
2147483647
And as has been mentioned in the followup replies to this answer, you may (and probably should) use the more semantically proper (for C++) method:
#include <iostream>
#include <limits>
int main () {
std::cout << std::numeric_limits<int>::max() << std::endl;
return 0;
}
...which ought to produce the same output.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?
My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I am expecting to get this output: 00A7
You also need setfill('0').