About the pow function in C++ [duplicate] - c++

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 6 years ago.
I just tried three pieces of code:
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a);
printf("%d",b);
return 0;
}
//Output:1000
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-1);
printf("%d",b);
return 0;
}
//Output:99
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-2);
printf("%d",b);
return 0;
}
//Output:10
I would like to know why the second block of code will output 99, is it because of floating point precision? Or is it because I should use float numbers in the pow function?(Such as 10.0)
I'm usually confused about the accuracy of C++, I will be grateful for your help.

For integer exponents the following template can be very handy:
template <typename T> inline constexpr T pow( T base, int exponent )
{
return (exponent == 0) ? static_cast<T>(1.0) : ( (exponent>0) ? base*pow(base, exponent-1) : pow( static_cast<T>(1.0)/base, -exponent ) );
}
If you plan to use it with a C++ standard prior C++11, just remove the constexpr keyword.

Conversion of a floating-point value to an integer is done by truncation — you get the next integer closest to zero. If pow is imprecise and too low, then truncation will exacerbate it.
lround(pow(10,2)) might be more appropriate.

Related

Number of permutations calculation in c++ [duplicate]

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!.

Overflowing remainder calculation in C++ [duplicate]

This question already has answers here:
Why is my integer math with std::pow giving the wrong answer?
(5 answers)
Calculating (a^b)%MOD
(7 answers)
Closed 10 months ago.
I want to calculate the below expression:
pow(2012, 17) % 3569
When I do this in Ruby like this, it shows 915.
print (2012 ** 17) % 3569
When I do this in C++ like this, it shows 1190.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << remainder(pow(2012, 17), 3569) << endl;
}
915 looks correct.
Why are they different and how to fix the C++ code?
Thanks to Goswin, I could rewrite the C++ code like this
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int result = 1;
for (int i = 0; i < 17; i++) {
result *= 2012;
result = result % 3569;
}
cout << result << endl;
}
Now it works. Thank you.
Why are they different
Because C++'s pow does floating-point rather than integer math.
In exact integer arithmetic, pow(2012, 17) would be 145,102,735,039,632,235,424,933,403,463,510,202,882,323,568,682,170,580,992. But the standard IEEE 754 double can only approximate this value as 1.4510273503963223e+56, or to be ultra-precise, 145,102,735,039,632,225,543,927,922,730,425,249,428,937,718,149,261,819,904, with only the first 16 sig figs correct. Taking this number modulo 3569 gives 1190, as you have seen.
To get the expected result of 915, you need to use integer arithmetic. I don't think the standard C++ library has a function for that, but it's easy to implement one. Here's a recursive version:
int powmod(int base, int exponent, int modulus)
{
if (exponent == 0)
{
return 1;
}
else if (exponent % 2 == 0)
{
int square_root = powmod(base, exponent / 2, modulus);
return square_root * square_root % modulus;
}
else
{
return powmod(base, exponent - 1, modulus) * base % modulus;
}
}
The else if (exponent % 2 == 0) special-casing of even exponents isn't required, but allows the algorithm to run in logarithmic rather than linear time.

Why a+=b*pow(10,c-i-1) == 99 c++? [duplicate]

This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 4 years ago.
I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100?
#include <iostream>
#include<math.h>
using namespace std;
int main ()
{
int skt = 0;
int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
skt = skt + (sk[i]*pow(10.0,nsk-i-1));
cout <<" "<<skt<<endl;
}
}
the result of this code
99
119
127
,but if I use library cmath it is correct answer
#include <iostream>
#include<cmath>
using namespace std;
int main ()
{
int skt = 0;
int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
skt = skt + (sk[i]*pow(10.0,nsk-i-1));
cout <<" "<<skt<<endl;
}
}
the result of this code
100
120
128
Could anybody explain why?
The math.h and cmath versions of pow are slightly different. Basically there are more supported inputs for the cmath version. You can compare the two at these links.
math.h and cmath
As people have commented, it's probably related to variable type conversion and floating point errors. Pow operates on doubles, which can have floating point errors. Chances are pow is returning a value like 99.9999999 which is then being converted to an integer. Since the integer conversion truncates instead of rounds, you get 99.
from this link cplusplus.com
Additional overloads are provided in this header ( cmath ) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).
I bet if you cast it it would be correct in math.h

Conversion from double to integer [duplicate]

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;
}

output does not meet my expected result, how to fix it? [duplicate]

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;