User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 429,781 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,982 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 236 | Replies: 3
Reply
Join Date: Jun 2008
Posts: 7
Reputation: bleh1318 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bleh1318 bleh1318 is offline Offline
Newbie Poster

losing decimal precision in division

  #1  
Jul 24th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Posts: 1,789
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 221
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: losing decimal precision in division

  #2  
Jul 24th, 2008
Originally Posted by bleh1318 View Post
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
Last edited by VernonDozier : Jul 24th, 2008 at 12:02 pm.
Reply With Quote  
Join Date: Jun 2008
Posts: 7
Reputation: bleh1318 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bleh1318 bleh1318 is offline Offline
Newbie Poster

Re: losing decimal precision in division

  #3  
Jul 24th, 2008
Thanks! Also was wondering if there is an easy to convert a string to a double...

-BLEH
Reply With Quote  
Join Date: Jan 2008
Posts: 1,789
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 221
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: losing decimal precision in division

  #4  
Jul 24th, 2008
Originally Posted by bleh1318 View Post
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/c...ib/strtod.html
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC