streamingfast / substreams-rs

7 stars 3 forks source link

Test out `big_decimal_exponent` in rust isolated environment #16

Closed Eduard-Voiculescu closed 1 year ago

Eduard-Voiculescu commented 1 year ago
pub fn big_decimal_exponated(amount: BigDecimal, exponent: BigInt) -> BigDecimal {
    log::info!("amount: {} exponent: {}", amount, exponent);
    if exponent.is_zero() {
        return BigDecimal::one();
    }
    let negative_exponent = exponent.lt(&BigInt::zero());
    let mut result = amount.clone();
    let mut exponent_abs = exponent.clone();

    if exponent.lt(&BigInt::zero()) {
        exponent_abs = exponent.clone().mul(BigInt::one().neg());
    }

    let mut i = BigInt::zero();
    while i.lt(exponent_abs.borrow()) {
        result = result.mul(amount.clone()).with_prec(100);
        i = i.add(BigInt::one());
    }

    if negative_exponent {
        result = safe_div(&BigDecimal::one(), &result);
    }

    return result;
}

Amount: 1.0001 Exponent: 50580 Result: 157.2516065180034170437593941810943348656947086171955950186834396686314552174742355550114493361303974 Expected Result: 157.2358829297104459991594782332771

Result returned by a simple javascript using bigdecimal:

