scientific & setprecision (iomanip)

P

PengYu.UT

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.01000000000000000020816681711721685132943093776702880859375
1.0000000000000000208166817117216851329430937767028808593750000000000000000000000000000000000000000000e-02
 
V

Victor Bazarov

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.01000000000000000020816681711721685132943093776702880859375
1.0000000000000000208166817117216851329430937767028808593750000000000000000000000000000000000000000000e-02

I believe 'fixed' (the default, when 'scientific' is not used), always
skips the trailing zeros.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Victor Bazarov

Erik said:
I setprecision to be 100 for both cases. I'm wondering why the
number of digits are different.
[..]
$ ./main-g.exe
0.01000000000000000020816681711721685132943093776702880859375
1.0000000000000000208166817117216851329430937767028808593750000000000000000000000000000000000000000000e-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top