Nth power of m for big numbers - c++

There will be two numbers in the input file which are between 1 ≤ n, m < 100.
I should display m to power n. When i use pow(x,y) function it cannot calculate big integers for ex ::: 12 to power 23 normally should show 6624737266949237011120128, but my code displays negative number. Can anyone solve this exercise ?

This is not very difficult to do without using external libraries. Store the digits of the number in a vector and multiply digit by digit (like you do in paper).
Example:
power(12,23):
Store as start->1->2->end
step 1 result: start->1->4->4->end
step 2 result: start->1->7->2->8->end
and so on...

Can you show your source code?
Use double is enough to store the result
Below is my test code for you reference:
#include <iostream>
#include <cmath>
using namespace std;
int main (int argc, char *argv[])
{
double result = pow (12, 23);
cout.precision (26);
cout << "Result: " << result << endl;
}

Try to store your base in a long double before calling pow :
long double base = 12;
long double result = pow(base, 23);
It is not required since C++11 though, you can get a good approximation like this :
#include <iomanip>
#include <iostream>
#include <cmath>
int main ()
{
std::cout << std::fixed << std::setprecision(0) << pow(99, 99) << std::endl;
}
Output :
369729637649726802192985226395427290145296428445515959701359650120802601667133273280053721002700400354392780458116125965728631706472588849812738072765460822138161108630185181415759762204338929270784
But it is an approximation, for instance Python2 code print 99 ** 99 outputs this :
369729637649726772657187905628805440595668764281741102430259972423552570455277523421410650010128232727940978889548326540119429996769494359451621570193644014418071060667659301384999779999159200499899
To get an exact result in C++, you should look at some BigInt libraries.

Related

C++ decimal placement

how would I make it so when i enter 2.785 for the input question the output will display the variable question as 2.79?
I tried using setprecision but for some reason it is not working unless i am doing it wrong
here is the user input question and what it should be:
Enter positive daily growth % (.1 must be entered as 10):
user enters "2.785"
output -> 0.02785
My desired output should look like:
desired output-> 2.79%
Any help is appreciated. I know it may seem simple to others but I have already tried looking online and everything I find just isn't making sense or doesn't work and I dont know what I am doing wrong.
Floating point arithmetic
The reason why it is challenging is that floating point cannot be represented accurately when you perform operations on them. See wikipedia article
It is a very intesting topic, if you have a bit of time, take a look at explanations about floating point and how its representation inside the computer.
If you are looking for the display only (only works for small decimals)
If you are just looking to display a small value you can use below code:
#include <cmath>
#include <iostream>
#include <iomanip>
#include <limits>
#include <sstream>
using namespace std;
string truncateAsString(double n, int precision) {
stringstream ss;
double remainder = static_cast<double>((int)floor((n - floor(n)) * precision) % precision);
ss << setprecision(numeric_limits<double> ::max_digits10 + __builtin_ctz(precision))<< floor(n);
if (remainder)
ss << "." << remainder;
cout << ss.str() << "%" << endl;
return ss.str();
}
int main(void) {
double a = 0.02785;
int precision = 100; // as many digits as you add zeroes. 3 zeroes means precision of 3.
string s = truncateAsString(a*100 + 0.5 / 100, precision);
return 0;
}
Looking for the true value?
Maybe you are looking for true value for your floating point, you can use boost multiprecision library
The Boost.Multiprecision library can be used for computations requiring precision exceeding that of standard built-in types such as float, double and long double. For extended-precision calculations, Boost.Multiprecision supplies a template data type called cpp_dec_float. The number of decimal digits of precision is fixed at compile-time via template parameter.
You need to use a custom library like boost/multiprecision because of the lack of precision for floating points, see my code below:
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
#include <cmath>
#include <iomanip>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
cpp_dec_float_50 truncate(cpp_dec_float_50 n, int precision) {
cpp_dec_float_50 remainder = static_cast<cpp_dec_float_50>((int)floor((n - floor(n)) * precision) % precision) / static_cast<cpp_dec_float_50>(precision);
return floor(n) + remainder;
}
int main(void) {
int precision = 100; // as many digits as you add zeroes. 5 zeroes means precision of 5.
cpp_dec_float_50 n = 0.02785 * 100;
n = truncate(n + 0.5/precision, precision); // first part is remainder, floor(n) is int value truncated.
cout << setprecision(numeric_limits<cpp_dec_float_50> ::max_digits10 + __builtin_ctz(precision)) << n << "%" << endl; // __builtin_ctz(precision) will equal the number of trailing 0, exactly the precision we need!
return 0;
}
Output (both cases)
2.79%
NB: I add 0.5 / precision to the truncate function to force it to act like a rounding.

How to print multiple Zero after the decimal number when I calculate the floating numbers in C++? I am solving URI 1005 problem

