How to convert a complex<float> variable into a float in C++? - c++

I have a variable in my code that is a complex float. I know that it only has a real part and I just want to type cast it to a float variable.
This should be super simple I feel but you can't just type cast and can't find the answer anywhere after over an hour of searching. Any help would be greatly appreciated!

There really isn't a "default" way to cast from a complex<float> to a float, because there are many interpretations:
Do you want the magnitude of the complex number? (sqrt(re*re + im*im))
Do you want the magnitude squared? This avoids the expensive square root.
Do you want the real portion only?
Some frameworks that do a lot with Digital Signal Processing (such as X-Midas, Midas 2K, PicklingTools) explicitly do not allow such conversions. They make the user pick the appropriate conversion so the user doesn't lose information in the transformation. Having said, if you want just the real component, I feel some of the answers above were fine. Here's a full, standalone program that illustrates:
#include <complex>
#include <iostream>
using namespace std;
int main()
{
complex<float> cf(1.1, 2.2);
float real_portion_only = cf.real();
cout << real_portion_only << endl;
}
The compile line and run looks like:
% g++ comp.cc -o comp
% ./comp
1.1

Related

Using Intel MKL to convert real time domain data to complex frequency and phase data and the inverse as well

I'm writing an audio plugin and I would like to convert a series of real numbers representing sample values into a complex array representing frequency and phase. Then I want to be able to do the opposite, turning a complex array of frequency and phases to a series of real numbers, reconstructing the original data.
I'm using Intel MKL and I see only the possibility to perform real->real conversions or complex->complex conversions. Here's the reference I'm using: Intel MKL FFT Functions.
In that reference, there are two overloaded functions: DftiComputeForward and DftiComputeBackward. So, I would like to use these to do the conversions. In the reference for DftiCreateDescriptor, the only options available for DFTI_FORWARD_DOMAIN are DFTI_COMPLEX and DFTI_REAL, but no option for mixed conversions.
Edit
I found that the phase is actually equal to atan(imaginary/real). I don't want to mislead anyone getting information from questions.
Edit
I just learned that it's best to use atan2(imaginary,real). More information is in the comments.
Every real number is a complex number: ℝ ⊂ ℤ. So going forward from float or double in time domain to complex is trivial. The language does that for you!
#include <complex>
#include <iostream>
int main() {
double d = 42.0;
std::complex<double> z = d;
std::cout << d << " = " << z << '\n';
}
Output: 42 = (42,0)
And the C++ standard library also does everything else. It's quite simple, in fact. For once, the library does pretty much what it says on the box.
Even better: std::complex offers array access. You can reinterpret-cast std::complex<T> to T[2], whether through a reference or a pointer. And thus, std::complex can be "stripped" of its identity and passed into any lower-level API that requires pairs of floats or pairs of doubles.
The complex frequency domain data can be converted to magnitude and phase, and back, as follows:
#include <complex>
#include <iostream>
int main() {
std::complex<double> z{0.7071, 0.7071};
double magnitude = abs(z);
double phase = arg(z); // in radians
std::cout << z << " ≈ (" << magnitude << "∠" << phase*180.0/M_PI << "°)\n";
std::complex<double> z2 = std::polar(magnitude, phase);
std::cout << " ≈ " << z2 << '\n';
}
Output:
(0.7071,0.7071) ≈ (0.99999∠45°)
≈ (0.7071,0.7071)
Once you get the "real" data back, it's not likely that the imaginary part of the time domain data will be zero - it depends on what processing you'll do with the frequency domain data. What you want to convert back is the magnitude of each complex time sample, using the abs function.
There's stuff in the C++ library that's mind-bogglingly overcomplicated, to the point where you have to have the reference open or you won't remember how to use it. See e.g. the mess known as the random number support. Ugh. But complex number support is relatively sane and even follows the notation used in teaching complex number arithmetic :)

Replacing every multiplication with a custom instruction

