My calculus about mean and standard deviation doesn't work [closed] - c++

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 2 years ago.
Improve this question
let's assume that I have the sum of the integer in my variable mean and the sum of the scarred integer in my variable std. I want to have the mean and the standard deviation with respectively 3 and 4 numbers after the decimals point. But I don't have the expected value at all for the mean and the std :
int main(){
mean = double(int((double(mean)/(n-1))*1000))/1000;
std = double(int(sqrt(double(std)/(n-1) - mean*mean)*10000))/10000;
return 0;
}
I put double(int(....*1000))/1000 to have the 3 numbers after the decimal point.

The first issue was to correct the formulas:
you need to divide by n for the mean, and by n-1 for the variance.
To get exactly the expected number of figures after the decimal point, it is better to
perform the calculations with best possible precision, and then for the final display to combine
<< std::fixed << set precision (3) << ...
With this solution, you get some additional zeros at the end if necessary.
Output:
mean = 1.090
std deviation = 0.0265
The code:
#include <iostream>
#include <cmath>
#include <iomanip>
int main(){
double sum_x = 5.45;
double sum_x2 = 5.9433;
int n = 5;
double mean = sum_x/n;
double variance = sum_x2/n - mean*mean;
variance *= double (n)/(n-1); // correction to get an unbiased estimation
double std_deviation = std::sqrt (variance);
std::cout << "mean = " << std::fixed << std::setprecision(3) << mean << "\n";
std::cout << "std deviation = " << std::fixed << std::setprecision(4) << std_deviation << "\n";
return 0;
}

Related

Circle Area C++ Truncation Calculation Problem [closed]

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 last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I tried to make a console application in C++ that calculates the area of a circle using the equation:
pi times radius squared.
By the way, I use Visual Studio to make console applications.
These are the steps the program takes:
Output some text, like the title, the equation.
Get input from the user for the radius and store it in a double(float) variable.
Use the equation: pi times radius squared. But replace the radius with the input from the user.
Output the results.
Sometimes, an annoying round off error(truncation) sometimes rounds off the decimals and I am frustrated.
Here is my code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
int main() {
double input = NULL;
const double pi = 3.14;
cout << "Enter in your radius: " << input << endl;
cin >> input;
double acc = pi * pow(2, input);
cout << "The area: " << acc << ". Don't forget to add the measurement type! (example: cm, in)" << endl;
}
I used to have a int for the input variable but I changed it to a double(float) variable. Because it is a double(big float), I thought that it would stop the truncation problem. Still the truncation(round off) problem still sometimes occurs.
I wanted it to output a result that is not rounded, and still am getting the round off error
You can use std::setprecision() to force to display a set number of digits, for example:
#include <iomanip>
// ...
double acc = pi * input * input;
int precision = log10(acc) + 7; // 7 = number of decimals + 1
cout << "The area: " << setprecision(precision) << acc << ". Don't forget to add the measurement type! (example: cm, in)" << endl;
Will yield 6 decimal digits.
Ex: for input : 211.123456 will print out
The area: 139959.576934. Don't forget to add the measurement type! (example: cm, in)
Note that the maximum precision for doubles is 19 digits altogether.

Use value before and after the decimal point in c++ float? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
double f;
cin >> f;
cout << f << endl;
int n1 = f;
int diff = f - n1;
cout << diff << endl;
I want the number before and after the decimal point in separate int. (Take the example of double f = 101101.11001 I am getting diff as zero but not the numbers.) I have just started learning C++. Need some guidance.
OP: I want the number before and after the decimal in separate int.
Avoid int n1 = f; as it does not have the range of double. An int cannot encode a fraction without scaling.
Research modf() to break a double into whole number and fractional parts.
double modf(double value, double *iptr);
The modf functions break the argument value into integral and fractional parts, each of which has the same type and sign as the argument. They store the integral part (in floating-point format) in the object pointed to by iptr.
#include <cmath>
double f;
cin >> f;
cout << f << endl;
double whole;
double frac = modf(f, &whole);
cout << whole << endl;
cout << frac << endl;
If code still want to go the int approach. Read as a string and parse, noting the number of characters after the '.'. Then scale the fraction by that count so input "123.00456" becomes 123 and 456/100000
Stick to the decimal data type for operation result, you can use float or double, doesn't matter. The mistake on your code above is you're using int diff as result, remember that int data type convert everything to integers itself meanwhile you wanted it decimal.
double f;
cin >> f;
int n1 = f;
double diff = f - n1;
float diff2 = f - n1;
cout << "Using double : " << diff << endl
<< "Using float : " << diff2 << endl;

best double value limit in c++ [closed]

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 6 years ago.
Improve this question
I am working with matrices in c++ and the type of elements is double. My limit for matrix dimentions is 10,000. I want to know what is the logical value to set as limit for elements, considering the fact that I am doing many sum and multiplication actions on them. I want this number to be as high as possible but without having infinity number problems.
The range of double is
1.7976931348623158e+308
Lets say maximum value of element as X;
for multiplication the maximum answer will be
X*X + X*X + X*X .....10,000 times(maximum row and column count)
i.e
10000*X*X
Therefore
`
1.7976931348623158e+308 = 10000*X*X
1.7976931348623158e+304 = X*X
X ~ 1.7976931348623158e+150
But you will lose precision.
This value is if you are going to multiply once.
You can always use std::numeric_limits.
For example:
#include <limits>
#include <iostream>
int main()
{
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::max() << '\n';
}

How to cout a float/double with dynamic precision? [closed]

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.

Printing floating-point GPA in C++ [duplicate]

This question already has answers here:
Floating point format for std::ostream
(8 answers)
Closed 8 years ago.
my problem is when I am trying to print a floating-point GPA in C++.
It seems like a simple issue, but I can't get it to work. Basically I have a floating point value set to 4.0 for a GPA. However, when I try to print it like this:
cout << gpa << endl;
I get the value of 4. Without the .0 on the end. However, I want the .0 to show up. I have tried setting a precision but with no luck. Any help is appreciated.
You can use std::fixed in conjunction with std::setprecision
#include <iostream> // std::fixed
#include <iomanip> // std::setprecision
int main() {
double gpa = 4.0;
std::cout << std::fixed << std::setprecision(1) << gpa << std::endl;
return 0;
}
// Output is 4.0
#include <iomanip>
...
cout.setf(ios::fixed); // use fixed-point notation
cout.setf(ios::showpoint); // show decimal point
cout.precision(1);
...
cout << gpa << endl;