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 391,926 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,662 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:
Views: 134 | Replies: 3
Reply
Join Date: Jun 2008
Posts: 3
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  
27 Days Ago
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,405
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: losing decimal precision in division

  #2  
27 Days Ago
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 : 27 Days Ago at 12:02 pm.
Reply With Quote  
Join Date: Jun 2008
Posts: 3
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  
27 Days Ago
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,405
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: losing decimal precision in division

  #4  
27 Days Ago
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.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

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