Sdk: [ask] how to use log(x) with base number

Created on 23 Sep 2019  路  5Comments  路  Source: dart-lang/sdk

i have a new on dart programming, so i need to use this function on my program to find log() with base number

so for example on php, i used like this
image
PHP log() function
how can i solve this on dart ?

type-question

Most helpful comment

Dart does not currently have an arbitrary base logarithm function, only base e (log). This is most likely inherited from JavaScript, which also only provides these two.

In general, the logarithm at one base, b1, can be found using the logarithm at any other base, b2 as logb1(x) = logb2(x) / logb2(b1).

So, to find the base-10 logarithm of 5 using only the natural logarithm (base e), you can write log(5)/log(10). The constant log(10) is available as ln10, so it can also be written log(5)/ln10.

If it happens often, you can define your own functions:

double logBase(num x, num base) => log(x) / log(base);
double log10(num x) => log(x) / ln10;

All 5 comments

Dart does not currently have an arbitrary base logarithm function, only base e (log). This is most likely inherited from JavaScript, which also only provides these two.

In general, the logarithm at one base, b1, can be found using the logarithm at any other base, b2 as logb1(x) = logb2(x) / logb2(b1).

So, to find the base-10 logarithm of 5 using only the natural logarithm (base e), you can write log(5)/log(10). The constant log(10) is available as ln10, so it can also be written log(5)/ln10.

If it happens often, you can define your own functions:

double logBase(num x, num base) => log(x) / log(base);
double log10(num x) => log(x) / ln10;

Thanks you very much for explanation @lrhn

@lrhn So if i want to chane base to 1000 just make

double log1000 =  log(9000) / log(1000)

That should compute log_1000(9000). You can see that the result changes by 1 when you change the 9000 value by a factor of 1000:

print(log(9000) / log(1000)); // 1.3180808364797751
print(log(9000000) / log(1000)); // 2.318080836479775
print(log(9) / log(1000)); // 0.318080836479775
print(pow(1000, log(9000) / log(1000)); // 9000.000000000011

That is the correct behavior for a base-1000 logarithm (with a little rounding).

Ahh thank you , now i can understand

Was this page helpful?
0 / 5 - 0 ratings