Register to reply 
C++ power of n number 
Share this thread: 
#1
May412, 03:54 PM

P: 5

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++] 


#2
May412, 04:21 PM

Mentor
P: 21,216

You can cast a double to an int using a cast operator, like this:



#3
May412, 04:35 PM

Mentor
P: 15,065

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 a^{b}.) 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().



#4
May412, 04:58 PM

P: 5

C++ power of n number
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
May412, 05:14 PM

Admin
P: 23,363

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
May412, 05:59 PM

Mentor
P: 15,065




#7
May412, 06:00 PM

P: 5

Could you show how works the Bignum or multiple precision arithmetic in the C/C++ code?



#8
May412, 06:06 PM

Mentor
P: 15,065

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
May412, 06:23 PM

P: 5

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
May512, 01:50 AM

Admin
P: 23,363

What you wrote doesn't make much sense to me.
You either have a problem expressing the question in English, or you are misunderstanding the problem. 


#11
May512, 02:06 AM

P: 4,572

If you want to use a library for noncommercial purposes, MIRACL is a good library that I have used in the past and it's free for noncommercial 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 (noncommercial) you will have to check the license for conditions. 


#12
May512, 11:15 AM

P: 450

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
May512, 11:22 AM

HW Helper
P: 6,187

Welcome to PF, cybergirl!
It seems to me that a bignumlibrary 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 forloop up to "p". 


#14
May512, 05:15 PM

Emeritus
Sci Advisor
PF Gold
P: 4,500

The other option is to just never calculate the integer in the first place.



Register to reply 
Related Discussions  
Taking a number to a complex number power  General Math  3  
What happens when we rise a number to the power of i ?  General Math  6  
Power of complex number  Linear & Abstract Algebra  1  
How to derive a number to the power x (e.g. 2^x)  Calculus  3  
Can Power of a Number Indicates Dimensions ?  General Physics  12 