Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > double to string retain "precision"

Reply
Thread Tools

double to string retain "precision"

 
 
rjsteele
Guest
Posts: n/a
 
      03-10-2011
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.
 
Reply With Quote
 
 
 
 
rjsteele
Guest
Posts: n/a
 
      03-10-2011
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();
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      03-10-2011
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-10-2011
rjsteele <> 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.
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-10-2011
"Juha Nieminen" <> wrote in message
news:4d7886fa$0$2892$
> rjsteele <> 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.)
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-10-2011
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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      03-11-2011
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?

 
Reply With Quote
 
rjsteele
Guest
Posts: n/a
 
      03-11-2011
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.
 
Reply With Quote
 
Keith H Duggar
Guest
Posts: n/a
 
      03-11-2011
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
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      03-11-2011
"Öö Tiib" <> wrote in message
news:18966ed9-128f-4f10-90fe-
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
char int string ,the different? and string 2 double convent problem. key9 C++ 2 11-29-2006 05:40 PM
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
from List <double> to double[] Web learner ASP .Net 3 04-26-2006 05:26 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM



Advertisments
 



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