473,506 Members | 17,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mixing doubles and integers?

In the below example a double is initialized with an integer. Afterwards
another double is added:

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?
Jan 3 '07 #1
3 1385
In article <en**********@news.net.uni-c.dkJohs <sd*@sdf.comwrites:
In the below example a double is initialized with an integer. Afterwards
another double is added:

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/
Jan 3 '07 #2

Johs wrote:
In the below example a double is initialized with an integer. Afterwards
another double is added:

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.

Jan 3 '07 #3
Johs wrote:
In the below example a double is initialized with an integer. Afterwards
another double is added:

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.

Jan 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2214
by: christine | last post by:
Hello, I wanted to create a currency translator with PHP. The code looks like this: <?php $faktor=2.95583; // a double $dm = $euro * $faktor; // $euro is a double variable with the...
10
7080
by: Mad Butch | last post by:
void Test() { float fValue = 0.5678f; // Value is 0.567800 double dValue = (double)fValue; // Value is 0.56779998540878 } Is there anyway I can round a float at 6 positions behind the...
4
1649
by: Graham | last post by:
Can someone point me to an algorithm for sorting double precision values that is stable with duplicates? Thanks, Graham
5
1325
by: Stef | last post by:
Hello I have been programming C now for a couple of months but at this moment I am a bit confused with types like double float etc.. My question wat type does the result have when a double...
25
7821
by: Allan Rydberg | last post by:
hi i'm trying to shift a double, but i'm getting the error message '>>' illegal, left operand double. althought that the manpages say, '>>' and '<<' can be applied for int's only, i was able...
68
6747
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
3
4681
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm...
8
2754
by: arnaudk | last post by:
Hello, I need to get the following string "12332321213,SomeText,3.141592654" into constituent variables: an unsigned long int, a string and a double. So far I have, string line =...
6
1408
by: Adrian20XX | last post by:
Hi, I need to mix pointers and integers, but can't figure out how to do it, so if any caritative soul can help me in here I'll really really appreciate it. How should I cast things in here? ...
2
1828
by: utab | last post by:
Dear all, On a formatted printing operation that I am trying to accomplish for doubles, I have a question. I would like to print a double, d, and its square, d*d, but I would like to make the...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7023
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5617
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.