User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,586 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,624 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1436 | Replies: 3 | Solved
Reply
Join Date: Oct 2006
Posts: 60
Reputation: asilter is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
asilter asilter is offline Offline
Junior Poster in Training

unsigned long division operation

  #1  
Nov 2nd, 2007
unsigned long nBolum = 0L;
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned long) ulA/ulB;
        nKalan = (unsigned long) ulA%ulB;
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        printf("HVFE.c exiting\n");
        exit(1);

it prints :
ulA=2607503366
ulB=16777215
 
nBolum = 65435
nKalan = 7100321
to the screen.

but nBolum must be 155.419.... Why is this happenning?

thanx.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: unsigned long division operation

  #2  
Nov 2nd, 2007
You can't possibly get 155.419 from an unsigned long, there is no decimal point from integers. You need float or double.

And dividing two integers always gives an integer, even if you load it into a floating point number. For example:
  1. double dval;
  2. dval = 25/2;
12.000 is loaded into dval, not 12.5.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2006
Posts: 60
Reputation: asilter is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
asilter asilter is offline Offline
Junior Poster in Training

Re: unsigned long division operation

  #3  
Nov 2nd, 2007
Originally Posted by WaltP View Post
You can't possibly get 155.419 from an unsigned long, there is no decimal point from integers. You need float or double.

And dividing two integers always gives an integer, even if you load it into a floating point number. For example:
  1. double dval;
  2. dval = 25/2;
12.000 is loaded into dval, not 12.5.


i want to get 155 as integer.
Reply With Quote  
Join Date: Oct 2006
Posts: 60
Reputation: asilter is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
asilter asilter is offline Offline
Junior Poster in Training

Re: unsigned long division operation

  #4  
Nov 3rd, 2007
Originally Posted by asilter View Post
unsigned long nBolum = 0L;
/* I have changed this line to : */
/* unsigned int nBolum = 0; */
 
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned long) ulA/ulB;
 
/* I have changed this line to : */
/* nBolum = (unsigned int) ulA/ulB;*/
 
        nKalan = (unsigned long) ulA%ulB;
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        printf("HVFE.c exiting\n");
        exit(1);

it prints :
ulA=2607503366
ulB=16777215
 
nBolum = 65435
nKalan = 7100321
to the screen.

but nBolum must be 155.419.... Why is this happenning?

thanx.


change the line above and nBolum problem is gone. But I also see that nKalan is not being calculated correctly. I have opened another topic for the problem.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC