Velocity Reviews > Mixing doubles and integers?

# Mixing doubles and integers?

Johs
Guest
Posts: n/a

 01-03-2007
In the below example a double is initialized with an integer. Afterwards

int mini = 0;
double dd = 7;
double a = mini;
double res = a + dd;

printf("res = %d\n", res);

res equals 0, but why does it not equal 7?

Dik T. Winter
Guest
Posts: n/a

 01-03-2007
In article <engfjt\$gmt\$(E-Mail Removed)-c.dk> Johs <(E-Mail Removed)> writes:
> In the below example a double is initialized with an integer. Afterwards
>
> int mini = 0;
> double dd = 7;
> double a = mini;
> double res = a + dd;
>
> printf("res = %d\n", res);
>
> res equals 0, but why does it not equal 7?

Are you sure the format specifier matches the argument to printf?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

Chris Dollin
Guest
Posts: n/a

 01-03-2007
Johs wrote:

> In the below example a double is initialized with an integer. Afterwards
>
> int mini = 0;
> double dd = 7;
> double a = mini;
> double res = a + dd;
>
> printf("res = %d\n", res);
>
> res equals 0, but why does it not equal 7?

`res` is a double. `%d` format expects an int. Your
code is broken: it will exhibit Undefined Behaviour,
which allows any of: behaving the way you want,
behaving the way /I/ want, behaving the way the
compiler-writer wanted, behaving the way the printf-
writer wanted, behaving the way that "came naturally"
for the code of the compiler and/or printf; such
behaviour including, but not limited to, printing
"rubbish", printing rubbish, printing a helpful
run-time diagnostic, printing a helpful compile-time
diagnostic, printing an /un/helpful run-time diagnostic,
printing nothing but corrupting memory so that a completely
obscure and apparently unrelated misbehaviour happens
later on, ditto but not until an important client
demo, ditto but not until used in a life-critical
application [1], posting your credit card details to
a selection of blogs, wiping your hard drive, and
teleporting Early Spike into your bedroom late at
night. Yes, the Standard doesn't even require Undefined
Behaviour to be physically possible: it doesn't require
anything at all. Hedgehogs may sing.

[1] Of course the code reviews and unit tests will
catch this before production [2].

[2] Nervous giggle.

--
Chris "hopefully not Pyecroft" Dollin
Nit-picking is best done among friends.

mark_bluemel@pobox.com
Guest
Posts: n/a

 01-03-2007

Johs wrote:
> In the below example a double is initialized with an integer. Afterwards
>
> int mini = 0;
> double dd = 7;
> double a = mini;
> double res = a + dd;
>
> printf("res = %d\n", res);
>
> res equals 0, but why does it not equal 7?

You don't know what it equals - you lied to printf() and told it you
were passing an int, then gave it a double...

Note - it's usually better to cut and paste a complete testcase program
(small enough to be postable) rather than just some code snippets like
these.