I need some help getting the right output stream format in decimal in C++17 - c++17

I made this little currency converter program that convert dollar to franc, but when I put an amount of, for example $2000 and more, I do not have the correct format, I have this: 1.15165e+006.
I want the entire decimal amount.
Thanks
Convert USD to Franc CFA
#include <iostream>
using namespace std;
int main()
{
const double cfa_per_usd {575.825};
cout <<"**********Welcome to the USD to Franc CFA Converter************" << endl;
double cfa {0.0};
cout <<"\nEnter value in USD: ";
double dollar {0};
cin >> dollar;
cfa = dollar * cfa_per_usd;
cout << dollar <<" Dollar(s) is equivalent to " << cfa << " Francs CFA" <<endl;
return 0;
}
I found a partial answer to my problem, I have added the library , then added "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(2) << cfa << " Francs CFA" <
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?

I found a partial answer to my problem, I have added the library, then add "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(3) << cfa << " Francs CFA" <<endl;
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?

Related

getline, stringstream, delimiter stuff

how do I use getline to separate a string ex. "you8only8live8once"? This solution only sets my first word to "you" and then stops at the first "8".
Also if I want to the "8" to be an arbitrary delimiter do I just put in cin >> delimiter; then change "getline(cin, phrase, '8');" to "getline(cin, phrase, delimiter);"?
istringstream inSS;
string phrase;
string firstWord;
string secondWord;
string thirdWord;
string fourthWord;
cout << "Please enter a digit infused string to explode:" << endl;
cout << "Please enter the digit delimiter:" << endl;
getline(cin, phrase, '8');
inSS.str(phrase);
inSS >> firstWord >> secondWord >> thirdWord >> fourthWord;
cout << "The 1st word is: " << firstWord << endl;
cout << "The 2nd word is: " << secondWord << endl;
cout << "The 3rd word is: " << thirdWord << endl;
cout << "The 4th word is: " << fourthWord << endl;
return 0;

implications of using _mm_shuffle_ps on integer vector

SSE intrinsics includes _mm_shuffle_ps xmm1 xmm2 immx which allows one to pick 2 elements from xmm1 concatenated with 2 elements from xmm2. However this is for floats, (implied by the _ps , packed single). However if you cast your packed integers __m128i, then you can use _mm_shuffle_ps as well:
#include <iostream>
#include <immintrin.h>
#include <sstream>
using namespace std;
template <typename T>
std::string __m128i_toString(const __m128i var) {
std::stringstream sstr;
const T* values = (const T*) &var;
if (sizeof(T) == 1) {
for (unsigned int i = 0; i < sizeof(__m128i); i++) {
sstr << (int) values[i] << " ";
}
} else {
for (unsigned int i = 0; i < sizeof(__m128i) / sizeof(T); i++) {
sstr << values[i] << " ";
}
}
return sstr.str();
}
int main(){
cout << "Starting SSE test" << endl;
cout << "integer shuffle" << endl;
int A[] = {1, -2147483648, 3, 5};
int B[] = {4, 6, 7, 8};
__m128i pC;
__m128i* pA = (__m128i*) A;
__m128i* pB = (__m128i*) B;
*pA = (__m128i)_mm_shuffle_ps((__m128)*pA, (__m128)*pB, _MM_SHUFFLE(3, 2, 1 ,0));
pC = _mm_add_epi32(*pA,*pB);
cout << "A[0] = " << A[0] << endl;
cout << "A[1] = " << A[1] << endl;
cout << "A[2] = " << A[2] << endl;
cout << "A[3] = " << A[3] << endl;
cout << "B[0] = " << B[0] << endl;
cout << "B[1] = " << B[1] << endl;
cout << "B[2] = " << B[2] << endl;
cout << "B[3] = " << B[3] << endl;
cout << "pA = " << __m128i_toString<int>(*pA) << endl;
cout << "pC = " << __m128i_toString<int>(pC) << endl;
}
Snippet of relevant corresponding assembly (mac osx, macports gcc 4.8, -march=native on an ivybridge CPU):
vshufps $228, 16(%rsp), %xmm1, %xmm0
vpaddd 16(%rsp), %xmm0, %xmm2
vmovdqa %xmm0, 32(%rsp)
vmovaps %xmm0, (%rsp)
vmovdqa %xmm2, 16(%rsp)
call __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
....
Thus it seemingly works fine on integers, which I expected as the registers are agnostic to types, however there must be a reason why the docs say that this instruction is only for floats. Does someone know any downsides, or implications I have missed?
There is no equivalent to _mm_shuffle_ps for integers. To achieve the same effect in this case you can do
SSE2
*pA = _mm_shuffle_epi32(_mm_unpacklo_epi32(*pA, _mm_shuffle_epi32(*pB, 0xe)),0xd8);
SSE4.1
*pA = _mm_blend_epi16(*pA, *pB, 0xf0);
or change to the floating point domain like this
*pA = _mm_castps_si128(
_mm_shuffle_ps(_mm_castsi128_ps(*pA),
_mm_castsi128_ps(*pB), _MM_SHUFFLE(3, 2, 1 ,0)));
But changing domains may incur bypass latency delays on some CPUs. Keep in mind that according to Agner
The bypass delay is important in long dependency chains where latency is a bottleneck, but
not where it is throughput rather than latency that matters.
You have to test your code and see which method above is more efficient.
Fortunately, on most Intel/AMD CPUs, there is usually no penalty for using shufps between most integer-vector instructions. Agner says:
For example, I found no delay when mixing PADDD and SHUFPS [on Sandybridge].
Nehalem does have 2 bypass-delay latency to/from SHUFPS, but even then a single SHUFPS is often still faster than multiple other instructions. Extra instructions have latency, too, as well as costing throughput.
The reverse (integer shuffles between FP math instructions) is not as safe:
In Agner Fog's microarchitecture on page 112 in Example 8.3a, he shows that using PSHUFD (_mm_shuffle_epi32) instead of SHUFPS (_mm_shuffle_ps) when in the floating point domain causes a bypass delay of four clock cycles. In Example 8.3b he uses SHUFPS to remove the delay (which works in his example).
On Nehalem there are actually five domains. Nahalem seems to be the most effected (the bypass delays did not exist before Nahalem). On Sandy Bridge the delays are less significant. This is even more true on Haswell. In fact on Haswell Agner said he found no delays between SHUFPS or PSHUFD (see page 140).

