0
float  FLOAT  = 1.0f;
double DOUBLE = 2.0;

float  a = FLOAT / DOUBLE;
double b = FLOAT / DOUBLE;

Are a and b calculated in the same way?

How are FLOAT and DOUBLE converted in compile?

It seems the default conversion is up-conversion to prevent loss.

Actually I'm doing some calculation on GPU which is precision sensitive and the code should look like this:

float a = FLOAT / 2.0 + 1.0/3.0;

where the code contains very long expression with many numbers and vars (actually generated from Matlab code).

Then how do I control such conversion behavior? Except writing all the numbers in like 2.0f (thousands of numbers in expressions).

CHCl3
  • 1
  • 2
  • That should be covered by [usual arithmetic conversion](http://en.cppreference.com/w/c/language/conversion#Usual_arithmetic_conversions). – Some programmer dude Mar 19 '18 at 12:18
  • You can have a look at expressions implicit type conversion in c, which helps in clearing your doubts – Vasanth Alagiriswamy Mar 19 '18 at 12:36
  • See [Implicit type promotion rules](https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules), scroll down to "the usual arithmetic conversions". (It isn't an exact duplicate of this question, since it mostly speaks of integers.) – Lundin Mar 19 '18 at 12:36

1 Answers1

0

According to the C Standard (6.3.1.8 Usual arithmetic conversions)

Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.

So in the both initializing expressions of the declarations

float  a = FLOAT / DOUBLE;
double b = FLOAT / DOUBLE;

the operand of the type float is converted to the type double. Thus the result of the expressions has the type double. In the first declaration the result further is converted to float because the variable a has the type float.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thanks. so it means I will suffer from the implicit conversion in the SP computation if the numbers are not written in like `2.0f`. Is there another way to control the implicit conversion? – CHCl3 Mar 19 '18 at 12:35
  • @CHCl3 To control an implicit conversion you should use an explicit conversion.:) – Vlad from Moscow Mar 19 '18 at 12:39
  • @CHCl3 The only thing you can do is to cast all operands that aren't float to the intended type. `float a = FLOAT / (float)DOUBLE;`. As you may notice now, using or not `f` for literals can be very important. – Lundin Mar 19 '18 at 12:40
  • @Vlad from Moscow As I mentioned above the expression is very long and such method is not cheap (I have to replace all the numbers to desired precision, maybe thousands of numbers). Is there a way to change the default conversion behavior? – CHCl3 Mar 19 '18 at 12:46
  • @CHCl3 Use a loop and an array of values. – Vlad from Moscow Mar 19 '18 at 13:00
  • @CHCl3 — there certainly isn’t a standard way to change the rules. You might find an option in your compiler’ manual, but there’s nothing in the question to identify the compiler you’re using. – Jonathan Leffler Mar 19 '18 at 14:05