From: Pat on
I know this is pretty basic, but is there any difference between:

double x = 3.0;
double y = x/4.0;

and

double x = 3;
double y = x/4;
?

In other words, if I don't include a decimal point when using a whole
number in a type double expression, as in the second case, does the
compiler assume it's an integer and do a type cast when evaluating it?
If so, does that mean it's more efficient to always include the
decimal point in this situation?

Thanks for the explanation. -Pat
From: Richard Heathfield on
Pat said:

> I know this is pretty basic, but is there any difference between:
>
> double x = 3.0;
> double y = x/4.0;
>
> and
>
> double x = 3;
> double y = x/4;

No.

> In other words, if I don't include a decimal point when using a whole
> number in a type double expression, as in the second case, does the
> compiler assume it's an integer and do a type cast when evaluating it?

No, because x is a double, so x/4 is calculated as a double.

int x = 3;
double y = x / 4;

would result in y taking the value 0, because x is an int, and int / int is
int. But double / int is double.

Sometimes you do want the ratio of two ints, expressed as a double - e.g.
you have i = 3, j = 4, and you want 0.75. Whilst you can use a cast to do
this, many programmers (rightly) frown on casts. The following does the
same thing without a cast:

y = i;
y /= j;


> If so, does that mean it's more efficient to always include the
> decimal point in this situation?

It's clearer, that's all. And if you wanted three divided by four to yield
0.75, adding the decimal point will do the trick: y = 3.0 / 4.0; Actually,
y = 3.0 / 4 or y = 3 / 4.0 will also do the trick, and for the same
reason: int / double and double / int both yield double.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
From: Pat on
Richard Heathfield wrote:
> Pat said:
>
>> I know this is pretty basic, but is there any difference between:
>>
>> double x = 3.0;
>> double y = x/4.0;
>>
>> and
>>
>> double x = 3;
>> double y = x/4;
>
> No.
>
>> In other words, if I don't include a decimal point when using a whole
>> number in a type double expression, as in the second case, does the
>> compiler assume it's an integer and do a type cast when evaluating it?
>
> No, because x is a double, so x/4 is calculated as a double.
>
> int x = 3;
> double y = x / 4;
>
> would result in y taking the value 0, because x is an int, and int / int is
> int. But double / int is double.
>
> Sometimes you do want the ratio of two ints, expressed as a double - e.g.
> you have i = 3, j = 4, and you want 0.75. Whilst you can use a cast to do
> this, many programmers (rightly) frown on casts. The following does the
> same thing without a cast:
>
> y = i;
> y /= j;
>
>
>> If so, does that mean it's more efficient to always include the
>> decimal point in this situation?
>
> It's clearer, that's all. And if you wanted three divided by four to yield
> 0.75, adding the decimal point will do the trick: y = 3.0 / 4.0; Actually,
> y = 3.0 / 4 or y = 3 / 4.0 will also do the trick, and for the same
> reason: int / double and double / int both yield double.
>

Thanks. I appreciate the clarification on this. -Pat
From: Keith Thompson on
Pat <pkelecy@_REMOVETHIS_gmail.com> writes:
> I know this is pretty basic, but is there any difference between:
>
> double x = 3.0;
> double y = x/4.0;
>
> and
>
> double x = 3;
> double y = x/4;
> ?
>
> In other words, if I don't include a decimal point when using a whole
> number in a type double expression, as in the second case, does the
> compiler assume it's an integer and do a type cast when evaluating
> it? If so, does that mean it's more efficient to always include the
> decimal point in this situation?

I'm going to expand a bit on what Richard wrote. If you don't want
all the gory details, feel free to ignore this and rely on Richard's
explanation.

The most important thing to remember (well, one of many important
things to remember) is that the type of an arithmetic expression in C
depends only on the expression itself, not on the context in which it
appears. Once the expression is evaluated, the context can force a
converstion to some appropriate type.

For example, in your first pair of declarations:

double x = 3.0;
double y = x/4.0;

all the expressions (3.0, x, 4.0, x/4.0) are of type double. Both 3.0
and x/4.0 are used to initialize objects of type double. It's all
very straightforward, and no type conversions are needed.

In your second pair of expressions:

double x = 3;
double y = x/4;

3 is of type int. This int value is implicitly converted from int to
double

A particularly stupid compiler might generate code to load the int
value 3 into a register, then convert it from int to double, then
store the result in x. But it's far more likely that the generated
code will perform the equivalent but faster task of assigning the
double value 3.0 to x.

In x/4, x is of type double, and 4 is of type int. There's no
built-in division operator that takes a double and an int as
arguments, so the int operand 4 is promoted to type double. (The
language has a precise set of rules for when and how these promotions
are performed.) So the division operator divides one double value by
another, yielding 0.75, which is used to initialize y.

Both examples yield the same results.

Now let's look at Richard's example:

int x = 3;
double y = x / 4;

Here 3 is of type int, and is used to initialize an object of type
int. In x / 4, x is of type int (not because x was initialized with
an int expression, but because x is declared as type int), and 4 is
also of type int. Since the types already match, no conversion is
done; the fact that the result is *going* to be used to initialize a
double object is not considered. Integer division truncates, so 3 / 4
yields 0. This result is then converted from int to double, and y is
initialized to 0.0.

(In some languages, Pascal in particular, the "/" operator applied to
integer arguments performs floating-point division, and can yield a
fractional result; Pascal uses a separate "DIV" operator for
truncating integer division. C doesn't do that. C's definition that
3 / 4 performs an integer division, yielding 0, may be surprising at
first glance, but it's consistent.)

So, which of these:
double x = 3.0;
double y = x/4.0;
or
double x = 3;
double y = x/4;

is better? I'd vote for the first. Not because it's more efficient
(any decent compiler will generate the same code for both), and not
because it's less work for the compiler (it probably is, but I don't
care), but because it expresses the intent more directly *to the human
reader*. Source code has two audiences, the compiler and human
readers. When I see "x/4", I have to go back to the declaration of x
to determine what kind of division is being done. When I see "x/4.0",
I know it's a floating-point division.

--
Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Scott Seidman on
Keith Thompson <kst-u(a)mib.org> wrote in
news:lnwsjwxqv6.fsf(a)nuthaus.mib.org:

> Source code has two audiences, the compiler and human
> readers.

I've always been told to code for two different people: me, and me a month
from mow.

--
Scott
Reverse name to reply