5

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?

vav
  • 63
  • 6

5 Answers5

11

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Thank you very much for the clear and detailed answer! – vav Mar 01 '16 at 22:56
  • 2
    I think "makes it clearer" is _**very** important_, because _someone_ will come along later and see `1 - value` and be confused. Also, if `double my_value = 1 - value;` is a ways below the `void my_func(double value)` function declaration you may have to look back to see what type `value` actually is to understand the statement. I would _always_ use `1.0 - value`. – Stephen P Mar 02 '16 at 00:01
  • @StephenP, I totally agree with you – vav Mar 04 '16 at 23:14
3

Yes, you assume correctly, but for more complex expressions, you must be very careful about mixing integer and floating point values. For example, the innocent looking code:

double x = 1 / 2;

will store 0 to x because the computation is done on int values and the result is converted to double.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
3

You are correct. The - operator works on objects of the same types. There is an implicit type conversation here, and the int is converted to a double.

Note that this can be source of bugs when mixing signed and unsigned types.

DevShark
  • 8,558
  • 9
  • 32
  • 56
3

In your example both will behave the same way

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?

With 1.0 - value also, you can't ensure it will be correct real number.Check some doc for Floating-Point Arithmetic.

cosmos
  • 2,263
  • 1
  • 17
  • 30
1

If either operand of an arithmetic operator is floating-point, the calculation is done in floating-point arithmetic. The calculation is done in double unless both operands are float, in which case the calculation is done in float.

cwschmidt
  • 1,194
  • 11
  • 17
  • 2
    long double if one of the operands is long double. – gnasher729 Mar 01 '16 at 22:57
  • I don't have enough reputation for up-voting, so I just comment with thanks to all of you who answer me nicely – vav Mar 01 '16 at 22:58
  • @gnasher729 "long double" is only relevant when non SSE instruction is used for extended floating point precision. If you can't control whether the 8087 Coprocessor (80 bit precise) or SSE instructions (64 bit precision) is used you don't know the current precision of the result. If SSE is used "long double" will be the same as "double". – cwschmidt Mar 01 '16 at 23:07
  • @cwschmidt Even on systems where `double` and `long double` have the same implementation, the type promotion still occurs - they are still different types. Select portions of C will produce different results as with `_Generic()` and pointer conversions. – chux - Reinstate Monica Mar 01 '16 at 23:14
  • @chux: That's what I tried to say: If you can't control which one is used e.g. by compiler options, be explicit and don't rely on the correct promotion, because you may get unexpected results. – cwschmidt Mar 01 '16 at 23:22