473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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 1827
ns*****@gmail.com 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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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.com 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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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\test>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.com 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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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.univ-mrs.frwrote:
nsa....@gmail.com 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_LENGTH = 32 */
int i,t,h;
double j,k,l;
for(t=0; t<SESSIONID_LENGTH; ++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.univ-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_LENGTH = 32 */
int i,t,h;
double j,k,l;
>> for(t=0; t<SESSIONID_LENGTH; ++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.com 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_LENGTH 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_LENGTH; ++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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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.com 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_LENGTH = 32 */
int i,t,h;
double j,k,l;

for(t=0; t<SESSIONID_LENGTH; ++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.univ-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.com wrote:
On Jul 28, 11:45 pm, regis <re...@dil.univ-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
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...
0
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...
1
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...
5
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...
5
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...
1
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...
26
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...
2
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.