Solve Integer Power with C++

  • C/C++
  • Thread starter cyber-girl
  • Start date
  • Tags
    C++ Power
In summary: Some people prefer the latter, and so they have to write "using namespace std;" before they can use <cmath> functions. I prefer the former since it keeps things a bit more organized, and I know that I'm using functions from the standard library and not some other library. It's really a matter of preference though.
  • #1
cyber-girl
5
0
Hello everybody!
Here I wrote simple code for power of n number. At result I need to get an integer value. But my code works only small number of power. (Here in my code is p: 10^p). If I increase p number, then the result will be like this: 2.74*10^6=2.7e+006. But I need the result will be integer value: 2740000.
Thanks!
[C++]#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <conio.h>

using namespace std;

int main(){
int p;
double d;
cout<<"Enter any double number: "<<endl;
cin>>d;
cout<<"Enter power of integer number: "<<endl;
cin>>p;

double a = pow((double)10.0 , p);
d=d*a;
cout<<"Result: "<<d;

getch();
return 0;
}
[/C++]
 
Technology news on Phys.org
  • #2
cyber-girl said:
Hello everybody!
Here I wrote simple code for power of n number. At result I need to get an integer value. But my code works only small number of power. (Here in my code is p: 10^p). If I increase p number, then the result will be like this: 2.74*10^6=2.7e+006. But I need the result will be integer value: 2740000.
Thanks!
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <conio.h>

using namespace std;

int main(){
	int p;
	double d;
	cout<<"Enter any double number: "<<endl;
	cin>>d;
	cout<<"Enter power of integer number: "<<endl;
	cin>>p;

	double a = pow((double)10.0 , p);
	d=d*a;
	cout<<"Result: "<<d;

getch();
return 0;
}

