#### Archived

This topic is now archived and is closed to further replies.

# integer to fraction conversion.

This topic is 5960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

this seems like a trivial problem, but i cant think of a good solution to this... Lets say I divide 240 by 32 then say I mod 240 by 32 i get two answers, 7 and 5. Now I would like to convert 5 to 0.5 What would be an easy way to do this? If the mod value was 25 then it would have to convert to 0.25 Thanks, edward ~ I''''m a wannabe programmer ~

##### Share on other sites
exp = 1+(int)(log(x)/log(10));
x = (float)x / pow(10,exp);

Edit: check if x != 0 first, otherwise log will barf.

Edited by - Fruny on January 29, 2002 4:31:32 PM

##### Share on other sites
whoaw.
this is not trivial at all, atleast not in my humble opinion.
i''ll try this out, meantime, i hope somebody will post other cool ideas for this conversion.

thanks anyways

~ I''''m a wannabe programmer ~

##### Share on other sites
if the above works then don''t read this

but you could also just keep dividing by 10 until you result is less than 1...

##### Share on other sites
quote:
Original post by GekkoCube
this is not trivial at all, atleast not in my humble opinion.

I assure you it is.

What it does is find the smallest power of 10 larger than your number and divide by it. log() is the base-e log function, and what you want is the base-10 log. You have log10(x) = log(x)/log(10). You then add 1 and truncate by casting to an int.

e.g.

x = 5, exp = 1, pow( 10, exp ) = 10, -> x = 0.5
x = 25, exp = 2, pow( 10, exp ) = 100, -> x = 0.25

##### Share on other sites
strange, i tried the log method, and im not getting the right answer.

ill try it again even harder, but to clear things up does this look right?

int mod = 240 % 32;
int exp = 1 + (int)(log(mod)/log(10));
float fraction = (float)(mod/pow(10,exp));
int offset = (int)(32 * fraction);

this looks like what you wrote, but im not getting the right answer (which should be 16 for this case).

~ I''''m a wannabe programmer ~

##### Share on other sites
quote:

int mod = 240 % 32;
int exp = 1 + (int)(log(mod)/log(10));
float fraction = (float)(mod/pow(10,exp));
int offset = (int)(32 * fraction);

Should work. mod = 16. exp = 1 + (int)1.2... = 1+1 = 2

fraction 16/10*10 = 0.16

- seb

##### Share on other sites
just to let you know, 240%32 while equal to 16 does not make the fractional portion 0.16 it makes it 16/32.0f

beyond that, why are you converting the remainder to the greatest number below 1 where the divsor is a power of 10? maybe i am naive but i am seeing very little use for this, unless this is for numbers between 0 and 0.31 that have very little bearing on the remainder?

please explain GekkoCube, or any one else for that matter. oh well, maybe i am looking at things too simply since i dont see how useful the fraction you are creating use the exp method is when dealing with offsets. if any one could explain a good use for this trick, please explain. since i am tucking this away with all those other, crazy math tricks that may be useful someday.

EDIT: silly typo, teach me to trust math from a person asking the question, heh.

Edited by - a person on January 29, 2002 10:25:45 PM

##### Share on other sites
I don''t think youundersand what mod returns.

Mod returns the remainder of division.

240 / 32 = 7
240 % 32 = 16

You take the remainder over the divisor to get the fraction. What this tells you is 240/32 equals 7 and 16/32. Take 7+16/32 and you''ll find that it equals 7.5.

##### Share on other sites
the fastest way to put a decimal in the front of a number:

  int base = 10;while(x > 1) x/=base;// this is 5+ times faster than the fruny

Edited by - evilcrap on January 29, 2002 10:04:46 PM

• 10
• 17
• 9
• 13
• 41