mch
mch

Reputation: 105

Proper use of constants in C

I just found a bug in my code which can be simplified to:

float c;
int a,b;
a=5;b=6;
c=(a+b)/2;

In my debugging c has the value 5, i.e. integer division was executed.

This leads me to the question: What is a good practice when using numbers that are not stored in a variable, i.e. constant. In my case, the denominator 2.

When I'm implementing mathematical formulas that carry various constant like this, should I always put .0 at the end to make sure its internally used as a double? Does it speed up the operating time of my program? How much extra work has to be done if a constant has to be converted from int to double?

formulas like

z = 180 * 3.1415 * x  + 5*y^2 - (10*x-y)/(y+x);

where x,y,z are doubles.

So my question is,

Is it "clean code" to write all factors with a .0 at the end ? It does decrease readability of the code quite alot, especially if the formulas are very long. But with this convention, I would prevent errors like the one discribed at the beginning.

Upvotes: 7

Views: 181

Answers (4)

rsonx
rsonx

Reputation: 456

I do it this way

float c;
int a,b;
a=5;b=6;
c=(float)(a+b)/2;

Just cast to whatever you expect it to be. Same goes with double. This way you avoid the trouble of adding .0f and other stuffs and the code remains clean (readable).

float

#include<stdio.h>
int main(){
  double c;
  int a = 20,b = 3;
  c=(float)a/b;
  printf("c:%.10f\n",c);
  return 0;
}

Output:

c:6.6666665077

double

#include<stdio.h>
int main(){
  double c;
  int a = 20,b = 3;
  c=(double)a/b;
  printf("c:%.10f\n",c);
  return 0;
}

Output:

c:6.6666666667

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180181

When I'm implementing mathematical formulas that carry various constant like this, should I always put .0 at the end to make sure its internally used as a double ?

Where indeed you do intend your constants to have type double, then yes, you should use one of the forms for constants of that type. Where those constants have a zero fractional part and are expressed without an exponent, appending .0 is a good way to put them in such a form.

On the other hand, no, you should not blindly slap .0 on all your constants. Just as with variables, you need to pay attention to what type you want each constant to have. Use a corresponding form.

Does it speed up the operating time of my program ?

The types of your constants may affect the correctness of your program, as indeed was your starting point. Where that is the case, relative performance is irrelevant. No matter how fast you do it, it does not help you to perform the wrong computation. On the other hand, where the change from an integer constant to a floating constant does not affect the correctness of the computation, it is probably because the context in which the constant appears requires a conversion anyway. In that event, it is likely that the compiler just inserts a constant of the converted type in the first place. There is then no runtime performance impact.

How much extra work has to be done if a constant has to be converted from int to double ?

At runtime, generally none. Both implicit and explicit conversions of constants will normally be performed at compile time, as discussed above.

Is it "clean code" to write all factors with a ".0" at the end ?

If they are all meant to be doubles? Yes, it's clean. And it can keep you out of trouble if you otherwise have difficulty remembering to pay attention to the types of constants.

It does tend to feel a little overdone to me when I see code that meticulously uses floating constants with integer values in contexts where an implicit integer -> floating-point conversion would be performed anyway, but it's a valid choice that serves a reasonable purpose.

Upvotes: 1

John Bode
John Bode

Reputation: 123458

When I'm implementing mathematical formulas that carry various constant like this, should I always put .0 at the end to make sure its internally used as a double ?

Only if you need to make sure the result is a double. If you want an integer result, then don't append a .0. If you want a float result, append .0f.

Upvotes: 1

dbush
dbush

Reputation: 223739

Constants have a type at compile time, just as variables do. If a numeric constant contains a decimal point and no type suffix then its type is double. If it does not contain a decimal point and no suffix it will be one of the integer types (which one depends on the value of the constant and the range of the relevant types). So there's no speedup or slowdown associated solely with the type of the constant.

When it comes to performing calculations, integer arithmetic tends to be faster than floating point, so as a rule don't use floating point values unless you need to.

Upvotes: 5

Related Questions