Optimized integer logarithm base2 for BigIntWhat is JavaScript's highest integer value that a number can go to without losing precision?Plot logarithmic axes with matplotlib in pythonLogarithmic sliderHow to sort an array of integers correctlyConvert a string to an integer in JavaScript?How do I check that a number is float or integer?Integer division with remainder in JavaScript?How can I get Google's Closure Compiler to eliminate attributesbigInt to base2How to obtain the amount of bits of a BigInt?
Why is “deal 6 damage” a legit phrase?
Are some indefinite integrals impossible to compute or just don't exist?
Best Ergonomic Design for a handheld ranged weapon
Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?
Adjective for when skills are not improving and I'm depressed about it
Derivative is just speed of change?
Could flaps be raised upward to serve as spoilers / lift dumpers?
Can I shorten this filter, that finds disk sizes over 100G?
Can I say "Gesundheit" if someone is coughing?
Why do MS SQL Server SEQUENCEs not have an ORDER parameter like Oracle?
Why are sugars in whole fruits not digested the same way sugars in juice are?
Is it really a problem to declare that a visitor to the UK is my "girlfriend", in terms of her successfully getting a Standard Visitor visa?
Should 2FA be enabled on service accounts?
Is there anyway to harden soft braised carrots?
Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?
The Enigma Machine (CLI) in Java
UX writing: When to use "we"?
Why should I use a big powerstone instead of smaller ones?
How to crop this photo of water drops on a leaf to improve the composition?
Why does Beijing's new Daxing airport have so few gates?
Disease transmitted by postage stamps
Being told my "network" isn't PCI Complaint. I don't even have a server! Do I have to comply?
What is my clock telling me to do?
mv Command Deleted Files In Source Directory and Target Directory
Optimized integer logarithm base2 for BigInt
What is JavaScript's highest integer value that a number can go to without losing precision?Plot logarithmic axes with matplotlib in pythonLogarithmic sliderHow to sort an array of integers correctlyConvert a string to an integer in JavaScript?How do I check that a number is float or integer?Integer division with remainder in JavaScript?How can I get Google's Closure Compiler to eliminate attributesbigInt to base2How to obtain the amount of bits of a BigInt?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is the naive algorithm that calculates integer log2(n),
function ilog2(n) // n is a positive non-zero BigInt
const C1 = BigInt(1)
const C2 = BigInt(2)
for(var count=0; n>C1; count++) n = n/C2
return count
// example ilog2(16n)==4
I am testing optimizations (see example below) but they are not "for log2", as commented here.
There are something better using WebAssembly?
PS: if it is not possible a generic solution, to solve my problem I can use functions like ilog2_64bits(n)
, with WebAssembly truncating or ignoring some bits of input BigInt.
non-WebAssembly
Example of not so optimized in pure Javascript (ideal is WebAssembly!)... Trying adaptation of integerLogarithm() of BigInteger.js:
function ilog2_pe(value, base=2n)
// example: ilog2_pe(255n) returns p: 128n, e: 7n
if (base <= value)
let i = ilog2_pe(value, base**2n);
let t = i.p*base
return (t <= value)
? p: t, e: i.e*2n + 1n
: p: i.p, e: i.e*2n
return p: 1n, e: 0n ;
function ilog2_str(n) // 2*faster!
return n.toString(2).length - 1
PS: it is running in modern browsers and last versions of NodeJS.
About performance... ilog2_pe(x64bits)
is 4 times faster tham the naive ilog2()
, ilog2_pe(x1024bits)
is ~9 times faster... Was expected... But:
Any non-WebAssembly solution has so ugly performance, even for 512, 1024, 2048 bits... counting string digits with ilog_str()
is 2 times faster!
For less tham 64 bits is 3 or more times faster.
javascript logarithm
add a comment |
This is the naive algorithm that calculates integer log2(n),
function ilog2(n) // n is a positive non-zero BigInt
const C1 = BigInt(1)
const C2 = BigInt(2)
for(var count=0; n>C1; count++) n = n/C2
return count
// example ilog2(16n)==4
I am testing optimizations (see example below) but they are not "for log2", as commented here.
There are something better using WebAssembly?
PS: if it is not possible a generic solution, to solve my problem I can use functions like ilog2_64bits(n)
, with WebAssembly truncating or ignoring some bits of input BigInt.
non-WebAssembly
Example of not so optimized in pure Javascript (ideal is WebAssembly!)... Trying adaptation of integerLogarithm() of BigInteger.js:
function ilog2_pe(value, base=2n)
// example: ilog2_pe(255n) returns p: 128n, e: 7n
if (base <= value)
let i = ilog2_pe(value, base**2n);
let t = i.p*base
return (t <= value)
? p: t, e: i.e*2n + 1n
: p: i.p, e: i.e*2n
return p: 1n, e: 0n ;
function ilog2_str(n) // 2*faster!
return n.toString(2).length - 1
PS: it is running in modern browsers and last versions of NodeJS.
About performance... ilog2_pe(x64bits)
is 4 times faster tham the naive ilog2()
, ilog2_pe(x1024bits)
is ~9 times faster... Was expected... But:
Any non-WebAssembly solution has so ugly performance, even for 512, 1024, 2048 bits... counting string digits with ilog_str()
is 2 times faster!
For less tham 64 bits is 3 or more times faster.
javascript logarithm
add a comment |
This is the naive algorithm that calculates integer log2(n),
function ilog2(n) // n is a positive non-zero BigInt
const C1 = BigInt(1)
const C2 = BigInt(2)
for(var count=0; n>C1; count++) n = n/C2
return count
// example ilog2(16n)==4
I am testing optimizations (see example below) but they are not "for log2", as commented here.
There are something better using WebAssembly?
PS: if it is not possible a generic solution, to solve my problem I can use functions like ilog2_64bits(n)
, with WebAssembly truncating or ignoring some bits of input BigInt.
non-WebAssembly
Example of not so optimized in pure Javascript (ideal is WebAssembly!)... Trying adaptation of integerLogarithm() of BigInteger.js:
function ilog2_pe(value, base=2n)
// example: ilog2_pe(255n) returns p: 128n, e: 7n
if (base <= value)
let i = ilog2_pe(value, base**2n);
let t = i.p*base
return (t <= value)
? p: t, e: i.e*2n + 1n
: p: i.p, e: i.e*2n
return p: 1n, e: 0n ;
function ilog2_str(n) // 2*faster!
return n.toString(2).length - 1
PS: it is running in modern browsers and last versions of NodeJS.
About performance... ilog2_pe(x64bits)
is 4 times faster tham the naive ilog2()
, ilog2_pe(x1024bits)
is ~9 times faster... Was expected... But:
Any non-WebAssembly solution has so ugly performance, even for 512, 1024, 2048 bits... counting string digits with ilog_str()
is 2 times faster!
For less tham 64 bits is 3 or more times faster.
javascript logarithm
This is the naive algorithm that calculates integer log2(n),
function ilog2(n) // n is a positive non-zero BigInt
const C1 = BigInt(1)
const C2 = BigInt(2)
for(var count=0; n>C1; count++) n = n/C2
return count
// example ilog2(16n)==4
I am testing optimizations (see example below) but they are not "for log2", as commented here.
There are something better using WebAssembly?
PS: if it is not possible a generic solution, to solve my problem I can use functions like ilog2_64bits(n)
, with WebAssembly truncating or ignoring some bits of input BigInt.
non-WebAssembly
Example of not so optimized in pure Javascript (ideal is WebAssembly!)... Trying adaptation of integerLogarithm() of BigInteger.js:
function ilog2_pe(value, base=2n)
// example: ilog2_pe(255n) returns p: 128n, e: 7n
if (base <= value)
let i = ilog2_pe(value, base**2n);
let t = i.p*base
return (t <= value)
? p: t, e: i.e*2n + 1n
: p: i.p, e: i.e*2n
return p: 1n, e: 0n ;
function ilog2_str(n) // 2*faster!
return n.toString(2).length - 1
PS: it is running in modern browsers and last versions of NodeJS.
About performance... ilog2_pe(x64bits)
is 4 times faster tham the naive ilog2()
, ilog2_pe(x1024bits)
is ~9 times faster... Was expected... But:
Any non-WebAssembly solution has so ugly performance, even for 512, 1024, 2048 bits... counting string digits with ilog_str()
is 2 times faster!
For less tham 64 bits is 3 or more times faster.
javascript logarithm
javascript logarithm
edited Mar 26 at 23:01
Peter Krauss
asked Mar 26 at 10:43
Peter KraussPeter Krauss
5,94311 gold badges90 silver badges193 bronze badges
5,94311 gold badges90 silver badges193 bronze badges
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355184%2foptimized-integer-logarithm-base2-for-bigint%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55355184%2foptimized-integer-logarithm-base2-for-bigint%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown