二分法求平方根
Scanner sc = new Scanner(System.in);
Double num = sc.nextDouble();
double left = 0;
double right = num;
double pre = 0; #通过pre跳过死循环
// while((right - left) > 0.000000001){ //也可以通过这里进行精度调节
while (left <= right) {
double mid = (right + left) / 2.0;
if (pre == mid) break;
if (num / mid < mid) {
right = mid;
} else if (num / mid > mid) {
left = mid;
}
pre = mid;
}
System.out.println("二分法:" + pre + " " + pre * pre);
double sqrt = Math.sqrt(num);
System.out.println();
System.out.println("Math.sqrt方法:" + sqrt + " " + sqrt * sqrt);
测试结果
输入:8
二分法:2.82842712474619 7.999999999999998
Math.sqrt方法:2.8284271247461903 8.000000000000002
输入:4567890876
二分法:67586.17370439015 4.567890875999999E9
Math.sqrt方法:67586.17370439017 4.567890876000001E9
二分法会造成死循环,这里通过pre进行判断,跳出。
死循环的原因应该是:精度问题,double的精度为16位
这里进行举例
JDK1.8测试
2.82842712474619 + 2.8284271247461903 = 5.65685424949238
(2.82842712474619 + 2.8284271247461903)/ 2 = 2.82842712474619
所以在输入8的时候一直在死循环。
看别人的牛顿迭代法,精度相差较大代码就不写了
测试结果
sqrt:2.8284271250498643
sqrt*sqrt:8.00000000171784
使用BigDecimal
public static void sqrt() {
Scanner sc = new Scanner(System.in);
Double num = sc.nextDouble();
BigDecimal right = new BigDecimal(num);
BigDecimal left = new BigDecimal(0);
while (left.compareTo(right) < 1) {
BigDecimal mid = (left.add(right)).divide(new BigDecimal(2));
BigDecimal multiply = mid.multiply(mid);
int compare = multiply.compareTo(new BigDecimal(num));
if (compare == 1) {
right = mid;
} else if (compare == -1) {
left = mid;
}
System.out.println(mid+" "+ multiply);
}
}
BigDecimal represents an immutable arbitrary-precision signed decimal number.
BigDecimal表示不可变的任意精度有符号十进制数。
A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point. If less than zero, the unscaled value of the number is multiplied by 10^(-scale).
BigDecimal由一个随机精度整数无标度值和一个32位整数标度组成。如果大于或等于零,则刻度为小数点右侧的位数。如果小于零,则数字的未标度值乘以10^(-scale)。
测试直接死循环
2.828427124746190097603377448419396157139343750753896146353359475981464956924214077700775068655283145470027692461824594049849672111701474425288242994199871662826445331855011185511599901002305564121142940219119943211940549069193724029457034837281778397219104658460968617428642901679525207255990502815979374506793092663617659281241230516704790109491500575519923459671150440675063714022708749206816997694320773799941398009630061088055580632908495646136985873837243161156926223193337426026031237137974474470577018529722498995430843666840857137212029364944154287170974831131413935530744045297089403171760324151694984531445200417116893304291679778788874185318360062277649293631416526020118971740800637296068438979455658128209014527376262747971051223464408049018245540045388225514725456099147621793500803673973673690145159872945812152599388276095130964745799436065360494884125853824971810436200891968430118224049888268345706295621160720674215461836573862942034222336783316345377883951743316430425645903697694417938926772578312576553190527028108454533509823163196668783594273130811097421800140347725190237706034891624885142778730629943759099804393713837926083282755207061419879790795730831985440589888636006152948668572045884823746343568639192783775208335700783511696523459299894753658133808670039193616321377715259848486245702821708183996840995107384478060684995638831404687754339588239687515169514437872027051439846595283866667034828656628788867221119638430344517536436503866778899544713814699102311368228292281161022318060238803565584066569025187037969558359179400284580291638880299637160865251829965325623610268416923269684225591459573682088434637590814630970950831064641507407770051503936817961474300263631108936259604686686436695870636674469315908841706438831453296683464477067240128057931064876953934609442796021170374006966540184312848896451780654541413285902308229310978080631177942151327705232066973750159732881503786003400693052207230473577599830554718899680261888875828249686412668206711251464164811855925543511192504380767398270314344856911728083157733275026105169116479370068372482384248384933098912320143326756094509764020425992870054814593967808996837241159896088383039202626986466765248618913646299207137294723927590009817019968499323582805498003170237476734828623968280064389148170323696679675290716011334317073070589956702089508012066757797881304233636471068272505586328011058947897864034951055064086274196250735518390552688956011679057002048094428005667411074190448735186546545989159218508864746985195199282956495807492867571527433080788680263250708187457920011600593473699554441711218793599722123337386407557094924431390783148824990685066945288329873639015417990899141756464855350609684848446923490024001924329122276891608151226919766306565659008485257834847260387471441826120960351860127474588330386767378496718004152570510922973951954717375521881000056167883869303157515867757358128011460728016770964130629712088277435556131365741927530561852682764315819526747471807825699974183692987166886924765878977952203648575448260106262749444035486556304875244454793092483971144518908139316489602390093651628047826963009285631956565513992199522293128230329614948928685227289967733435628932537531792113257186838668045187208430795778691349440892279399121675972090942972668162387207653698236068623144620335924585505671904504813087396308292558351952462468531744052694504556098435945826790129971739922665955983827057451662767562083899974822998046875
8.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005673043511515250790617162939242239529116204160613960973880157057716983726135396869622250180765544997131601469918296320602329050170927141184099447564675215953746027629908292270396548378738513662113100439595487841685840024137033284553793293453302598111061411415463258721366061115326574635275436320830481387397475725109983131350483332991079629752529254797598923500920864309113503041579107769850746303047387313282414230126555683492237751961979702963844832660074393008548914708104979311438014110018772730512843602098243687969720238566523498297139965961976442401038364378194273937942119582873255606941017860362242226092582195859340833925623803748771486114356566444474265487348691096531200075145434190995502855838076175235385927860624470144949118979466849093864262556445260496712452483034216642143792293358158910706169993854552866703844881945072875638567255080465467643942541117134790906216671033375914335090151226710902297319365465921808881168809227394450717200123631539370749158418776648298549522797204492840062731745897452327527929000220520838466227865311246705891665481193295950249261444024018421028005137342842502848431189217089331350004249099399389468730885733732506892347866893978500961421750510377529016055992409846533027308404738486334390249808934387333224009295176815174471793338721676500935023425872069131210578321150120902703565669049901503672628234099939550392399712757253912307456753457341956371928138553340709064231871774738378542848130911730286018909671071932394121949423395407442102923648756128007794712609622515408072637395233593378616769730285290947280206476595811250982367354149994492537564826018518293299062150037087704649210489866690681672311550132318670295718527546305032624246493618812667637334059753461541727158637668915638796600456130068848510949643151683864131179160814979976261743464414424133517350051389468285814435199767867730170693770705988581229612497388371391303064378725080094927562060840954551547966559783232426863695223443848367870536707021803561754277539098091871983352924738961584422654913521294058004211376719039693594210051483736583024836704710966758525201700073400760616083299142671111364020587925224421836689299382934556396833630523391679953686665477526074122066380294664111719711029186460347035785784026710838086650766168399479159998825580562224887027901296765315576405685965458533761703209528151164780130937967554550198812150182156240296047517668398562417719621319689221600184334223101425729284419430212816203585159581797990360574238465050335949455386468843938586114893044244097613256457725837523552216523622085370396528094561016915247356837727989105426505954128241478184605165364941647880574844625408586642830660994878946645134044074270405366980914981061267782653172564926857024002613455869587611739995672873828894829303566273619786656908383028349319017911144894922922957808887424628016589440364463102458701632230442700238802978057360562712429605350805681303247295059635935335171054114455230583326635994543392506468471995680170070089245972451904622767425828548032213650596230505422634438786768458688940030687991462313153694366218906949258352636784142644453578096625528538862934820741348194924985269058178217160022079783002121114538410883053730353380067553211456078678635559000724745474362682511272547511861409675560406506722023688641122819144447096441669549747863676196064998673167803653654078815978559758107669615436168796816797332336395624201275784057531575866454245650012180847209006063195888915640285676186545078246838179077776404077899148720960644134122692463832130475173155626702593658664767042770289256734430038498470674007659383530714659994350646651553651022666868988911577180374048053390770386860202340660744241346081981387650770451639662285123450686951066985696288480011498656339894750858707313164379469169670123193819827939236337688917842676612614331520656017680339390667025229322629282442970149364491285851635211171762204372932014647595512443121001252027065052522298424252005933878769163462960526129610671297354944082540424984894868251080409310097562166843172321338525541109573714455309158860875027808849725478584502786817133003478439441124967177367620327015731673532605623627264431287652298159704133605710508459366880075854885189975724858412642439485890555903557584125445454360698248080098211568566738572353112355853702199416215251678207309080366204511762716055496655467906805100146415792689251373662587081864572029614241715948518873052259121071897320025012861368275045722035558555695085256935124638697411439139926217466723011474832534495234074578242101181004369719463053803204180646119676122286541349543829645570092094536293169852372631158877282050391518363456082412324981294590123682786378921797014192723901293592275706033233020565485968918885831372591351182299560879808275129427028238892825808268547863893770600998990986693504088477898199758511864181262498474894794758617130128368482700149953634533634443981875713788424103062382082162187230580992120909153875077435915857931812188541675736786156442251204836724628510887859990115994993145022400185516417295962467575699574803979068846238552669157597621606645603930540949941564541311771300597221481940233954404741747173665018569238317309417574293040221533127062095689997073087126464215932213184586249325928706325501902680247275875242215493095336881389556002554468576312802631798959046935533436361805495055516862406416060178261033417681778516887007484472814692684663541660519803296876189077168338572221227276847690362628382273653484725693392785766974137683776119509639076692736589797302898233423982817804578713532213402241914149707444447703884175835226494775017117337781274131837945994236617939025498911307048550871953083136235272374510648734986805646425302673198764504557882836297881013320088924772960696401364941506349388451473196767755039653778427594547865373874576701718317552258805437382768140361229482110223495440470831141510643647052347660064697265625