Question

With the following 2 functions focus on the rows with the * comments. When the cout<< statement executes, there is no decimal place showing. 3021680380 /10000000 turns into 302. instead of 302.1680.

void convert(){
    setprecision(4);   //************************
    newFileTime = new double[numRec];   //***********
    newOffset = new int[numRec];
    newSize = new int[numRec];
    newNum = new int[numRec];
    newType = new int[numRec];
    newTime = new int[numRec];

    for(int i = 0; i<numRec; i++){
        newFileTime[i] = fileTime[i] / 10000000; //**********
        newOffset[i] = offset[i] / 512;
        newSize[i] = fileSize[i] / 512;
        newNum[i] = 0;
        if(type[i] == "Write"){
            newType[i] = 0;
        }else{
            newType[i] = 1;
        }
        newTime[i] = responseTime[i] / 10000000;
    }
}

void exports(){
    setprecision(4);  //**************
    ofstream fout;
    fout.open("prxy_0.ascii");
    {
    if(!fout){
            cout <<"File opening failed. \n";
        }
    }
    fout<< numRec <<endl;

    for(int i = 0; i < numRec; i++){
        fout<< newFileTime[i] << " " << newNum[i] << " " << newOffset[i] << " " << newSize[i] << " " << newType[i] << " " << newTime[i];
        cout<< fileTime[i] << " " << newFileTime[i] <<endl; //**********
        if(i != numRec - 1){
            fout<<endl;
        }
    }
    fout.close();
}

Any ideas?

Was it helpful?

Solution

Whenever you divide two integers, the result of this expression also is an integer. Integer division is always rounded down.

    newFileTime[i] = fileTime[i] / 10000000;
    //               ^^^^^^^^^^^   ^^^^^^^^
    //                   int          int

To fix your expression, cast one of the operands to a double to make the division a floating point division (use one of the following possibilities):

    newFileTime[i] = static_cast<double>(fileTime[i]) / 10000000;
    // or
    newFileTime[i] = fileTime[i] / 10000000.0;

Please note that setting the precission requires you to put the call of std::setprecision(...) into the stream for which you want to set the precision. Also, this only sets the output precision (when writing to the stream), not how the calculations are performed:

    std::cout << std::setprecision(4) << [some double values]

Also note that std::setprecision sets the number of magnificant digets rather than the decimals after the decimal point. So your 302.1680 would be printed as 302.2 (four maginificant digits). To set a fixed number of digits after the decimal point, also write std::fixed to the stream, either before or after std::setprecision:

    std::cout << std::fixed << std::setprecision(4) << [some double values]

Note that such configurations will be kept during the runtime of your program until you change them again. To keep them local in a function, make sure that you restore the configuration after you're done.

Of course, std::cout was only an exemplary stream. The same applies for writing to files (any std::ostream object).

OTHER TIPS

Not quite what you asked for (already been answered), but this:

newFileTime = new double[numRec];   //***********
newOffset = new int[numRec];
newSize = new int[numRec];
newNum = new int[numRec];
newType = new int[numRec];
newTime = new int[numRec];

begs to be:

struct FileData
{
    double fileTime;
    int offset;
    int size;
    int num;
    int type;
    int time;
};

FileData *newData = new FileData[numRec];

newData[i].fileTime = ... 

Makes the whole thing much more manageable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top