Pages: 1
Author Topic: Help with Maths  (34,903 Views)
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Posted on: February 07, 2014, 05:44:17 PM
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 (cpp) 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?
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #1 Posted on: February 07, 2014, 05:56:32 PM
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:
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:
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.
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Reply #2 Posted on: February 07, 2014, 06:01:24 PM
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!
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #3 Posted on: February 07, 2014, 06:03:26 PM
Yeah I made a typo in my post. I wanted to say "But log(0)=-infinity".
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Reply #4 Posted on: February 07, 2014, 06:07:16 PM
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?
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #5 Posted on: February 07, 2014, 06:28:11 PM
Quote160-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.

QuoteEdit: don't I actually need an inverse log?
No.
Offline (Unknown gender) Goombert

Developer
Joined: Jan 2013
Posts: 2,991
View profile
Reply #6 Posted on: February 07, 2014, 06:40:14 PM


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.
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #7 Posted on: February 07, 2014, 07:02:23 PM
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.
Pages: 1