Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Substract double from integer

I have a question that might save a lot of debugging time for many people...

Given a function:

void my_func(double value)

Is there any difference between the 2 following code lines?

double my_value = 1 - value;

and

double my_value = 1.0 - value;

I.e. given that value is double, if I use 1 - value, can I feel safe that the result will be the correct real number, like when using 1.0 - value?

like image 543
vav Avatar asked Mar 01 '16 22:03

vav


1 Answers

There is no difference. To subtract a double from an int, the int has to be promoted to a double. I personally prefer to use 1.0 because I think that makes it clearer that it's not integer subtraction. But it's purely a style issue.

like image 100
David Schwartz Avatar answered Sep 30 '22 00:09

David Schwartz