Jump to content
  • Advertisement

Archived

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

GekkoCube

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!