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 Code:
float RatioTrkToFullAP = 0.13134765625 Code:
cout << RatioTrkToFullAP << endl; Code:
0.131348 of the variable. i really do require this level of precision. thanks, BabaG |
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.
|
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 |
thanks to you both. i'd forgotten about setprecision.
this worked for the given example: Code:
#include <iostream> Code:
#include <iostream> Code:
0.551057622173595884618180207326076924800872802734375 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 |
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. |
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 |
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 |
Quote:
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”. |
thanks to you both. both clear and helpful!
BabaG |
Quote:
|
All times are GMT -5. The time now is 09:54 AM. |