How to print multiple Zero after the decimal number when I calculate the floating numbers in C++?
I am Using std::cout<<"MEDIA = "<<std::setprecision(6)<<av; to show 5 digits after decimal number. Actually, I am trying to solve URI 1005 (Average 1) problem.
It's working but it's not working when the calculation result is decimal(like: 5,9,10). Then it's not showing the 5 00000 digits after the decimal number.
This is the code.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float A=0;float B=0;
cin>>A;
cin>>B;
float av=((A*3.5)+(B*7.5))/11;
std::cout<<"MEDIA = "<<std::setprecision(6)<<av<<endl;
return 0;
}
Here not getting the 10.00000 if I enter both input 10.0.
Include std::fixed in your output printing:
#include <iomanip>
std::cout << "MEDIA = " << std::fixed << std::setprecision(6) << av << std::endl;
Live Demo

how to print specific number of digits in c++?For example ,printing 8 digits totally(before+after decimal point combined)

how to print specific number of digits in c++?For example ,printing 8 digits totally(before and after decimal point combined)
Edit: For further clarification, setprecision sets the digits when i have decimal digits to display.I want to display integer 30 also as 30.000000 ,in 8 digits.
The setprecision command puts fixed no. of digits after decimal and i don't want that.
In short , I want an alternative of c command printf("%8d",N) in C++.
You can do it using setprecision() function from include iomanip and fixed like:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double d = 1000;
double t = d;
int dc=0;
while(t>0.9)
{
dc++;
t= t/10;
}
cout<<"dc:"<<dc<<endl;
cout << fixed;
std::cout << std::setprecision(dc);
std::cout << d;
return 0;
}
The setprecision() will not work fine every time So you have to use fixed as well.
You should use the c++ header iomanip what you want is the setprecision() function:
std::cout << std::setprecision(5) << 12.3456789 << std::endl;
outputs 12.346. It also has other modifiers you can find here
EDIT
If you want to print trailing 0s, you need to also use std::fixed. This says to use that number of digits, regardless of whether or not they are significant. If you want that to be the total number, you could figure out the size of the number, then change the precision you set it to based on that, so something like:
#include <iostream>
#include <iomanip>
#include <cmath>
int main()
{
double input = 30;
int magnitude = 0;
while(input / pow(10, magnitude))
{
++magnitude;
}
std::cout << std::fixed << std::setprecision(8 - magnitude) << input << std::endl;
return 0;
}
This returns 30.000000. You can also do something similar by outputting to a string, then displaying that string.

How to separate whole number and decimal number to two different variable

I am trying to separate whole number and decimal number
two int variable
#include <iostream>
#include <iomanip>
#include<math.h>
using namespace std;
int main()
{
double initialAmount = 42800.13;
double fractionalPart = initialAmount - floor(initialAmount);
cout<<int(initialAmount)<<"\n"<<fractionalPart<<"\n";
}
Present output is
42800
0.13
expected output
42800
13
It can be more than two decimal places .
any easy way to do that
Maybe not the best solution, but at least it is simple :)
Store your double initialAmount in a int - which will truncate the decimal part. Then you have the number before the decimal point. Then subtract that from the initial value and you'll be left with just the part after the decimal point. Multiply that by however many powers of 10 that you deem significant and then again store that in a int and you'll have your two values.
As suggested in the comments, it could help to convert the number into a string. Here's one possibility:
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
int main() {
double d = 42800.13;
std::vector<std::string> numbers_s;
std::string s;
std::istringstream numberAsString(std::to_string( d ));
while (getline(numberAsString, s, '.')) {
numbers_s.push_back(s);
}
std::cout << "whole number:" << numbers_s[0] << std::endl;
//[ remove trailing zeros from fractional part:
numbers_s[1].erase(numbers_s[1].find_last_not_of('0') + 1, std::string::npos);
//]
std::cout << "fractional part:" << std::stoi(numbers_s[1]) << std::endl;
}
Error checks should be added to capture cases when the number does not contain a decimal point.

New to C++: having issues maintaining trailing zeros on a decimal value [duplicate]

I see many questions about the precision number for floating point numbers but specifically I want to know why this code
#include <iostream>
#include <stdlib.h>
int main()
{
int a = 5;
int b = 10;
std::cout.precision(4);
std::cout << (float)a/(float)b << "\n";
return 0;
}
shows 0.5? I expect to see 0.5000.
Is it because of the original integer data types?
#include <iostream>
#include <stdlib.h>
#include <iomanip>
int main()
{
int a = 5;
int b = 10;
std::cout << std::fixed;
std::cout << std::setprecision(4);
std::cout << (float)a/(float)b << "\n";
return 0;
}
You need to pass std::fixed manipulator to cout in order to show trailing zeroes.
std::cout.precision(4); tells the maximum number of digits to use not the minimum.
that means, for example, if you use
precision 4 on 1.23456 you get 1.235
precision 5 on 1.23456 you get 1.2346
If you want to get n digits at all times you would have to use std::fixed.
The behavior is correct. The argument specifies the maximum meaningful amount of digits to use. It is not a minimum. If only zeroes follow, they are not printed because zeroes at the end of a decimal part are not meaningful. If you want the zeroes printed, then you need to set appropriate flags:
std::cout.setf(std::ios::fixed, std::ios::floatfield);
This sets the notation to "fixed", which prints all digits.