//Extra Credit question // //code written by Dean Gadberry // //Karson Kelley asks //why is it that when I use the showpoint and/or fixed stream manipulators, //the output isn't always what's intended? // //Example: // //{ //float number = 10.2; // //cout << fixed << showpoint << setprecision(8)<< number; //} // //The expected output is 10.20000000, but when the program is run, //the output is 10.19999981. Why does this happen? #include<iostream> #include<iomanip> using namespace std; int main() { float number = 10.2; //this is the number that Karson uses cout<<"This is Karson's original number: "; cout << fixed <<setprecision(8) << showpoint << number <<endl; //first i tried to reassign Karson's number to a double double assign = number; cout <<endl<<"This is the double of Karson's number: "; cout << fixed <<setprecision(8) << showpoint << assign <<endl; // because Karson's number is holding it's precision // value through the assignment process, // I think that the number in storage is having issues // I recall the numbers float and double having // different precision levels. I expect that they are // stored differently in memory //This is a double with the same value double dbl = 10.2; cout <<endl<<"This is the double of 10.2 with 8 decimal values: " <<endl; cout << fixed <<setprecision(8) << showpoint << dbl <<endl; // This is a test of different setprecision numbers float precise = 10.2; cout <<endl<<"This is the float of 10.2 with different decimal values: " <<endl; cout << fixed <<setprecision(6) << showpoint << "6: "<<precise <<endl; cout << fixed <<setprecision(7) << showpoint << "7: "<<precise <<endl; cout << fixed <<setprecision(8) << showpoint << "8: "<<precise <<endl; cout << fixed <<setprecision(9) << showpoint << "9: "<<precise <<endl; cout << fixed <<setprecision(10) << showpoint << "10: "<<precise <<endl; // short explanation of phenomena cout <<endl <<"notice how the above numbers become less precise" <<endl <<"depending on the value passed to setprecision()" <<endl; // I've been reading about the different properties of floats and doubles. // floats are only 4 bytes, but doubles are 8. long doubles are a whole 12 // I think that the real situation is that the 10.2 is stored in memory // with a limited set of spaces, and we are asking for the computer // to retrieve a number that requires a larger memory allocation // the Interesting thing to me is the setprecision() function // I will be interested to understand how it works someday // as a proof that this is a memory issue, // I will be using the double that was declared on line // 45 to test other precision lengths cout <<endl<<"testing double 10.2"<<endl; cout << fixed << showpoint << setprecision(10) << "10: " << dbl << endl << setprecision(11) << "11: " << dbl << endl << setprecision(12) << "12: " << dbl << endl << setprecision(13) << "13: " << dbl << endl << setprecision(14) << "14: " << dbl << endl << setprecision(15) << "15: " << dbl << endl << setprecision(16) << "16: " << dbl << endl << setprecision(17) << "17: " << dbl << endl << setprecision(18) << "19: " << dbl << endl << setprecision(20) << "20: " << dbl << endl <<endl; //proofs complete cout <<"Here we see the display of a double" <<endl <<"which has twice as many bytes as a float" <<endl <<"(double: 8 bytes, float: 4 bytes)" <<endl <<endl <<"The setprecision function (which calls os.precision" <<endl <<"does not have the capacity to show more than so many decimal points" <<endl <<"because the number is not allocated enough memory" <<endl <<endl <<"This is exactly why these variables are refered to as having" <<endl <<"single or double precision" <<endl <<"because they are more precise, depending on the ammount" <<endl <<"of memory allocated" <<endl; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}