LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-13-2008, 05:54 PM   #1
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Rep: Reputation: 80
precision() and setprecision()


What is the difference between the precision() function for cout and the setprecision()? They both appear to be the same.
 
Old 07-13-2008, 07:17 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
One is a member function, while the other is an IO manipulator

eg:
Code:
cout.precision(42);
vs
cout << setprecision(42);
 
Old 07-14-2008, 05:53 PM   #3
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Original Poster
Rep: Reputation: 80
Do they both result in the same thing? Do they both return the number of digits for precision and change that number if an argument is given?
 
Old 07-14-2008, 06:30 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
setprecision doesn't return the current precision (the argument is required). Other than that, they behave the same.
 
Old 07-15-2008, 01:24 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: san antonio, texas
Distribution: Fedora 64 bit RAID0 + LUKS, CentOS (server), Backtrack, Gentoo Hardened
Posts: 1,440

Original Poster
Rep: Reputation: 80
Thanks, that clears up some of my confusion. In this example my book gives me, I noticed that setprecision() takes a 3 and then later a streamtype value, so is it an overloaded function?

Code:
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3) 
     << 0.2 * midterm + 0.4 * final + 0.4 * median
     << setprecision(prec) << endl;
 
Old 07-15-2008, 10:46 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,445

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
No, streamsize is a typedef.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cout.precision() dewjunkie Programming 1 01-30-2008 11:26 AM
Cron / second precision ? TrAvIkK Linux - General 8 07-23-2007 03:08 PM
Dell Precision 670? etcetera Linux - Hardware 11 03-20-2006 09:34 PM
C- precision problem vivekr Programming 30 01-13-2006 11:43 AM
fortran 77 precision buddy_epson Programming 0 07-19-2003 03:51 AM


All times are GMT -5. The time now is 11:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration