Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Best search algorithm to find a condition within a range. Date: Fri, 10 Apr 2015 00:02:27 +0100 Organization: A noiseless patient Spider Lines: 33 Message-ID: <87pp7c29do.fsf@bsb.me.uk> References: <87vbh8yvgv.fsf@bsb.me.uk> <777f7633-724b-442e-864b-d6484c5e87cf@googlegroups.com> <87pp7gysv5.fsf@bsb.me.uk> <2186a589-be06-4453-affc-3c84f9e48379@googlegroups.com> <87egnwymxr.fsf@bsb.me.uk> <878ue3zrws.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="21420"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19k18lI3A9LprkUwFO4NAyiA2TJRrzUy2g=" Cancel-Lock: sha1:HDtNGjraMPvu+t8fkPUAvTozK/A= sha1:7cWl7J2V944Brj7/Iv7hzBKG2tM= X-BSB-Auth: 1.50cd8c00596de6154268.20150410000227BST.87pp7c29do.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:27288 Dr J R Stockton writes: > In comp.lang.javascript message <878ue3zrws.fsf@bsb.me.uk>, Tue, 7 Apr > 2015 19:58:59, Ben Bacarisse posted: > >>If, however, you only want to find the leading digit, there may be a >>better way, but it's likely to use logarithms: >> >> log(3243787...54395)/log(4294967295) = 8.15.. >> floor(3243787...54395 / 4294967295^8) = 28 > > > It seems probable that, with real rather than integer arithmetic, there > will be at least one case in which the small errors in the division and > logarithms will put the value on the wrong side of an integer, in the > opinion of float. > > So either one must test all possible cases or do some rather difficult > reasoning to prove that it always works correctly, or accept the risk of > occasional error. Yes, it's not hard to test both possible powers so the occasional error hardly matters. In fact, one might even use a very crude log function (maybe just take the digit length?), accept the ensuing errors, and test the possible powers. BTW, I make not claim that this is of any value (I can't see how it could be, really) just that it's a way to do what the OP (incorrectly, in my option) claimed to want. -- Ben.