LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 06: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,441

Rep: Reputation: 81
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, 08:17 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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, 06: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,441

Original Poster
Rep: Reputation: 81
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, 07:30 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
setprecision doesn't return the current precision (the argument is required). Other than that, they behave the same.
 
Old 07-15-2008, 02: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,441

Original Poster
Rep: Reputation: 81
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, 11:46 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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 12:26 PM
Cron / second precision ? TrAvIkK Linux - General 8 07-23-2007 04:08 PM
Dell Precision 670? etcetera Linux - Hardware 11 03-20-2006 10:34 PM
C- precision problem vivekr Programming 30 01-13-2006 12:43 PM
fortran 77 precision buddy_epson Programming 0 07-19-2003 04:51 AM


All times are GMT -5. The time now is 08:05 AM.

Main Menu
Advertisement
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