FIAS . Impressum . Privacy

double -> float automatic convertion

Matthias Kretz [please enable javascript to see the address]
Tue May 14 15:42:35 CEST 2013


On Tuesday 14 May 2013 13:27:01 Kulakov, Igor wrote:
> Fit.cxx:109:31: error: no match for 'operator/' in 'st.Station::thick /
> 2.0e+0'
> 
> I understand the reason is that the compiler can't automatically convert
> double to float. Can it be changed somehow?

No, that's part of the language. In C++ only automatic promotion (converting 
from a smaller to a larger type) is specified. E.g. float -> double is 
automatic. But demotion needs to be explicit. Thus, if you want to convert a 
double to a float you need to be explicit.

Thus:
float x = 1 + 2.1;
is interpreted as
* promote int(1) to double(1)
* multiply double(1) * double(2.1)
* convert double(2.1) to float(2.1)

IMHO a good compiler will warn you about that last conversion.

Now with Vc 0.7 you finally get sane type conversion. With 0.6 you got the 
following:
float_v x = int_v(1) * 2.1f;
this was interpreted as:
* promote double(2.1) to int(2)
* promote int(2) to int_v(2)
* multiply int_v(1) * int_v(2)
* convert int_v(2) to float_v(2)

With Vc 0.7 it is now interpreted as:
* promote int_v(1) to float_v(1)
* promote float(2.1) to float_v(2.1)
* multiply float_v(1) * float_v(2.1)
* assign the result (no conversion)

Once you mix float_v and double the C++ rules would require the float to be 
promoted to double. But automatic promotion of float_v to double_v cannot work 
because float_v::Size != double_v::Size. Therefore this automatic promotion is 
disabled and you get the compile error.

Simply go and fix your code to say what you actually wanted, namely
  st.thick / 2.f
instead of
  st.thick / 2.0

Cheers,
	Matthias
-- 
Dipl.-Phys. Matthias Kretz

Phone: +49 69 798 44110
Web:   http://compeng.uni-frankfurt.de/?mkretz

SIMD easy and portable: http://compeng.uni-frankfurt.de/?vc



More information about the Vc-devel mailing list
FIAS . Impressum . Privacy