How do I use a logarithm with a specific base?

Get help using Construct 2

Post » Sun Nov 20, 2016 3:20 pm

Hi,

I need a logarithm function with the base of 2. I checked the manual and there is only the logarithm with the base of e and 10.
What I'm looking for is something like that

log(base, x) Log to "base" of "x". -> for instance log(2, 2048)

Is there something like that in C2 or do I need to make a own function @Ashley ?

Thank you!
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,136
Reputation: 28,085

Post » Sun Nov 20, 2016 4:12 pm

You can call JavaScript directly, and get a return value:
Browser.ExecJS("Math.log2(3)")

This sample goes out of it's way to show how to pass float values into the script call.

http://www.blackhornettechnologies.com/Construct2Stuff/JavaScriptLog2Sample.capx
ImageImageImage
B
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » Sun Nov 20, 2016 4:35 pm

You can calculate the base with logb(x) = loga(x)/loga(b). With base 2 it would be f(x)=log10(x)/log10(2)
B
24
S
3
G
1
Posts: 451
Reputation: 2,165

Post » Mon Nov 21, 2016 11:54 am

@Asmodean's solution is the correct one. Don't execute JS for this.
Scirra Founder
B
397
S
236
G
88
Posts: 24,422
Reputation: 194,558

Post » Mon Nov 21, 2016 4:52 pm

Asmodean wrote:You can calculate the base with logb(x) = loga(x)/loga(b). With base 2 it would be f(x)=log10(x)/log10(2)


Thank you :) I recall that from the math course at school as well. Thanks for the hint.
Image Image iTunes: Match3 | Google Play: Match3
B
100
S
35
G
30
Posts: 1,136
Reputation: 28,085


Return to How do I....?

Who is online

Users browsing this forum: AllanR, BackendFreak and 33 guests