Pages: 1
  Print  
Author Topic: Help with Maths  (Read 1878 times)
Offline (Male) Goombert
Posted on: February 07, 2014, 12:44:17 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
Can someone help me with something?

I need to generate the following numbers in a combobox, so I don't have to type them out statically.
Code: (C++) [Select]
8, 16, 24, 32
40, 48, 56, 64
80, 96, 112, 128
144, 160, 192, 224,
256, 320, 512

If you notice there is a pattern, every even square of 8 the increment increases by 8. For instance, the numbers increase by 8 up until it hits 64, which is a perfect square of 8, and then it starts adding 16, it continues this pattern.

Just added information is that this is for the audio bit depth in the new sound editor for LGM 1.8.4

Anyway, I wrote the following for loop.
Code: (Java) [Select]
for (int i = 0; i <= 512; i += 8 * Math.sqrt(i)) {
  bitOptions.add(Integer.toString(i));
}

However I only need to increment i by the multiplicative of the lowest square root, so if for instance i is 0 or 8, I need the result to be 1, but when it is 64 I need the result to be 2 * 8. Got any ideas?
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #1 Posted on: February 07, 2014, 12:56:32 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
What you need is a base 8 logarithm of i. For example, log8(8)=1, log8(64)=2 and so on. Sadly, programming languages usually don't provide a general logarithmic function like that. But logx(y) is the same as log(y)/log(x). So you can write it like so:
Code: [Select]
for (int i = 0; i <= 512; i += 8 * Math.floor(Math.log(i)/Math.log(8))) {
  bitOptions.add(Integer.toString(i));
}
But log(0)=-infinity, so you need to make an exception for that. Like:
Code: [Select]
for (int i = 0; i <= 512; i += 8 * (i>0?Math.floor(Math.log(i)/Math.log(8)):1) {
  bitOptions.add(Integer.toString(i));
}
Although I don't know if ternary expressions go in Java.
Also, precomputing log(8) and such is also a smart idea.
« Last Edit: February 07, 2014, 01:03:11 PM by TheExDeus » Logged
Offline (Male) Goombert
Reply #2 Posted on: February 07, 2014, 01:01:24 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
Fuck, the JVM is crapping out when i is 0.
Quote from: Java Console
: 0
: -2147483648
: 0
: -2147483648
: 0
: -2147483648
: 0

Edit: I see your update, actually, the numbers start at 8 so if I just set i = 8; it's all good and gives me the correct numbers! Thank you Harri!
« Last Edit: February 07, 2014, 01:03:52 PM by Robert B Colton » Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #3 Posted on: February 07, 2014, 01:03:26 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Yeah I made a typo in my post. I wanted to say "But log(0)=-infinity".
Logged
Offline (Male) Goombert
Reply #4 Posted on: February 07, 2014, 01:07:16 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile
Wait, shit, it's still not giving me exactly the correct numbers.

Code: (Java) [Select]
                for (int i = 8; i <= 512; i += 8 * Math.floor(Math.log(i)/Math.log(8))) {
                        Debug.getInstance("").println(Integer.toString(i));
                bitOptions.add(Integer.toString(i));
                }

Quote from: Java Console
: 8 16 24 32 40 48 56 64 80 96 112 128 144 160 176 192 208 224 240 256 272 288 304 320 336 352 368 384 400 416 432 448 464 480 496 512

160-176 is supposed to be 160-192

Edit: don't I actually need an inverse log?
« Last Edit: February 07, 2014, 01:11:15 PM by Robert B Colton » Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #5 Posted on: February 07, 2014, 01:28:11 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Quote
160-176 is supposed to be 160-192
Why? The numbers you wrote manually in the OP are actually incorrect. 8^3 = 512, so in the range 64-512 the increment is always 16. Just like the for() loop returns.

Quote
Edit: don't I actually need an inverse log?
No.
Logged
Offline (Male) Goombert
Reply #6 Posted on: February 07, 2014, 01:40:14 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3108

View Profile


But the point is these are supposed to be audio bit rates, I don't think these are all technically usable.
https://en.wikipedia.org/wiki/Bit_rate

Those are the only ones Studio lets you select, that's what those original numbers were.

I see that your code is working correctly though considering the original rules I gave.
« Last Edit: February 07, 2014, 01:47:43 PM by Robert B Colton » Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) TheExDeus
Reply #7 Posted on: February 07, 2014, 02:02:23 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Check what bitrates Java supports. If you want to support only those what GM:S supports, then you will need to manually remove the unnecessary ones.
Logged
Pages: 1
  Print