Anyone wanna help me work out some math?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Anyone wanna help me work out some math?

Post by AaronV »

Okay so I'm going to be looping through some number between 0 and 1, adding it over and over until it becomes some whole number.

Probably won't be anything too complicated in reality - something that will work itself out on the first pass of 1 or 2 - but I don't want it to break years down the road when I forget the implementation is a corner cutter, right? I might end up using really obscure fractions like 999/1000 to simulate more natural patterns of what I'm working on, which should have a noticeable impact.

Now the two concerns I have is that I'm working with fractions implicitly, but floats explicitly. Those two don't agree much when you're checking if x == 1.

Again, what I'm trying to do is calculate ahead of time how many steps I would need to hit a nice, whole number.
I'm thinking I should use factorization into primes, eliminate any prime subsets in common, and roll with some kind of least common denominator or whatever it's called? But how would I do this with floats that might have decimals? I guess since I'm working with floats I should just convert everything to a float but it's more of an analog thing than a digital thing so I'm kind of not wanting to go too far into the bits world without at least some really high degree of accuracy approximation.


My algorithm for generating the floats is going to look like this:

step = (Maximum / (Number * Multiplier))

Number should be like 500 but can be 500.01 and I need that to be pretty accurate. At most 15000, at least 20.
Multiplier should be like 6.0 but could again be 6.01 and I need that to be pretty accurate as well. At most 1000, at least .001.
Maximum will always be some large number like 50000, but might be something like 50024.
What have I gotten myself into?
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Anyone wanna help me work out some math?

Post by Xugro »

AaronV wrote:Okay so I'm going to be looping through some number between 0 and 1, adding it over and over until it becomes some whole number.
Take your number (e.g. 0.75), count the decimal places (e.g. 2) and multiply it by ten to the power of the decimal places, to get a whole number (e.g. 0.75 * 10^2 = 75). Now you have a nice fraction (e.g. 75/100) and the factorization into primes is easy, because the denominator ist just two to the power of the decimal places times five to the power of the decimal places (e.g. 100 = 2^2 * 5^2). Just take your number and find the exponent of the primes factors two and five (e.g. 75 = 5^2*2^0*3) and substract these exponents from the exponents of the factorization of the denominator to get the number of steps (e.g. 5^(2-2)*2^(2-0) = 4).

The hardest part is the prime factorization for the primes 2 and 5, but modulo and division are your friends.
AaronV
Prole
Posts: 16
Joined: Sun Jul 08, 2012 4:11 am

Re: Anyone wanna help me work out some math?

Post by AaronV »

Well that sure sounds like it will work, thanks Xugro!

So like if it were .6 I would need to loop it 5 times to get a whole number - 3.0.
.6*((2*5)^1)
2^(1-1==0) * 3^(1-0==1) * 5^(0-1>>0) == 1*3*1 == 3
I think I get the concept.

So do I just have to brute force check for primes then?
That's gonna be funky but I can do it pretty quickly.

And this algorithm is optimal? It looks optimal.
Cool. Thanks.
What have I gotten myself into?
Post Reply

Who is online

Users browsing this forum: No registered users and 225 guests