473,748 Members | 9,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double to int weirdness

Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

Outputs:
t=0 j=10.000000 k=-2113929216 h=1103175862
t=1 j=33.000000 k=-1807745024 h=1105006335
t=2 j=8.000000 k=1358954496 h=1102651077
t=3 j=5.000000 k=-1560281088 h=1102130931
t=4 j=23.000000 k=1132462080 h=1104436416
t=5 j=36.000000 k=-444596224 h=1105187835
t=6 j=15.000000 k=746586112 h=1103685495
t=7 j=26.000000 k=1702887424 h=1104606873
t=8 j=18.000000 k=1308622848 h=1104051639
t=9 j=17.000000 k=1602224128 h=1104022361
t=10 j=15.000000 k=-452984832 h=1103710345
t=11 j=1.000000 k=1610612736 h=1097455182
t=12 j=2.000000 k=0 h=1100513581
t=13 j=13.000000 k=1593835520 h=1103471617
t=14 j=36.000000 k=209715200 h=1105179076
t=15 j=29.000000 k=1006632960 h=1104771106
t=16 j=16.000000 k=-1795162112 h=1103811038
t=17 j=30.000000 k=692060160 h=1104803125
t=18 j=8.000000 k=2030043136 h=1102713577
t=19 j=22.000000 k=-29360128 h=1104351131
t=20 j=10.000000 k=1526726656 h=1103155560
t=21 j=25.000000 k=910163968 h=1104551646
t=22 j=7.000000 k=-469762048 h=1102595806
t=23 j=22.000000 k=1937768448 h=1104341221
t=24 j=22.000000 k=-1891631104 h=1104326173
t=25 j=9.000000 k=-1543503872 h=1102952931
t=26 j=27.000000 k=1761607680 h=1104646387
t=27 j=19.000000 k=1140850688 h=1104153565
t=28 j=17.000000 k=-612368384 h=1103948406
t=29 j=35.000000 k=-1153433600 h=1105106370
t=30 j=19.000000 k=1933574144 h=1104187859
t=31 j=26.000000 k=-1363148800 h=1104610710

Jul 28 '07 #1
9 1843
ns*****@gmail.c om wrote:
Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);

#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}
k and h are double, so either cast them to int in printf:

printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);

or change the last two %d to %f:

printf("t=%d j=%f k=%f h=%f",t,j,k,h);
Jul 28 '07 #2
ns*****@gmail.c om wrote:
Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}
When I compile under lcc-win32 I obtain:
d:\lcc\mc68\tes t>lc trand.c
Warning trand.c: 18 printf argument mismatch for format d. Expected int
got double
0 errors, 1 warning

Change the printf format and recompile.

jacob
Jul 28 '07 #3
ns*****@gmail.c om wrote:
Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