Put [ code ] and [ /code ] tags (but without the extra spaces) around your code, not [ c++ + tags.

You can cast a double to an int using a cast operator, like this:
Code:
int result;
double a = pow((double)10.0 , p);
result  = (int)a;

I don't understand this part of your code:
Code:
d=d*a;
Why are you doing this?
 
  • #3
Mark44 said:
You can cast a double to an int using a cast operator, like this:
Code:
int result;
double a = pow((double)10.0 , p);
result  = (int)a;
While this most likely will work, there's no guarantee that calculating 10p via pow() will work, at least not exactly.

If you want an integer power it is much better to use your own integer power function. (Aside: There is another choice, which is to switch back to a language better suited to numerical computation. Sometimes I wonder why so many of us scientists and engineers switched from Fortran to C. Those sometimes are strongly correlated when I have to deal with matrices or when I have to compute ab.)


There are at least two other ways to solve this problem.

1. Don't use C++ I/O. It bites, particularly for output Use C's printf().
Code:
// cout<<"Result: "<<d;
printf ("Result: %f\n", d);

2. Tell the C++ output stream you want the number in fixed format.

Code:
// cout<<"Result: "<<d;
std::cout << "Result: " << std::fixed << d << "\n";
 
  • #4
Thx for your answers. But the result give us with small number of power. The task is put big number of power. For ex, If I put 30 or maybe more than it (10^30), then the result must be with any double number 2.5*10^14=2500000000000000000000000000000. Is it possible to get such kind of number on the console with C/C++ language?
 
  • #5
You may have to use a specialized library for dealing with large integers, or you may have to implement integer multiplication by yourself. Bignum arithmetic is quite often an exercise given to students, so could be that's what you are expected to do - hard to tell without knowing context.
 
  • #6
Borek said:
Bignum arithmetic is quite often an exercise given to students, so could be that's what you are expected to do - hard to tell without knowing context.
Exactly.
 
  • #7
Could you show how works the Bignum or multiple precision arithmetic in the C/C++ code?
 
  • #8
You have to overload the arithmetic operators. You would, for example, define Bignum operator+ (const Bignum & a, const Bignum & b) so you could add one large number such as 1234567890123456789012345678901234567890 to some other very large number.

What exactly is the assignment you were given?
 
  • #9
To display full an integer value, which calculated by this formula: doub_num*(10^num_p). Here doub_num is any double number, num_p is any big interger number (for ex, 25, 49, 102 etc). In the simple case the result of this formula gives us floating point numbers, like this: 25.7e+3. But in my task to convert that floating point numbers to an integer number. But the int number should take full correct value (answer) with very looooong numbers.
 
  • #10
What you wrote doesn't make much sense to me.

cyber-girl said:
But in my task to convert that floating point numbers to an integer number.

In general you can't express a float as an integer. Would it be possible, we would do all calculations using integers, as it is much faster.

You either have a problem expressing the question in English, or you are misunderstanding the problem.
 
  • #11
cyber-girl said:
Could you show how works the Bignum or multiple precision arithmetic in the C/C++ code?

There are many big integer libraries, and creating one that is both flexible and fast.

If you want to use a library for non-commercial purposes, MIRACL is a good library that I have used in the past and it's free for non-commercial use (read the license just to make sure for yourself) and you can download the library.

If you want to create commercial software, you will need to pay them a license though. Also if you want to release your software under an open source license of some sort (non-commercial) you will have to check the license for conditions.
 
  • #12
Code:
#include <math.h>
#include <cmath>
I don' think you need to include cmath and math.h. What I have been told is that cmath is the math library for c++, and math.h is the same thing except for C.
 
  • #13
Welcome to PF, cyber-girl! :smile:


It seems to me that a bignum-library is overkill.
Your assignment does not ask for many different types of calculations - only the basic d*10^p exponential notation.

If I understand correctly, you are not yet doing advanced classes in C++.
So I suspect the solution of DH, using std::fixed to output the number will suffice.
Note that it will indeed output long numbers, although you may get rounding errors if you have more than 15 digits.
Alternatively you could output the number digit for digit with a for-loop up to "p".



jreelawg said:
I don' think you need to include cmath and math.h. What I have been told is that cmath is the math library for c++, and math.h is the same thing except for C.

Yep. The only difference is that <cmath> has all functions in the namespace "std", while <math.h> has all functions in the global namespace (compatible with C).
 
  • #14
The other option is to just never calculate the integer in the first place.
For ex, If I put 30 or maybe more than it (10^30), then the result must be with any double number 2.5*10^14=2500000000000000000000000000000. Is it possible to get such kind of number on the console with C/C++ language?

You don't need to store an integer with value 2500000000000000000000000000000, all you need to do is print a 2, a 5, then 13 zeros
 

1. How do you declare an integer variable in C++?

To declare an integer variable in C++, you use the keyword "int" followed by a variable name of your choice. For example, "int num = 5;" would declare an integer variable named "num" with a value of 5.

2. How can I use C++ to solve integer powers?

C++ offers the built-in function "pow()" to solve integer powers. This function takes two parameters, the base number and the exponent, and returns the result as a double. For example, "pow(2, 3)" would return the value 8.

3. Can I use negative exponents with C++ to solve integer powers?

Yes, you can use negative exponents with the "pow()" function in C++. However, the result will be a decimal number because it is calculated as the reciprocal of the integer power. For example, "pow(2, -2)" would return the value 0.25.

4. Is there a limit to the size of integer powers that can be solved with C++?

Yes, there is a limit to the size of integer powers that can be solved with C++. This limit is determined by the data type used for the base and exponent numbers. For example, if using the "int" data type, the maximum value for an integer power would be 2^31 - 1.

5. Are there any special considerations when solving integer powers with C++?

One consideration when solving integer powers with C++ is the possibility of overflow. This can occur when the result of the operation is larger than the maximum value allowed for the data type, causing unexpected results. To avoid this, you can use a larger data type or check for overflow before performing the calculation.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top