This question already has answers here:
Is floating point math broken?
(31 answers)
Why would a c++ double be limiting itself to 5 decimal places?
(1 answer)
c++ long double printing all digits with precision
(1 answer)
Closed 2 years ago.
I've written this code but it is not giving the Exact (Precise) answer. It automatically rounds up the answer. Please take a look at this and help me solve this issue. When I calculate with calculator it shows me different results and when I calculate with my program it show me different results which I think are not Exact, I want exact results. The value I am using for rad1 = 281.531 and rad2 = 118.213. See the attached image. Note: This is the first time I am asking a question online, so please ignore my mistakes.
#include <iostream>
using namespace std;
double circleArea (double);
int main()
{
double rad1,rad2;
double ringarea;
cout << "Please enter the radius of Outer Circle: ";
cin >> rad1;
cout << "Please enter the radius of Inner Circle: ";
cin >> rad2;
ringarea = circleArea(rad1) - circleArea(rad2);
cout << "The area of the Outer Circle is: " << circleArea(rad1) << endl;
cout << "The area of the Inner Circle is: " << circleArea(rad2) << endl;
cout << "The area of the Ring is: " << ringarea << endl;
return 0;
}
double circleArea (double r)
{
double pi = 3.1415926;
return (pi * r * r);
}
Screenshot of Output
Related
This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 1 year ago.
As a newbie in the world of programming, I have to write a bit of C++ code to find the average of two numbers.
However, my code somehow appears to be incorrect. Please take a look at my code:
#include <iostream>
using namespace std;
int main() {
float a, b, average;
cin >> a;
cin >> b;
average = (a+b)/2;
cout << average << endl;
}
however, it says I am wrong because when I input 10 10 it outputs 10 but the system wants me to output 10.00
You need to use some I/O manipulators.
std::setprecision and std::fixed
Example:
#include <iomanip>
#include <iostream>
int main() {
if(float a, b; std::cin >> a >> b) {
float average = (a+b)/2;
std::cout << std::fixed << std::setprecision(2) << average << '\n';
}
}
Like others have said you need to change your cout line to:
cout << fixed << setprecision(2) << average << endl;
Remember that with the <<s you're putting a stream of data (an iostream) into cout. The first piece of data is std::fixed which says "Display floats to a fixed number of decimal places, don't cut off any trailing zeros." And then std::setprecision(2) says "Make that fixed number of decimal places 2." You could use an int variable or another number in place of 2 if you wanted. From there the stream has your average and an endline like before.
Set decimal precision
Sets the decimal precision to be used to format floating-point values on output operations.
#include <iostream>
using namespace std;
#include <iomanip> // std::setprecision
int main() {
float a, b, average;
cin >> a;
cin >> b;
average = (a+b)/2;
cout << fixed << setprecision(2) << average << endl;
}
This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 2 years ago.
Ques:
A student will not be allowed to sit in exam if his/her attendence is less than 75%. Take following input from user Number of classes held Number of classes attended. And print percentage of class attended Is student is allowed to sit in exam or not.
MY_solution :
int main()
{
int nca;
int nch;
int pattend;
cout << "Enter no. of classes held : " << endl;
cinnch;
cout << "Enter no. of classes attended : " << endl;
cinnca;
pattend = (nca / nch * 100);
cout << "Percentage attendence : " << pattend << endl;
if (pattend = 75) {
cout << "Allowed to Give EXAMS :) " << endl;
}
else {
cout << "NOT ALLOWED! Wasted" << endl;
}
}
My_output:
**Enter no. of classes held : 100
Enter no. of classes attended : 50
Percentage attendence : 0
NOT ALLOWED! Wasted**
I'm not getting percentage attendance correct .
Dividing two integers will give an integer result, so in your case 50/100 = 0.
If you want a floating point result, you need to change your variable types to float or double.
I am new to Stack Overflow, and programming in general. I am in a few classes for programming C++ and have come across an assignment I am having a bit of trouble with. This program is supposed to take fahrenheit and convert it to celsius. I have seen other programs, but could not find a duplicate to my particular problem. This is my code.
#include <iostream>
using namespace std;
int main()
{
int fahrenheit;
cout << "Please enter Fahrenheit degrees: ";
cin >> fahrenheit;
int celsius = 5.0 / 9 * (fahrenheit - 32.0);
cout << "Celsius: " << celsius << endl;
return 0;
}
So this is working great on 4 of the 5 tests that are run. It rounds 22.22 to 22 and 4.44 to 4 like it should, but when 0 F is put in, it rounds -17.77 to -17 instead of -18. I have been researching for about an hour and would love some help! Thank you.
Use std::round() instead of relying on the implicit conversion from double to int. Either that, or do not use conversion at all, show the temperature as a double.
EDIT: As others already pointed out, implicit conversion will not round but truncate the number instead (simply cut off everything after the decimal point).
Integers round down implicitly, as do casts to integer types.
Most likely, using a float in place of an int would give the most sane results:
#include <iostream>
using namespace std;
int main()
{
int fahrenheit;
cout << "Please enter Fahrenheit degrees: ";
cin >> fahrenheit;
float celsius = 5.0 / 9 * (fahrenheit - 32.0);
cout << "Celsius: " << celsius << endl;
return 0;
}
To get normal-looking output (fixed-point like "14.25", not scientific with e notation), pass std::fixed to cout before printing the floating point. You can also use cout.precision() to set the number of digits you would like in the output.
If for some other reason you need an int, use std::round() around the right hand of the expression.
When the compiler converts a floating point number to an integer, it doesn't round, it truncates. I.e. it simply cuts of the digits after the decimal point. So your program behaves as it is programmed to do.
int x = 3.99;
int y = std::round(3.99);
std::cout
<< "x = " << x << std::endl
<< "y = " << y << std::endl
;
-->
x = 3
y = 4
C/C++ is not doing floating point round when static_cast<int>-ing a float to an int. If you want to round, you need to call library function std::round()
#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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float a = 3.14159;
double b = 3.14159;
cout.precision(6);
cout << a << endl; //3.14159
cout << b << endl; //3.14159
cout.precision(10);
cout << a << endl; //3.141590118
cout << b << endl; //3.14159
cout.precision(20);
cout << a << endl; //3.141590118408203125
cout << b << endl; //3.1415899999999998826
return 0;
}
Can anyone explain the difference between float and double?
How do we print float/double with dynamic precision?
Assuming I have your definition of dynamic correct something like this should work:
void print(float toPrint, int precision)
{
cout.precision(precision);
cout << toPrint <<endl;
}
cout.precision only changes the precision of the printing, it doesn't actually affect how precise the numbers are. If you print with more digits than your numbers have precision, you will get inaccurate digits.
Of course, cout.precision also only changes the maximum precision of the printing. To force it to print trailing zeros, do something like this:
void print(float toPrint, int precision)
{
cout.precision(precision);
cout << fixed;
cout << toPrint <<endl;
}
The difference between a float and a double is that a double is approximately twice as precise as a float. In general, a float has something like 7 or 8 digits of precision, and a double has 15 or 16 digits of precision.
If I'm reading your question correctly you are wondering why both floats and doubles lose precision after you adjust cout.precision.
This occurs because floating point numbers are stored in binary differently than normal whole numbers. A common example of why this matters is that the number 0.6 is stored in binary as 0011111100101.... This, like 0.6666666... in decimal, is an infinitely long number. Thus, your computer needs to decide at what point it should round/approximate the value. When you declare and initialize your floating point numbers a and b, the computer knows that it does not need to cram any value other than 3.14159 into the variable. However, when you then change cout.precision, the computer thinks it needs to round the floating point at a later location. Furthermore, floats are only 16 bits so it will almost always be less precise than the double, which is 32 bits. See here for their ranges.
Obviously to get the correct precision you shouldn't adjust cout.precision to be greater than the number of digits of your variable. However if you want to adjust the precision and just print out a bunch of zeroes after the end of your initial variable value, just use cout << fixed << setprecision(number). See below:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float a = 3.14159;
double b = 3.14159;
cout.precision(6);
cout << a << endl; //3.14159
cout << b << endl; //3.14159
cout << fixed << setprecision(10);
cout << a << endl; //3.141590118
cout << b << endl; //3.141590000
return 0;
}
Edit: Another option is to use limits.
It doesn't make sense to have a "dynamic precision" where all digits different from 0 are displayed. That mode would have issues with fractional numbers that have infinite decimal digits, like the result of 1.0 / 3.
The best you can do is to set the maximum precision you are willing to see with precision, just like in your example.