![]() |
double to string retain "precision"
How do I retain the value (precision) of the double as a string?
double d = 117.9879878988754135 stringstream ss; ss << d; cout << ss.str() << endl; Output: 117.987 ---------- I want this to be the same as the initial double. Thanks in advance. |
Re: double to string retain "precision"
On Mar 9, 5:00*pm, rjsteele <raymondste...@gmail.com> wrote:
> How do I retain the value (precision) of the double as a string? > > double d = 117.9879878988754135 > stringstream ss; > ss << d; > > cout << ss.str() *<< endl; > > Output: * 117.987 *---------- I want this to be the same as the > initial double. > > Thanks in advance. I think I go it. std::stringstream ss; ss << std::setprecision(std::numeric_limits<double>::dig its10); ss << d; return ss.str(); |
Re: double to string retain "precision"
rjsteele wrote:
> rjsteele wrote: > > > How do I retain the value (precision) of the double as a string? > > I think I go it. > > *std::stringstream ss; > ss << std::setprecision(std::numeric_limits<double>::dig its10); > ss << d; > return ss.str(); I don't recall how exactly digits10 was defined but in case double refers to an IEEE-754 64-bit float on your machine, you will need 17 significant decimal digits for a lossless double-string-double roundtrip. For a lossless string-double-string roundtrip (*) you need to limit your strings to 15 significant decimal digits (* lossless in the sense that the numbers the strings represent are the same and not necessarily the strings itself, so "0.15" would be equal to "0.1500" for example) Cheers! SG |
Re: double to string retain "precision"
rjsteele <raymondsteele@gmail.com> wrote:
> How do I retain the value (precision) of the double as a string? In general, you can't. Most floating point values have an infinite decimal representation. If you want a certain amount of decimals, use the precision() modifier. |
Re: double to string retain "precision"
"Juha Nieminen" <nospam@thanks.invalid> wrote in message
news:4d7886fa$0$2892$7b1e8fa0@news.nbl.fi > rjsteele <raymondsteele@gmail.com> wrote: >> How do I retain the value (precision) of the double as a string? > > In general, you can't. Most floating point values have an infinite > decimal representation. No. In many cases foating point values are stored as finite numbers with base 2. As 2 is a divisor of 10, a finite number in base 2 can be represented as a finite number in base 10. (But not the other way around. 1/5 = 0.2 is not a finite number in base 2.) |
Re: double to string retain "precision"
On Mar 10, 8:08 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> rjsteele <raymondste...@gmail.com> wrote: > > How do I retain the value (precision) of the double as a string? > In general, you can't. Most floating point values have an infinite > decimal representation. No. All can be represented exactly: ten is a multiple of 2, so for n bits precision in machine format, you are guaranteed an exact representation with at most n decimal digits. Of course, n is typically 52, and only a very small subset of 52 digit numbers correspond exactly to a double. And since in most cases, it's highly unlikely that the double corresponded to the exact value to begin with, you don't use 52 digits. -- James Kanze |
Re: double to string retain "precision"
On Mar 10, 10:59*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Mar 10, 8:08 am, Juha Nieminen <nos...@thanks.invalid> wrote: > > > rjsteele <raymondste...@gmail.com> wrote: > > > How do I retain the value (precision) of the double as a string? > > In general, you can't. Most floating point values have an infinite > > decimal representation. > > No. *All can be represented exactly: ten is a multiple of 2, so for n > bits precision in machine format, you are guaranteed an exact > representation with at most n decimal digits. > > Of course, n is typically 52, and only a very small subset of 52 digit > numbers correspond exactly to a double. *And since in most cases, it's > highly unlikely that the double corresponded to the exact value to > begin > with, you don't use 52 digits. 52 decimal digits? That means your float types have over 180 bits? |
Re: double to string retain "precision"
On Mar 10, 5:49*pm, Öö Tiib <oot...@hot.ee> wrote:
> On Mar 10, 10:59*pm, James Kanze <james.ka...@gmail.com> wrote: > > > > > > > On Mar 10, 8:08 am, Juha Nieminen <nos...@thanks.invalid> wrote: > > > > rjsteele <raymondste...@gmail.com> wrote: > > > > How do I retain the value (precision) of the double as a string? > > > In general, you can't. Most floating point values have an infinite > > > decimal representation. > > > No. *All can be represented exactly: ten is a multiple of 2, so for n > > bits precision in machine format, you are guaranteed an exact > > representation with at most n decimal digits. > > > Of course, n is typically 52, and only a very small subset of 52 digit > > numbers correspond exactly to a double. *And since in most cases, it's > > highly unlikely that the double corresponded to the exact value to > > begin > > with, you don't use 52 digits. > > 52 decimal digits? That means your float types have over 180 bits? Thanks for all the responses. |
Re: double to string retain "precision"
On Mar 9, 7:06*pm, rjsteele <raymondste...@gmail.com> wrote:
> On Mar 9, 5:00*pm, rjsteele <raymondste...@gmail.com> wrote: > > > How do I retain the value (precision) of the double as a string? > > > double d = 117.9879878988754135 > > stringstream ss; > > ss << d; > > > cout << ss.str() *<< endl; > > > Output: * 117.987 *---------- I want this to be the same as the > > initial double. > > > Thanks in advance. > > I think I go it. > > *std::stringstream ss; * ss << > std::setprecision(std::numeric_limits<double>::dig its10); * ss << d; > return ss.str(); Use std::numeric_limits<double>::digits10 + 1 instead. KHD |
Re: double to string retain "precision"
"Öö Tiib" <ootiib@hot.ee> wrote in message
news:18966ed9-128f-4f10-90fe-2eb0f2da7aef@a26g2000vbo.googlegroups.com > On Mar 10, 10:59 pm, James Kanze <james.ka...@gmail.com> wrote: >> On Mar 10, 8:08 am, Juha Nieminen <nos...@thanks.invalid> wrote: >> >>> rjsteele <raymondste...@gmail.com> wrote: >>>> How do I retain the value (precision) of the double as a string? >>> In general, you can't. Most floating point values have an infinite >>> decimal representation. >> >> No. All can be represented exactly: ten is a multiple of 2, so for n >> bits precision in machine format, you are guaranteed an exact >> representation with at most n decimal digits. >> >> Of course, n is typically 52, and only a very small subset of 52 >> digit numbers correspond exactly to a double. And since in most >> cases, it's highly unlikely that the double corresponded to the >> exact value to begin >> with, you don't use 52 digits. > > 52 decimal digits? That means your float types have over 180 bits? No. n bits corresponds with n decimal digits if printed in full precision. Numbers are nomally normalized such that the mantisse is a fractional number 0.5 <= M < 0.25. Each bit corresponds with one of the following values: 1/2 = 0.5 1/4 = 0.25 1/8 = 0.125 etc. The position of the last decimal shifts one place to the right for each bit.. Since the bit for the value 1/2 is always non-zero, the total length of the decimal representation is always smaller or equal to the number of bits. |
| All times are GMT. The time now is 09:32 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.