OpenCV line detection and extraction using ios

Hi i am using opencv for the line detection in ios. This is for my final year project. I want to upload the image of ecg and then extract the line from this for that i have used the opencv and sucessfuly i have extracted the line but now i want to save the line and facing much problem in that can anyone help me or any tutorial ?
I wrote something quickly, I think it can help you:
void write_lines_to_file ( string file_name, vector<vector<Point> > lines )
{
FileStorage fs ( file_name, FileStorage::WRITE );
fs << "lines" << "[";
for ( unsigned int i = 0; i < lines.size(); i++ )
{
// I've assumed each line is a vector< Point >, in index [0] the head and index [1] the tail.
fs << "{:" << "x1" << lines[i].at ( 0 ).x << "y1" << lines[i].at ( 0 ).y << "x2" << lines[i].at ( 1 ).x << "y2" << lines[i].at ( 1 ).y << "}";
}
fs.release();
}
I didn't test it and I made my own assumptions, so please be cautious when you want to use it.

Issue with cv::Mat::zeros initialization

my problem is just astonishing. This is the code
#define NCHANNEL 3
#define NFRAME 100
Mat RR = Mat::zeros(NCHANNEL, NFRAME-1, CV_64FC1);
double *p_0 = RR.ptr<double>(0);
double *p_1 = RR.ptr<double>(1);
double *p_2 = RR.ptr<double>(2);
cout<< p_0[NFRAME-1] << endl << p_1[NFRAME-1] << endl << p_2[NFRAME-1] << endl;
And the output is: 0 0 -6.27744e+066 .
Where is that awful number come from? it seems I'm printing a pointer or something rough in memory. (uh, 0 is the value of all other elements, of course).
You are accessing after the last element of Mat. If you use NFRAME-1 for initialization then the last element has NFRAME-2 index.

setw() - adjustfield (left, right or internal)

I am going though the string functions doing tests to learn them (I am a newbie programmer)
Anyway, I am currently looking at setw() but I seam to not understand it... I think I understand the basic use and the use of setfil
here is my test code
http://ideone.com/czAXH
Anyway the cplusplus website says.. "format flag adjustfield (left, right or internal)" but doesn't say how to use this?
I assume this means I can do the above code but place the "spacing" after the word instead of before it..
How do I do that?
std::cout << std::left
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::internal
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::right
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
Outputs:
[1 ,-100 ]
[ 1,- 100]
[ 1, -100]

Resources