Why does setf(ios::fixed) skip the 6th number in a double? - c++

#include <iostream>
using namespace std;
int main() {
double pi = 0.1234567;
cout << "1234567890" << endl;
// cout.width(10);
cout.setf(ios::fixed);
cout << pi << endl;
}
outputs
1234567890
0.123457
Why does it print that instead of 0.123456?

Because it rounds it correctly, that's why. 0.1234567 rounded to 6 decimal places (the default) is 0.123457.

Related

Why my code is given me wrong results?

I don't know why my code is giving to me incorrect results.
When I put a number like 6670680902 the result is 6.67068e+0.7 (which is 66706800). That is not the correct result.
When I use the calculator the correct result of 667006080902 / 100 is 66706809.02.
What should I do to fix it?
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
float a;
float b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout << "result: " << b;
_getch();
return 0;
}
The first issue here is by default C++ will show larger numbers using scientific notation, there are ways to prevent this for floating point numbers like floats. One simple way is to add << fixed before your number:
cout << "result: " << fixed << b;
Which will return 66706812.0.
The next problem is that floats are not good at being precise, which is why the number still isn't correct. Floats are less precise compared to something like a double which has twice the precision. If you use a double instead for a and b:
int main()
{
double a;
double b;
//...
cout << "result: " << fixed << b;
//...
}
you will get the value you expect: 66706809.02
Can do by using 'limits`
#include "iostream"
#include <string>
#include <limits>
using namespace System;
using namespace std;
int main()
{
double a;
double b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << "result: " << b << endl;
_getch();
return 0;
}
Output:
result: 66706809.02

How to make negative numbers not show?

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
float V1, V0, t;
cout << fixed << showpoint << setprecision(2);
cout << "Please enter starting V0, V1, and t in terms of seconds" << endl;
cin >> V1 >> V0 >> t;
cout << (V1 - V0) / t << endl;
cin.get();
cin.get();
return 0;
}
(Formula is in code) I'm doing a problem and the example shows like this
Enter V0, V1 and t: 5.5 50.9 4.5
The Average is : 10.0889
However from my code which is rounded two decimal places when I enter those numbers I get
The Average is : - 10.09
Am I wrong? Can the average of velocity not be a negative number? And if so how do I make it so the negative does not appear. If anyone could lend me a hand that would be great.
You can use abs from cmath
#include <cmath>
...
cout << abs((V1 - V0) / t) << endl;

C++ - Why is my cout code displaying decimals inconsistently?

I'm working on an app that needs to print an array through cout on one line, and show 2 decimal places. Currently, my code prints the first two items with 2 decimals, then switches to 1.
Here is the code:
cout << " Inches ";
cout << showpoint << setprecision(2) << right;
for (int i = 0; i < 12; i++)
{
cout << setw(5) << precipitation[i];
}
cout << endl;
And here is the output:
Inches 0.72 0.89 2.0 3.0 4.8 4.2 2.8 3.8 2.7 2.1 1.6 1.0
Can someone please tell me why this change is precision is occurring and what I can do to fix it?
Thanks
You need to use "fixed" mode. In default floating-point mode, precision() sets the number of significant figures to display. In "fixed" mode, it sets the number of places after the decimal. Case in point:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
float pi = 3.14;
cout.precision(2);
cout << pi << endl;
cout << fixed << pi << endl;
}
Gives the output:
3.1
3.14
HTH.
If you just add cout << fixed before output statements in addition to showpoint and setprecision, you will get a consistent formatting for all outputs.
See below:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double precipitation[12] = { .72, .89, 2, 3, 4.8, 4.2, 2.8, 3.8, 2.7, 2.1, 1.6, 1 };
cout << " Inches ";
cout << showpoint << fixed << setprecision(2) << right;
for (int i = 0; i < 12; i++)
{
cout << setw(5) << precipitation[i];
}
cout << endl;
return 0;
}
Now, the output will be as bellow:
Inches 0.72 0.89 2.00 3.00 4.80 4.20 2.80 3.80 2.70 2.10 1.60 1.00
I was troubled with this problem too. You need to use 'fixed' to do this.
Refer to this link:
C++ setprecision(2) printing one decimal?

How to read the entire value of a double using cin?

long double m;
cout << "enter double: "; cin >> m;
cout << "m = " << m <<endl;
Input:
enter double: 1.546640625
Output:
m = 1.54664
I have to convert into a binary with point, and when I read numbers like 2.359375000
Output:
m = 2.35938
And it works, but I think the problem is the zero in 1.546640625
You have read the whole value of the double. The problem is with the cout. It by default rounds the value to 6 digits after the decimal point.
To set the precision cout uses, use setprecision from <iomanip>:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long double d;
cin >> d;
cout << setprecision(10) << d << endl;
return 0;
}

Convert a letter to its ascii decimal form

i am in a basic programming class and know practically nothing about programming, we are using c++ and my current project is to pull up the console and do the following
input a letter and output its ascii decimal equivalent
input a number between 33 and 254 and output its letter equivalent
input a lower case letter and output itscapital
input a number of hours and output the number of minutes
input a number greater than 60 and output the number of hours and minutes
this is the work i have so far
#include <iostream>
using namespace std;
void main ()
{
cout<<"Assignment 2"<<endl;
char somechar;
int charval;
int input_number;
char output_letter2;
char input_lower_letter;
char output_upper_letter;
int input_hours;
int output_minutes;
int input_minutes2;
int output_hours2;
int output_remainder_minutes;
cout<<"Enter a letter"<<endl;
cin>>somechar>>endl;
cout<< somechar='a';
int charval = somechar;
printf("%c = %d\n",somechar,charval);
system("pause");
}
any tips and help are deeply appreciated
One crucial lesson (which they never seem to teach in school) is to start with a very simple program, get it working perfectly, then build up, testing at every step.
Your code doesn't compile. Let's strip your code down and start from scratch:
void main()
{
}
This doesn't compile. Fix it:
int main()
{
return(0);
}
Now add some output:
#include <iostream>
using namespace std;
int main()
{
cout << "Assignment 2" << endl;
return(0);
}
So far, so good. Now input:
#include <iostream>
using namespace std;
int main()
{
cout << "Assignment 2" << endl;
char somechar;
cout << "Enter a letter" << endl;
cin >> somechar >> endl;
return(0);
}
This doesn't compile. Fix it.
And so on. See how it works?
#include <iostream>
using namespace std;
int main() {
char a ='a';
cout << a << " => integer: " << (int)(a) << endl;
int i = 98;
cout << i << " => character: " << (char)(i) << endl;
char b='b';
cout << b <<" => lower: " << (char)(b+('a'-'A')) << endl;
char c='c';
cout << c << " => upper: " << (char)(c-('a'-'A')) << endl;
int hours = 15;
cout << hours << " hours => minutes: " << hours * 60 << endl;
int minutes = 75;
cout << minutes << " minutes => hours:minutes: " << minutes/60 << ":" << minutes % 60 << endl;
system("PAUSE");
return 0;
}
For this you can take a look at casting. To cast a character (char) to an integer ASCII value, you have to cast an int on that char value. For example:
#include <iostream>
using namespace std;
int main() {
char myvalue;
cout<<"Enter a character: ";
cin>>myvalue;
cout<<endl<<"The ASCII value is: "<<(int)myvalue<<endl;
return 0;
}
Now because this is homework, I wont finish the steps for you, but it should be pretty straight forward from here.
For more information about casting, I highly suggest: http://www.cplusplus.com/doc/tutorial/typecasting/