CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how div_t and ldiv_t are defined

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
young



Joined: 24 Jun 2004
Posts: 285

View user's profile Send private message

how div_t and ldiv_t are defined
PostPosted: Mon Jul 12, 2004 6:33 am     Reply with quote

I need to get both the quotient and remainder of two long integert division in order to control system flow. does this work? I did not know what is the type ldiv_t, div_t is defined in ccs.

long quotient, remainer, result
result=ldiv(lnum,ldenom);
quotient=result.ldenom;
remainder=result.rem;

like

result=ldiv(1200000000,65536);
quotient=result.ldenom;
remainder=result.rem
Ttelmah
Guest







Re: how div_t and ldiv_t are defined
PostPosted: Mon Jul 12, 2004 6:57 am     Reply with quote

young wrote:
I need to get both the quotient and remainder of two long integert division in order to control system flow. does this work? I did not know what is the type ldiv_t, div_t is defined in ccs.

long quotient, remainer, result
result=ldiv(lnum,ldenom);
quotient=result.ldenom;
remainder=result.rem;

like

result=ldiv(1200000000,65536);
quotient=result.ldenom;
remainder=result.rem

ldiv_t is defined in stdlib.h.
What you show won't work, since a 'long' in CCS, is only a 16bit integer. It also won't work, because you can't use the 'dot' notation without defining the structure.
If you only need to divide by 65536, then remember that given a 'long' (or more importantly an int32, since your arithmetic would need it), is unsigned, you can simply code as:

union {
int32 lword;
int16 word[2];
} value;

value.lword=1200000000L;

Then value.word[0] is this /65536, and value.word[1] is the remainder.

Best Wishes
Guest








PostPosted: Mon Jul 12, 2004 1:12 pm     Reply with quote

Thank you:
you have really sharp eyes. yes, as you pointed out, I realized late also that what I given as an example is not correct. since in ldiv (lnum,ldenom), lnum is long, not int32, ldenom is long; however, I still could not clearly understand the sample you are providing, could you provide a more detail int32 divide function?
Ttelmah
Guest







PostPosted: Mon Jul 12, 2004 2:04 pm     Reply with quote

Anonymous wrote:
Thank you:
you have really sharp eyes. yes, as you pointed out, I realized late also that what I given as an example is not correct. since in ldiv (lnum,ldenom), lnum is long, not int32, ldenom is long; however, I still could not clearly understand the sample you are providing, could you provide a more detail int32 divide function?

I'm not using a divide function.
If you want to divide by 65536, then you only need the top 16bits of the value, and the remainder is the low 16bits of the same value. The union, says that the 'lword' value, is stored into the same memory locations, as the two 16bit array elements. You can just store a number into the 32bit location, and access the two parts as seperate variables. it is faster than any form of multiplication or division.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group