bigDecimalExponated:  bigDecimal {
  value: '157.235882929710445999159478233271007764918216795516043414342005468084646752798955659445504785651835096248355929662162661822900250318809705715937204806511722525802526328996937186646869272619210581526774021053232219434433621148388441610534211148981006512697670749391670646458505528422515052500170130108472832954270281197595677558183972188172864431104227493505826280435883339471301655603892502331289179997410577343295406222746131218584380619065769972882016562454619753307519731297438711399233363543448900264770658178752387554514416832807707040925345814801615221609523939116431771379255985591486310793421776669285143795834247201034311800576897095843745315042413879924375880679203281897266428067367630569190260077371546279031924216679101602564960515504587212296775588828838939432952577749359187861116887804561085411226310511160001896942860610482467133254630529110349231401367406226931794655253841262421659502586722246874970194157138162295419437377833200290845645737088947091507396030744175033281407171328580107540965676201769975787270521114581334680139868010515201911348487933107423982701494105849036363256498550285815572705035403664027733323124911519098165522041304391549637270484852284186541171460828633683758070051210188151314851537675830138570074274952987048817222044290892059338934228263838408902995712019851170166949297063105971111358402342342791155489437121762590966641544949841723202089246026574404410343956447616139596042715346890801043263746845294182362610518863442120798903067208628654209887362301699881983980375843689871219410524365811559059918433814026293476721215494003926925690122894401754770563006381392928487853069747882203428638778681230291017702920020101817982275416148871797642915495321404602506099978949699109645586764975688103554915631914038783702864884363279080594970681746478419508730844342188428884934963074037403569975655767553104941143422276712069069204834401310292477632172074426610237540362585095374540353115552459329844009847785276537206698444259781175473503554742487288904178135233915344198613778899754901978222525887803018146510960559023603144832214100558533017029344777760905280824415477822627746731011162785482812979371371235281193012499740810783340696380891106975569424970646327182321030220493173039433889466620606436004702413421349984123277593762366454203641131009274050350912612796488389166811364894824853331943027975973782383933440805282199427667288685094756068316412392457460209230408461402822181983078876167927211302651422922108360206632693389961596238806089586173683084067601374711075800783234704438774494289854521775176759168333265151862643971175130933490184423388171675115406624383964262214281320537909444352051460133566067882538975249687749733754241442616402273031188947589852302123261751764302316147740481585448658527717201657719399125364874528763030595367213864472933778772517296589625920442222090608830091224676052434603071176358668087060265363726824146178252413165449586735301754363439355552023051122095450865491836669646659190293810832933711783577211620187283641165741819716885225661358942217161135997099625663121376591414986543116378909237629885274513174586190172144522363534205157000825361010303284276681168172910137018604257006885404593382767824874594365227624411872044328046116742414392841241159150265175718186482948704051852987437176025259245885655014549167150252997813600437531289283783557596985352880642968214638277866429175758412676559839990216074615679007753327702265306252910108951634940277893577655677833054209743990225877532117174138660973889896621639072643271952771468195391813151254900272321850605911943593874419005243493477358527835291021677524344429145360459751857276963427479072289880999799214109819678889205648662313776415842176661006091581522984606160263830532720532354814165584090375072716077946633373136417369000484962243275157329487223153208564729929695806715312745253797891453592951363731738513749135898530089083464320565125108206161258841716334242427458094629370882212651355656546492730533713403643251355539394247797177947260183826518879848507496351920171733370110440923635343228456581894132464983564889121159388525288624583887334799651836837713588022035559536939652226086747860533529212816077235882322728722608699597237180413746906605723937683342823323260153121776172496998062024285949716331253150580851231620681806501973347018705730734842060568212779788003636247675348988604431011682721355021434420787501191077026937984192279291769726280209021966401642504413306133884704100479963128148934103422601150690343703838306000442164050874662600362730293762205680491566987911762617188195849042532822490735928278344685369087572297474603920990185578589929865538732425946069120479780236933990486086899830924028272932887638890014920936542956478178930862757487471448087271647584983352304820452605122635631355810767182331711235353165194219009899702162710072050424920615888203994902379644384952694314825023540082147473010718958926561239513306192204456077975469852041176638811092452819367120985266936681551894736344630001820097522415836086789098048039629136636518953238642581011031399926239167751498355493739642706577954843179222377072581068999601559447027695512801953976155316154304748537282506728503936831307282239464548169298888028833440590595977785617169314021964455094074170562641227687292534743480000685107589670697346225007826283545461569407671885038848511995662625060403601858714366025564000771062541677528390882218525889826493872037897268000281988875262839867044853720128060388881648674399096995461704940815766146178653317622743976689683744881886802505026643680409416372031270054319870363625928400194581923732366861946256680136500168720179946042800992425106572267300478800729538955487356598425727752487238132485570022798829271080094412831629817884583653147518065989303928262649321596274378898970379773353236653616088619690608397694924261313466830839311603923365680549195964397535161287627832343349925128855138439087106762878622894075788125778440333928970825497616946816228224600724821449328077341505455214848927893238378201233508166064964933584610073049451208214441412625369475668856333673290691802718103926787036595494951819002929981445458258937471014958002925175739823443674736862883172313174391291738685523463819560062011125370854190994218473482369702769241660519451804005027883457099426744526090688654596526748550807519745929487391499316666858962230984788497472270171060510246871982677540820594820498369124245050218334551741278431975952941223862830654515948715009260796778101295696910256859805555511971983007987467733288122211288130869638337342845851847890755106373480271550629468336013596348217317334586628134752846783952604343242252051846416688633518400442929196212103813001174777523334544332988972775996061044638966379230242676490789259909781341694115844759986887539407522388927295722359236545082163041165932092380938585977836493200654043166327895886512334579534131329187708895765723855243153720456235116566146391159558256675715127081485045337185868145727633029223672799141720614127215722644612336010919192725683557398107604803613143695114652253409777978726351345494886719964240324948675588326325159407412219277878169585750227159159771913591886990748172594201829193050262715173594281707571343924155228106405925358511986290751665931535314057850435758130978885613841247329634931625519667470805650442678971416562786559146539841828929896365110773891074402850701751019277431247839841916350171994722600798472240532036252889251731624458477894887731882300777788179819245729466270561842205174077821918799431526243444149235134569416873770116092502380504681164412748695718125791902310587959872212546686449464710244421349946419145227911515938139396050987613287221468347812710704848787049036098169314843284746861015173052617319622815142252678503075270876242895290290146440335092036144059071489437444584216436003476291963521554570617691486542401810108906388249009740857850830653909063210119895772668064646108545216185770434253428775301006712049245988014835489034042580607123858648562718083527514411639776474424744294414519691319481211694523761608510996306746569721349127676729038184748802257390679775284352448191970960865160518978579741469362093540070930649093932846199900445430375750061970603464020190580273253386672826834017518412444208092258043983877053079630070264348677403757242385683106170056641976714764747815612939792470915985246309279983658193522997052557085231085965790489751775200405863233855082139388617085025146586305713883350594382340702958801971966080626945632064985984098192505299890739914146534722199432096417517656060338288841820321492324014868519720588183957325003690692972973517887661312760039131535606246161921234622621180086202412238692828953224685759852964455944079905747807092830057782607126944171749750988027312463856146445692020340297151174204653731358426949859958023466575426268523755911523802767620964887651497090483818106293377592002898853906533575499626186188875151215085000132550088000639302754366765837235839088999945148106044297494150343103461579992302711843437147024476622146791585689877042562371322046548894565875108182701221031346925739264514559417329944211584624822431571727889489394397385060514088094483881881667752209639756629495185363500140682465877643111097007561256586616555510611198420899858823316349272516151407210640100378397050562380426319260051619606459271258176738571046830145548566732729220796872919046603125445077842296387279084777152168146896222212419631195710670379720906094984378241516757934480195217023121525758937160230797848297443179322423174912143144320365554798467657145858761594050056785131206663508145166261684581717045848045169325206048598711460909865777527248048647936279799671212728167239802413326724230953816952384071060621142501927190380808314982130886174399909101446830430383815395238452965778606378050253406061246063631756427644899254281127462915730172302000045759780709568556756811515569577748368154660488045427089413140596602401954088318226008140602882954621960387002108508977222534578408347766503625326959697266609627708907619524473984912725094627250677196489953614639927902'... 192324 more characters
}
Eduard-Voiculescu commented 1 year ago

Time: 100 prec cargo run 1.35s user 0.02s system 93% cpu 1.461 total Time: 200 prec cargo run 2.18s user 0.02s system 94% cpu 2.332 total Time: 1000 prec cargo run 14.66s user 0.07s system 99% cpu 14.870 total Time: 10000 prec cargo run 685.34s user 3.47s system 99% cpu 11:31.39 total

sduchesneau commented 1 year ago

fixed, the issue was just an off-by-one.