Remove precision from divide - c++

well basically if I write something like this -
float a = 0;
a = (float) 1/5;
a += (float) 1/9;
a += (float) 1/100;
It will automatically decrase precision to 2 digits after comma, but I need to have 5 digits after comma, is it available to create, so it displays 5 digits? With setprecision(5) it, just shows 00000 after comma.
It get's all data from input file just fine.

setprecision do not modify value. It's only display desired precision when you using ofstream

You have to use setprecision like this:
cout << setprecision (5) << a << endl;
http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
EDIT: I haven't used C++ in a while but you may be getting some problems because you are doing integer division and then casting the result to a float. Try doing it like this instead to force a float division:
a+=1.0f/100;

this will give 5 digits after comma:
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argi, char** argc) {
float a = 0;
a = (float) 1/5;
a += (float) 1/9;
a += (float) 1/100;
cout << setprecision(5) << a << endl;
return 0;
}
if you want to have always 5 digits on output, maybe use this:
cout << setprecision(5) << setfill ('0')<< setw(5) << a << endl;

You have some reading to do:
http://www.google.com/?q=what+every+programmer+should+know+about+floating+point

Related

Printing integers with a fixed number of zeros in C++

The question is simple. I have messed up with std::fixed, std::setprecicion, std::setw, but none of them solves my problem.
If I have a integer variable equal to 1, I want it to be printed as:
1000
Thank you.
You can use std::left together with std::setw and std::setfill to add a number of specific fill characters on the right. Example with std::cout:
#include <iostream>
#include <iomanip>
#include <ios>
int main() {
std::cout << std::setw(4) << std::setfill('0') << std::left << 1;
return 0;
}
I believe it should do the job.
I think you mean you want it to print as 0001, not as 1000.
Look at printf / sprintf with a format string. For example:
#include <iostream>
int main(int argc, char **argv) {
int i = 1;
printf("%04d\n", i);
}
The question is not very clear on what you are trying to achieve.
If you want to print the value of your variable with 3 zeroes appended to it, you should just print the variable and then 3 zeroes.
cout << x << "000" << endl;
If you need to do any computation with it, you could always just multiply it by 1000.
int y = x * 1000;
cout << y << endl;
Keep in mind that this may cause an overflow, though...

Printing only decimal places of a number

I was wondering how can you only print the decimal places of the number?
I need to print 10.30 as 10m,30cm and I have no idea how to do that.
I have tried to google the solution, but didn't find anything helpful.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double P;
cin >> P;
cout << fixed << setprecision(0) << P << "m," << setprecision(2) << P << "cm";
return 0;
}
Given a double P, and assuming you wanted to round half-way any fractions of cm, this is one approach:
unsigned long cm = std::round(P * 100); // ToDo - check the possibility of overflow
std::cout << cm / 100 << "m, " << cm % 100 << "cm";
Where I'm using integer arithmetic to extract the m, and modulo arithmetic to extract the cm. Enhance to handle negative P as required.

how to print specific number of digits in c++?For example ,printing 8 digits totally(before+after decimal point combined)

how to print specific number of digits in c++?For example ,printing 8 digits totally(before and after decimal point combined)
Edit: For further clarification, setprecision sets the digits when i have decimal digits to display.I want to display integer 30 also as 30.000000 ,in 8 digits.
The setprecision command puts fixed no. of digits after decimal and i don't want that.
In short , I want an alternative of c command printf("%8d",N) in C++.
You can do it using setprecision() function from include iomanip and fixed like:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double d = 1000;
double t = d;
int dc=0;
while(t>0.9)
{
dc++;
t= t/10;
}
cout<<"dc:"<<dc<<endl;
cout << fixed;
std::cout << std::setprecision(dc);
std::cout << d;
return 0;
}
The setprecision() will not work fine every time So you have to use fixed as well.
You should use the c++ header iomanip what you want is the setprecision() function:
std::cout << std::setprecision(5) << 12.3456789 << std::endl;
outputs 12.346. It also has other modifiers you can find here
EDIT
If you want to print trailing 0s, you need to also use std::fixed. This says to use that number of digits, regardless of whether or not they are significant. If you want that to be the total number, you could figure out the size of the number, then change the precision you set it to based on that, so something like:
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
double input = 30;
int magnitude = 0;
while(input / pow(10, magnitude))
{
++magnitude;
}
std::cout << std::fixed << std::setprecision(8 - magnitude) << input << std::endl;
return 0;
}
This returns 30.000000. You can also do something similar by outputting to a string, then displaying that string.

Use of double in codeblocks gives me int output

#include <iostream> using namespace std;
int main()
{
double x=5.0,y=4.0,z;
z=x+y;
cout<<x<<endl<<y<<endl<<z;
return 0;
}
The above program gives me the following output:
5
4
9
When I have declared the variables to be double and even z as double why do I get the output as integer value(9)??
cout is being helpful here: if the double value is a whole number, then it, by default, does not display a decimal separator followed by an arbitrary number of zeros.
If you want to display as many numbers as the precision that your particular double on your platform has, then use something on the lines of
cout.precision(std::numeric_limits<double>::max_digits10);
cout << fixed << x << endl;
Floating point numbers with no digits after the floating point are printed as integers by default.
To always show the floating point, use setiosflags(ios::showpoint).
You can combine that with fixed and setprecision(n) I/O flags to limit how many digits to print after the floating point. For example:
double d = 5.0;
cout << setiosflags(ios::showpoint) << d << endl; // prints 5.00000
cout << setiosflags(ios::showpoint) << fixed << setprecision(1)
<< d << endl; // prints 5.0

Print one (float) decimal using cout

I have a float number and I want to print one digit after decimal. How can I do this using cout? I have tried the following code but its giving wrong display.
#include <iostream>
using namespace std;
int main()
{
float time = 2.2;
cout.precision(1);
cout << time << endl;
return 0;
}
You need to set tge precision to one and float formatting flags to fixed:
std::cout << std::fixed << std::setprecision(1);
BTW, don't use std::endl. To get a newline use '\n' and if you really mean to flush the stream use std::flush.