So in c++11 the Chrono Library provides, duration_cast:
Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished
And c++17's floor:
Returns the greatest duration t representable in ToDuration that is less or equal to d
So for all x will the result of these 2 calls be equal:
chrono::duration_cast<chrono::seconds>(x)
chrono::floor<chrono::seconds>(x)
As far as I can tell, same as the difference between static_cast and std::floor: Negatives are rounded down instead of truncated toward zero.
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
int main() {
std::cout << "duration_cast:" << std::endl;
std::cout << "1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::duration_cast<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::duration_cast<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::duration_cast<std::chrono::seconds>(-1600ms).count() << std::endl;
std::cout << "floor:" << std::endl;
std::cout << "1.4s: " << std::chrono::floor<std::chrono::seconds>(1400ms).count() << std::endl;
std::cout << "1.5s: " << std::chrono::floor<std::chrono::seconds>(1500ms).count() << std::endl;
std::cout << "1.6s: " << std::chrono::floor<std::chrono::seconds>(1600ms).count() << std::endl;
std::cout << "-1.4s: " << std::chrono::floor<std::chrono::seconds>(-1400ms).count() << std::endl;
std::cout << "-1.5s: " << std::chrono::floor<std::chrono::seconds>(-1500ms).count() << std::endl;
std::cout << "-1.6s: " << std::chrono::floor<std::chrono::seconds>(-1600ms).count() << std::endl;
return 0;
}
.
duration_cast:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -1
-1.5s: -1
-1.6s: -1
floor:
1.4s: 1
1.5s: 1
1.6s: 1
-1.4s: -2
-1.5s: -2
-1.6s: -2
https://wandbox.org/permlink/SsmpRz6RkvbL6Sru
Related
If I run this snippet of code
int main()
{
int i = 8;
std::cout << std::oct << i;
}
The console shows 10.
Shouldn't give 010, in C++ Octal Numbers start with 0.
You can use std::showbase for this.
#include <iostream>
int main()
{
int i = 100;
std::cout << "100 without showbase:\n";
std::cout << "decimal: " << std::dec << i << "\n";
std::cout << " hex: " << std::hex << i << "\n";
std::cout << " octal: " << std::oct << i << "\n";
std::cout << "100 with showbase:\n";
std::cout << "decimal: " << std::showbase << std::dec << i << "\n";
std::cout << " hex: " << std::showbase << std::hex << i << "\n";
std::cout << " octal: " << std::showbase << std::oct << i << "\n";
return 0;
}
Output:
100 without showbase:
decimal: 100
hex: 64
octal: 144
100 with showbase:
decimal: 100
hex: 0x64
octal: 0144
Demo
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result of the code above is:
init 0x7ffed27c6450 0x56480bc1eeb0 5
value 0x7ffed27c6470 0x56480bc1eeb0 5
done 0x7ffed27c6450 0 0
Looks great:
Now, move to:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
void foo2 (std::vector<int>&& rvalure_ref) {
std::cout << "rvalue_ref "
<< &rvalure_ref
<< " "
<< rvalure_ref.data()
<< " "
<< rvalure_ref.size()
<< std::endl;
}
int main() {
std::vector<int> value(5, 0);
std::cout << "init "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
foo2(std::move(value));
std::cout << "done "
<< &value
<< " "
<< value.data()
<< " "
<< value.size()
<< std::endl;
}
The result is:
init 0x7ffccc93a5c0 0x56124b3a8eb0 5
rvalue_ref 0x7ffccc93a5c0 0x56124b3a8eb0 5
done 0x7ffccc93a5c0 0x56124b3a8eb0 5
My problem is:
For the 1st case, it is perfectly called by "move semantics", and as you see, the ownership of the vector has been transfered to the function parameter. Finally, at "done", the data is null to verify the the vector at main() no longer owns the vector.
Now to explicitly claim the parameter is "rvalue reference", as case 2. As you see, actually it is like "call by (l)reference".
How can I figure out it?
This question already has answers here:
cout not printing unsigned char
(5 answers)
Closed 2 years ago.
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 compiler:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
std::cout << "numeric_limits<uint8_t>::max() = " << numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << numeric_limits<int8_t>::min() << std::endl;
std::cout << "numeric_limits<uint16_t>::max() = " << numeric_limits<uint16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::max() = " << numeric_limits<int16_t>::max() << std::endl;
std::cout << "numeric_limits<int16_t>::min() = " << numeric_limits<int16_t>::min() << std::endl;
std::cout << "numeric_limits<uint32_t>::max() = " << numeric_limits<uint32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::max() = " << numeric_limits<int32_t>::max() << std::endl;
std::cout << "numeric_limits<int32_t>::min() = " << numeric_limits<int32_t>::min() << std::endl;
std::cout << "numeric_limits<uint64_t>::max() = " << numeric_limits<uint64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::max() = " << numeric_limits<int64_t>::max() << std::endl;
std::cout << "numeric_limits<int64_t>::min() = " << numeric_limits<int64_t>::min() << std::endl;
return 0;
}
gives output:
numeric_limits<uint8_t>::max() = �
numeric_limits<int8_t>::max() =
numeric_limits<int8_t>::min() = �
numeric_limits<uint16_t>::max() = 65535
numeric_limits<int16_t>::max() = 32767
numeric_limits<int16_t>::min() = -32768
numeric_limits<uint32_t>::max() = 4294967295
numeric_limits<int32_t>::max() = 2147483647
numeric_limits<int32_t>::min() = -2147483648
numeric_limits<uint64_t>::max() = 18446744073709551615
numeric_limits<int64_t>::max() = 9223372036854775807
numeric_limits<int64_t>::min() = -9223372036854775808
It does work. The output is interpreted as ASCII characters though. If you cast to int before you print, you will see the correct values:
std::cout << "numeric_limits<uint8_t>::max() = " << static_cast<int>(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << static_cast<int>(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << static_cast<int>(numeric_limits<int8_t>::min()) << std::endl;
std::cout << "numeric_limits<uint8_t>::max() = " << std::to_string(numeric_limits<uint8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << std::to_string(numeric_limits<int8_t>::max()) << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << std::to_string(numeric_limits<int8_t>::min()) << std::endl;
try to convert them to string, before inserting them into cout.
int8 types are probably defined as chars, so don't print the values as charbut as ints:
int main() {
std::cout << "numeric_limits<uint8_t>::max() = " << (int)numeric_limits<uint8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::max() = " << (int)numeric_limits<int8_t>::max() << std::endl;
std::cout << "numeric_limits<int8_t>::min() = " << (int)numeric_limits<int8_t>::min() << std::endl;
}
I want to display my result aligned on the column by the decimal place.
I have tried to just put setw(7) and setw(6) in parts of the display but it seems to not change the output at all.
int main()
{
double x;
char more = 'y';
while(more=='y' || more=='Y')
{
cout << "\n\t\t\tInput x:";
cin >> x;
cout << "\n\n\t\t\t LibraryResult\tMyResult" << endl;
cout << setprecision(2) << fixed << "\n\t\tsin(" << x << ")\t"
<< setprecision(6) << sin(x) << "\t" << mySin(x) << endl;
cout << setprecision(2) << fixed << "\n\t\tcos(" << x << ")\t"
<< setprecision(6) << cos(x) << "\t" << myCos(x) << endl;
cout << setprecision(2) << fixed << "\n\t\texp(" << x << ")\t"
<< setprecision(6) << exp(x) << "\t" << myExp(x) << endl;
}
}
I want the resultants of the program to be aligned by decimal, so when you put in a number like 2 the decimals are all in the same column.
If you set a precision of 6, a width of 6 or 7 is simply not enough to contain the number. You have to either reduce the precision or increase the width.
Try playing around with the stream manipulators in the following snippet
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
int main()
{
const double period = 2 * 3.141592653589793;
const int steps = 20;
std::cout << std::string(44, '=') << '\n';
std::cout << std::setw(3) << "x" << std::setw(12) << "sin(x)"
<< std::setw(12) << "cos(x)" << std::setw(14) << "tan(x)" << '\n';
std::cout << std::string(44, '-') << '\n';
for(int i = 0; i <= steps; ++i)
{
double x = i * period / steps;
std::cout << std::setprecision(2) << std::fixed << x
<< std::setprecision(6) << std::setw(12) << std::sin(x)
<< std::setprecision(6) << std::setw(12) << std::cos(x)
<< std::setprecision(6)
<< std::setw(16) // <- Try to decrease it
<< std::scientific // <- Try to keep it as std::fixed
<< std::tan(x) << '\n';
}
std::cout << std::string(44, '-') << '\n';
}
You must use several manipulators (one is not enough)
You might want to try with the following combination, that right-aligns your numbers, with a fixed number of decimals.
std::cout.width(15);
std::cout << std::fixed << std::setprecision(6) << std::right << exp(x) << std::endl;
You can find information about manipulators here
I am working on the latest revision of the C++ programming language (think it's 5) and run into a problem with g++ version 5.2.
My code is a variation of Small_size template from chap 24.
#include <iostream>
template<int N>
bool is_small ()
{
std::cerr << sizeof(N) << std::endl;
std::cerr << N << std::endl;
return N <= 255;
}
bool ism (int i_n)
{
return i_n <= 255;
}
int main ()
{
std::cout << "hallo welt" << std::endl;
std::cout << 0 << " " << is_small<0> << std::endl;
std::cout << 255 << " " <<is_small<255> << std::endl;
std::cout << -4100000000 << " " << is_small<-4100000000> << std::endl;
std::cout << 256 << " " << is_small<256> << std::endl;
std::cout << 256 << " " << ism(256) << std::endl;
std::cout << 256 << " " << (256 <= 255) << std::endl;
}
When I compile it, it's ok. But when I run the thing, it simply seems to be broken.
[cpp11#hydra src]$ cat ~/bin/g14
#!/bin/bash
g++-52 -std=c++14 "${1}.C" -L$LIBPATH -o "$1"
[cpp11#hydra src]$ g14 konzept_small
[cpp11#hydra src]$ ./konzept_small
hallo welt
0 1
255 1
-4100000000 1
256 1 //1
256 0
256 0
[cpp11#hydra src]$
My problem is that:
the result for 256 and higher is wrong. See comment //1
there is no output of the template code on cerr
I started with a version without the cerr, but got only the wrong template result.
I removed a constexpr from the template, but no change.
So I added as last step the cerr to see whats wrong.
Any ideas?
You are not calling is_small<N>, but just printing out its address. You need to change your code to
std::cout << 0 << " " << is_small<0>() << std::endl;
std::cout << 255 << " " <<is_small<255>() << std::endl;
std::cout << -4100000000 << " " << is_small<-4100000000>() << std::endl;
std::cout << 256 << " " << is_small<256>() << std::endl;
Note the added (). Not sure why you are getting the output you are though, are you sure you are running the same code you posted?
is_small is a function you should add the parenthesis :
change
std::cout << 0 << " " << is_small<0> << std::endl;
to this
std::cout << 0 << " " << is_small<0>() << std::endl;
It worked fine for me with this change