Is this a bug in mpf_class types?

96 views Asked by At

I came across a strange thing today while playing with the higher precision mpf_class of data type: it seems that any pre-multiplication, more than once, of a complex <mpf_class> object by an mpf_class type is problematic, but single pre-multiplication is fine, as is post-multiplication of these objects by several mpf_class objects:

#include <stlib.h>
#include <complex>
#include <gmpxx.h>

typedef  mpf_class my_float;
const my_float two("2.0",150);  //150 bit prec float

int main( int argc , char **argv )
{      
  mpf_set_default_prec(150); //default prec in bits

  complex<my_float> q1(my_float("5.268E-10"),my_float("8.789541E2"));
  complex<my_float> q2=two*q1;          //no problems
  complex<my_float> q3=q1*two;          //no problems
  complex<my_float> q3b=two*two*two;    //no problems
  complex<my_float> q4=two*q1*two;      //no problems
  complex<my_float> q5=q1*two*two;      //no problems
  //complex<my_float> q6=two*two*q1;    //!doesn't like!
  //complex<my_float> q7=(two*two)*q1;  //!doesn't like!
  complex<my_float> q8=-two*q1;         //!doesn't like!       

return 0;

}

It seem if you pre-multiply a complex of these mpf_class objects more than once you are in trouble. Yet post-multiplication is fine numerous times. Similarly multiplying the mpf_class objects between themselves is also fine as many times as you like.

What is going on here?

1

There are 1 answers

0
headmyshoulder On

std::complex< T > shows well defined behaviour only for T = float, double, or long double. If you use other types the behaviour is undefined.