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
122
S
44
G
31
Posts: 1,203
Reputation: 30,257

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
72
S
22
G
288
Posts: 3,862
Reputation: 158,455

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
26
S
3
G
1
Posts: 497
Reputation: 2,324

Post » Mon Nov 21, 2016 11:54 am

@Asmodean's solution is the correct one. Don't execute JS for this.
Scirra Founder
B
414
S
245
G
92
Posts: 25,203
Reputation: 200,347

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
122
S
44
G
31
Posts: 1,203
Reputation: 30,257


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 5 guests