Advertisement

Divide and get whole number

Started by February 28, 2002 10:58 PM
7 comments, last by Daishim 22 years, 11 months ago
Is there a function that will divide a number by another number and return the amount of times it can be divided into it evenly?

I know only that which I know, but I do not know what I know.
div()
Advertisement
If I understand what you are saying then no. Righting a function to do that wouldn''t be too hard though. Let''s see....

  int Number_Times_Divided(int a, int b){  int counter = 0;  while (1)  {    if (a % b == 0)    {      counter++;      a/=b;    }    else      return counter;  }}  

This function should work if I understand the problem correctly. Hope it helps!!!

Scout

Visit Subversive Systems!!!

--Remember, be quick to listen, slow to speak, and slow to get angry.
ScoutVisit Subversive Systems!!!--Remember, be quick to listen, slow to speak, and slow to get angry.
just do this
  int divisor = 99;    // or whateverint divider = 10;    // or whateverint answer = divisor/divider;// answer = 9 which is what you are looking for  



"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
Of course, if you''re looking at floating point numbers.......

You can just cast the floating point result to an int:

float a = 0.00072314678;
float b = 0.000115233;

(I picked small examples so the int approach would fail without scaling the numbers)

int result = (int)(a/b) = 6

Is that correct? Lets see.

6*115.223 = 0.000691398

The remainder is then 0.00072314678 - 0.000691398 = 0.00003174878

which is less than the number we were diving by.

So, (int)(a/b) is a way to do it for floating point numbers!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
I don''t know of any language where given

int a = 99
int b = 10

a devided by b will not be 9.

Or you could do (a-(a%b))/b in C and get exactly the same thing.
Advertisement
As I said earlier, the function div , which is implemented in the C standard library, and almost every mathematical package for other languages that I can think of, provides the answer you seek. It performs the computation that Graham indicated.

Actually the C version returns a div_type, which stores the quotient and remainder of the division.

Regards,

Timkin
quote:
Original post by Timkin
As I said earlier, the function div , which is implemented in the C standard library, and almost every mathematical package for other languages that I can think of, provides the answer you seek. It performs the computation that Graham indicated.


The one condition being that div takes integer arguments and produces integer results.



Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Some quick asm would do the job.

  inline int div(int a, int b){	__asm	{		mov eax,a		cdq		idiv b	}}inline unsigned int div(unsigned int a, unsigned int b){	__asm	{		mov eax,a		xor edx,edx		div b	}}  

This topic is closed to new replies.

Advertisement