468,289 Members | 1,915 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,289 developers. It's quick & easy.

scientific & setprecision (iomanip)

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

Sep 3 '07 #1
3 4675
Pe*******@gmail.com wrote:
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
Sep 3 '07 #2
On 2007-09-03 18:03, Victor Bazarov wrote:
Pe*******@gmail.com wrote:
>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
Sep 3 '07 #3
Erik Wikström wrote:
On 2007-09-03 18:03, Victor Bazarov wrote:
>Pe*******@gmail.com wrote:
>>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
Sep 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Woodster | last post: by
2 posts views Thread by Ville Ahonen | last post: by
1 post views Thread by Gary Wessle | last post: by
3 posts views Thread by Wing | last post: by
1 post views Thread by Wing | last post: by
7 posts views Thread by jacek.dziedzic | last post: by
4 posts views Thread by Johannes Bauer | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.