Use the following template functions:
complex<double>c=u16todbm<complex<double>>(10,32768.0,0.0);
can be compiled without any problems, but
complex<short>c=u16todbm<complex<short>>(10,32768.0,0.0);
If you change to *
or +
in the line,
error:no match for 'operator*'
error:no match for 'operator+'
is unable to compile.
·Called side
template<typename sample_type>
sample_type u16todbm(samp_type a, double add, double mul)
{
// scale offset
a = a*mul;
a = a + add;
return a;
}
·Calling side
int main (intargc, char*args[])
{
complex<short>c=u16todbm<complex>short>>(10,32768.0,0.0);//compilation error
complex<double>c=u16todbm<complex<double>>(10,32768.0,0.0);// Compile through
}
I wonder if complex and double * and + are not defined, but I don't know how to implement them.
Could you please let me know?
inta=1.0;
Because of the language specification (there are several ways to convert floating-point numbers to integers, such as truncation and rounding, implicit conversion is not defined), I think complex<T> and T is an integer type, the four operational types are defined only for implicit conversion to T.I haven't seen the definition of complex, so I guess it defines an implicit translation from T to complex<T> assuming the value of T as real.
From the above, I think the following is appropriate for u16 todbm.
u16 todbm<complex<short>>(10,32768,0);
If you want to specify floating-point numbers, you should define another template function equivalent to u16todbm that explicitly implements conversion to integers, but it looks a little ugly when implemented.
template<typename sample_type, typename cast_type>
samp_type u16 todbm_cast(samp_type a, double add, double mul)
{
a = a*(cast_type)mul;
a = a + (cast_type) add;
return a;
}
I have no C++ environment at hand, so please forgive me if there are any mistakes.
in short
(assuming 16 bit short)
- Add 32768 to indicate that the signed integer overflow is undefined behavior or
- Even if you multiply it by 0.5, the accuracy will decrease.
- When using sqrt()
, sqrt()
, the code that is designed to prevent the accuracy from deteriorating is
They only consider floating-point numbers.
std::complex
specifies only float
double
long double
.
Other types are unspecified or unspecified.
std::complex<short>
behavior is not defined, that is,
- std::complex<short>
is correct, but
- No one has defined the result, i.e.
- You may get the desired result
- You may get disappointing results
std::complex<short>
to cause compilation errors.
I guess gcc doesn't offer that kind of operator on purpose.
If the double, which is mul/add, gives only the real part of the complex number and the imaginary part is zero,
template<typename T>
Tu16 todbm(Ta, double add, double mul) {
a = a*T(mul);
a = a + T(add);
return a;
}
I can do it.
Whether this is meaningful for std::complex<short>
is another matter.
I don't think the result will be as expected due to overflowing or decreasing accuracy in the middle.
int main(){
std::complex<short>c(1,-2);
std::cout<<u16todbm(c,1,2)<<std::endl;// Probably as expected
std::cout<<u16todbm(c,0.5,-0.5)<<std::endl;//probably disappointing
}
I've written something like this on the joke code.I managed to live up to my expectations.
int main(){
std::complex<std::string>a("Hello", "Alice");
std::complex<std::string>b("World", "Bob");
std::cout<<a+b<<std::endl;
}
© 2024 OneMinuteCode. All rights reserved.