Connecting Tech Pros Worldwide Forums | Help | Site Map

scientific & setprecision (iomanip)

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Sep 3 '07
Hi,

I setprecision to be 100 for both cases. I'm wondering why the number
of digits are different.

Also, for a double number, I think any digits that are longer than 15
(or 16) are not meaningful, because it exceed the double number's
precision limit. Even if I setprecision to be 100, shall it truncate
the number to be of 15(or 16) digits?

Thanks,
Peng

$ cat main.cc
#include <iostream>
#include <iomanip>

int main() {
double a = .01;
std::cout << std::setprecision(100) << a << std::endl;
std::cout << std::scientific << std::setprecision(100) << a <<
std::endl;
}
$ ./main-g.exe
0.010000000000000000208166817117216851329430937767 02880859375
1.000000000000000020816681711721685132943093776702 88085937500000000000000000000000000000000000000000 00e-02


Victor Bazarov
Guest
 
Posts: n/a
#2: Sep 3 '07

re: scientific & setprecision (iomanip)


PengYu.UT@gmail.com wrote:
Quote:
I setprecision to be 100 for both cases. I'm wondering why the number
of digits are different.
>
Also, for a double number, I think any digits that are longer than 15
(or 16) are not meaningful, because it exceed the double number's
precision limit. Even if I setprecision to be 100, shall it truncate
the number to be of 15(or 16) digits?
>
Thanks,
Peng
>
$ cat main.cc
#include <iostream>
#include <iomanip>
>
int main() {
double a = .01;
std::cout << std::setprecision(100) << a << std::endl;
std::cout << std::scientific << std::setprecision(100) << a <<
std::endl;
}
$ ./main-g.exe
0.010000000000000000208166817117216851329430937767 02880859375
1.000000000000000020816681711721685132943093776702 88085937500000000000000000000000000000000000000000 00e-02
I believe 'fixed' (the default, when 'scientific' is not used), always
skips the trailing zeros.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
#3: Sep 3 '07

re: scientific & setprecision (iomanip)


On 2007-09-03 18:03, Victor Bazarov wrote:
Quote:
PengYu.UT@gmail.com wrote:
Quote:
>I setprecision to be 100 for both cases. I'm wondering why the number
>of digits are different.
>>
>Also, for a double number, I think any digits that are longer than 15
>(or 16) are not meaningful, because it exceed the double number's
>precision limit. Even if I setprecision to be 100, shall it truncate
>the number to be of 15(or 16) digits?
>>
>Thanks,
>Peng
>>
>$ cat main.cc
>#include <iostream>
>#include <iomanip>
>>
>int main() {
> double a = .01;
> std::cout << std::setprecision(100) << a << std::endl;
> std::cout << std::scientific << std::setprecision(100) << a <<
>std::endl;
>}
>$ ./main-g.exe
>0.01000000000000000020816681711721685132943093776 702880859375
>1.00000000000000002081668171172168513294309377670 28808593750000000000000000000000000000000000000000 000e-02
>
I believe 'fixed' (the default, when 'scientific' is not used), always
skips the trailing zeros.
Running the same code under Windows using VC++ 2005 I get the same
number of digits printed, but it's all zeroes after the 1. I find that a
bit troubling, since I thought that 0.01 was a number that couldn't be
exactly represented.

--
Erik Wikström
Victor Bazarov
Guest
 
Posts: n/a
#4: Sep 3 '07

re: scientific & setprecision (iomanip)


Erik Wikström wrote:
Quote:
On 2007-09-03 18:03, Victor Bazarov wrote:
Quote:
>PengYu.UT@gmail.com wrote:
Quote:
>>I setprecision to be 100 for both cases. I'm wondering why the
>>number of digits are different.
>>[..]
>>$ ./main-g.exe
>>0.0100000000000000002081668171172168513294309377 6702880859375
>>1.0000000000000000208166817117216851329430937767 02880859375000000000000000000000000000000000000000 0000e-02
>>
>I believe 'fixed' (the default, when 'scientific' is not used),
>always skips the trailing zeros.
>
Running the same code under Windows using VC++ 2005 I get the same
number of digits printed, but it's all zeroes after the 1. I find
that a bit troubling, since I thought that 0.01 was a number that
couldn't be exactly represented.
What you see is the external equivalent of the imprecise internal
representation. A decent library should be able to successfully
round the number when outputting is so that the round-trip (no pun
indended) from external rep to internal and back to external does
not change the appearance of the number. "0.01" in--0xB1AB1AAA (or
whatever) out--"0.01"

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Closed Thread