C++ full number of degree [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This is a simple code that counts two to the right degree. Starting from somewhere around 60 degrees the answer is incorrect.
I need to count 2^200. The answer shouldn't be in the form like "1.606938e+60", but by numbers. How to do this in C++?
#include <iostream>
using namespace std;
int main()
{
unsigned long long int n,z;
cin>>n;
z=pow(2,n);
cout<<z<<endl;
return 0;
}

You need to use std::set_precision(n) to get it to print in the format that you're expecting it, but if your numbers get high enough, you'll run into a second issue. pow returns a double, which loses precision in a big way with huge numbers. For more information on how to solve that, refer to this Stack Overflow answer.

Anyway you cant print such big number as a single integer or double value in c++ (not yet). Maybe there exist some 256 or 512 machine architectures and implementations which build in types are large enough, but its not possible in common. You probably need to use some data structure to store you number and operate on that.
This, this and this examples may be helpful.

Related

Advanced Power Function in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
during my home task I had to create calculator that includes power function for rational numbers. At that time I wanted to create power function for complex and irrational numbers(something like modern calculator) using only pure c++. But unfortunately I could not find way even to represent complex and irrational numbers. So I'm stuck here.
Hope you can help me))
Edit: Apparently, you can use std::complex to try and solve your problem. Credit to #Pete Becker.
I'm just giving a suggestion here for complex numbers.
First, you can make a struct, where one member stores the constant of the real part, and one member stores the constant beside i in the imaginary part. You can store these as doubles.
i.e.
struct complex_num{
double real_part;
double complex_part;
}
Then, to compute a number raised to the complex number: we can do:
k^(a+bi) = k^a * k^bi
To calculate k^a, you can just use the built-in pow function.
To calculate k^bi, we need to use some more maths.
As you might know, k^bi can be written as (e^ln(k))^bi.
This can be rearranged to get (e^bi*ln(k))
e^((ln(k)*b)i) can be rewritten as cos(ln(k)*b) + i sin(ln(k)*b), which we can call q.
Hence: k^(a+bi) can be rewritten as pow(k,a) * q. This result can be stored in a variable of the type complex_num. I think to do the maths for this, all the functions you need are probably in the builtin maths library for c++.
I haven't got visual studio installed on my laptop so I can't check but I hope this helps.
If this hasn't outputted the right value, please reply to this so I can try and find the error and edit my answer.

difference between two ways of division in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
what is the difference between
int n,s;
cin>>n;
s=n*1/10;
cout<<s;
and
int n,s;
cin>>n;
s=n*10/100;
cout<<s;
?
and also 100/1000 and 1000/10000 and ...
when I enter big numbers for n(such as 1000000000),it has different outputs.
I got my answer!!
the problem is because of order of evaluation and also overflowing in integer
If you have a large number and multiply by 10, the number may overflow. This means that the number that you're trying to store is too big to fit into the memory allocated for that number. The exact consequence for a signed integer overflowing is called "undefined behaviour" meaning that it's up to the compiler to decide how to deal with this.
When you divide by 100, the number you're dividing is now different than the number than you're expecting - thus giving you the wrong result.
This is likely only a problem on debug builds; as the compiler will probably replace the *10/100 to be /10 as part of the optimisation that it will do for a release build.
The other item where you multiply by 1 and then divide by 10 will always be defined however, since the multiplication will leave the number unchanged.

What is the most efficient way to calculate PI in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm have to write a C program, what it does is takes a integer as input. And gives output to the input(th) number of PI after decimal. Like if input is 100, output will be 9(100th digit of pi, after decimal is 9).
What will be the best algorithm for that?
p.s I don't want to save the value of pi into the string, and do it.
Your question is more a math question than a C programming one (so perhaps off-topic). Read first the wikipage on Pi and Approximations of π
If you need to compute only a few hundred (or even hundred thousands) digits, you just need to use some algorithm and code it using some bignum library (e.g. GMPlib, which has mpfr_const_pî ass commented by chtz).
Things become interesting for many billions of digits. I'm not expert on Pi, but look into Fabrice Bellard work on it (read the technical notes mentioning Chudnovsky's algorithm).

Why C++ not have in starting double main() or like that and only int/void main()? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Also how would I write a program to work on a very large number. I want to find the largest prime factor of a number in the range of 600000000000
Because that int main () returns an exit code of the program. If returned 0 - everything is fine. You need to perform calculations in this function because it is the program entry point. If the number is too large advise to use the example of long arithmetic.
int main => return 0; which indicates if the program finished it's running as the programmer expected. Return 2^31 gives one enough numbers for indicating error codes.
For large numbers linked lists can be used.

This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{int primes[521153];
return 0;
}
This works for any numbers smaller than 521153. This way it does not work and returns a very large negative number.
You have exceeded you stack size. The C languages work like "portable assembler", where all language operations map directly onto machine operations, even if those operations cause "undefined behavior". In this case your stack collides with the heap, and your program overwrites its own brains and dies.
Use int *primes = new int[BIG_NUMBER]; IIRC, that's well-defined to raise an exception if it cannot allocate that much memory. And your heap (where new gets its storage) can grow arbitrarily (on modern architectures), where your stack cannot. You C++ tutorial will define all these terms for you.