Outputs:
t=0 j=10.000000 k=-2113929216 h=1103175862
t=1 j=33.000000 k=-1807745024 h=1105006335
t=2 j=8.000000 k=1358954496 h=1102651077
t=3 j=5.000000 k=-1560281088 h=1102130931
t=4 j=23.000000 k=1132462080 h=1104436416
t=5 j=36.000000 k=-444596224 h=1105187835
t=6 j=15.000000 k=746586112 h=1103685495
t=7 j=26.000000 k=1702887424 h=1104606873
t=8 j=18.000000 k=1308622848 h=1104051639
t=9 j=17.000000 k=1602224128 h=1104022361
t=10 j=15.000000 k=-452984832 h=1103710345
t=11 j=1.000000 k=1610612736 h=1097455182
t=12 j=2.000000 k=0 h=1100513581
t=13 j=13.000000 k=1593835520 h=1103471617
t=14 j=36.000000 k=209715200 h=1105179076
t=15 j=29.000000 k=1006632960 h=1104771106
t=16 j=16.000000 k=-1795162112 h=1103811038
t=17 j=30.000000 k=692060160 h=1104803125
t=18 j=8.000000 k=2030043136 h=1102713577
t=19 j=22.000000 k=-29360128 h=1104351131
t=20 j=10.000000 k=1526726656 h=1103155560
t=21 j=25.000000 k=910163968 h=1104551646
t=22 j=7.000000 k=-469762048 h=1102595806
t=23 j=22.000000 k=1937768448 h=1104341221
t=24 j=22.000000 k=-1891631104 h=1104326173
t=25 j=9.000000 k=-1543503872 h=1102952931
t=26 j=27.000000 k=1761607680 h=1104646387
t=27 j=19.000000 k=1140850688 h=1104153565
t=28 j=17.000000 k=-612368384 h=1103948406
t=29 j=35.000000 k=-1153433600 h=1105106370
t=30 j=19.000000 k=1933574144 h=1104187859
t=31 j=26.000000 k=-1363148800 h=1104610710
Who is to guess what you are doing, if you don't post compilable C code?
For example, if you were writing C, you would not only make a function
of this, and include standard headers, but you would use printf formats
which agree with the data types. Any C compiler would have at least
optional diagnostics, which you should read. If you used printf() in a
normal way, you could not get the results you show. There must be
several relevant topics in the FAQ, but you don't reveal which you
considered.
Jul 28 '07 #4
On Jul 28, 5:45 pm, regis <re...@dil.un iv-mrs.frwrote:
nsa....@gmail.c om wrote:
Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias
/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;
for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

k and h are double, so either cast them to int in printf:

printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);

or change the last two %d to %f:

printf("t=%d j=%f k=%f h=%f",t,j,k,h);
note k is a double, but h is an int. The original format is wrong
for k, but right for h. However, it looks like
both k and h are not being correctly output.
Since the format specifier for k is wrong, it makes
sense that k is not printing correctly, but why is
h not printing correctly.

In general there is no way to know. Once you get
one format specifier wrong you invoke undefined behaviour and
anything can happen.

At a guess the arguments are put in a stack byte by byte
the number of bytes to put is determined by the type
of the argument and the number of bytes to use is determined by the
format
specifier. In this case k is a double, so 8 bytes
are put on the array, however the format specifier is int
so only four bytes are used when k is printed. When h is printed
you get the next four bytes of k, rather than the four bytes
of h. Of course this is only a guess, something else entirely
may be happening, and the 8 and 4 bytes, while common, are hardly
universal. Still, we have one plausible scenario

Try fixing the format specifier and recompiling.
(Worked for me)

Your compiler may be able to warn you about this type of problem.
With gcc 3.2.2 using gcc - Wall gives the warning

int format, double arg (arg 4)

- William Hughes

Jul 28 '07 #5
William Hughes wrote:
On Jul 28, 5:45 pm, regis <re...@dil.un iv-mrs.frwrote:
>>nsa....@gmail .com wrote:
>>>Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias
>> /* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;
>> for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
>> #ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

k and h are double, so either cast them to int in printf:
printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);
or change the last two %d to %f:
printf("t=%d j=%f k=%f h=%f",t,j,k,h);

note k is a double, but h is an int. The original format is wrong
for k, but right for h.
oups!

Jul 28 '07 #6
ns*****@gmail.c om wrote:
Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
#include <stdio.h /* mha: added, needed for printf */
#include <stdlib.h /* mha: added, needed for rand() */
#define SESSIONID_LENGT H 32 /* mha: needed to make compilable code */
#define DEBUG /* mha: added to active printf's */

/* mha: added main, needed to have a program at all */
int main(void)
{
int t, h; /* mha: removed unused variable i */
double j, k, l;

for (t = 0; t < SESSIONID_LENGT H; ++t) {
k = rand();
l = RAND_MAX;
j = (k / l) * 36;
j = abs(j) + 1;
h = j; /* mha: Removed unneeded cast.
Unnecessary casts should not appear
in your code. */
#if 0
/* mha: the above 5 lines are better replaced with */
h = 1 + 36. * rand() / (RAND_MAX + 1.);
#endif
#ifdef DEBUG
/* mha: fixed format "%d" for k, which is a double */
printf("t=%d j=%g k=%g h=%d", t, j, k, h);
printf("\n");
#endif
}
return 0; /* mha: added. Needed for C89, a good
idea for C99 */
}

Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);
#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

