Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   double to string retain "precision" (http://www.velocityreviews.com/forums/t744868-double-to-string-retain-precision.html)

rjsteele 03-10-2011 12:00 AM

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.

rjsteele 03-10-2011 12:06 AM

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();

SG 03-10-2011 01:23 AM

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

Juha Nieminen 03-10-2011 08:08 AM

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.

Fred Zwarts 03-10-2011 10:44 AM

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.)

James Kanze 03-10-2011 08:59 PM

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

Öö Tiib 03-11-2011 12:49 AM

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?


rjsteele 03-11-2011 02:36 PM

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.

Keith H Duggar 03-11-2011 02:48 PM

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

Fred Zwarts 03-11-2011 03:07 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57