473,698 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setprecision problem

Hello everyone,

I have the following code:

///////////////////////////////////////
double num;
num=1234567890;
num+=(1.0/3.0);

cout.setf(ios:: fixed);
cout<<setprecis ion(10);
cout<<num<<endl ;
///////////////////////////////////////

The output is 1234567890.3333 332539. I am wondering why the floating
numbers are not all "3". Why "2539" appears in the output?

Can anyone help me to solve this problem? Thank for so much.

Oct 11 '06 #1
2 2313
Wing wrote:
Hello everyone,

I have the following code:

///////////////////////////////////////
double num;
num=1234567890;
num+=(1.0/3.0);

cout.setf(ios:: fixed);
cout<<setprecis ion(10);
cout<<num<<endl ;
///////////////////////////////////////

The output is 1234567890.3333 332539. I am wondering why the floating
numbers are not all "3". Why "2539" appears in the output?

Can anyone help me to solve this problem? Thank for so much.
This is a FAQ. See
http://www.parashift.com/c++-faq-lit...html#faq-29.16

Nate
Oct 11 '06 #2
Wing wrote:
///////////////////////////////////////
double num;
num=1234567890;
num+=(1.0/3.0);

cout.setf(ios:: fixed);
cout<<setprecis ion(10);
cout<<num<<endl ;
///////////////////////////////////////

The output is 1234567890.3333 332539. I am wondering why the floating
numbers are not all "3". Why "2539" appears in the output?
http://en.wikipedia.org/wiki/Floatin...uracy_problems

http://docs.sun.com/source/806-3568/ncg_goldberg.html
Oct 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22575
by: tarmat | last post by:
I'm trying to create a function that will turn a float into a std::string that always shows the number to two decimal places. I have the following but it doesn't give the desired output: inline std::string ftos(float f) { std::ostringstream buffer; buffer << std::setprecision(2) << f;
2
8717
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";
1
2671
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
2494
by: Anjo Gasa | last post by:
I'm having some cases where setprecision in combination with iostreams gives some unepected behavior. Consider the following program: #include <iostream> #include <iomanip> int _tmain(int argc, _TCHAR* argv) { float value = 0.063397526741027832;
7
12503
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
1808
by: mahesh.kanakaraj | last post by:
Hi All, I am new to C++ programming. I encounter an unexpected behavior of 'setprecision'. The code snippet is : #include <iostream.h> #include <stdio.h> #include <iomanip.h>
3
5061
by: PengYu.UT | last post by:
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?
1
4875
by: jthep | last post by:
Hi, I'm converting codes that I worked with in C to C++ and having a problem with setprecision. At first the code worked, then I made some changes. When the changes didn't work I wrote it back to how it was before and now it's not working. I keep getting the error message where setprecision is undefined. Can anyone help plz. #include <iostream> using namespace std; float convertFtoC(int temperature); int roundUpNextMultiple(int...
2
5517
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
0
8683
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
8610
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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
8873
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...
1
6528
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...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.