Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

setprecision problems in c++

brtkltbrtklt Member Posts: 237
[b][red]This message was edited by brtklt at 2002-9-20 22:24:29[/red][/b][hr]
I have an assignment that requires output of the same thing in different precisions (1, 5 and 7). The problem I'm having is when I set precision to 1 in messes up the output of a variable giving me some exponential stuff on a simple float type that holds a value of something like 150.54 as well as w/ the results that are supposed to have the precision of one. The rest of the precisions (5 and 7) don't give 5 or 7 points of precision exactly. I wrote a simple program to test this w/ a value that held 5 digits after the period and it w/ precision set to 2 or 3 or whatever it doesn't give the right amount. Is this normal or am I doing something wrong? Here's the code:







[code]






#include



#include



#include







using namespace std;







const float KILCONV = 0.4536;



const double GRAMCONV = 453.59237;



const string QUESTION = "What is the weight in pounds you wish to convert: ";



const string NAME = "Brian Tong";



const string CLASS = "CMIS140: Assignment #2";



const string DD = "09/28/02";







int main()



{



{



// DECLARE VARIABLES







float lbs;



float kilos;



double grams;







// PROMPT FOR POUNDAGE AND DO CONVERSIONS







cout << QUESTION;



cin >> lbs;



kilos = lbs * KILCONV;



grams = lbs * GRAMCONV;







// OUTPUT CLASS INFO AND CONVERSIONS







cout << setw(11) << NAME << setw(52) << CLASS << setw(28) << DD;



cout << "

";



cout << "The following is printed with 1 point of precision:

";



cout << "Float Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(2) << float(kilos) << " kg.
";



cout << lbs << " lbs. converted to grams is ";



cout << setprecision(2) << float(grams) << " grams

";



cout << "Double Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(2) << double(kilos) << " kg.
";



cout << lbs << " lbs. converted to grams is ";



cout << setprecision(2) << double(grams) << " grams


";







cout << "The following is printed with 5 points of precision:

";



cout << "Float Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(6) << float(kilos) << " kg.
";



cout << lbs << " lbs. converted to grams is ";



cout << setprecision(6) << float(grams) << " grams

";



cout << "Double Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(6) << double(kilos) << " kg.
";



cout << lbs << " lbs. converted to grams is ";



cout << setprecision(6) << double(grams) << " grams


";







cout << "The following is printed with 7 points of precision:

";



cout << "Float Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(8) << float(kilos) << " kg.
";







cout << lbs << " lbs. converted to grams is ";



cout << setprecision(8) << float(grams) << " grams

";



cout << "Double Format:
";



cout << lbs << " lbs. converted to kilograms is ";



cout << setprecision(8) << double(kilos) << " kg.
";



cout << lbs << " lbs. converted to grams is ";



cout << setprecision(8) << double(grams) << " grams

";







return 0;



}



[code]




Comments

  • Shawn CarterShawn Carter Member Posts: 0

    ______ / http://forcoder.org / free video tutorials and ebooks about | PHP JavaScript Visual Basic Go C Java C++ Assembly Swift R Python C# MATLAB Visual Basic .NET Delphi Ruby Scratch Perl Objective-C PL/SQL SAS LabVIEW Hack Scheme Alice Fortran Rust Bash D Clojure Kotlin FoxPro Scala Lisp Apex VBScript ML Julia Lua COBOL Prolog Crystal Erlang Logo Awk Dart Ada ABAP F# Transact-SQL |

Sign In or Register to comment.