Advanced Power Function in C++ [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 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.

Related

What does the single quote means in C++ Math programming? [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 3 years ago.
Improve this question
so recently I was reading a book that teach you how to program in C++ and I am at basic math stuff. I'm not a beginner in C++ but I have seen something strange that I can't find on the internet.
It is a math expression: 5*3(6' 4) and it has a single quote on it. I found didn't know what it was so I checked up on the internet for this and I found only that it is referred as prime. But I don't think it solve my problem and I don't know how correct that is.
Thanks in advance.
EDIT: I want to address all people who commented and answered (and those who will to do the same thing)in this post that I HAVE MADE A BIG MISTAKE. The real math expression was: 5*6(6*4) but my friend's book(which I got this expression) was not printed well and it looked like the one I have wrote in the past. I'm really really sorry about this....
C++14 and up allows single quote ' inside integer literals to allow for grouping digits. Thanks to this, 123'456'789 can be used as more readable equivalent to 123456789.
Depending on a country, it may be also used in mathematical (outside of C++) expressions, although Polish people would rather use 123 456 789 and UK people would use a comma AFAIK - 123,456,789. None of these is valid in C++ (or it would yield results far from expected), but it would be reasonable in mathematical text/formula.
Nevertheless, your example still wouldn't compile. There is no operator between 3 and opening bracket (.

C++ full number of degree [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 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.

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

invalid conversion from int to int 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 9 years ago.
Improve this question
I'm trying to make something in WinAPI but I get big loads of errors, corrected most of them but i cant find solution to that one:
for (LPINT i = 1; i <= ilosc; i++)
The compiler shows that error is is the "1".
I would post the entire code but its so unorganized that it would be hard to find this line in it the loop for declares its own variable so i think that I'm using the wrong type of variable (no clue what would be correct)
I changed the names of every variable I have to LP* and it solved almost all of my problems except this one.
Also if someone is well oriented in the topic of WinAPI could you teach me how to declare a variable in a textbox? I want to use textboxes as input sources for the variables so the program would count on numbers I type there? (the most of tutorials is written in very scientific language so I cant clarify myself good)
Also I know how bad at typing I am and that it lacks capitalization, punctuation, etc (I wasn't listening on language lessons in primary school and this mistake keeps showing off so don't rage at me cause I'm fixing it).
LPINT is a long pointer on int. Thats why you cannot assign a 1 to that variable.
LPINT is a typedef for typedef int *LPINT (http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx)
It's a pointer, not an int.

Ideas for storing and operating on huge numbers 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 9 years ago.
Improve this question
I am supposed to build a program for storing and handling huge integers. I know that there are many answers out there but I need ideas that I can implement easily, bearing in mind that I can use any of the basic concepts of C/C++.
How should I go about it?
This is the first time I am asking a question here so please correct me if I am wrong about anything.
Edit: Actually what I wanted to know was how should I go about storing a huge integer... Obviously an array is what comes to mind at first glance but are there any other methods out there at the basic level?
EDIT2: I came across a very nice solution to this problem a while ago, but was just a bit lazy to put it on here. We can use the concept of number systems to deal with huge numbers. We can declare an array that holds the co-efficient of powers of 256, thus obtaining a base 256 system. We can then use fundamental concepts like those of the various number systems to obtain our required results.
Matt McCutchen has a Big Integer Library
If you want to do this yourself his code would be a great starting point. As you can overload arithmetic operators in C++ it is not too difficult to make a new BigInteger class and make this handle any number of bits per integer.
There is also a stack overflow answer to this question: here
I consider this as a question about theory, as such I suggest to browse the internet using the right keywords for documents/articles or to take a sneak peek at libraries that are implementing this feature and are well tested, this projects also tend to offer a mailing list or a forum where developers can communicate, it can be a good place to start writing about this stuff.