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
112
S
42
G
31
Posts: 1,188
Reputation: 29,737

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
267
Posts: 3,807
Reputation: 147,745

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: 462
Reputation: 2,187

Post » Mon Nov 21, 2016 11:54 am

@Asmodean's solution is the correct one. Don't execute JS for this.
Scirra Founder
B
405
S
239
G
91
Posts: 24,765
Reputation: 197,354

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
112
S
42
G
31
Posts: 1,188
Reputation: 29,737


Return to How do I....?

Who is online

Users browsing this forum: VictoryX and 27 guests