Outputs:
t=0 j=10.000000 k=-2113929216 h=1103175862
t=1 j=33.000000 k=-1807745024 h=1105006335
t=2 j=8.000000 k=1358954496 h=1102651077
t=3 j=5.000000 k=-1560281088 h=1102130931
t=4 j=23.000000 k=1132462080 h=1104436416
t=5 j=36.000000 k=-444596224 h=1105187835
t=6 j=15.000000 k=746586112 h=1103685495
t=7 j=26.000000 k=1702887424 h=1104606873
t=8 j=18.000000 k=1308622848 h=1104051639
t=9 j=17.000000 k=1602224128 h=1104022361
t=10 j=15.000000 k=-452984832 h=1103710345
t=11 j=1.000000 k=1610612736 h=1097455182
t=12 j=2.000000 k=0 h=1100513581
t=13 j=13.000000 k=1593835520 h=1103471617
t=14 j=36.000000 k=209715200 h=1105179076
t=15 j=29.000000 k=1006632960 h=1104771106
t=16 j=16.000000 k=-1795162112 h=1103811038
t=17 j=30.000000 k=692060160 h=1104803125
t=18 j=8.000000 k=2030043136 h=1102713577
t=19 j=22.000000 k=-29360128 h=1104351131
t=20 j=10.000000 k=1526726656 h=1103155560
t=21 j=25.000000 k=910163968 h=1104551646
t=22 j=7.000000 k=-469762048 h=1102595806
t=23 j=22.000000 k=1937768448 h=1104341221
t=24 j=22.000000 k=-1891631104 h=1104326173
t=25 j=9.000000 k=-1543503872 h=1102952931
t=26 j=27.000000 k=1761607680 h=1104646387
t=27 j=19.000000 k=1140850688 h=1104153565
t=28 j=17.000000 k=-612368384 h=1103948406
t=29 j=35.000000 k=-1153433600 h=1105106370
t=30 j=19.000000 k=1933574144 h=1104187859
t=31 j=26.000000 k=-1363148800 h=1104610710
Jul 28 '07 #7
regis wrote:
ns*****@gmail.c om wrote:
>Hi,
Converting a double to int by doing h=(int)(j)in the below code
doesn't give the expected result. I'm trying to create 32 random
numbers from 1 to 36. So as you see in the output, I get f.ex. in the
first line j=10.000000 but converted to int is equals h=1103175862
???? what gives? please help, I have a headache now, I read the faq it
doesn't help :-(
Cheers and thanks!,
Tobias

