Hi All,

I'm having a weird problem. I'm trying to do division like this:

vector = timeStamp / 12800.0;

where vector is a double and timeStamp is an int. The answer that I get is a double, however it's only to 2 places of precision. For instance 28521602 / 12800 = 2228.25015625 but it's chopping it off to 2228.25. I'm using GCC to run and compile my code, any help would be greatly appreciated. Thanks,

Bleh

Recommended Answers

All 3 Replies

Hi All,

I'm having a weird problem. I'm trying to do division like this:

vector = timeStamp / 12800.0;

where vector is a double and timeStamp is an int. The answer that I get is a double, however it's only to 2 places of precision. For instance 28521602 / 12800 = 2228.25015625 but it's chopping it off to 2228.25. I'm using GCC to run and compile my code, any help would be greatly appreciated. Thanks,

Bleh

vector is not getting chopped off in memory, just in the display. It is stored in memory correctly. What IS being chopped off is the DISPLAY to the screen of vector. cout has a default of 6 significant digits with doubles, which can be changed with setprecision.

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
    int timestamp = 28521602;
    double vector = timestamp / 12800.0;
    cout << setprecision(20) << vector << endl;
 
    return 0;
}

http://bytes.com/forum/thread531727.html

Thanks! Also was wondering if there is an easy to convert a string to a double...

-BLEH

Thanks! Also was wondering if there is an easy to convert a string to a double...

-BLEH

Use strtod from cstdlib. You'll need to convert the string to a c-string before applying strtod.

string stringNum = "67.34512";
double number = strtod(stringNum.c_str(), NULL);

http://www.cplusplus.com/reference/clibrary/cstdlib/strtod.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.