This question already has answers here:
Why does adding a small float to a large float just drop the small one?
(4 answers)
Closed 2 years ago.
I have a problem with summing two doubles
double a = 1e+020;
double b = -4000;
double res = a + b; //result = 1e+020
Why res is equal to a?
double has not infinite number of significant digits. You can query number of digits in base 10 with std::numeric_limits:
#include <iostream>
#include <limits>
int main(){
std::cout << std::numeric_limits<double>::digits10;
}
Typical output is
15
but you are requesting 16 significant digits.
Related
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 6 months ago.
I am learning c++ and having a bit of trouble with my homework.
We have to write code using the double variable type and use two variables to calculate the number of permutations of the potential team arrangements…
The question specifies that there are 18 people in the group and you want to divide the group into teams of 3 members.
This is my current code:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int fact(int n) {
if (n == 0) return 1;
if (n > 0) return n * fact(n - 1);
}
int main()
{
double n = 18.0;
double r = 3.0;
double answer = fact(n) / (fact(r) * fact(n - r));
cout << “The number of arrangements = “ << answer << endl;
system(“pause”);
return 0;
}
When I run the code I am receiving “The number of arrangements = 1”
This is not the correct answer. Can someone please help me figure out what I am doing wrong?
Thanks!
Your fact function is returning an int, not a double.
Since your factorial function is returning an int, fact(n) is an int, and fact(r) * fact(n - r) is also an int.
Together, they will perform integer division, i.e. floor division, not true division.
Edit: Looked at the code again, I realized the problem wasn't integer division, it was the fact that int overflowed, where as double wouldn't overflow until 171!.
This question already has an answer here:
Why do I get a negative number by multiplying two short ints?
(1 answer)
Closed 2 years ago.
I am coding a C++ program to calculate a math problem. I first write:
int x = 1
int y = 1
int z = 1
int answer = x * y * z
cout << answer << endl;
And when I build and run that, it prints 1. But when I do this:
int x = 1234;
int y = 5243;
int z = 1142;
int answer = x * y * z;
cout << answer << endl ;
It prints out instead a answer, which is 7388502404, but a code I don't understand: "-1201352188".
Could you explain why does this code happens, and how to solve the code?
It's not a code, it's the value as stored in the integer result. You're experiencing integer overflow. An int can only hold values of a certain size, and when you exceed that size you end up with bit patters that aren't what you want but that are still storable in an int. Sometimes those bit patterns are the integer representations of negative numbers which is what's happened here.
You might consider using a larger integer type such as long or long long.
This question already has answers here:
How to truncate a floating point number after a certain number of decimal places (no rounding)?
(3 answers)
Closed 7 years ago.
I want to create a c++ program to calculate the average of 3 positive numbers
where (x,y,z)>0 and (x,y,z)<=10
I have this code:
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
int main()
{
int x,y,z;
cin >> x >> y >> z;
double x1,y1,z1,ma;
x1 = x;
y1 = y;
z1 = z;
if(x>0 && x<=10 && y>0 && y<=10 && z>0 && z<=10)
ma = (x1+y1+z1)/3;
else
return 0;
printf("%.2f" , ma);
return 0;
}
For x=9, y=9 and z=5 the average is 23/3=7.666666666666667 and when I format to 2 decimal places, the result will be 7.67, but I want to appear 7.66 not 7.67.
Please, can someone help me?
Thank you!
Without using other functions, you could do:
double x = (double)((int)(23 * 100 / 3)) / 100.0;
Or even a bit simpler:
double x = (double)(int)(23 * 100 / 3) / 100.0;
The int cast truncates the remaining digits (so there is no rounding).
In C++11 there is also the trunc() function to do just that.
This question already has answers here:
C++: How to round a double to an int? [duplicate]
(5 answers)
round() for float in C++
(23 answers)
Closed 8 years ago.
I am stuck in problem where the double number is not getting properly converted to integer.
In this case->
int x=1000;
double cuberoot=pow(x,(1/(double)3));
int a=cuberoot;
cout<<"cuberoot="<<cuberoot<<endl;
cout<<"a="<<a<<endl;
Output:
cuberoot=10
a=9
Why here a=9 and not 10?
Any solution to this problem??
Also I don't want to round the value..if a=3.67 then it should be converted to 3 only
and not 4.
Because the cuberoot is very close to 10 but not quite 10. std::cout truncates and rounds the number to 10, but a double to integer conversion will strip the decimal which is why a = 9. To solve this problem, you can use std::round():
int a=round(cuberoot);
Try this and see why!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
int x = 1000;
double cube = pow(x, 1.0/3.0);
int a = cube;
cout<<"cube="<< fixed << setprecision(16) << cube<<endl;
cout<<"a="<<a<<endl;
}
This question already has answers here:
How to get fractions in an integer division?
(2 answers)
Closed 9 years ago.
my expected result is average=73.5 ,i have set the type of average as double but it result 73 what's the problem?
#include <iostream>
using namespace std;
int main(){
int x=0;
int total=0;
double average=0;
int counter=0;
cout<<"Question 1"<<endl<<"Enter integer(-100 to end);";
cin>>x;
if (x!=-100)
{
for(;x!=-100;counter++)
{
total=total+x;
cin>>x;
}
average = total/counter;
}
cout<<"The average is:"<<average<<endl;
return 0 ;
}
You're doing integer calculations. Cast one of the integers to double:
average = ((double)total)/counter;
Integer operations yield integers as result. In C and C++ they never yield floating point results. You need to involve a floating point value in the computation, e.g.
average = (1.0 * total) / counter;