/* SESSIONID_LENGT H = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LEN GTH; ++t) {
k = rand();
l = RAND_MAX;
j = ( k / l ) * 36;
j = abs(j)+1;
h = (int)(j);

#ifdef DEBUG
printf("t=%d j=%f k=%d h=%d",t,j,k,h);
printf("\n");
#endif
}

k and h are double, so either cast them to int in printf:
h is not a double. Using the wrong specifier for k caused printf not to
be able to find h where it was promised to be:
>
printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);
^^^
useless cast
>
or change the last two %d to %f:

printf("t=%d j=%f k=%f h=%f",t,j,k,h);
^^
incorrect specifier
Jul 28 '07 #8
On Jul 28, 11:45 pm, regis <re...@dil.un iv-mrs.frwrote:
k and h are double, so either cast them to int in printf:

printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);

or change the last two %d to %f:

printf("t=%d j=%f k=%f h=%f",t,j,k,h);
Hey thanks! h is an int so I still don't understand why i have to cast
it?? but if I cast both k and h then it works, if I cast only h then
it does not work (somehow k interferes with h?).

Anyway I got it now so thanks.
Tobias

Jul 28 '07 #9
ns*****@gmail.c om wrote:
On Jul 28, 11:45 pm, regis <re...@dil.un iv-mrs.frwrote:
>k and h are double, so either cast them to int in printf:

printf("t=%d j=%f k=%d h=%d",t,j,(int) k,(int)h);

or change the last two %d to %f:

printf("t=%d j=%f k=%f h=%f",t,j,k,h);

Hey thanks! h is an int so I still don't understand why i have to cast
it??
you don't have to.
my mistake, I saw h as a double.
but if I cast both k and h then it works, if I cast only h then
it does not work (somehow k interferes with h?).
yes, because printf is a variadic function.
If you want to understand why k interferes with h,
see Willian Hughes's post.
He answers your question, but replied to me...

Jul 29 '07 #10

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

Similar topics

1
2434
by: (Pete Cresswell) | last post by:
TabControl on the right side of a form with two tabs: Tab #1 contains two subforms that show some dynamic query results. Tab #2 contains a ListView that gets dynamically populated as the user navigates a TreeView on the left side of the form. The first time I load the ListView, if the tab containing it is not selected (i.e. the ListView is not visible) the screen draws the ListView's contents in the upper left portion of the form -...
0
1559
by: Bruce B | last post by:
Hi group, I'm experiencing some extreme weirdness over the last week with IIS related to it's Mappings and how .NET is behaving. I can't explain the behavior as it seems very random. Our app has about 50 file extensions that we map in IIS and handle in our HTTPHandler to catch the reference and then lookup the correct content from a database. In addition, we do some magic in a HTTPModule to rewrite paths for file types we don't...
1
1288
by: VB Programmer | last post by:
My development machine has been working perfectly, writing ASP.NET apps, etc... I just went to open a project and it said: "Visual Studio .NET has detected that the specified web server is not running ASP.NET version 1.1. You will be unable to run ASP.NET Web apps or services." I reran "aspnet_regiis -i " but it still gives me the error. Here's the output:
5
2278
by: Phil Weber | last post by:
I'm attempting to debug an ASP.NET Web application in VS.NET 2003. I'm running the app and the debugger on my local machine (Windows XP Professional). I am logged in as a local administrator. In order to rule out permissions issues, I have set the processModel username to "SYSTEM" in my machine.config. Here's my problem: The ASP.NET app (compiled in debug mode) works correctly, whether I start it with debugging (F5) or without (Ctrl+F5)....
5
1706
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for several users that the directory it is in has, including IUSR_JASMINE (my system is named jasmine). Here is the weird part. In my aps .net code I have the following: String jname = Request.PhysicalApplicationPath + "images\\java_" + fileNum +...
1
1659
by: rhino | last post by:
I've got some positioning problems that I can't figure out. Can anyone help? My website was working fine in IE7 and the current releases of Firefox and Opera so I had a look at it in IE6 and found that it was messed up. The major problem is that in IE6 the index, which was supposed to be on the left, was on the right, OVERLAYING the content on its right side. I finally resolved that problem in a way that allows both versions of IE6 and...
26
2288
by: Prisoner at War | last post by:
Hi, All: I have a JavaScript search engine that always causes MSIE 7 to do a top-of-page security "warning" (that top-of-page-bar, and not an "alert" )...but other websites' JavaScripts do not trigger that...what's going on? When I visit other JavaScript sites there's no warning and the scripts work fine, but mine (it's still under construction, offline) occasions that warning and I have to manually allow MSIE to run JavaScript every...
2
2372
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from one to the other. At no time do I ever modify the original dom object, yet it gets modified.
0
942
by: Dilip | last post by:
I have a weird problem with versioning one of my C# binaries. I have the usual AssemblyVersion/AssemblyFileVersion attributes set. When I use reflector to poke into the compiled binary I can find all the version/company information being set correctly. However I am seeing a confusing issue when I right click on this binary and click the Properties option from the context menu: On XP: the Version tab is altogether absent On Vista:...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
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 we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.