LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2008, 12:19 AM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
c++ - how to avoid truncating or rounding a double numerical value


working in mandriva linux 2007.1. compiling with g++.

i have some lengthy variable values that are rounding off
when i don't want them to. how can i avoid this?

an example is the value:
Code:
0.13134765625
i have tried declaring it as:

Code:
float RatioTrkToFullAP =  0.13134765625
double RatioTrkToFullAP =  0.13134765625
long double RatioTrkToFullAP =  0.13134765625
but all of these, when run as:
Code:
cout << RatioTrkToFullAP << endl;
print out as:
Code:
0.131348
how do i declare these so they'll retain the full value
of the variable. i really do require this level of
precision.

thanks,
BabaG

Last edited by babag; 05-18-2008 at 12:43 AM.
 
Old 05-18-2008, 01:03 AM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
I am not really familiar with "cout," but I think the truncation is probably just happening when you print. Internally it probably has the proper precision. Try adjusting the format on cout or use fprintf formatted for sufficient precision.
 
Old 05-18-2008, 01:41 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

What you're looking for are C++ "manipulators" like "width()" and "precision()":

http://www.codeguru.com/forum/archiv.../t-295798.html

http://www.arachnoid.com/cpptutor/student3.html
 
Old 05-18-2008, 12:56 PM   #4
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks to you both. i'd forgotten about setprecision.
this worked for the given example:
Code:
#include <iostream>
#include <iomanip>

double RatioTrkToFullAP = 0.13134765625;
cout << setflags(ios::fixed) <<setprecision(11) << RatioTrkToFullAP << endl;
however, i also have this, very long example:
Code:
#include <iostream>
#include <iomanip>

double RatioTrkToFullAP = 0.55105762217359591539022611232677;
cout << setflags(ios::fixed) <<setprecision(32) << RatioTrkToFullAP << endl;
which returns:
Code:
0.551057622173595884618180207326076924800872802734375
which, obviously is not the value that was input. (i had
to change the setprecision value to a larger value to
get the entire thiing to print out just to find out what
the system was doing. the above is the value i finally
found. it was truncated or rounded if i kept the original
value of 32 for setprecision.) it matches for the first
fiften spaces, then veers off.

so what's going on here, now?

thanks again, gradually getting through it with you help,
BabaG

Last edited by babag; 05-18-2008 at 12:58 PM.
 
Old 05-18-2008, 01:06 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
I do not think you understand the concept of floating point numbers. The rule-of-thumb is that a double-precision float will hold around 15 decimal digits. Almost any arithmetic you do will decrease the precision. The reasons for this are detailed many places (including on this forum).

If you want arbitrary precision arithmetic, look for a bignum library such as GMP.
 
Old 05-18-2008, 02:18 PM   #6
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
for now i'd just like to know why, since no operations
have been performed on the variable, it doesn't output
the value it was given. i don't know what to search for
on the forum to clarify this as, i expect, something as
simple as 'float' will return thousands of irrelevant
hits.

also, i've recently had trouble with the search on the
site. lately it's returned an error that the search
criteria was insufficient and must be at least three
words in length; this, even if i've entered whole
sentences.

thanks,
BabaG
 
Old 05-18-2008, 02:49 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Put simply, can you correctly store 1/3 in decimal? Well floating point numbers have the same sort of problem.
There are links at the bottom of the following thread for further reading.
http://www.linuxquestions.org/questi...light=IEEE+754

Last edited by dmail; 05-18-2008 at 02:52 PM.
 
Old 05-18-2008, 02:53 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by babag View Post
for now i'd just like to know why, since no operations
have been performed on the variable, it doesn't output
the value it was given.
Well, a double-precision float variable (i.e., “double”) is, if following IEEE 754, 64 bits wide. The first bit is the sign bit, the next eleven bits are for the exponent, and the remaining 52 bits are for the mantissa (and since this is normalized, the leading one is not actually stored, so there is a “hidden bit”).

The 15 or 16-digit rule of thumb comes from the limit on the mantissa. When you try to fit a wider number into smaller container, you must lose precision. In the C standard, the header file float.h contains the macro DBL_DIG which must be greater than or equal to 10, and represents “number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits”.
 
Old 05-18-2008, 03:18 PM   #9
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks to you both. both clear and helpful!

BabaG
 
Old 05-18-2008, 05:00 PM   #10
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by osor View Post
Well, a double-precision float variable (i.e., “double”) is, if following IEEE 754, 64 bits wide. The first bit is the sign bit, the next eleven bits are for the exponent, and the remaining 52 bits are for the mantissa (and since this is normalized, the leading one is not actually stored, so there is a “hidden bit”).
The most significant part of this being that numbers are stored in binary rather than in decimal. And that there are numbers that have terminating representations in decimal but not in binary. For example, decimal 0.2 = binary 0.0011 0011 0011 0011.... Just like dozenal 0.4 = decimal 0.333333333333...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
rounding a large double to 2 decimal places linuxmandrake Programming 2 03-17-2006 03:31 PM
HEELLP ~~ Converting an integer minute into time, Rounding off a double ~~ HELLP Mistro116@yahoo.com Programming 5 10-04-2005 11:51 AM
C++ Rounding and Truncation Opeth Programming 4 09-17-2005 07:16 PM
Java Precission rounding. Tru_Messiah Programming 4 05-14-2004 11:23 PM
Truncating a string nkendrick Linux - General 5 11-29-2003 09:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration