473,221 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 5039
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream...
2
by: Ville Ahonen | last post by:
I'm a total beginner at C++ so please bear with me.. When i write cout << stddev * stddev << endl; I get the output "4.49513e-05". How can I rephrase the above so that I would instead get the...
1
by: Gary Wessle | last post by:
hi the code below is giving me what I want but it is very ugly. and will not work for a long list of different length numbers. could you please look at it and comment. thank you the...
3
by: Wing | last post by:
Hello eveyone, I am programming some scientific computation based on C++. I would like to set every output/input (e.g. cout, io file stream) to be a high precision. What C++ command should I...
1
by: Wing | last post by:
Thanks for those who answered my question previously. Everytime I want to output high precision numbers, I use the following code: cout << setprecision (9) << f << endl; where f is some...
7
by: Woody714 | last post by:
I'm fairly new to C++ and have an assignment that is knocking me out. The text file is fairly simple: Plain Egg 1.45 Bacon and Egg 2.45 Muffin 0.99..... etc....
7
by: jacek.dziedzic | last post by:
Hello! Can someone tell me what the expected output of the following program is? #include <fstream> #include <iomanip> using namespace std;
2
by: victoryusami | last post by:
I'm not 100% sure whats even going on, but I'm working on a C++ assignment for school, and its a road trip program #include <iostream> #include <iomanip> using namespace std; char const...
4
by: Johannes Bauer | last post by:
Hello group, I've a simple problem with the precision specifiers of stringstream. Let's say I have this: double x = 123.4567890; std::stringstream Strm; Strm.precision(4); Strm <<...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.