I have some basic C++ code (included below). I want to be able to replace every multiplication that occurs (including within the cos function included in math.h) with a custom instruction. I am able to directly call the custom instruction within my main, but the 'cos' will still use the old multiplication instruction.
I have considered doing operator overloading, but I was unable to make this function properly.
#include <iostream>
#include <string>
#include <math.h>
int main()
{
int a = 2;
int b = 5;
std::cout << "mult: " << a * b << std::endl;
std::cout << "cos: " << cos(b) << std::endl;
return 0;
}
What is the best way to approach implementing this custom instruction in the way I would like?
There is a way to substitute some functionalities without touching your code. It depends, off course, on how you're building your code.
Assuming that you're using Linux you can inject features using LD_PRELOAD.
Here is a nice explanation on how to do that:
https://rafalcieslak.wordpress.com/2013/04/02/dynamic-linker-tricks-using-ld_preload-to-cheat-inject-features-and-investigate-programs/
If that does not apply exactly to your case, you can grab some ideas or simply learn this cool feature.
std::cos provides overloads only for the built-in floating point types float, double, long double (any integral type also can be provided, but it's cast to double internally). You cannot overload operators for built-in types. This means that you can't do what you are asking.
The best you can get is to write your own numeric class that overloads the multiplication operator and write your own trigonometric functions for it using your alternative multiplication implementation.

Why doesn't my C++ program calculate more digits of 'e'?

I recently picked up the c++ programming language, and I'm trying to calculate the digits of 'e' for a Calculus Project at school. I'll paste the pgoram that I've written below. It's based on e= lim(1+1/x)^x, as x-> infinity. In this program, I set x=100,000. I also set x=1,000,000 and noticed that the answers are somehow being subjected to a round-off error, instead of becoming longer in length.
Program:
#include <iostream>
#include <math.h>
using namespace std;
long double sum;
int main()
{
long double x=100000;
sum= (pow((1+(1/x)),(x)));
cout<<sum;
}
Any tips/ advice in making it print out more digits would be great. Thanks in advance.
On the first hand long double is limited in the number of digits it can produce, and because of how the real numbers are implemented it won't produce exact results.
But, to answer your question you can set cout's precision by doing
cout.precision(15);
cout << sum;
Also see this answer for more explanations and details see
How do I print a double value with full precision using cout?
Double in c++ is a floating point number. For accurate calculation like this you need use decimal number.
See this answer about decimal in cpp

C++ calculating more precise than double or long double

I'm teaching myself C++ and on this practice question it asks to write code that can calculate PI to >30 digits. I learned that double / long double are both 16 digits precise on my computer.
I think the lesson of this question is to be able to calculate precision beyond what is available. Therefore how do I do this? Is it possible?
my code for calculating Pi right now is
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main(){
double pi;
pi = 4*atan(1.0);
cout<<setprecision(30)<<pi;
return 0;
}
Output is to 16 digits and pi to 30 digits is listed below for comparison.
3.1415926535897931
3.141592653589793238462643383279
Any suggestions for increasing precision or is this something that won't matter ever? Alternatively if there is another lesson you think I should be learning here feel free to offer it. Thank you!
You will need to perform the calculation using some other method than floating point. There are libraries for doing "long math" such as GMP.
If that's not what you're looking for, you can also write code to do this yourself. The simplest way is to just use a string, and store a digit per character. Do the math just like you would do if you did it by hand on paper. Adding numbers together is relatively easy, so is subtracting. Doing multiplication and division is a little harder.
For non-integer numbers, you'll need to make sure you line up the decimal point for add/subtract...
It's a good learning experience to write that, but don't expect it to be something you knock up in half an hour without much thought [add and subtract, perhaps!]
You can use quad math, builtin type __float128 and q/Q suffixes in GCC/clang.
#include <stdio.h>
#include <quadmath.h>
int main ()
{
__float128 x = strtoflt128("1234567891234567891234567891234566", nullptr);
auto y = 1.0q;
printf("%.Qf", x + y); // there is quadmath_snprintf, but this also works fine
return 0;
}

Division in C++ not working as expected

I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0.
#include <iostream>
int main(int argc, char** argv)
{
double f=3/5;
std::cout << f;
return 0;
}
What am I missing?
You are missing the fact that 3 and 5 are integers, so you are getting integer division. To make the compiler perform floating point division, make one of them a real number:
double f = 3.0 / 5;
It doesn't need to be .0, you can also do 3./5 or 3/5. or 3e+0 / 5 or 3 / 5e-0 or 0xCp-2 / 5 or... There only needs to be an indicator involved so that the compiler knows it's supposed to perform the division as floating point.
Another possibility: double f=double(3)/5. That's much more typing, but it leaves no doubt to what you are doing.
Or simply use double f=.6, that also does the trick...
try this:
double f = 3.0/5.0;
this should fix your problem
Try putting a .0 after one of the divisors. This will convert them into floating point literals.
You are using integers. You can many things to make your constants double like leftaroundabout states, however that is not good clean good. It is hard to read and confusing. If you want 3 and 5 make them 3.0 and 5.0. Everyone will know what you mean if they are forced to read your code. Much of what he/she states really requires you to know C/C++ and how floats are storage to make heads or tails.
In case, you save your generic variables with int and would like to obtain the ratio as double:
using namespace std;
int main()
{
int x = 7;
int y = 4;
double ratio;
ratio = static_cast<double>(x)/static_cast<double>(y);
cout << "ratio =\t"<<ratio<< endl;
}