473,656 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::setprecisi on(100) << a << std::endl;
std::cout << std::scientific << std::setprecisi on(100) << a <<
std::endl;
}
$ ./main-g.exe
0.0100000000000 000002081668171 172168513294309 377670288085937 5
1.0000000000000 000208166817117 216851329430937 767028808593750 000000000000000 000000000000000 000000000000e-02

Sep 3 '07 #1
3 5060
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::setprecisi on(100) << a << std::endl;
std::cout << std::scientific << std::setprecisi on(100) << a <<
std::endl;
}
$ ./main-g.exe
0.0100000000000 000002081668171 172168513294309 377670288085937 5
1.0000000000000 000208166817117 216851329430937 767028808593750 000000000000000 000000000000000 000000000000e-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::setprecisi on(100) << a << std::endl;
std::cout << std::scientific << std::setprecisi on(100) << a <<
std::endl;
}
$ ./main-g.exe
0.010000000000 000000208166817 117216851329430 937767028808593 75
1.000000000000 000020816681711 721685132943093 776702880859375 000000000000000 000000000000000 0000000000000e-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.01000000000 000000020816681 711721685132943 093776702880859 375
1.00000000000 000002081668171 172168513294309 377670288085937 500000000000000 000000000000000 00000000000000e-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
8715
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 buffer; buffer << setprecision(1) << 40.0 << "° C";
2
2681
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 output "0.000049513"? Thanks alot, Ville
1
2670
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 desired output is: 0.555504 of an inch foreach 12.55
3
2634
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 use? Thanks so much.
1
2153
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 double number. However, I don't want to add "setprecision (9)" in every "cout" and
7
5233
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
12500
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
5516
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 METRIC = 'M'; // Input read as metric char const ENGLISH = 'E'; // Input read as english char const QUIT = 'Q'; // Input read to quit program char const QUIT2 = 'q'; // Input read to quit program
4
8702
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 << std::scientific << x; std::cerr << Strm.str() << std::endl;
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.