numbers.c 299 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429
  1. /* Copyright 1995-2016,2018-2019
  2. Free Software Foundation, Inc.
  3. Portions Copyright 1990-1993 by AT&T Bell Laboratories and Bellcore.
  4. See scm_divide.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. /* General assumptions:
  18. * All objects satisfying SCM_BIGP() are too large to fit in a fixnum.
  19. * If an object satisfies integer?, it's either an inum, a bignum, or a real.
  20. * If floor (r) == r, r is an int, and mpz_set_d will DTRT.
  21. * XXX What about infinities? They are equal to their own floor! -mhw
  22. * All objects satisfying SCM_FRACTIONP are never an integer.
  23. */
  24. /* TODO:
  25. - see if special casing bignums and reals in integer-exponent when
  26. possible (to use mpz_pow and mpf_pow_ui) is faster.
  27. - look in to better short-circuiting of common cases in
  28. integer-expt and elsewhere.
  29. - see if direct mpz operations can help in ash and elsewhere.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <assert.h>
  35. #include <math.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <unicase.h>
  39. #include <unictype.h>
  40. #include <verify.h>
  41. #if HAVE_COMPLEX_H
  42. #include <complex.h>
  43. #endif
  44. #include "bdw-gc.h"
  45. #include "boolean.h"
  46. #include "deprecation.h"
  47. #include "eq.h"
  48. #include "feature.h"
  49. #include "finalizers.h"
  50. #include "goops.h"
  51. #include "gsubr.h"
  52. #include "modules.h"
  53. #include "pairs.h"
  54. #include "ports.h"
  55. #include "smob.h"
  56. #include "strings.h"
  57. #include "values.h"
  58. #include "numbers.h"
  59. /* values per glibc, if not already defined */
  60. #ifndef M_LOG10E
  61. #define M_LOG10E 0.43429448190325182765
  62. #endif
  63. #ifndef M_LN2
  64. #define M_LN2 0.69314718055994530942
  65. #endif
  66. #ifndef M_PI
  67. #define M_PI 3.14159265358979323846
  68. #endif
  69. /* FIXME: We assume that FLT_RADIX is 2 */
  70. verify (FLT_RADIX == 2);
  71. /* Make sure that scm_t_inum fits within a SCM value. */
  72. verify (sizeof (scm_t_inum) <= sizeof (scm_t_bits));
  73. /* Several functions below assume that fixnums fit within a long, and
  74. furthermore that there is some headroom to spare for other operations
  75. without overflowing. */
  76. verify (SCM_I_FIXNUM_BIT <= SCM_LONG_BIT - 2);
  77. /* Some functions that use GMP's mpn functions assume that a
  78. non-negative fixnum will always fit in a 'mp_limb_t'. */
  79. verify (SCM_MOST_POSITIVE_FIXNUM <= (mp_limb_t) -1);
  80. #define scm_from_inum(x) (scm_from_signed_integer (x))
  81. /* Test an inum to see if it can be converted to a double without loss
  82. of precision. Note that this will sometimes return 0 even when 1
  83. could have been returned, e.g. for large powers of 2. It is designed
  84. to be a fast check to optimize common cases. */
  85. #define INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE(n) \
  86. (SCM_I_FIXNUM_BIT-1 <= DBL_MANT_DIG \
  87. || ((n) ^ ((n) >> (SCM_I_FIXNUM_BIT-1))) < (1L << DBL_MANT_DIG))
  88. #if ! HAVE_DECL_MPZ_INITS
  89. /* GMP < 5.0.0 lacks `mpz_inits' and `mpz_clears'. Provide them. */
  90. #define VARARG_MPZ_ITERATOR(func) \
  91. static void \
  92. func ## s (mpz_t x, ...) \
  93. { \
  94. va_list ap; \
  95. \
  96. va_start (ap, x); \
  97. while (x != NULL) \
  98. { \
  99. func (x); \
  100. x = va_arg (ap, mpz_ptr); \
  101. } \
  102. va_end (ap); \
  103. }
  104. VARARG_MPZ_ITERATOR (mpz_init)
  105. VARARG_MPZ_ITERATOR (mpz_clear)
  106. #endif
  107. /*
  108. Wonder if this might be faster for some of our code? A switch on
  109. the numtag would jump directly to the right case, and the
  110. SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests...
  111. #define SCM_I_NUMTAG_NOTNUM 0
  112. #define SCM_I_NUMTAG_INUM 1
  113. #define SCM_I_NUMTAG_BIG scm_tc16_big
  114. #define SCM_I_NUMTAG_REAL scm_tc16_real
  115. #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex
  116. #define SCM_I_NUMTAG(x) \
  117. (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \
  118. : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \
  119. : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \
  120. : SCM_I_NUMTAG_NOTNUM)))
  121. */
  122. /* the macro above will not work as is with fractions */
  123. /* Default to 1, because as we used to hard-code `free' as the
  124. deallocator, we know that overriding these functions with
  125. instrumented `malloc' / `free' is OK. */
  126. int scm_install_gmp_memory_functions = 1;
  127. static SCM flo0;
  128. static SCM exactly_one_half;
  129. static SCM flo_log10e;
  130. #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0)
  131. /* FLOBUFLEN is the maximum number of characters necessary for the
  132. * printed or scm_string representation of an inexact number.
  133. */
  134. #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  135. #if !defined (HAVE_ASINH)
  136. static double asinh (double x) { return log (x + sqrt (x * x + 1)); }
  137. #endif
  138. #if !defined (HAVE_ACOSH)
  139. static double acosh (double x) { return log (x + sqrt (x * x - 1)); }
  140. #endif
  141. #if !defined (HAVE_ATANH)
  142. static double atanh (double x) { return 0.5 * log ((1 + x) / (1 - x)); }
  143. #endif
  144. /* mpz_cmp_d in GMP before 4.2 didn't recognise infinities, so
  145. xmpz_cmp_d uses an explicit check. Starting with GMP 4.2 (released
  146. in March 2006), mpz_cmp_d now handles infinities properly. */
  147. #if 1
  148. #define xmpz_cmp_d(z, d) \
  149. (isinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d))
  150. #else
  151. #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d)
  152. #endif
  153. #if defined (GUILE_I)
  154. #if defined HAVE_COMPLEX_DOUBLE
  155. /* For an SCM object Z which is a complex number (ie. satisfies
  156. SCM_COMPLEXP), return its value as a C level "complex double". */
  157. #define SCM_COMPLEX_VALUE(z) \
  158. (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
  159. static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
  160. /* Convert a C "complex double" to an SCM value. */
  161. static inline SCM
  162. scm_from_complex_double (complex double z)
  163. {
  164. return scm_c_make_rectangular (creal (z), cimag (z));
  165. }
  166. #endif /* HAVE_COMPLEX_DOUBLE */
  167. #endif /* GUILE_I */
  168. static mpz_t z_negative_one;
  169. /* Clear the `mpz_t' embedded in bignum PTR. */
  170. static void
  171. finalize_bignum (void *ptr, void *data)
  172. {
  173. SCM bignum;
  174. bignum = SCM_PACK_POINTER (ptr);
  175. mpz_clear (SCM_I_BIG_MPZ (bignum));
  176. }
  177. /* The next three functions (custom_libgmp_*) are passed to
  178. mp_set_memory_functions (in GMP) so that memory used by the digits
  179. themselves is known to the garbage collector. This is needed so
  180. that GC will be run at appropriate times. Otherwise, a program which
  181. creates many large bignums would malloc a huge amount of memory
  182. before the GC runs. */
  183. static void *
  184. custom_gmp_malloc (size_t alloc_size)
  185. {
  186. return scm_malloc (alloc_size);
  187. }
  188. static void *
  189. custom_gmp_realloc (void *old_ptr, size_t old_size, size_t new_size)
  190. {
  191. return scm_realloc (old_ptr, new_size);
  192. }
  193. static void
  194. custom_gmp_free (void *ptr, size_t size)
  195. {
  196. free (ptr);
  197. }
  198. /* Return a new uninitialized bignum. */
  199. static inline SCM
  200. make_bignum (void)
  201. {
  202. scm_t_bits *p;
  203. /* Allocate one word for the type tag and enough room for an `mpz_t'. */
  204. p = scm_gc_malloc_pointerless (sizeof (scm_t_bits) + sizeof (mpz_t),
  205. "bignum");
  206. p[0] = scm_tc16_big;
  207. scm_i_set_finalizer (p, finalize_bignum, NULL);
  208. return SCM_PACK (p);
  209. }
  210. SCM
  211. scm_i_mkbig ()
  212. {
  213. /* Return a newly created bignum. */
  214. SCM z = make_bignum ();
  215. mpz_init (SCM_I_BIG_MPZ (z));
  216. return z;
  217. }
  218. static SCM
  219. scm_i_inum2big (scm_t_inum x)
  220. {
  221. /* Return a newly created bignum initialized to X. */
  222. SCM z = make_bignum ();
  223. mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
  224. return z;
  225. }
  226. SCM
  227. scm_i_long2big (long x)
  228. {
  229. /* Return a newly created bignum initialized to X. */
  230. SCM z = make_bignum ();
  231. mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
  232. return z;
  233. }
  234. SCM
  235. scm_i_ulong2big (unsigned long x)
  236. {
  237. /* Return a newly created bignum initialized to X. */
  238. SCM z = make_bignum ();
  239. mpz_init_set_ui (SCM_I_BIG_MPZ (z), x);
  240. return z;
  241. }
  242. SCM
  243. scm_i_clonebig (SCM src_big, int same_sign_p)
  244. {
  245. /* Copy src_big's value, negate it if same_sign_p is false, and return. */
  246. SCM z = make_bignum ();
  247. mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big));
  248. if (!same_sign_p)
  249. mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
  250. return z;
  251. }
  252. int
  253. scm_i_bigcmp (SCM x, SCM y)
  254. {
  255. /* Return neg if x < y, pos if x > y, and 0 if x == y */
  256. /* presume we already know x and y are bignums */
  257. int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  258. scm_remember_upto_here_2 (x, y);
  259. return result;
  260. }
  261. SCM
  262. scm_i_dbl2big (double d)
  263. {
  264. /* results are only defined if d is an integer */
  265. SCM z = make_bignum ();
  266. mpz_init_set_d (SCM_I_BIG_MPZ (z), d);
  267. return z;
  268. }
  269. /* Convert a integer in double representation to a SCM number. */
  270. SCM
  271. scm_i_dbl2num (double u)
  272. {
  273. /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both
  274. powers of 2, so there's no rounding when making "double" values
  275. from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could
  276. get rounded on a 64-bit machine, hence the "+1".
  277. The use of floor() to force to an integer value ensures we get a
  278. "numerically closest" value without depending on how a
  279. double->long cast or how mpz_set_d will round. For reference,
  280. double->long probably follows the hardware rounding mode,
  281. mpz_set_d truncates towards zero. */
  282. /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not
  283. representable as a double? */
  284. if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
  285. && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
  286. return SCM_I_MAKINUM ((scm_t_inum) u);
  287. else
  288. return scm_i_dbl2big (u);
  289. }
  290. static SCM round_right_shift_exact_integer (SCM n, long count);
  291. /* scm_i_big2dbl_2exp() is like frexp for bignums: it converts the
  292. bignum b into a normalized significand and exponent such that
  293. b = significand * 2^exponent and 1/2 <= abs(significand) < 1.
  294. The return value is the significand rounded to the closest
  295. representable double, and the exponent is placed into *expon_p.
  296. If b is zero, then the returned exponent and significand are both
  297. zero. */
  298. static double
  299. scm_i_big2dbl_2exp (SCM b, long *expon_p)
  300. {
  301. size_t bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2);
  302. size_t shift = 0;
  303. if (bits > DBL_MANT_DIG)
  304. {
  305. shift = bits - DBL_MANT_DIG;
  306. b = round_right_shift_exact_integer (b, shift);
  307. if (SCM_I_INUMP (b))
  308. {
  309. int expon;
  310. double signif = frexp (SCM_I_INUM (b), &expon);
  311. *expon_p = expon + shift;
  312. return signif;
  313. }
  314. }
  315. {
  316. long expon;
  317. double signif = mpz_get_d_2exp (&expon, SCM_I_BIG_MPZ (b));
  318. scm_remember_upto_here_1 (b);
  319. *expon_p = expon + shift;
  320. return signif;
  321. }
  322. }
  323. /* scm_i_big2dbl() rounds to the closest representable double,
  324. in accordance with R5RS exact->inexact. */
  325. double
  326. scm_i_big2dbl (SCM b)
  327. {
  328. long expon;
  329. double signif = scm_i_big2dbl_2exp (b, &expon);
  330. return ldexp (signif, expon);
  331. }
  332. SCM
  333. scm_i_normbig (SCM b)
  334. {
  335. /* convert a big back to a fixnum if it'll fit */
  336. /* presume b is a bignum */
  337. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b)))
  338. {
  339. scm_t_inum val = mpz_get_si (SCM_I_BIG_MPZ (b));
  340. if (SCM_FIXABLE (val))
  341. b = SCM_I_MAKINUM (val);
  342. }
  343. return b;
  344. }
  345. static SCM_C_INLINE_KEYWORD SCM
  346. scm_i_mpz2num (mpz_t b)
  347. {
  348. /* convert a mpz number to a SCM number. */
  349. if (mpz_fits_slong_p (b))
  350. {
  351. scm_t_inum val = mpz_get_si (b);
  352. if (SCM_FIXABLE (val))
  353. return SCM_I_MAKINUM (val);
  354. }
  355. {
  356. SCM z = make_bignum ();
  357. mpz_init_set (SCM_I_BIG_MPZ (z), b);
  358. return z;
  359. }
  360. }
  361. /* Make the ratio NUMERATOR/DENOMINATOR, where:
  362. 1. NUMERATOR and DENOMINATOR are exact integers
  363. 2. NUMERATOR and DENOMINATOR are reduced to lowest terms: gcd(n,d) == 1 */
  364. static SCM
  365. scm_i_make_ratio_already_reduced (SCM numerator, SCM denominator)
  366. {
  367. /* Flip signs so that the denominator is positive. */
  368. if (scm_is_false (scm_positive_p (denominator)))
  369. {
  370. if (SCM_UNLIKELY (scm_is_eq (denominator, SCM_INUM0)))
  371. scm_num_overflow ("make-ratio");
  372. else
  373. {
  374. numerator = scm_difference (numerator, SCM_UNDEFINED);
  375. denominator = scm_difference (denominator, SCM_UNDEFINED);
  376. }
  377. }
  378. /* Check for the integer case */
  379. if (scm_is_eq (denominator, SCM_INUM1))
  380. return numerator;
  381. return scm_double_cell (scm_tc16_fraction,
  382. SCM_UNPACK (numerator),
  383. SCM_UNPACK (denominator), 0);
  384. }
  385. static SCM scm_exact_integer_quotient (SCM x, SCM y);
  386. /* Make the ratio NUMERATOR/DENOMINATOR */
  387. static SCM
  388. scm_i_make_ratio (SCM numerator, SCM denominator)
  389. #define FUNC_NAME "make-ratio"
  390. {
  391. /* Make sure the arguments are proper */
  392. if (!SCM_LIKELY (SCM_I_INUMP (numerator) || SCM_BIGP (numerator)))
  393. SCM_WRONG_TYPE_ARG (1, numerator);
  394. else if (!SCM_LIKELY (SCM_I_INUMP (denominator) || SCM_BIGP (denominator)))
  395. SCM_WRONG_TYPE_ARG (2, denominator);
  396. else
  397. {
  398. SCM the_gcd = scm_gcd (numerator, denominator);
  399. if (!(scm_is_eq (the_gcd, SCM_INUM1)))
  400. {
  401. /* Reduce to lowest terms */
  402. numerator = scm_exact_integer_quotient (numerator, the_gcd);
  403. denominator = scm_exact_integer_quotient (denominator, the_gcd);
  404. }
  405. return scm_i_make_ratio_already_reduced (numerator, denominator);
  406. }
  407. }
  408. #undef FUNC_NAME
  409. static mpz_t scm_i_divide2double_lo2b;
  410. /* Return the double that is closest to the exact rational N/D, with
  411. ties rounded toward even mantissas. N and D must be exact
  412. integers. */
  413. static double
  414. scm_i_divide2double (SCM n, SCM d)
  415. {
  416. int neg;
  417. mpz_t nn, dd, lo, hi, x;
  418. ssize_t e;
  419. if (SCM_LIKELY (SCM_I_INUMP (d)))
  420. {
  421. if (SCM_LIKELY
  422. (SCM_I_INUMP (n)
  423. && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (n))
  424. && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (d))))
  425. /* If both N and D can be losslessly converted to doubles, then
  426. we can rely on IEEE floating point to do proper rounding much
  427. faster than we can. */
  428. return ((double) SCM_I_INUM (n)) / ((double) SCM_I_INUM (d));
  429. if (SCM_UNLIKELY (scm_is_eq (d, SCM_INUM0)))
  430. {
  431. if (scm_is_true (scm_positive_p (n)))
  432. return 1.0 / 0.0;
  433. else if (scm_is_true (scm_negative_p (n)))
  434. return -1.0 / 0.0;
  435. else
  436. return 0.0 / 0.0;
  437. }
  438. mpz_init_set_si (dd, SCM_I_INUM (d));
  439. }
  440. else
  441. mpz_init_set (dd, SCM_I_BIG_MPZ (d));
  442. if (SCM_I_INUMP (n))
  443. mpz_init_set_si (nn, SCM_I_INUM (n));
  444. else
  445. mpz_init_set (nn, SCM_I_BIG_MPZ (n));
  446. neg = (mpz_sgn (nn) < 0) ^ (mpz_sgn (dd) < 0);
  447. mpz_abs (nn, nn);
  448. mpz_abs (dd, dd);
  449. /* Now we need to find the value of e such that:
  450. For e <= 0:
  451. b^{p-1} - 1/2b <= b^-e n / d < b^p - 1/2 [1A]
  452. (2 b^p - 1) <= 2 b b^-e n / d < (2 b^p - 1) b [2A]
  453. (2 b^p - 1) d <= 2 b b^-e n < (2 b^p - 1) d b [3A]
  454. For e >= 0:
  455. b^{p-1} - 1/2b <= n / b^e d < b^p - 1/2 [1B]
  456. (2 b^p - 1) <= 2 b n / b^e d < (2 b^p - 1) b [2B]
  457. (2 b^p - 1) d b^e <= 2 b n < (2 b^p - 1) d b b^e [3B]
  458. where: p = DBL_MANT_DIG
  459. b = FLT_RADIX (here assumed to be 2)
  460. After rounding, the mantissa must be an integer between b^{p-1} and
  461. (b^p - 1), except for subnormal numbers. In the inequations [1A]
  462. and [1B], the middle expression represents the mantissa *before*
  463. rounding, and therefore is bounded by the range of values that will
  464. round to a floating-point number with the exponent e. The upper
  465. bound is (b^p - 1 + 1/2) = (b^p - 1/2), and is exclusive because
  466. ties will round up to the next power of b. The lower bound is
  467. (b^{p-1} - 1/2b), and is inclusive because ties will round toward
  468. this power of b. Here we subtract 1/2b instead of 1/2 because it
  469. is in the range of the next smaller exponent, where the
  470. representable numbers are closer together by a factor of b.
  471. Inequations [2A] and [2B] are derived from [1A] and [1B] by
  472. multiplying by 2b, and in [3A] and [3B] we multiply by the
  473. denominator of the middle value to obtain integer expressions.
  474. In the code below, we refer to the three expressions in [3A] or
  475. [3B] as lo, x, and hi. If the number is normalizable, we will
  476. achieve the goal: lo <= x < hi */
  477. /* Make an initial guess for e */
  478. e = mpz_sizeinbase (nn, 2) - mpz_sizeinbase (dd, 2) - (DBL_MANT_DIG-1);
  479. if (e < DBL_MIN_EXP - DBL_MANT_DIG)
  480. e = DBL_MIN_EXP - DBL_MANT_DIG;
  481. /* Compute the initial values of lo, x, and hi
  482. based on the initial guess of e */
  483. mpz_inits (lo, hi, x, NULL);
  484. mpz_mul_2exp (x, nn, 2 + ((e < 0) ? -e : 0));
  485. mpz_mul (lo, dd, scm_i_divide2double_lo2b);
  486. if (e > 0)
  487. mpz_mul_2exp (lo, lo, e);
  488. mpz_mul_2exp (hi, lo, 1);
  489. /* Adjust e as needed to satisfy the inequality lo <= x < hi,
  490. (but without making e less than the minimum exponent) */
  491. while (mpz_cmp (x, lo) < 0 && e > DBL_MIN_EXP - DBL_MANT_DIG)
  492. {
  493. mpz_mul_2exp (x, x, 1);
  494. e--;
  495. }
  496. while (mpz_cmp (x, hi) >= 0)
  497. {
  498. /* If we ever used lo's value again,
  499. we would need to double lo here. */
  500. mpz_mul_2exp (hi, hi, 1);
  501. e++;
  502. }
  503. /* Now compute the rounded mantissa:
  504. n / b^e d (if e >= 0)
  505. n b^-e / d (if e <= 0) */
  506. {
  507. int cmp;
  508. double result;
  509. if (e < 0)
  510. mpz_mul_2exp (nn, nn, -e);
  511. else
  512. mpz_mul_2exp (dd, dd, e);
  513. /* mpz does not directly support rounded right
  514. shifts, so we have to do it the hard way.
  515. For efficiency, we reuse lo and hi.
  516. hi == quotient, lo == remainder */
  517. mpz_fdiv_qr (hi, lo, nn, dd);
  518. /* The fractional part of the unrounded mantissa would be
  519. remainder/dividend, i.e. lo/dd. So we have a tie if
  520. lo/dd = 1/2. Multiplying both sides by 2*dd yields the
  521. integer expression 2*lo = dd. Here we do that comparison
  522. to decide whether to round up or down. */
  523. mpz_mul_2exp (lo, lo, 1);
  524. cmp = mpz_cmp (lo, dd);
  525. if (cmp > 0 || (cmp == 0 && mpz_odd_p (hi)))
  526. mpz_add_ui (hi, hi, 1);
  527. result = ldexp (mpz_get_d (hi), e);
  528. if (neg)
  529. result = -result;
  530. mpz_clears (nn, dd, lo, hi, x, NULL);
  531. return result;
  532. }
  533. }
  534. double
  535. scm_i_fraction2double (SCM z)
  536. {
  537. return scm_i_divide2double (SCM_FRACTION_NUMERATOR (z),
  538. SCM_FRACTION_DENOMINATOR (z));
  539. }
  540. static SCM
  541. scm_i_from_double (double val)
  542. {
  543. SCM z;
  544. z = SCM_PACK_POINTER (scm_gc_malloc_pointerless (sizeof (scm_t_double), "real"));
  545. SCM_SET_CELL_TYPE (z, scm_tc16_real);
  546. SCM_REAL_VALUE (z) = val;
  547. return z;
  548. }
  549. SCM_PRIMITIVE_GENERIC (scm_exact_p, "exact?", 1, 0, 0,
  550. (SCM x),
  551. "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
  552. "otherwise.")
  553. #define FUNC_NAME s_scm_exact_p
  554. {
  555. if (SCM_INEXACTP (x))
  556. return SCM_BOOL_F;
  557. else if (SCM_NUMBERP (x))
  558. return SCM_BOOL_T;
  559. else
  560. return scm_wta_dispatch_1 (g_scm_exact_p, x, 1, s_scm_exact_p);
  561. }
  562. #undef FUNC_NAME
  563. int
  564. scm_is_exact (SCM val)
  565. {
  566. return scm_is_true (scm_exact_p (val));
  567. }
  568. SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0,
  569. (SCM x),
  570. "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
  571. "else.")
  572. #define FUNC_NAME s_scm_inexact_p
  573. {
  574. if (SCM_INEXACTP (x))
  575. return SCM_BOOL_T;
  576. else if (SCM_NUMBERP (x))
  577. return SCM_BOOL_F;
  578. else
  579. return scm_wta_dispatch_1 (g_scm_inexact_p, x, 1, s_scm_inexact_p);
  580. }
  581. #undef FUNC_NAME
  582. int
  583. scm_is_inexact (SCM val)
  584. {
  585. return scm_is_true (scm_inexact_p (val));
  586. }
  587. SCM_PRIMITIVE_GENERIC (scm_odd_p, "odd?", 1, 0, 0,
  588. (SCM n),
  589. "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
  590. "otherwise.")
  591. #define FUNC_NAME s_scm_odd_p
  592. {
  593. if (SCM_I_INUMP (n))
  594. {
  595. scm_t_inum val = SCM_I_INUM (n);
  596. return scm_from_bool ((val & 1L) != 0);
  597. }
  598. else if (SCM_BIGP (n))
  599. {
  600. int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n));
  601. scm_remember_upto_here_1 (n);
  602. return scm_from_bool (odd_p);
  603. }
  604. else if (SCM_REALP (n))
  605. {
  606. double val = SCM_REAL_VALUE (n);
  607. if (isfinite (val))
  608. {
  609. double rem = fabs (fmod (val, 2.0));
  610. if (rem == 1.0)
  611. return SCM_BOOL_T;
  612. else if (rem == 0.0)
  613. return SCM_BOOL_F;
  614. }
  615. }
  616. return scm_wta_dispatch_1 (g_scm_odd_p, n, 1, s_scm_odd_p);
  617. }
  618. #undef FUNC_NAME
  619. SCM_PRIMITIVE_GENERIC (scm_even_p, "even?", 1, 0, 0,
  620. (SCM n),
  621. "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
  622. "otherwise.")
  623. #define FUNC_NAME s_scm_even_p
  624. {
  625. if (SCM_I_INUMP (n))
  626. {
  627. scm_t_inum val = SCM_I_INUM (n);
  628. return scm_from_bool ((val & 1L) == 0);
  629. }
  630. else if (SCM_BIGP (n))
  631. {
  632. int even_p = mpz_even_p (SCM_I_BIG_MPZ (n));
  633. scm_remember_upto_here_1 (n);
  634. return scm_from_bool (even_p);
  635. }
  636. else if (SCM_REALP (n))
  637. {
  638. double val = SCM_REAL_VALUE (n);
  639. if (isfinite (val))
  640. {
  641. double rem = fabs (fmod (val, 2.0));
  642. if (rem == 1.0)
  643. return SCM_BOOL_F;
  644. else if (rem == 0.0)
  645. return SCM_BOOL_T;
  646. }
  647. }
  648. return scm_wta_dispatch_1 (g_scm_even_p, n, 1, s_scm_even_p);
  649. }
  650. #undef FUNC_NAME
  651. SCM_PRIMITIVE_GENERIC (scm_finite_p, "finite?", 1, 0, 0,
  652. (SCM x),
  653. "Return @code{#t} if the real number @var{x} is neither\n"
  654. "infinite nor a NaN, @code{#f} otherwise.")
  655. #define FUNC_NAME s_scm_finite_p
  656. {
  657. if (SCM_REALP (x))
  658. return scm_from_bool (isfinite (SCM_REAL_VALUE (x)));
  659. else if (scm_is_real (x))
  660. return SCM_BOOL_T;
  661. else
  662. return scm_wta_dispatch_1 (g_scm_finite_p, x, 1, s_scm_finite_p);
  663. }
  664. #undef FUNC_NAME
  665. SCM_PRIMITIVE_GENERIC (scm_inf_p, "inf?", 1, 0, 0,
  666. (SCM x),
  667. "Return @code{#t} if the real number @var{x} is @samp{+inf.0} or\n"
  668. "@samp{-inf.0}. Otherwise return @code{#f}.")
  669. #define FUNC_NAME s_scm_inf_p
  670. {
  671. if (SCM_REALP (x))
  672. return scm_from_bool (isinf (SCM_REAL_VALUE (x)));
  673. else if (scm_is_real (x))
  674. return SCM_BOOL_F;
  675. else
  676. return scm_wta_dispatch_1 (g_scm_inf_p, x, 1, s_scm_inf_p);
  677. }
  678. #undef FUNC_NAME
  679. SCM_PRIMITIVE_GENERIC (scm_nan_p, "nan?", 1, 0, 0,
  680. (SCM x),
  681. "Return @code{#t} if the real number @var{x} is a NaN,\n"
  682. "or @code{#f} otherwise.")
  683. #define FUNC_NAME s_scm_nan_p
  684. {
  685. if (SCM_REALP (x))
  686. return scm_from_bool (isnan (SCM_REAL_VALUE (x)));
  687. else if (scm_is_real (x))
  688. return SCM_BOOL_F;
  689. else
  690. return scm_wta_dispatch_1 (g_scm_nan_p, x, 1, s_scm_nan_p);
  691. }
  692. #undef FUNC_NAME
  693. /* Guile's idea of infinity. */
  694. static double guile_Inf;
  695. /* Guile's idea of not a number. */
  696. static double guile_NaN;
  697. static void
  698. guile_ieee_init (void)
  699. {
  700. /* Some version of gcc on some old version of Linux used to crash when
  701. trying to make Inf and NaN. */
  702. #ifdef INFINITY
  703. /* C99 INFINITY, when available.
  704. FIXME: The standard allows for INFINITY to be something that overflows
  705. at compile time. We ought to have a configure test to check for that
  706. before trying to use it. (But in practice we believe this is not a
  707. problem on any system guile is likely to target.) */
  708. guile_Inf = INFINITY;
  709. #elif defined HAVE_DINFINITY
  710. /* OSF */
  711. extern unsigned int DINFINITY[2];
  712. guile_Inf = (*((double *) (DINFINITY)));
  713. #else
  714. double tmp = 1e+10;
  715. guile_Inf = tmp;
  716. for (;;)
  717. {
  718. guile_Inf *= 1e+10;
  719. if (guile_Inf == tmp)
  720. break;
  721. tmp = guile_Inf;
  722. }
  723. #endif
  724. #ifdef NAN
  725. /* C99 NAN, when available */
  726. guile_NaN = NAN;
  727. #elif defined HAVE_DQNAN
  728. {
  729. /* OSF */
  730. extern unsigned int DQNAN[2];
  731. guile_NaN = (*((double *)(DQNAN)));
  732. }
  733. #else
  734. guile_NaN = guile_Inf / guile_Inf;
  735. #endif
  736. }
  737. SCM_DEFINE (scm_inf, "inf", 0, 0, 0,
  738. (void),
  739. "Return Inf.")
  740. #define FUNC_NAME s_scm_inf
  741. {
  742. static int initialized = 0;
  743. if (! initialized)
  744. {
  745. guile_ieee_init ();
  746. initialized = 1;
  747. }
  748. return scm_i_from_double (guile_Inf);
  749. }
  750. #undef FUNC_NAME
  751. SCM_DEFINE (scm_nan, "nan", 0, 0, 0,
  752. (void),
  753. "Return NaN.")
  754. #define FUNC_NAME s_scm_nan
  755. {
  756. static int initialized = 0;
  757. if (!initialized)
  758. {
  759. guile_ieee_init ();
  760. initialized = 1;
  761. }
  762. return scm_i_from_double (guile_NaN);
  763. }
  764. #undef FUNC_NAME
  765. SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
  766. (SCM x),
  767. "Return the absolute value of @var{x}.")
  768. #define FUNC_NAME s_scm_abs
  769. {
  770. if (SCM_I_INUMP (x))
  771. {
  772. scm_t_inum xx = SCM_I_INUM (x);
  773. if (xx >= 0)
  774. return x;
  775. else if (SCM_POSFIXABLE (-xx))
  776. return SCM_I_MAKINUM (-xx);
  777. else
  778. return scm_i_inum2big (-xx);
  779. }
  780. else if (SCM_LIKELY (SCM_REALP (x)))
  781. {
  782. double xx = SCM_REAL_VALUE (x);
  783. /* If x is a NaN then xx<0 is false so we return x unchanged */
  784. if (xx < 0.0)
  785. return scm_i_from_double (-xx);
  786. /* Handle signed zeroes properly */
  787. else if (SCM_UNLIKELY (xx == 0.0))
  788. return flo0;
  789. else
  790. return x;
  791. }
  792. else if (SCM_BIGP (x))
  793. {
  794. const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  795. if (sgn < 0)
  796. return scm_i_clonebig (x, 0);
  797. else
  798. return x;
  799. }
  800. else if (SCM_FRACTIONP (x))
  801. {
  802. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
  803. return x;
  804. return scm_i_make_ratio_already_reduced
  805. (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  806. SCM_FRACTION_DENOMINATOR (x));
  807. }
  808. else
  809. return scm_wta_dispatch_1 (g_scm_abs, x, 1, s_scm_abs);
  810. }
  811. #undef FUNC_NAME
  812. SCM_PRIMITIVE_GENERIC (scm_quotient, "quotient", 2, 0, 0,
  813. (SCM x, SCM y),
  814. "Return the quotient of the numbers @var{x} and @var{y}.")
  815. #define FUNC_NAME s_scm_quotient
  816. {
  817. if (SCM_LIKELY (scm_is_integer (x)))
  818. {
  819. if (SCM_LIKELY (scm_is_integer (y)))
  820. return scm_truncate_quotient (x, y);
  821. else
  822. return scm_wta_dispatch_2 (g_scm_quotient, x, y, SCM_ARG2, s_scm_quotient);
  823. }
  824. else
  825. return scm_wta_dispatch_2 (g_scm_quotient, x, y, SCM_ARG1, s_scm_quotient);
  826. }
  827. #undef FUNC_NAME
  828. SCM_PRIMITIVE_GENERIC (scm_remainder, "remainder", 2, 0, 0,
  829. (SCM x, SCM y),
  830. "Return the remainder of the numbers @var{x} and @var{y}.\n"
  831. "@lisp\n"
  832. "(remainder 13 4) @result{} 1\n"
  833. "(remainder -13 4) @result{} -1\n"
  834. "@end lisp")
  835. #define FUNC_NAME s_scm_remainder
  836. {
  837. if (SCM_LIKELY (scm_is_integer (x)))
  838. {
  839. if (SCM_LIKELY (scm_is_integer (y)))
  840. return scm_truncate_remainder (x, y);
  841. else
  842. return scm_wta_dispatch_2 (g_scm_remainder, x, y, SCM_ARG2, s_scm_remainder);
  843. }
  844. else
  845. return scm_wta_dispatch_2 (g_scm_remainder, x, y, SCM_ARG1, s_scm_remainder);
  846. }
  847. #undef FUNC_NAME
  848. SCM_PRIMITIVE_GENERIC (scm_modulo, "modulo", 2, 0, 0,
  849. (SCM x, SCM y),
  850. "Return the modulo of the numbers @var{x} and @var{y}.\n"
  851. "@lisp\n"
  852. "(modulo 13 4) @result{} 1\n"
  853. "(modulo -13 4) @result{} 3\n"
  854. "@end lisp")
  855. #define FUNC_NAME s_scm_modulo
  856. {
  857. if (SCM_LIKELY (scm_is_integer (x)))
  858. {
  859. if (SCM_LIKELY (scm_is_integer (y)))
  860. return scm_floor_remainder (x, y);
  861. else
  862. return scm_wta_dispatch_2 (g_scm_modulo, x, y, SCM_ARG2, s_scm_modulo);
  863. }
  864. else
  865. return scm_wta_dispatch_2 (g_scm_modulo, x, y, SCM_ARG1, s_scm_modulo);
  866. }
  867. #undef FUNC_NAME
  868. /* Return the exact integer q such that n = q*d, for exact integers n
  869. and d, where d is known in advance to divide n evenly (with zero
  870. remainder). For large integers, this can be computed more
  871. efficiently than when the remainder is unknown. */
  872. static SCM
  873. scm_exact_integer_quotient (SCM n, SCM d)
  874. #define FUNC_NAME "exact-integer-quotient"
  875. {
  876. if (SCM_LIKELY (SCM_I_INUMP (n)))
  877. {
  878. scm_t_inum nn = SCM_I_INUM (n);
  879. if (SCM_LIKELY (SCM_I_INUMP (d)))
  880. {
  881. scm_t_inum dd = SCM_I_INUM (d);
  882. if (SCM_UNLIKELY (dd == 0))
  883. scm_num_overflow ("exact-integer-quotient");
  884. else
  885. {
  886. scm_t_inum qq = nn / dd;
  887. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  888. return SCM_I_MAKINUM (qq);
  889. else
  890. return scm_i_inum2big (qq);
  891. }
  892. }
  893. else if (SCM_LIKELY (SCM_BIGP (d)))
  894. {
  895. /* n is an inum and d is a bignum. Given that d is known to
  896. divide n evenly, there are only two possibilities: n is 0,
  897. or else n is fixnum-min and d is abs(fixnum-min). */
  898. if (nn == 0)
  899. return SCM_INUM0;
  900. else
  901. return SCM_I_MAKINUM (-1);
  902. }
  903. else
  904. SCM_WRONG_TYPE_ARG (2, d);
  905. }
  906. else if (SCM_LIKELY (SCM_BIGP (n)))
  907. {
  908. if (SCM_LIKELY (SCM_I_INUMP (d)))
  909. {
  910. scm_t_inum dd = SCM_I_INUM (d);
  911. if (SCM_UNLIKELY (dd == 0))
  912. scm_num_overflow ("exact-integer-quotient");
  913. else if (SCM_UNLIKELY (dd == 1))
  914. return n;
  915. else
  916. {
  917. SCM q = scm_i_mkbig ();
  918. if (dd > 0)
  919. mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), dd);
  920. else
  921. {
  922. mpz_divexact_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), -dd);
  923. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  924. }
  925. scm_remember_upto_here_1 (n);
  926. return scm_i_normbig (q);
  927. }
  928. }
  929. else if (SCM_LIKELY (SCM_BIGP (d)))
  930. {
  931. SCM q = scm_i_mkbig ();
  932. mpz_divexact (SCM_I_BIG_MPZ (q),
  933. SCM_I_BIG_MPZ (n),
  934. SCM_I_BIG_MPZ (d));
  935. scm_remember_upto_here_2 (n, d);
  936. return scm_i_normbig (q);
  937. }
  938. else
  939. SCM_WRONG_TYPE_ARG (2, d);
  940. }
  941. else
  942. SCM_WRONG_TYPE_ARG (1, n);
  943. }
  944. #undef FUNC_NAME
  945. /* two_valued_wta_dispatch_2 is a version of SCM_WTA_DISPATCH_2 for
  946. two-valued functions. It is called from primitive generics that take
  947. two arguments and return two values, when the core procedure is
  948. unable to handle the given argument types. If there are GOOPS
  949. methods for this primitive generic, it dispatches to GOOPS and, if
  950. successful, expects two values to be returned, which are placed in
  951. *rp1 and *rp2. If there are no GOOPS methods, it throws a
  952. wrong-type-arg exception.
  953. FIXME: This obviously belongs somewhere else, but until we decide on
  954. the right API, it is here as a static function, because it is needed
  955. by the *_divide functions below.
  956. */
  957. static void
  958. two_valued_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos,
  959. const char *subr, SCM *rp1, SCM *rp2)
  960. {
  961. SCM vals = scm_wta_dispatch_2 (gf, a1, a2, pos, subr);
  962. scm_i_extract_values_2 (vals, rp1, rp2);
  963. }
  964. SCM_DEFINE (scm_euclidean_quotient, "euclidean-quotient", 2, 0, 0,
  965. (SCM x, SCM y),
  966. "Return the integer @var{q} such that\n"
  967. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  968. "where @math{0 <= @var{r} < abs(@var{y})}.\n"
  969. "@lisp\n"
  970. "(euclidean-quotient 123 10) @result{} 12\n"
  971. "(euclidean-quotient 123 -10) @result{} -12\n"
  972. "(euclidean-quotient -123 10) @result{} -13\n"
  973. "(euclidean-quotient -123 -10) @result{} 13\n"
  974. "(euclidean-quotient -123.2 -63.5) @result{} 2.0\n"
  975. "(euclidean-quotient 16/3 -10/7) @result{} -3\n"
  976. "@end lisp")
  977. #define FUNC_NAME s_scm_euclidean_quotient
  978. {
  979. if (scm_is_false (scm_negative_p (y)))
  980. return scm_floor_quotient (x, y);
  981. else
  982. return scm_ceiling_quotient (x, y);
  983. }
  984. #undef FUNC_NAME
  985. SCM_DEFINE (scm_euclidean_remainder, "euclidean-remainder", 2, 0, 0,
  986. (SCM x, SCM y),
  987. "Return the real number @var{r} such that\n"
  988. "@math{0 <= @var{r} < abs(@var{y})} and\n"
  989. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  990. "for some integer @var{q}.\n"
  991. "@lisp\n"
  992. "(euclidean-remainder 123 10) @result{} 3\n"
  993. "(euclidean-remainder 123 -10) @result{} 3\n"
  994. "(euclidean-remainder -123 10) @result{} 7\n"
  995. "(euclidean-remainder -123 -10) @result{} 7\n"
  996. "(euclidean-remainder -123.2 -63.5) @result{} 3.8\n"
  997. "(euclidean-remainder 16/3 -10/7) @result{} 22/21\n"
  998. "@end lisp")
  999. #define FUNC_NAME s_scm_euclidean_remainder
  1000. {
  1001. if (scm_is_false (scm_negative_p (y)))
  1002. return scm_floor_remainder (x, y);
  1003. else
  1004. return scm_ceiling_remainder (x, y);
  1005. }
  1006. #undef FUNC_NAME
  1007. SCM_DEFINE (scm_i_euclidean_divide, "euclidean/", 2, 0, 0,
  1008. (SCM x, SCM y),
  1009. "Return the integer @var{q} and the real number @var{r}\n"
  1010. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1011. "and @math{0 <= @var{r} < abs(@var{y})}.\n"
  1012. "@lisp\n"
  1013. "(euclidean/ 123 10) @result{} 12 and 3\n"
  1014. "(euclidean/ 123 -10) @result{} -12 and 3\n"
  1015. "(euclidean/ -123 10) @result{} -13 and 7\n"
  1016. "(euclidean/ -123 -10) @result{} 13 and 7\n"
  1017. "(euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  1018. "(euclidean/ 16/3 -10/7) @result{} -3 and 22/21\n"
  1019. "@end lisp")
  1020. #define FUNC_NAME s_scm_i_euclidean_divide
  1021. {
  1022. if (scm_is_false (scm_negative_p (y)))
  1023. return scm_i_floor_divide (x, y);
  1024. else
  1025. return scm_i_ceiling_divide (x, y);
  1026. }
  1027. #undef FUNC_NAME
  1028. void
  1029. scm_euclidean_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1030. {
  1031. if (scm_is_false (scm_negative_p (y)))
  1032. scm_floor_divide (x, y, qp, rp);
  1033. else
  1034. scm_ceiling_divide (x, y, qp, rp);
  1035. }
  1036. static SCM scm_i_inexact_floor_quotient (double x, double y);
  1037. static SCM scm_i_exact_rational_floor_quotient (SCM x, SCM y);
  1038. SCM_PRIMITIVE_GENERIC (scm_floor_quotient, "floor-quotient", 2, 0, 0,
  1039. (SCM x, SCM y),
  1040. "Return the floor of @math{@var{x} / @var{y}}.\n"
  1041. "@lisp\n"
  1042. "(floor-quotient 123 10) @result{} 12\n"
  1043. "(floor-quotient 123 -10) @result{} -13\n"
  1044. "(floor-quotient -123 10) @result{} -13\n"
  1045. "(floor-quotient -123 -10) @result{} 12\n"
  1046. "(floor-quotient -123.2 -63.5) @result{} 1.0\n"
  1047. "(floor-quotient 16/3 -10/7) @result{} -4\n"
  1048. "@end lisp")
  1049. #define FUNC_NAME s_scm_floor_quotient
  1050. {
  1051. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1052. {
  1053. scm_t_inum xx = SCM_I_INUM (x);
  1054. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1055. {
  1056. scm_t_inum yy = SCM_I_INUM (y);
  1057. scm_t_inum xx1 = xx;
  1058. scm_t_inum qq;
  1059. if (SCM_LIKELY (yy > 0))
  1060. {
  1061. if (SCM_UNLIKELY (xx < 0))
  1062. xx1 = xx - yy + 1;
  1063. }
  1064. else if (SCM_UNLIKELY (yy == 0))
  1065. scm_num_overflow (s_scm_floor_quotient);
  1066. else if (xx > 0)
  1067. xx1 = xx - yy - 1;
  1068. qq = xx1 / yy;
  1069. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  1070. return SCM_I_MAKINUM (qq);
  1071. else
  1072. return scm_i_inum2big (qq);
  1073. }
  1074. else if (SCM_BIGP (y))
  1075. {
  1076. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1077. scm_remember_upto_here_1 (y);
  1078. if (sign > 0)
  1079. return SCM_I_MAKINUM ((xx < 0) ? -1 : 0);
  1080. else
  1081. return SCM_I_MAKINUM ((xx > 0) ? -1 : 0);
  1082. }
  1083. else if (SCM_REALP (y))
  1084. return scm_i_inexact_floor_quotient (xx, SCM_REAL_VALUE (y));
  1085. else if (SCM_FRACTIONP (y))
  1086. return scm_i_exact_rational_floor_quotient (x, y);
  1087. else
  1088. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  1089. s_scm_floor_quotient);
  1090. }
  1091. else if (SCM_BIGP (x))
  1092. {
  1093. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1094. {
  1095. scm_t_inum yy = SCM_I_INUM (y);
  1096. if (SCM_UNLIKELY (yy == 0))
  1097. scm_num_overflow (s_scm_floor_quotient);
  1098. else if (SCM_UNLIKELY (yy == 1))
  1099. return x;
  1100. else
  1101. {
  1102. SCM q = scm_i_mkbig ();
  1103. if (yy > 0)
  1104. mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
  1105. else
  1106. {
  1107. mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
  1108. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  1109. }
  1110. scm_remember_upto_here_1 (x);
  1111. return scm_i_normbig (q);
  1112. }
  1113. }
  1114. else if (SCM_BIGP (y))
  1115. {
  1116. SCM q = scm_i_mkbig ();
  1117. mpz_fdiv_q (SCM_I_BIG_MPZ (q),
  1118. SCM_I_BIG_MPZ (x),
  1119. SCM_I_BIG_MPZ (y));
  1120. scm_remember_upto_here_2 (x, y);
  1121. return scm_i_normbig (q);
  1122. }
  1123. else if (SCM_REALP (y))
  1124. return scm_i_inexact_floor_quotient
  1125. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  1126. else if (SCM_FRACTIONP (y))
  1127. return scm_i_exact_rational_floor_quotient (x, y);
  1128. else
  1129. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  1130. s_scm_floor_quotient);
  1131. }
  1132. else if (SCM_REALP (x))
  1133. {
  1134. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1135. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1136. return scm_i_inexact_floor_quotient
  1137. (SCM_REAL_VALUE (x), scm_to_double (y));
  1138. else
  1139. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  1140. s_scm_floor_quotient);
  1141. }
  1142. else if (SCM_FRACTIONP (x))
  1143. {
  1144. if (SCM_REALP (y))
  1145. return scm_i_inexact_floor_quotient
  1146. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1147. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1148. return scm_i_exact_rational_floor_quotient (x, y);
  1149. else
  1150. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  1151. s_scm_floor_quotient);
  1152. }
  1153. else
  1154. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG1,
  1155. s_scm_floor_quotient);
  1156. }
  1157. #undef FUNC_NAME
  1158. static SCM
  1159. scm_i_inexact_floor_quotient (double x, double y)
  1160. {
  1161. if (SCM_UNLIKELY (y == 0))
  1162. scm_num_overflow (s_scm_floor_quotient); /* or return a NaN? */
  1163. else
  1164. return scm_i_from_double (floor (x / y));
  1165. }
  1166. static SCM
  1167. scm_i_exact_rational_floor_quotient (SCM x, SCM y)
  1168. {
  1169. return scm_floor_quotient
  1170. (scm_product (scm_numerator (x), scm_denominator (y)),
  1171. scm_product (scm_numerator (y), scm_denominator (x)));
  1172. }
  1173. static SCM scm_i_inexact_floor_remainder (double x, double y);
  1174. static SCM scm_i_exact_rational_floor_remainder (SCM x, SCM y);
  1175. SCM_PRIMITIVE_GENERIC (scm_floor_remainder, "floor-remainder", 2, 0, 0,
  1176. (SCM x, SCM y),
  1177. "Return the real number @var{r} such that\n"
  1178. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1179. "where @math{@var{q} = floor(@var{x} / @var{y})}.\n"
  1180. "@lisp\n"
  1181. "(floor-remainder 123 10) @result{} 3\n"
  1182. "(floor-remainder 123 -10) @result{} -7\n"
  1183. "(floor-remainder -123 10) @result{} 7\n"
  1184. "(floor-remainder -123 -10) @result{} -3\n"
  1185. "(floor-remainder -123.2 -63.5) @result{} -59.7\n"
  1186. "(floor-remainder 16/3 -10/7) @result{} -8/21\n"
  1187. "@end lisp")
  1188. #define FUNC_NAME s_scm_floor_remainder
  1189. {
  1190. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1191. {
  1192. scm_t_inum xx = SCM_I_INUM (x);
  1193. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1194. {
  1195. scm_t_inum yy = SCM_I_INUM (y);
  1196. if (SCM_UNLIKELY (yy == 0))
  1197. scm_num_overflow (s_scm_floor_remainder);
  1198. else
  1199. {
  1200. scm_t_inum rr = xx % yy;
  1201. int needs_adjustment;
  1202. if (SCM_LIKELY (yy > 0))
  1203. needs_adjustment = (rr < 0);
  1204. else
  1205. needs_adjustment = (rr > 0);
  1206. if (needs_adjustment)
  1207. rr += yy;
  1208. return SCM_I_MAKINUM (rr);
  1209. }
  1210. }
  1211. else if (SCM_BIGP (y))
  1212. {
  1213. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1214. scm_remember_upto_here_1 (y);
  1215. if (sign > 0)
  1216. {
  1217. if (xx < 0)
  1218. {
  1219. SCM r = scm_i_mkbig ();
  1220. mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
  1221. scm_remember_upto_here_1 (y);
  1222. return scm_i_normbig (r);
  1223. }
  1224. else
  1225. return x;
  1226. }
  1227. else if (xx <= 0)
  1228. return x;
  1229. else
  1230. {
  1231. SCM r = scm_i_mkbig ();
  1232. mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
  1233. scm_remember_upto_here_1 (y);
  1234. return scm_i_normbig (r);
  1235. }
  1236. }
  1237. else if (SCM_REALP (y))
  1238. return scm_i_inexact_floor_remainder (xx, SCM_REAL_VALUE (y));
  1239. else if (SCM_FRACTIONP (y))
  1240. return scm_i_exact_rational_floor_remainder (x, y);
  1241. else
  1242. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  1243. s_scm_floor_remainder);
  1244. }
  1245. else if (SCM_BIGP (x))
  1246. {
  1247. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1248. {
  1249. scm_t_inum yy = SCM_I_INUM (y);
  1250. if (SCM_UNLIKELY (yy == 0))
  1251. scm_num_overflow (s_scm_floor_remainder);
  1252. else
  1253. {
  1254. scm_t_inum rr;
  1255. if (yy > 0)
  1256. rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), yy);
  1257. else
  1258. rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy);
  1259. scm_remember_upto_here_1 (x);
  1260. return SCM_I_MAKINUM (rr);
  1261. }
  1262. }
  1263. else if (SCM_BIGP (y))
  1264. {
  1265. SCM r = scm_i_mkbig ();
  1266. mpz_fdiv_r (SCM_I_BIG_MPZ (r),
  1267. SCM_I_BIG_MPZ (x),
  1268. SCM_I_BIG_MPZ (y));
  1269. scm_remember_upto_here_2 (x, y);
  1270. return scm_i_normbig (r);
  1271. }
  1272. else if (SCM_REALP (y))
  1273. return scm_i_inexact_floor_remainder
  1274. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  1275. else if (SCM_FRACTIONP (y))
  1276. return scm_i_exact_rational_floor_remainder (x, y);
  1277. else
  1278. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  1279. s_scm_floor_remainder);
  1280. }
  1281. else if (SCM_REALP (x))
  1282. {
  1283. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1284. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1285. return scm_i_inexact_floor_remainder
  1286. (SCM_REAL_VALUE (x), scm_to_double (y));
  1287. else
  1288. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  1289. s_scm_floor_remainder);
  1290. }
  1291. else if (SCM_FRACTIONP (x))
  1292. {
  1293. if (SCM_REALP (y))
  1294. return scm_i_inexact_floor_remainder
  1295. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1296. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1297. return scm_i_exact_rational_floor_remainder (x, y);
  1298. else
  1299. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  1300. s_scm_floor_remainder);
  1301. }
  1302. else
  1303. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG1,
  1304. s_scm_floor_remainder);
  1305. }
  1306. #undef FUNC_NAME
  1307. static SCM
  1308. scm_i_inexact_floor_remainder (double x, double y)
  1309. {
  1310. /* Although it would be more efficient to use fmod here, we can't
  1311. because it would in some cases produce results inconsistent with
  1312. scm_i_inexact_floor_quotient, such that x != q * y + r (not even
  1313. close). In particular, when x is very close to a multiple of y,
  1314. then r might be either 0.0 or y, but those two cases must
  1315. correspond to different choices of q. If r = 0.0 then q must be
  1316. x/y, and if r = y then q must be x/y-1. If quotient chooses one
  1317. and remainder chooses the other, it would be bad. */
  1318. if (SCM_UNLIKELY (y == 0))
  1319. scm_num_overflow (s_scm_floor_remainder); /* or return a NaN? */
  1320. else
  1321. return scm_i_from_double (x - y * floor (x / y));
  1322. }
  1323. static SCM
  1324. scm_i_exact_rational_floor_remainder (SCM x, SCM y)
  1325. {
  1326. SCM xd = scm_denominator (x);
  1327. SCM yd = scm_denominator (y);
  1328. SCM r1 = scm_floor_remainder (scm_product (scm_numerator (x), yd),
  1329. scm_product (scm_numerator (y), xd));
  1330. return scm_divide (r1, scm_product (xd, yd));
  1331. }
  1332. static void scm_i_inexact_floor_divide (double x, double y,
  1333. SCM *qp, SCM *rp);
  1334. static void scm_i_exact_rational_floor_divide (SCM x, SCM y,
  1335. SCM *qp, SCM *rp);
  1336. SCM_PRIMITIVE_GENERIC (scm_i_floor_divide, "floor/", 2, 0, 0,
  1337. (SCM x, SCM y),
  1338. "Return the integer @var{q} and the real number @var{r}\n"
  1339. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1340. "and @math{@var{q} = floor(@var{x} / @var{y})}.\n"
  1341. "@lisp\n"
  1342. "(floor/ 123 10) @result{} 12 and 3\n"
  1343. "(floor/ 123 -10) @result{} -13 and -7\n"
  1344. "(floor/ -123 10) @result{} -13 and 7\n"
  1345. "(floor/ -123 -10) @result{} 12 and -3\n"
  1346. "(floor/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
  1347. "(floor/ 16/3 -10/7) @result{} -4 and -8/21\n"
  1348. "@end lisp")
  1349. #define FUNC_NAME s_scm_i_floor_divide
  1350. {
  1351. SCM q, r;
  1352. scm_floor_divide(x, y, &q, &r);
  1353. return scm_values_2 (q, r);
  1354. }
  1355. #undef FUNC_NAME
  1356. #define s_scm_floor_divide s_scm_i_floor_divide
  1357. #define g_scm_floor_divide g_scm_i_floor_divide
  1358. void
  1359. scm_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1360. {
  1361. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1362. {
  1363. scm_t_inum xx = SCM_I_INUM (x);
  1364. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1365. {
  1366. scm_t_inum yy = SCM_I_INUM (y);
  1367. if (SCM_UNLIKELY (yy == 0))
  1368. scm_num_overflow (s_scm_floor_divide);
  1369. else
  1370. {
  1371. scm_t_inum qq = xx / yy;
  1372. scm_t_inum rr = xx % yy;
  1373. int needs_adjustment;
  1374. if (SCM_LIKELY (yy > 0))
  1375. needs_adjustment = (rr < 0);
  1376. else
  1377. needs_adjustment = (rr > 0);
  1378. if (needs_adjustment)
  1379. {
  1380. rr += yy;
  1381. qq--;
  1382. }
  1383. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  1384. *qp = SCM_I_MAKINUM (qq);
  1385. else
  1386. *qp = scm_i_inum2big (qq);
  1387. *rp = SCM_I_MAKINUM (rr);
  1388. }
  1389. }
  1390. else if (SCM_BIGP (y))
  1391. {
  1392. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1393. scm_remember_upto_here_1 (y);
  1394. if (sign > 0)
  1395. {
  1396. if (xx < 0)
  1397. {
  1398. SCM r = scm_i_mkbig ();
  1399. mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
  1400. scm_remember_upto_here_1 (y);
  1401. *qp = SCM_I_MAKINUM (-1);
  1402. *rp = scm_i_normbig (r);
  1403. }
  1404. else
  1405. {
  1406. *qp = SCM_INUM0;
  1407. *rp = x;
  1408. }
  1409. }
  1410. else if (xx <= 0)
  1411. {
  1412. *qp = SCM_INUM0;
  1413. *rp = x;
  1414. }
  1415. else
  1416. {
  1417. SCM r = scm_i_mkbig ();
  1418. mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
  1419. scm_remember_upto_here_1 (y);
  1420. *qp = SCM_I_MAKINUM (-1);
  1421. *rp = scm_i_normbig (r);
  1422. }
  1423. }
  1424. else if (SCM_REALP (y))
  1425. scm_i_inexact_floor_divide (xx, SCM_REAL_VALUE (y), qp, rp);
  1426. else if (SCM_FRACTIONP (y))
  1427. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  1428. else
  1429. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1430. s_scm_floor_divide, qp, rp);
  1431. }
  1432. else if (SCM_BIGP (x))
  1433. {
  1434. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1435. {
  1436. scm_t_inum yy = SCM_I_INUM (y);
  1437. if (SCM_UNLIKELY (yy == 0))
  1438. scm_num_overflow (s_scm_floor_divide);
  1439. else
  1440. {
  1441. SCM q = scm_i_mkbig ();
  1442. SCM r = scm_i_mkbig ();
  1443. if (yy > 0)
  1444. mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1445. SCM_I_BIG_MPZ (x), yy);
  1446. else
  1447. {
  1448. mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1449. SCM_I_BIG_MPZ (x), -yy);
  1450. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  1451. }
  1452. scm_remember_upto_here_1 (x);
  1453. *qp = scm_i_normbig (q);
  1454. *rp = scm_i_normbig (r);
  1455. }
  1456. }
  1457. else if (SCM_BIGP (y))
  1458. {
  1459. SCM q = scm_i_mkbig ();
  1460. SCM r = scm_i_mkbig ();
  1461. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1462. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  1463. scm_remember_upto_here_2 (x, y);
  1464. *qp = scm_i_normbig (q);
  1465. *rp = scm_i_normbig (r);
  1466. }
  1467. else if (SCM_REALP (y))
  1468. scm_i_inexact_floor_divide (scm_i_big2dbl (x), SCM_REAL_VALUE (y),
  1469. qp, rp);
  1470. else if (SCM_FRACTIONP (y))
  1471. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  1472. else
  1473. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1474. s_scm_floor_divide, qp, rp);
  1475. }
  1476. else if (SCM_REALP (x))
  1477. {
  1478. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1479. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1480. scm_i_inexact_floor_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  1481. qp, rp);
  1482. else
  1483. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1484. s_scm_floor_divide, qp, rp);
  1485. }
  1486. else if (SCM_FRACTIONP (x))
  1487. {
  1488. if (SCM_REALP (y))
  1489. scm_i_inexact_floor_divide
  1490. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  1491. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1492. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  1493. else
  1494. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1495. s_scm_floor_divide, qp, rp);
  1496. }
  1497. else
  1498. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG1,
  1499. s_scm_floor_divide, qp, rp);
  1500. }
  1501. static void
  1502. scm_i_inexact_floor_divide (double x, double y, SCM *qp, SCM *rp)
  1503. {
  1504. if (SCM_UNLIKELY (y == 0))
  1505. scm_num_overflow (s_scm_floor_divide); /* or return a NaN? */
  1506. else
  1507. {
  1508. double q = floor (x / y);
  1509. double r = x - q * y;
  1510. *qp = scm_i_from_double (q);
  1511. *rp = scm_i_from_double (r);
  1512. }
  1513. }
  1514. static void
  1515. scm_i_exact_rational_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1516. {
  1517. SCM r1;
  1518. SCM xd = scm_denominator (x);
  1519. SCM yd = scm_denominator (y);
  1520. scm_floor_divide (scm_product (scm_numerator (x), yd),
  1521. scm_product (scm_numerator (y), xd),
  1522. qp, &r1);
  1523. *rp = scm_divide (r1, scm_product (xd, yd));
  1524. }
  1525. static SCM scm_i_inexact_ceiling_quotient (double x, double y);
  1526. static SCM scm_i_exact_rational_ceiling_quotient (SCM x, SCM y);
  1527. SCM_PRIMITIVE_GENERIC (scm_ceiling_quotient, "ceiling-quotient", 2, 0, 0,
  1528. (SCM x, SCM y),
  1529. "Return the ceiling of @math{@var{x} / @var{y}}.\n"
  1530. "@lisp\n"
  1531. "(ceiling-quotient 123 10) @result{} 13\n"
  1532. "(ceiling-quotient 123 -10) @result{} -12\n"
  1533. "(ceiling-quotient -123 10) @result{} -12\n"
  1534. "(ceiling-quotient -123 -10) @result{} 13\n"
  1535. "(ceiling-quotient -123.2 -63.5) @result{} 2.0\n"
  1536. "(ceiling-quotient 16/3 -10/7) @result{} -3\n"
  1537. "@end lisp")
  1538. #define FUNC_NAME s_scm_ceiling_quotient
  1539. {
  1540. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1541. {
  1542. scm_t_inum xx = SCM_I_INUM (x);
  1543. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1544. {
  1545. scm_t_inum yy = SCM_I_INUM (y);
  1546. if (SCM_UNLIKELY (yy == 0))
  1547. scm_num_overflow (s_scm_ceiling_quotient);
  1548. else
  1549. {
  1550. scm_t_inum xx1 = xx;
  1551. scm_t_inum qq;
  1552. if (SCM_LIKELY (yy > 0))
  1553. {
  1554. if (SCM_LIKELY (xx >= 0))
  1555. xx1 = xx + yy - 1;
  1556. }
  1557. else if (xx < 0)
  1558. xx1 = xx + yy + 1;
  1559. qq = xx1 / yy;
  1560. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  1561. return SCM_I_MAKINUM (qq);
  1562. else
  1563. return scm_i_inum2big (qq);
  1564. }
  1565. }
  1566. else if (SCM_BIGP (y))
  1567. {
  1568. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1569. scm_remember_upto_here_1 (y);
  1570. if (SCM_LIKELY (sign > 0))
  1571. {
  1572. if (SCM_LIKELY (xx > 0))
  1573. return SCM_INUM1;
  1574. else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  1575. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  1576. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  1577. {
  1578. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  1579. scm_remember_upto_here_1 (y);
  1580. return SCM_I_MAKINUM (-1);
  1581. }
  1582. else
  1583. return SCM_INUM0;
  1584. }
  1585. else if (xx >= 0)
  1586. return SCM_INUM0;
  1587. else
  1588. return SCM_INUM1;
  1589. }
  1590. else if (SCM_REALP (y))
  1591. return scm_i_inexact_ceiling_quotient (xx, SCM_REAL_VALUE (y));
  1592. else if (SCM_FRACTIONP (y))
  1593. return scm_i_exact_rational_ceiling_quotient (x, y);
  1594. else
  1595. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1596. s_scm_ceiling_quotient);
  1597. }
  1598. else if (SCM_BIGP (x))
  1599. {
  1600. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1601. {
  1602. scm_t_inum yy = SCM_I_INUM (y);
  1603. if (SCM_UNLIKELY (yy == 0))
  1604. scm_num_overflow (s_scm_ceiling_quotient);
  1605. else if (SCM_UNLIKELY (yy == 1))
  1606. return x;
  1607. else
  1608. {
  1609. SCM q = scm_i_mkbig ();
  1610. if (yy > 0)
  1611. mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
  1612. else
  1613. {
  1614. mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
  1615. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  1616. }
  1617. scm_remember_upto_here_1 (x);
  1618. return scm_i_normbig (q);
  1619. }
  1620. }
  1621. else if (SCM_BIGP (y))
  1622. {
  1623. SCM q = scm_i_mkbig ();
  1624. mpz_cdiv_q (SCM_I_BIG_MPZ (q),
  1625. SCM_I_BIG_MPZ (x),
  1626. SCM_I_BIG_MPZ (y));
  1627. scm_remember_upto_here_2 (x, y);
  1628. return scm_i_normbig (q);
  1629. }
  1630. else if (SCM_REALP (y))
  1631. return scm_i_inexact_ceiling_quotient
  1632. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  1633. else if (SCM_FRACTIONP (y))
  1634. return scm_i_exact_rational_ceiling_quotient (x, y);
  1635. else
  1636. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1637. s_scm_ceiling_quotient);
  1638. }
  1639. else if (SCM_REALP (x))
  1640. {
  1641. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1642. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1643. return scm_i_inexact_ceiling_quotient
  1644. (SCM_REAL_VALUE (x), scm_to_double (y));
  1645. else
  1646. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1647. s_scm_ceiling_quotient);
  1648. }
  1649. else if (SCM_FRACTIONP (x))
  1650. {
  1651. if (SCM_REALP (y))
  1652. return scm_i_inexact_ceiling_quotient
  1653. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1654. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1655. return scm_i_exact_rational_ceiling_quotient (x, y);
  1656. else
  1657. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1658. s_scm_ceiling_quotient);
  1659. }
  1660. else
  1661. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG1,
  1662. s_scm_ceiling_quotient);
  1663. }
  1664. #undef FUNC_NAME
  1665. static SCM
  1666. scm_i_inexact_ceiling_quotient (double x, double y)
  1667. {
  1668. if (SCM_UNLIKELY (y == 0))
  1669. scm_num_overflow (s_scm_ceiling_quotient); /* or return a NaN? */
  1670. else
  1671. return scm_i_from_double (ceil (x / y));
  1672. }
  1673. static SCM
  1674. scm_i_exact_rational_ceiling_quotient (SCM x, SCM y)
  1675. {
  1676. return scm_ceiling_quotient
  1677. (scm_product (scm_numerator (x), scm_denominator (y)),
  1678. scm_product (scm_numerator (y), scm_denominator (x)));
  1679. }
  1680. static SCM scm_i_inexact_ceiling_remainder (double x, double y);
  1681. static SCM scm_i_exact_rational_ceiling_remainder (SCM x, SCM y);
  1682. SCM_PRIMITIVE_GENERIC (scm_ceiling_remainder, "ceiling-remainder", 2, 0, 0,
  1683. (SCM x, SCM y),
  1684. "Return the real number @var{r} such that\n"
  1685. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1686. "where @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
  1687. "@lisp\n"
  1688. "(ceiling-remainder 123 10) @result{} -7\n"
  1689. "(ceiling-remainder 123 -10) @result{} 3\n"
  1690. "(ceiling-remainder -123 10) @result{} -3\n"
  1691. "(ceiling-remainder -123 -10) @result{} 7\n"
  1692. "(ceiling-remainder -123.2 -63.5) @result{} 3.8\n"
  1693. "(ceiling-remainder 16/3 -10/7) @result{} 22/21\n"
  1694. "@end lisp")
  1695. #define FUNC_NAME s_scm_ceiling_remainder
  1696. {
  1697. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1698. {
  1699. scm_t_inum xx = SCM_I_INUM (x);
  1700. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1701. {
  1702. scm_t_inum yy = SCM_I_INUM (y);
  1703. if (SCM_UNLIKELY (yy == 0))
  1704. scm_num_overflow (s_scm_ceiling_remainder);
  1705. else
  1706. {
  1707. scm_t_inum rr = xx % yy;
  1708. int needs_adjustment;
  1709. if (SCM_LIKELY (yy > 0))
  1710. needs_adjustment = (rr > 0);
  1711. else
  1712. needs_adjustment = (rr < 0);
  1713. if (needs_adjustment)
  1714. rr -= yy;
  1715. return SCM_I_MAKINUM (rr);
  1716. }
  1717. }
  1718. else if (SCM_BIGP (y))
  1719. {
  1720. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1721. scm_remember_upto_here_1 (y);
  1722. if (SCM_LIKELY (sign > 0))
  1723. {
  1724. if (SCM_LIKELY (xx > 0))
  1725. {
  1726. SCM r = scm_i_mkbig ();
  1727. mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
  1728. scm_remember_upto_here_1 (y);
  1729. mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
  1730. return scm_i_normbig (r);
  1731. }
  1732. else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  1733. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  1734. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  1735. {
  1736. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  1737. scm_remember_upto_here_1 (y);
  1738. return SCM_INUM0;
  1739. }
  1740. else
  1741. return x;
  1742. }
  1743. else if (xx >= 0)
  1744. return x;
  1745. else
  1746. {
  1747. SCM r = scm_i_mkbig ();
  1748. mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
  1749. scm_remember_upto_here_1 (y);
  1750. mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
  1751. return scm_i_normbig (r);
  1752. }
  1753. }
  1754. else if (SCM_REALP (y))
  1755. return scm_i_inexact_ceiling_remainder (xx, SCM_REAL_VALUE (y));
  1756. else if (SCM_FRACTIONP (y))
  1757. return scm_i_exact_rational_ceiling_remainder (x, y);
  1758. else
  1759. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1760. s_scm_ceiling_remainder);
  1761. }
  1762. else if (SCM_BIGP (x))
  1763. {
  1764. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1765. {
  1766. scm_t_inum yy = SCM_I_INUM (y);
  1767. if (SCM_UNLIKELY (yy == 0))
  1768. scm_num_overflow (s_scm_ceiling_remainder);
  1769. else
  1770. {
  1771. scm_t_inum rr;
  1772. if (yy > 0)
  1773. rr = -mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy);
  1774. else
  1775. rr = mpz_fdiv_ui (SCM_I_BIG_MPZ (x), -yy);
  1776. scm_remember_upto_here_1 (x);
  1777. return SCM_I_MAKINUM (rr);
  1778. }
  1779. }
  1780. else if (SCM_BIGP (y))
  1781. {
  1782. SCM r = scm_i_mkbig ();
  1783. mpz_cdiv_r (SCM_I_BIG_MPZ (r),
  1784. SCM_I_BIG_MPZ (x),
  1785. SCM_I_BIG_MPZ (y));
  1786. scm_remember_upto_here_2 (x, y);
  1787. return scm_i_normbig (r);
  1788. }
  1789. else if (SCM_REALP (y))
  1790. return scm_i_inexact_ceiling_remainder
  1791. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  1792. else if (SCM_FRACTIONP (y))
  1793. return scm_i_exact_rational_ceiling_remainder (x, y);
  1794. else
  1795. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1796. s_scm_ceiling_remainder);
  1797. }
  1798. else if (SCM_REALP (x))
  1799. {
  1800. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1801. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1802. return scm_i_inexact_ceiling_remainder
  1803. (SCM_REAL_VALUE (x), scm_to_double (y));
  1804. else
  1805. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1806. s_scm_ceiling_remainder);
  1807. }
  1808. else if (SCM_FRACTIONP (x))
  1809. {
  1810. if (SCM_REALP (y))
  1811. return scm_i_inexact_ceiling_remainder
  1812. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1813. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1814. return scm_i_exact_rational_ceiling_remainder (x, y);
  1815. else
  1816. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1817. s_scm_ceiling_remainder);
  1818. }
  1819. else
  1820. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG1,
  1821. s_scm_ceiling_remainder);
  1822. }
  1823. #undef FUNC_NAME
  1824. static SCM
  1825. scm_i_inexact_ceiling_remainder (double x, double y)
  1826. {
  1827. /* Although it would be more efficient to use fmod here, we can't
  1828. because it would in some cases produce results inconsistent with
  1829. scm_i_inexact_ceiling_quotient, such that x != q * y + r (not even
  1830. close). In particular, when x is very close to a multiple of y,
  1831. then r might be either 0.0 or -y, but those two cases must
  1832. correspond to different choices of q. If r = 0.0 then q must be
  1833. x/y, and if r = -y then q must be x/y+1. If quotient chooses one
  1834. and remainder chooses the other, it would be bad. */
  1835. if (SCM_UNLIKELY (y == 0))
  1836. scm_num_overflow (s_scm_ceiling_remainder); /* or return a NaN? */
  1837. else
  1838. return scm_i_from_double (x - y * ceil (x / y));
  1839. }
  1840. static SCM
  1841. scm_i_exact_rational_ceiling_remainder (SCM x, SCM y)
  1842. {
  1843. SCM xd = scm_denominator (x);
  1844. SCM yd = scm_denominator (y);
  1845. SCM r1 = scm_ceiling_remainder (scm_product (scm_numerator (x), yd),
  1846. scm_product (scm_numerator (y), xd));
  1847. return scm_divide (r1, scm_product (xd, yd));
  1848. }
  1849. static void scm_i_inexact_ceiling_divide (double x, double y,
  1850. SCM *qp, SCM *rp);
  1851. static void scm_i_exact_rational_ceiling_divide (SCM x, SCM y,
  1852. SCM *qp, SCM *rp);
  1853. SCM_PRIMITIVE_GENERIC (scm_i_ceiling_divide, "ceiling/", 2, 0, 0,
  1854. (SCM x, SCM y),
  1855. "Return the integer @var{q} and the real number @var{r}\n"
  1856. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1857. "and @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
  1858. "@lisp\n"
  1859. "(ceiling/ 123 10) @result{} 13 and -7\n"
  1860. "(ceiling/ 123 -10) @result{} -12 and 3\n"
  1861. "(ceiling/ -123 10) @result{} -12 and -3\n"
  1862. "(ceiling/ -123 -10) @result{} 13 and 7\n"
  1863. "(ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  1864. "(ceiling/ 16/3 -10/7) @result{} -3 and 22/21\n"
  1865. "@end lisp")
  1866. #define FUNC_NAME s_scm_i_ceiling_divide
  1867. {
  1868. SCM q, r;
  1869. scm_ceiling_divide(x, y, &q, &r);
  1870. return scm_values_2 (q, r);
  1871. }
  1872. #undef FUNC_NAME
  1873. #define s_scm_ceiling_divide s_scm_i_ceiling_divide
  1874. #define g_scm_ceiling_divide g_scm_i_ceiling_divide
  1875. void
  1876. scm_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1877. {
  1878. if (SCM_LIKELY (SCM_I_INUMP (x)))
  1879. {
  1880. scm_t_inum xx = SCM_I_INUM (x);
  1881. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1882. {
  1883. scm_t_inum yy = SCM_I_INUM (y);
  1884. if (SCM_UNLIKELY (yy == 0))
  1885. scm_num_overflow (s_scm_ceiling_divide);
  1886. else
  1887. {
  1888. scm_t_inum qq = xx / yy;
  1889. scm_t_inum rr = xx % yy;
  1890. int needs_adjustment;
  1891. if (SCM_LIKELY (yy > 0))
  1892. needs_adjustment = (rr > 0);
  1893. else
  1894. needs_adjustment = (rr < 0);
  1895. if (needs_adjustment)
  1896. {
  1897. rr -= yy;
  1898. qq++;
  1899. }
  1900. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  1901. *qp = SCM_I_MAKINUM (qq);
  1902. else
  1903. *qp = scm_i_inum2big (qq);
  1904. *rp = SCM_I_MAKINUM (rr);
  1905. }
  1906. }
  1907. else if (SCM_BIGP (y))
  1908. {
  1909. int sign = mpz_sgn (SCM_I_BIG_MPZ (y));
  1910. scm_remember_upto_here_1 (y);
  1911. if (SCM_LIKELY (sign > 0))
  1912. {
  1913. if (SCM_LIKELY (xx > 0))
  1914. {
  1915. SCM r = scm_i_mkbig ();
  1916. mpz_sub_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), xx);
  1917. scm_remember_upto_here_1 (y);
  1918. mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
  1919. *qp = SCM_INUM1;
  1920. *rp = scm_i_normbig (r);
  1921. }
  1922. else if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  1923. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  1924. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  1925. {
  1926. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  1927. scm_remember_upto_here_1 (y);
  1928. *qp = SCM_I_MAKINUM (-1);
  1929. *rp = SCM_INUM0;
  1930. }
  1931. else
  1932. {
  1933. *qp = SCM_INUM0;
  1934. *rp = x;
  1935. }
  1936. }
  1937. else if (xx >= 0)
  1938. {
  1939. *qp = SCM_INUM0;
  1940. *rp = x;
  1941. }
  1942. else
  1943. {
  1944. SCM r = scm_i_mkbig ();
  1945. mpz_add_ui (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y), -xx);
  1946. scm_remember_upto_here_1 (y);
  1947. mpz_neg (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r));
  1948. *qp = SCM_INUM1;
  1949. *rp = scm_i_normbig (r);
  1950. }
  1951. }
  1952. else if (SCM_REALP (y))
  1953. scm_i_inexact_ceiling_divide (xx, SCM_REAL_VALUE (y), qp, rp);
  1954. else if (SCM_FRACTIONP (y))
  1955. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  1956. else
  1957. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  1958. s_scm_ceiling_divide, qp, rp);
  1959. }
  1960. else if (SCM_BIGP (x))
  1961. {
  1962. if (SCM_LIKELY (SCM_I_INUMP (y)))
  1963. {
  1964. scm_t_inum yy = SCM_I_INUM (y);
  1965. if (SCM_UNLIKELY (yy == 0))
  1966. scm_num_overflow (s_scm_ceiling_divide);
  1967. else
  1968. {
  1969. SCM q = scm_i_mkbig ();
  1970. SCM r = scm_i_mkbig ();
  1971. if (yy > 0)
  1972. mpz_cdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1973. SCM_I_BIG_MPZ (x), yy);
  1974. else
  1975. {
  1976. mpz_fdiv_qr_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1977. SCM_I_BIG_MPZ (x), -yy);
  1978. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  1979. }
  1980. scm_remember_upto_here_1 (x);
  1981. *qp = scm_i_normbig (q);
  1982. *rp = scm_i_normbig (r);
  1983. }
  1984. }
  1985. else if (SCM_BIGP (y))
  1986. {
  1987. SCM q = scm_i_mkbig ();
  1988. SCM r = scm_i_mkbig ();
  1989. mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  1990. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  1991. scm_remember_upto_here_2 (x, y);
  1992. *qp = scm_i_normbig (q);
  1993. *rp = scm_i_normbig (r);
  1994. }
  1995. else if (SCM_REALP (y))
  1996. scm_i_inexact_ceiling_divide (scm_i_big2dbl (x), SCM_REAL_VALUE (y),
  1997. qp, rp);
  1998. else if (SCM_FRACTIONP (y))
  1999. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  2000. else
  2001. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  2002. s_scm_ceiling_divide, qp, rp);
  2003. }
  2004. else if (SCM_REALP (x))
  2005. {
  2006. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2007. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2008. scm_i_inexact_ceiling_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  2009. qp, rp);
  2010. else
  2011. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  2012. s_scm_ceiling_divide, qp, rp);
  2013. }
  2014. else if (SCM_FRACTIONP (x))
  2015. {
  2016. if (SCM_REALP (y))
  2017. scm_i_inexact_ceiling_divide
  2018. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  2019. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2020. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  2021. else
  2022. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  2023. s_scm_ceiling_divide, qp, rp);
  2024. }
  2025. else
  2026. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG1,
  2027. s_scm_ceiling_divide, qp, rp);
  2028. }
  2029. static void
  2030. scm_i_inexact_ceiling_divide (double x, double y, SCM *qp, SCM *rp)
  2031. {
  2032. if (SCM_UNLIKELY (y == 0))
  2033. scm_num_overflow (s_scm_ceiling_divide); /* or return a NaN? */
  2034. else
  2035. {
  2036. double q = ceil (x / y);
  2037. double r = x - q * y;
  2038. *qp = scm_i_from_double (q);
  2039. *rp = scm_i_from_double (r);
  2040. }
  2041. }
  2042. static void
  2043. scm_i_exact_rational_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2044. {
  2045. SCM r1;
  2046. SCM xd = scm_denominator (x);
  2047. SCM yd = scm_denominator (y);
  2048. scm_ceiling_divide (scm_product (scm_numerator (x), yd),
  2049. scm_product (scm_numerator (y), xd),
  2050. qp, &r1);
  2051. *rp = scm_divide (r1, scm_product (xd, yd));
  2052. }
  2053. static SCM scm_i_inexact_truncate_quotient (double x, double y);
  2054. static SCM scm_i_exact_rational_truncate_quotient (SCM x, SCM y);
  2055. SCM_PRIMITIVE_GENERIC (scm_truncate_quotient, "truncate-quotient", 2, 0, 0,
  2056. (SCM x, SCM y),
  2057. "Return @math{@var{x} / @var{y}} rounded toward zero.\n"
  2058. "@lisp\n"
  2059. "(truncate-quotient 123 10) @result{} 12\n"
  2060. "(truncate-quotient 123 -10) @result{} -12\n"
  2061. "(truncate-quotient -123 10) @result{} -12\n"
  2062. "(truncate-quotient -123 -10) @result{} 12\n"
  2063. "(truncate-quotient -123.2 -63.5) @result{} 1.0\n"
  2064. "(truncate-quotient 16/3 -10/7) @result{} -3\n"
  2065. "@end lisp")
  2066. #define FUNC_NAME s_scm_truncate_quotient
  2067. {
  2068. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2069. {
  2070. scm_t_inum xx = SCM_I_INUM (x);
  2071. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2072. {
  2073. scm_t_inum yy = SCM_I_INUM (y);
  2074. if (SCM_UNLIKELY (yy == 0))
  2075. scm_num_overflow (s_scm_truncate_quotient);
  2076. else
  2077. {
  2078. scm_t_inum qq = xx / yy;
  2079. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  2080. return SCM_I_MAKINUM (qq);
  2081. else
  2082. return scm_i_inum2big (qq);
  2083. }
  2084. }
  2085. else if (SCM_BIGP (y))
  2086. {
  2087. if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  2088. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  2089. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  2090. {
  2091. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  2092. scm_remember_upto_here_1 (y);
  2093. return SCM_I_MAKINUM (-1);
  2094. }
  2095. else
  2096. return SCM_INUM0;
  2097. }
  2098. else if (SCM_REALP (y))
  2099. return scm_i_inexact_truncate_quotient (xx, SCM_REAL_VALUE (y));
  2100. else if (SCM_FRACTIONP (y))
  2101. return scm_i_exact_rational_truncate_quotient (x, y);
  2102. else
  2103. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  2104. s_scm_truncate_quotient);
  2105. }
  2106. else if (SCM_BIGP (x))
  2107. {
  2108. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2109. {
  2110. scm_t_inum yy = SCM_I_INUM (y);
  2111. if (SCM_UNLIKELY (yy == 0))
  2112. scm_num_overflow (s_scm_truncate_quotient);
  2113. else if (SCM_UNLIKELY (yy == 1))
  2114. return x;
  2115. else
  2116. {
  2117. SCM q = scm_i_mkbig ();
  2118. if (yy > 0)
  2119. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), yy);
  2120. else
  2121. {
  2122. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (x), -yy);
  2123. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  2124. }
  2125. scm_remember_upto_here_1 (x);
  2126. return scm_i_normbig (q);
  2127. }
  2128. }
  2129. else if (SCM_BIGP (y))
  2130. {
  2131. SCM q = scm_i_mkbig ();
  2132. mpz_tdiv_q (SCM_I_BIG_MPZ (q),
  2133. SCM_I_BIG_MPZ (x),
  2134. SCM_I_BIG_MPZ (y));
  2135. scm_remember_upto_here_2 (x, y);
  2136. return scm_i_normbig (q);
  2137. }
  2138. else if (SCM_REALP (y))
  2139. return scm_i_inexact_truncate_quotient
  2140. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  2141. else if (SCM_FRACTIONP (y))
  2142. return scm_i_exact_rational_truncate_quotient (x, y);
  2143. else
  2144. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  2145. s_scm_truncate_quotient);
  2146. }
  2147. else if (SCM_REALP (x))
  2148. {
  2149. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2150. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2151. return scm_i_inexact_truncate_quotient
  2152. (SCM_REAL_VALUE (x), scm_to_double (y));
  2153. else
  2154. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  2155. s_scm_truncate_quotient);
  2156. }
  2157. else if (SCM_FRACTIONP (x))
  2158. {
  2159. if (SCM_REALP (y))
  2160. return scm_i_inexact_truncate_quotient
  2161. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2162. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2163. return scm_i_exact_rational_truncate_quotient (x, y);
  2164. else
  2165. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  2166. s_scm_truncate_quotient);
  2167. }
  2168. else
  2169. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG1,
  2170. s_scm_truncate_quotient);
  2171. }
  2172. #undef FUNC_NAME
  2173. static SCM
  2174. scm_i_inexact_truncate_quotient (double x, double y)
  2175. {
  2176. if (SCM_UNLIKELY (y == 0))
  2177. scm_num_overflow (s_scm_truncate_quotient); /* or return a NaN? */
  2178. else
  2179. return scm_i_from_double (trunc (x / y));
  2180. }
  2181. static SCM
  2182. scm_i_exact_rational_truncate_quotient (SCM x, SCM y)
  2183. {
  2184. return scm_truncate_quotient
  2185. (scm_product (scm_numerator (x), scm_denominator (y)),
  2186. scm_product (scm_numerator (y), scm_denominator (x)));
  2187. }
  2188. static SCM scm_i_inexact_truncate_remainder (double x, double y);
  2189. static SCM scm_i_exact_rational_truncate_remainder (SCM x, SCM y);
  2190. SCM_PRIMITIVE_GENERIC (scm_truncate_remainder, "truncate-remainder", 2, 0, 0,
  2191. (SCM x, SCM y),
  2192. "Return the real number @var{r} such that\n"
  2193. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  2194. "where @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
  2195. "@lisp\n"
  2196. "(truncate-remainder 123 10) @result{} 3\n"
  2197. "(truncate-remainder 123 -10) @result{} 3\n"
  2198. "(truncate-remainder -123 10) @result{} -3\n"
  2199. "(truncate-remainder -123 -10) @result{} -3\n"
  2200. "(truncate-remainder -123.2 -63.5) @result{} -59.7\n"
  2201. "(truncate-remainder 16/3 -10/7) @result{} 22/21\n"
  2202. "@end lisp")
  2203. #define FUNC_NAME s_scm_truncate_remainder
  2204. {
  2205. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2206. {
  2207. scm_t_inum xx = SCM_I_INUM (x);
  2208. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2209. {
  2210. scm_t_inum yy = SCM_I_INUM (y);
  2211. if (SCM_UNLIKELY (yy == 0))
  2212. scm_num_overflow (s_scm_truncate_remainder);
  2213. else
  2214. return SCM_I_MAKINUM (xx % yy);
  2215. }
  2216. else if (SCM_BIGP (y))
  2217. {
  2218. if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  2219. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  2220. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  2221. {
  2222. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  2223. scm_remember_upto_here_1 (y);
  2224. return SCM_INUM0;
  2225. }
  2226. else
  2227. return x;
  2228. }
  2229. else if (SCM_REALP (y))
  2230. return scm_i_inexact_truncate_remainder (xx, SCM_REAL_VALUE (y));
  2231. else if (SCM_FRACTIONP (y))
  2232. return scm_i_exact_rational_truncate_remainder (x, y);
  2233. else
  2234. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  2235. s_scm_truncate_remainder);
  2236. }
  2237. else if (SCM_BIGP (x))
  2238. {
  2239. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2240. {
  2241. scm_t_inum yy = SCM_I_INUM (y);
  2242. if (SCM_UNLIKELY (yy == 0))
  2243. scm_num_overflow (s_scm_truncate_remainder);
  2244. else
  2245. {
  2246. scm_t_inum rr = (mpz_tdiv_ui (SCM_I_BIG_MPZ (x),
  2247. (yy > 0) ? yy : -yy)
  2248. * mpz_sgn (SCM_I_BIG_MPZ (x)));
  2249. scm_remember_upto_here_1 (x);
  2250. return SCM_I_MAKINUM (rr);
  2251. }
  2252. }
  2253. else if (SCM_BIGP (y))
  2254. {
  2255. SCM r = scm_i_mkbig ();
  2256. mpz_tdiv_r (SCM_I_BIG_MPZ (r),
  2257. SCM_I_BIG_MPZ (x),
  2258. SCM_I_BIG_MPZ (y));
  2259. scm_remember_upto_here_2 (x, y);
  2260. return scm_i_normbig (r);
  2261. }
  2262. else if (SCM_REALP (y))
  2263. return scm_i_inexact_truncate_remainder
  2264. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  2265. else if (SCM_FRACTIONP (y))
  2266. return scm_i_exact_rational_truncate_remainder (x, y);
  2267. else
  2268. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  2269. s_scm_truncate_remainder);
  2270. }
  2271. else if (SCM_REALP (x))
  2272. {
  2273. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2274. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2275. return scm_i_inexact_truncate_remainder
  2276. (SCM_REAL_VALUE (x), scm_to_double (y));
  2277. else
  2278. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  2279. s_scm_truncate_remainder);
  2280. }
  2281. else if (SCM_FRACTIONP (x))
  2282. {
  2283. if (SCM_REALP (y))
  2284. return scm_i_inexact_truncate_remainder
  2285. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2286. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2287. return scm_i_exact_rational_truncate_remainder (x, y);
  2288. else
  2289. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  2290. s_scm_truncate_remainder);
  2291. }
  2292. else
  2293. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG1,
  2294. s_scm_truncate_remainder);
  2295. }
  2296. #undef FUNC_NAME
  2297. static SCM
  2298. scm_i_inexact_truncate_remainder (double x, double y)
  2299. {
  2300. /* Although it would be more efficient to use fmod here, we can't
  2301. because it would in some cases produce results inconsistent with
  2302. scm_i_inexact_truncate_quotient, such that x != q * y + r (not even
  2303. close). In particular, when x is very close to a multiple of y,
  2304. then r might be either 0.0 or sgn(x)*|y|, but those two cases must
  2305. correspond to different choices of q. If quotient chooses one and
  2306. remainder chooses the other, it would be bad. */
  2307. if (SCM_UNLIKELY (y == 0))
  2308. scm_num_overflow (s_scm_truncate_remainder); /* or return a NaN? */
  2309. else
  2310. return scm_i_from_double (x - y * trunc (x / y));
  2311. }
  2312. static SCM
  2313. scm_i_exact_rational_truncate_remainder (SCM x, SCM y)
  2314. {
  2315. SCM xd = scm_denominator (x);
  2316. SCM yd = scm_denominator (y);
  2317. SCM r1 = scm_truncate_remainder (scm_product (scm_numerator (x), yd),
  2318. scm_product (scm_numerator (y), xd));
  2319. return scm_divide (r1, scm_product (xd, yd));
  2320. }
  2321. static void scm_i_inexact_truncate_divide (double x, double y,
  2322. SCM *qp, SCM *rp);
  2323. static void scm_i_exact_rational_truncate_divide (SCM x, SCM y,
  2324. SCM *qp, SCM *rp);
  2325. SCM_PRIMITIVE_GENERIC (scm_i_truncate_divide, "truncate/", 2, 0, 0,
  2326. (SCM x, SCM y),
  2327. "Return the integer @var{q} and the real number @var{r}\n"
  2328. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  2329. "and @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
  2330. "@lisp\n"
  2331. "(truncate/ 123 10) @result{} 12 and 3\n"
  2332. "(truncate/ 123 -10) @result{} -12 and 3\n"
  2333. "(truncate/ -123 10) @result{} -12 and -3\n"
  2334. "(truncate/ -123 -10) @result{} 12 and -3\n"
  2335. "(truncate/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
  2336. "(truncate/ 16/3 -10/7) @result{} -3 and 22/21\n"
  2337. "@end lisp")
  2338. #define FUNC_NAME s_scm_i_truncate_divide
  2339. {
  2340. SCM q, r;
  2341. scm_truncate_divide(x, y, &q, &r);
  2342. return scm_values_2 (q, r);
  2343. }
  2344. #undef FUNC_NAME
  2345. #define s_scm_truncate_divide s_scm_i_truncate_divide
  2346. #define g_scm_truncate_divide g_scm_i_truncate_divide
  2347. void
  2348. scm_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2349. {
  2350. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2351. {
  2352. scm_t_inum xx = SCM_I_INUM (x);
  2353. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2354. {
  2355. scm_t_inum yy = SCM_I_INUM (y);
  2356. if (SCM_UNLIKELY (yy == 0))
  2357. scm_num_overflow (s_scm_truncate_divide);
  2358. else
  2359. {
  2360. scm_t_inum qq = xx / yy;
  2361. scm_t_inum rr = xx % yy;
  2362. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  2363. *qp = SCM_I_MAKINUM (qq);
  2364. else
  2365. *qp = scm_i_inum2big (qq);
  2366. *rp = SCM_I_MAKINUM (rr);
  2367. }
  2368. }
  2369. else if (SCM_BIGP (y))
  2370. {
  2371. if (SCM_UNLIKELY (xx == SCM_MOST_NEGATIVE_FIXNUM)
  2372. && SCM_UNLIKELY (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  2373. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  2374. {
  2375. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  2376. scm_remember_upto_here_1 (y);
  2377. *qp = SCM_I_MAKINUM (-1);
  2378. *rp = SCM_INUM0;
  2379. }
  2380. else
  2381. {
  2382. *qp = SCM_INUM0;
  2383. *rp = x;
  2384. }
  2385. }
  2386. else if (SCM_REALP (y))
  2387. scm_i_inexact_truncate_divide (xx, SCM_REAL_VALUE (y), qp, rp);
  2388. else if (SCM_FRACTIONP (y))
  2389. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  2390. else
  2391. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  2392. s_scm_truncate_divide, qp, rp);
  2393. }
  2394. else if (SCM_BIGP (x))
  2395. {
  2396. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2397. {
  2398. scm_t_inum yy = SCM_I_INUM (y);
  2399. if (SCM_UNLIKELY (yy == 0))
  2400. scm_num_overflow (s_scm_truncate_divide);
  2401. else
  2402. {
  2403. SCM q = scm_i_mkbig ();
  2404. scm_t_inum rr;
  2405. if (yy > 0)
  2406. rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q),
  2407. SCM_I_BIG_MPZ (x), yy);
  2408. else
  2409. {
  2410. rr = mpz_tdiv_q_ui (SCM_I_BIG_MPZ (q),
  2411. SCM_I_BIG_MPZ (x), -yy);
  2412. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  2413. }
  2414. rr *= mpz_sgn (SCM_I_BIG_MPZ (x));
  2415. scm_remember_upto_here_1 (x);
  2416. *qp = scm_i_normbig (q);
  2417. *rp = SCM_I_MAKINUM (rr);
  2418. }
  2419. }
  2420. else if (SCM_BIGP (y))
  2421. {
  2422. SCM q = scm_i_mkbig ();
  2423. SCM r = scm_i_mkbig ();
  2424. mpz_tdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  2425. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2426. scm_remember_upto_here_2 (x, y);
  2427. *qp = scm_i_normbig (q);
  2428. *rp = scm_i_normbig (r);
  2429. }
  2430. else if (SCM_REALP (y))
  2431. scm_i_inexact_truncate_divide (scm_i_big2dbl (x), SCM_REAL_VALUE (y),
  2432. qp, rp);
  2433. else if (SCM_FRACTIONP (y))
  2434. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  2435. else
  2436. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  2437. s_scm_truncate_divide, qp, rp);
  2438. }
  2439. else if (SCM_REALP (x))
  2440. {
  2441. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2442. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2443. scm_i_inexact_truncate_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  2444. qp, rp);
  2445. else
  2446. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  2447. s_scm_truncate_divide, qp, rp);
  2448. }
  2449. else if (SCM_FRACTIONP (x))
  2450. {
  2451. if (SCM_REALP (y))
  2452. scm_i_inexact_truncate_divide
  2453. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  2454. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2455. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  2456. else
  2457. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  2458. s_scm_truncate_divide, qp, rp);
  2459. }
  2460. else
  2461. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG1,
  2462. s_scm_truncate_divide, qp, rp);
  2463. }
  2464. static void
  2465. scm_i_inexact_truncate_divide (double x, double y, SCM *qp, SCM *rp)
  2466. {
  2467. if (SCM_UNLIKELY (y == 0))
  2468. scm_num_overflow (s_scm_truncate_divide); /* or return a NaN? */
  2469. else
  2470. {
  2471. double q = trunc (x / y);
  2472. double r = x - q * y;
  2473. *qp = scm_i_from_double (q);
  2474. *rp = scm_i_from_double (r);
  2475. }
  2476. }
  2477. static void
  2478. scm_i_exact_rational_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2479. {
  2480. SCM r1;
  2481. SCM xd = scm_denominator (x);
  2482. SCM yd = scm_denominator (y);
  2483. scm_truncate_divide (scm_product (scm_numerator (x), yd),
  2484. scm_product (scm_numerator (y), xd),
  2485. qp, &r1);
  2486. *rp = scm_divide (r1, scm_product (xd, yd));
  2487. }
  2488. static SCM scm_i_inexact_centered_quotient (double x, double y);
  2489. static SCM scm_i_bigint_centered_quotient (SCM x, SCM y);
  2490. static SCM scm_i_exact_rational_centered_quotient (SCM x, SCM y);
  2491. SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
  2492. (SCM x, SCM y),
  2493. "Return the integer @var{q} such that\n"
  2494. "@math{@var{x} = @var{q}*@var{y} + @var{r}} where\n"
  2495. "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
  2496. "@lisp\n"
  2497. "(centered-quotient 123 10) @result{} 12\n"
  2498. "(centered-quotient 123 -10) @result{} -12\n"
  2499. "(centered-quotient -123 10) @result{} -12\n"
  2500. "(centered-quotient -123 -10) @result{} 12\n"
  2501. "(centered-quotient -123.2 -63.5) @result{} 2.0\n"
  2502. "(centered-quotient 16/3 -10/7) @result{} -4\n"
  2503. "@end lisp")
  2504. #define FUNC_NAME s_scm_centered_quotient
  2505. {
  2506. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2507. {
  2508. scm_t_inum xx = SCM_I_INUM (x);
  2509. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2510. {
  2511. scm_t_inum yy = SCM_I_INUM (y);
  2512. if (SCM_UNLIKELY (yy == 0))
  2513. scm_num_overflow (s_scm_centered_quotient);
  2514. else
  2515. {
  2516. scm_t_inum qq = xx / yy;
  2517. scm_t_inum rr = xx % yy;
  2518. if (SCM_LIKELY (xx > 0))
  2519. {
  2520. if (SCM_LIKELY (yy > 0))
  2521. {
  2522. if (rr >= (yy + 1) / 2)
  2523. qq++;
  2524. }
  2525. else
  2526. {
  2527. if (rr >= (1 - yy) / 2)
  2528. qq--;
  2529. }
  2530. }
  2531. else
  2532. {
  2533. if (SCM_LIKELY (yy > 0))
  2534. {
  2535. if (rr < -yy / 2)
  2536. qq--;
  2537. }
  2538. else
  2539. {
  2540. if (rr < yy / 2)
  2541. qq++;
  2542. }
  2543. }
  2544. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  2545. return SCM_I_MAKINUM (qq);
  2546. else
  2547. return scm_i_inum2big (qq);
  2548. }
  2549. }
  2550. else if (SCM_BIGP (y))
  2551. {
  2552. /* Pass a denormalized bignum version of x (even though it
  2553. can fit in a fixnum) to scm_i_bigint_centered_quotient */
  2554. return scm_i_bigint_centered_quotient (scm_i_long2big (xx), y);
  2555. }
  2556. else if (SCM_REALP (y))
  2557. return scm_i_inexact_centered_quotient (xx, SCM_REAL_VALUE (y));
  2558. else if (SCM_FRACTIONP (y))
  2559. return scm_i_exact_rational_centered_quotient (x, y);
  2560. else
  2561. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  2562. s_scm_centered_quotient);
  2563. }
  2564. else if (SCM_BIGP (x))
  2565. {
  2566. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2567. {
  2568. scm_t_inum yy = SCM_I_INUM (y);
  2569. if (SCM_UNLIKELY (yy == 0))
  2570. scm_num_overflow (s_scm_centered_quotient);
  2571. else if (SCM_UNLIKELY (yy == 1))
  2572. return x;
  2573. else
  2574. {
  2575. SCM q = scm_i_mkbig ();
  2576. scm_t_inum rr;
  2577. /* Arrange for rr to initially be non-positive,
  2578. because that simplifies the test to see
  2579. if it is within the needed bounds. */
  2580. if (yy > 0)
  2581. {
  2582. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  2583. SCM_I_BIG_MPZ (x), yy);
  2584. scm_remember_upto_here_1 (x);
  2585. if (rr < -yy / 2)
  2586. mpz_sub_ui (SCM_I_BIG_MPZ (q),
  2587. SCM_I_BIG_MPZ (q), 1);
  2588. }
  2589. else
  2590. {
  2591. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  2592. SCM_I_BIG_MPZ (x), -yy);
  2593. scm_remember_upto_here_1 (x);
  2594. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  2595. if (rr < yy / 2)
  2596. mpz_add_ui (SCM_I_BIG_MPZ (q),
  2597. SCM_I_BIG_MPZ (q), 1);
  2598. }
  2599. return scm_i_normbig (q);
  2600. }
  2601. }
  2602. else if (SCM_BIGP (y))
  2603. return scm_i_bigint_centered_quotient (x, y);
  2604. else if (SCM_REALP (y))
  2605. return scm_i_inexact_centered_quotient
  2606. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  2607. else if (SCM_FRACTIONP (y))
  2608. return scm_i_exact_rational_centered_quotient (x, y);
  2609. else
  2610. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  2611. s_scm_centered_quotient);
  2612. }
  2613. else if (SCM_REALP (x))
  2614. {
  2615. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2616. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2617. return scm_i_inexact_centered_quotient
  2618. (SCM_REAL_VALUE (x), scm_to_double (y));
  2619. else
  2620. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  2621. s_scm_centered_quotient);
  2622. }
  2623. else if (SCM_FRACTIONP (x))
  2624. {
  2625. if (SCM_REALP (y))
  2626. return scm_i_inexact_centered_quotient
  2627. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2628. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2629. return scm_i_exact_rational_centered_quotient (x, y);
  2630. else
  2631. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  2632. s_scm_centered_quotient);
  2633. }
  2634. else
  2635. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG1,
  2636. s_scm_centered_quotient);
  2637. }
  2638. #undef FUNC_NAME
  2639. static SCM
  2640. scm_i_inexact_centered_quotient (double x, double y)
  2641. {
  2642. if (SCM_LIKELY (y > 0))
  2643. return scm_i_from_double (floor (x/y + 0.5));
  2644. else if (SCM_LIKELY (y < 0))
  2645. return scm_i_from_double (ceil (x/y - 0.5));
  2646. else if (y == 0)
  2647. scm_num_overflow (s_scm_centered_quotient); /* or return a NaN? */
  2648. else
  2649. return scm_nan ();
  2650. }
  2651. /* Assumes that both x and y are bigints, though
  2652. x might be able to fit into a fixnum. */
  2653. static SCM
  2654. scm_i_bigint_centered_quotient (SCM x, SCM y)
  2655. {
  2656. SCM q, r, min_r;
  2657. /* Note that x might be small enough to fit into a
  2658. fixnum, so we must not let it escape into the wild */
  2659. q = scm_i_mkbig ();
  2660. r = scm_i_mkbig ();
  2661. /* min_r will eventually become -abs(y)/2 */
  2662. min_r = scm_i_mkbig ();
  2663. mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
  2664. SCM_I_BIG_MPZ (y), 1);
  2665. /* Arrange for rr to initially be non-positive,
  2666. because that simplifies the test to see
  2667. if it is within the needed bounds. */
  2668. if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
  2669. {
  2670. mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  2671. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2672. scm_remember_upto_here_2 (x, y);
  2673. mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
  2674. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  2675. mpz_sub_ui (SCM_I_BIG_MPZ (q),
  2676. SCM_I_BIG_MPZ (q), 1);
  2677. }
  2678. else
  2679. {
  2680. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  2681. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2682. scm_remember_upto_here_2 (x, y);
  2683. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  2684. mpz_add_ui (SCM_I_BIG_MPZ (q),
  2685. SCM_I_BIG_MPZ (q), 1);
  2686. }
  2687. scm_remember_upto_here_2 (r, min_r);
  2688. return scm_i_normbig (q);
  2689. }
  2690. static SCM
  2691. scm_i_exact_rational_centered_quotient (SCM x, SCM y)
  2692. {
  2693. return scm_centered_quotient
  2694. (scm_product (scm_numerator (x), scm_denominator (y)),
  2695. scm_product (scm_numerator (y), scm_denominator (x)));
  2696. }
  2697. static SCM scm_i_inexact_centered_remainder (double x, double y);
  2698. static SCM scm_i_bigint_centered_remainder (SCM x, SCM y);
  2699. static SCM scm_i_exact_rational_centered_remainder (SCM x, SCM y);
  2700. SCM_PRIMITIVE_GENERIC (scm_centered_remainder, "centered-remainder", 2, 0, 0,
  2701. (SCM x, SCM y),
  2702. "Return the real number @var{r} such that\n"
  2703. "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}\n"
  2704. "and @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  2705. "for some integer @var{q}.\n"
  2706. "@lisp\n"
  2707. "(centered-remainder 123 10) @result{} 3\n"
  2708. "(centered-remainder 123 -10) @result{} 3\n"
  2709. "(centered-remainder -123 10) @result{} -3\n"
  2710. "(centered-remainder -123 -10) @result{} -3\n"
  2711. "(centered-remainder -123.2 -63.5) @result{} 3.8\n"
  2712. "(centered-remainder 16/3 -10/7) @result{} -8/21\n"
  2713. "@end lisp")
  2714. #define FUNC_NAME s_scm_centered_remainder
  2715. {
  2716. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2717. {
  2718. scm_t_inum xx = SCM_I_INUM (x);
  2719. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2720. {
  2721. scm_t_inum yy = SCM_I_INUM (y);
  2722. if (SCM_UNLIKELY (yy == 0))
  2723. scm_num_overflow (s_scm_centered_remainder);
  2724. else
  2725. {
  2726. scm_t_inum rr = xx % yy;
  2727. if (SCM_LIKELY (xx > 0))
  2728. {
  2729. if (SCM_LIKELY (yy > 0))
  2730. {
  2731. if (rr >= (yy + 1) / 2)
  2732. rr -= yy;
  2733. }
  2734. else
  2735. {
  2736. if (rr >= (1 - yy) / 2)
  2737. rr += yy;
  2738. }
  2739. }
  2740. else
  2741. {
  2742. if (SCM_LIKELY (yy > 0))
  2743. {
  2744. if (rr < -yy / 2)
  2745. rr += yy;
  2746. }
  2747. else
  2748. {
  2749. if (rr < yy / 2)
  2750. rr -= yy;
  2751. }
  2752. }
  2753. return SCM_I_MAKINUM (rr);
  2754. }
  2755. }
  2756. else if (SCM_BIGP (y))
  2757. {
  2758. /* Pass a denormalized bignum version of x (even though it
  2759. can fit in a fixnum) to scm_i_bigint_centered_remainder */
  2760. return scm_i_bigint_centered_remainder (scm_i_long2big (xx), y);
  2761. }
  2762. else if (SCM_REALP (y))
  2763. return scm_i_inexact_centered_remainder (xx, SCM_REAL_VALUE (y));
  2764. else if (SCM_FRACTIONP (y))
  2765. return scm_i_exact_rational_centered_remainder (x, y);
  2766. else
  2767. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  2768. s_scm_centered_remainder);
  2769. }
  2770. else if (SCM_BIGP (x))
  2771. {
  2772. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2773. {
  2774. scm_t_inum yy = SCM_I_INUM (y);
  2775. if (SCM_UNLIKELY (yy == 0))
  2776. scm_num_overflow (s_scm_centered_remainder);
  2777. else
  2778. {
  2779. scm_t_inum rr;
  2780. /* Arrange for rr to initially be non-positive,
  2781. because that simplifies the test to see
  2782. if it is within the needed bounds. */
  2783. if (yy > 0)
  2784. {
  2785. rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), yy);
  2786. scm_remember_upto_here_1 (x);
  2787. if (rr < -yy / 2)
  2788. rr += yy;
  2789. }
  2790. else
  2791. {
  2792. rr = - mpz_cdiv_ui (SCM_I_BIG_MPZ (x), -yy);
  2793. scm_remember_upto_here_1 (x);
  2794. if (rr < yy / 2)
  2795. rr -= yy;
  2796. }
  2797. return SCM_I_MAKINUM (rr);
  2798. }
  2799. }
  2800. else if (SCM_BIGP (y))
  2801. return scm_i_bigint_centered_remainder (x, y);
  2802. else if (SCM_REALP (y))
  2803. return scm_i_inexact_centered_remainder
  2804. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  2805. else if (SCM_FRACTIONP (y))
  2806. return scm_i_exact_rational_centered_remainder (x, y);
  2807. else
  2808. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  2809. s_scm_centered_remainder);
  2810. }
  2811. else if (SCM_REALP (x))
  2812. {
  2813. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2814. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2815. return scm_i_inexact_centered_remainder
  2816. (SCM_REAL_VALUE (x), scm_to_double (y));
  2817. else
  2818. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  2819. s_scm_centered_remainder);
  2820. }
  2821. else if (SCM_FRACTIONP (x))
  2822. {
  2823. if (SCM_REALP (y))
  2824. return scm_i_inexact_centered_remainder
  2825. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2826. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2827. return scm_i_exact_rational_centered_remainder (x, y);
  2828. else
  2829. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  2830. s_scm_centered_remainder);
  2831. }
  2832. else
  2833. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG1,
  2834. s_scm_centered_remainder);
  2835. }
  2836. #undef FUNC_NAME
  2837. static SCM
  2838. scm_i_inexact_centered_remainder (double x, double y)
  2839. {
  2840. double q;
  2841. /* Although it would be more efficient to use fmod here, we can't
  2842. because it would in some cases produce results inconsistent with
  2843. scm_i_inexact_centered_quotient, such that x != r + q * y (not even
  2844. close). In particular, when x-y/2 is very close to a multiple of
  2845. y, then r might be either -abs(y/2) or abs(y/2)-epsilon, but those
  2846. two cases must correspond to different choices of q. If quotient
  2847. chooses one and remainder chooses the other, it would be bad. */
  2848. if (SCM_LIKELY (y > 0))
  2849. q = floor (x/y + 0.5);
  2850. else if (SCM_LIKELY (y < 0))
  2851. q = ceil (x/y - 0.5);
  2852. else if (y == 0)
  2853. scm_num_overflow (s_scm_centered_remainder); /* or return a NaN? */
  2854. else
  2855. return scm_nan ();
  2856. return scm_i_from_double (x - q * y);
  2857. }
  2858. /* Assumes that both x and y are bigints, though
  2859. x might be able to fit into a fixnum. */
  2860. static SCM
  2861. scm_i_bigint_centered_remainder (SCM x, SCM y)
  2862. {
  2863. SCM r, min_r;
  2864. /* Note that x might be small enough to fit into a
  2865. fixnum, so we must not let it escape into the wild */
  2866. r = scm_i_mkbig ();
  2867. /* min_r will eventually become -abs(y)/2 */
  2868. min_r = scm_i_mkbig ();
  2869. mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
  2870. SCM_I_BIG_MPZ (y), 1);
  2871. /* Arrange for rr to initially be non-positive,
  2872. because that simplifies the test to see
  2873. if it is within the needed bounds. */
  2874. if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
  2875. {
  2876. mpz_cdiv_r (SCM_I_BIG_MPZ (r),
  2877. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2878. mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
  2879. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  2880. mpz_add (SCM_I_BIG_MPZ (r),
  2881. SCM_I_BIG_MPZ (r),
  2882. SCM_I_BIG_MPZ (y));
  2883. }
  2884. else
  2885. {
  2886. mpz_fdiv_r (SCM_I_BIG_MPZ (r),
  2887. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2888. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  2889. mpz_sub (SCM_I_BIG_MPZ (r),
  2890. SCM_I_BIG_MPZ (r),
  2891. SCM_I_BIG_MPZ (y));
  2892. }
  2893. scm_remember_upto_here_2 (x, y);
  2894. return scm_i_normbig (r);
  2895. }
  2896. static SCM
  2897. scm_i_exact_rational_centered_remainder (SCM x, SCM y)
  2898. {
  2899. SCM xd = scm_denominator (x);
  2900. SCM yd = scm_denominator (y);
  2901. SCM r1 = scm_centered_remainder (scm_product (scm_numerator (x), yd),
  2902. scm_product (scm_numerator (y), xd));
  2903. return scm_divide (r1, scm_product (xd, yd));
  2904. }
  2905. static void scm_i_inexact_centered_divide (double x, double y,
  2906. SCM *qp, SCM *rp);
  2907. static void scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp);
  2908. static void scm_i_exact_rational_centered_divide (SCM x, SCM y,
  2909. SCM *qp, SCM *rp);
  2910. SCM_PRIMITIVE_GENERIC (scm_i_centered_divide, "centered/", 2, 0, 0,
  2911. (SCM x, SCM y),
  2912. "Return the integer @var{q} and the real number @var{r}\n"
  2913. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  2914. "and @math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
  2915. "@lisp\n"
  2916. "(centered/ 123 10) @result{} 12 and 3\n"
  2917. "(centered/ 123 -10) @result{} -12 and 3\n"
  2918. "(centered/ -123 10) @result{} -12 and -3\n"
  2919. "(centered/ -123 -10) @result{} 12 and -3\n"
  2920. "(centered/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  2921. "(centered/ 16/3 -10/7) @result{} -4 and -8/21\n"
  2922. "@end lisp")
  2923. #define FUNC_NAME s_scm_i_centered_divide
  2924. {
  2925. SCM q, r;
  2926. scm_centered_divide(x, y, &q, &r);
  2927. return scm_values_2 (q, r);
  2928. }
  2929. #undef FUNC_NAME
  2930. #define s_scm_centered_divide s_scm_i_centered_divide
  2931. #define g_scm_centered_divide g_scm_i_centered_divide
  2932. void
  2933. scm_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2934. {
  2935. if (SCM_LIKELY (SCM_I_INUMP (x)))
  2936. {
  2937. scm_t_inum xx = SCM_I_INUM (x);
  2938. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2939. {
  2940. scm_t_inum yy = SCM_I_INUM (y);
  2941. if (SCM_UNLIKELY (yy == 0))
  2942. scm_num_overflow (s_scm_centered_divide);
  2943. else
  2944. {
  2945. scm_t_inum qq = xx / yy;
  2946. scm_t_inum rr = xx % yy;
  2947. if (SCM_LIKELY (xx > 0))
  2948. {
  2949. if (SCM_LIKELY (yy > 0))
  2950. {
  2951. if (rr >= (yy + 1) / 2)
  2952. { qq++; rr -= yy; }
  2953. }
  2954. else
  2955. {
  2956. if (rr >= (1 - yy) / 2)
  2957. { qq--; rr += yy; }
  2958. }
  2959. }
  2960. else
  2961. {
  2962. if (SCM_LIKELY (yy > 0))
  2963. {
  2964. if (rr < -yy / 2)
  2965. { qq--; rr += yy; }
  2966. }
  2967. else
  2968. {
  2969. if (rr < yy / 2)
  2970. { qq++; rr -= yy; }
  2971. }
  2972. }
  2973. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  2974. *qp = SCM_I_MAKINUM (qq);
  2975. else
  2976. *qp = scm_i_inum2big (qq);
  2977. *rp = SCM_I_MAKINUM (rr);
  2978. }
  2979. }
  2980. else if (SCM_BIGP (y))
  2981. /* Pass a denormalized bignum version of x (even though it
  2982. can fit in a fixnum) to scm_i_bigint_centered_divide */
  2983. scm_i_bigint_centered_divide (scm_i_long2big (xx), y, qp, rp);
  2984. else if (SCM_REALP (y))
  2985. scm_i_inexact_centered_divide (xx, SCM_REAL_VALUE (y), qp, rp);
  2986. else if (SCM_FRACTIONP (y))
  2987. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  2988. else
  2989. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  2990. s_scm_centered_divide, qp, rp);
  2991. }
  2992. else if (SCM_BIGP (x))
  2993. {
  2994. if (SCM_LIKELY (SCM_I_INUMP (y)))
  2995. {
  2996. scm_t_inum yy = SCM_I_INUM (y);
  2997. if (SCM_UNLIKELY (yy == 0))
  2998. scm_num_overflow (s_scm_centered_divide);
  2999. else
  3000. {
  3001. SCM q = scm_i_mkbig ();
  3002. scm_t_inum rr;
  3003. /* Arrange for rr to initially be non-positive,
  3004. because that simplifies the test to see
  3005. if it is within the needed bounds. */
  3006. if (yy > 0)
  3007. {
  3008. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  3009. SCM_I_BIG_MPZ (x), yy);
  3010. scm_remember_upto_here_1 (x);
  3011. if (rr < -yy / 2)
  3012. {
  3013. mpz_sub_ui (SCM_I_BIG_MPZ (q),
  3014. SCM_I_BIG_MPZ (q), 1);
  3015. rr += yy;
  3016. }
  3017. }
  3018. else
  3019. {
  3020. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  3021. SCM_I_BIG_MPZ (x), -yy);
  3022. scm_remember_upto_here_1 (x);
  3023. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  3024. if (rr < yy / 2)
  3025. {
  3026. mpz_add_ui (SCM_I_BIG_MPZ (q),
  3027. SCM_I_BIG_MPZ (q), 1);
  3028. rr -= yy;
  3029. }
  3030. }
  3031. *qp = scm_i_normbig (q);
  3032. *rp = SCM_I_MAKINUM (rr);
  3033. }
  3034. }
  3035. else if (SCM_BIGP (y))
  3036. scm_i_bigint_centered_divide (x, y, qp, rp);
  3037. else if (SCM_REALP (y))
  3038. scm_i_inexact_centered_divide (scm_i_big2dbl (x), SCM_REAL_VALUE (y),
  3039. qp, rp);
  3040. else if (SCM_FRACTIONP (y))
  3041. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  3042. else
  3043. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  3044. s_scm_centered_divide, qp, rp);
  3045. }
  3046. else if (SCM_REALP (x))
  3047. {
  3048. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  3049. SCM_BIGP (y) || SCM_FRACTIONP (y))
  3050. scm_i_inexact_centered_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  3051. qp, rp);
  3052. else
  3053. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  3054. s_scm_centered_divide, qp, rp);
  3055. }
  3056. else if (SCM_FRACTIONP (x))
  3057. {
  3058. if (SCM_REALP (y))
  3059. scm_i_inexact_centered_divide
  3060. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  3061. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  3062. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  3063. else
  3064. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  3065. s_scm_centered_divide, qp, rp);
  3066. }
  3067. else
  3068. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG1,
  3069. s_scm_centered_divide, qp, rp);
  3070. }
  3071. static void
  3072. scm_i_inexact_centered_divide (double x, double y, SCM *qp, SCM *rp)
  3073. {
  3074. double q, r;
  3075. if (SCM_LIKELY (y > 0))
  3076. q = floor (x/y + 0.5);
  3077. else if (SCM_LIKELY (y < 0))
  3078. q = ceil (x/y - 0.5);
  3079. else if (y == 0)
  3080. scm_num_overflow (s_scm_centered_divide); /* or return a NaN? */
  3081. else
  3082. q = guile_NaN;
  3083. r = x - q * y;
  3084. *qp = scm_i_from_double (q);
  3085. *rp = scm_i_from_double (r);
  3086. }
  3087. /* Assumes that both x and y are bigints, though
  3088. x might be able to fit into a fixnum. */
  3089. static void
  3090. scm_i_bigint_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  3091. {
  3092. SCM q, r, min_r;
  3093. /* Note that x might be small enough to fit into a
  3094. fixnum, so we must not let it escape into the wild */
  3095. q = scm_i_mkbig ();
  3096. r = scm_i_mkbig ();
  3097. /* min_r will eventually become -abs(y/2) */
  3098. min_r = scm_i_mkbig ();
  3099. mpz_tdiv_q_2exp (SCM_I_BIG_MPZ (min_r),
  3100. SCM_I_BIG_MPZ (y), 1);
  3101. /* Arrange for rr to initially be non-positive,
  3102. because that simplifies the test to see
  3103. if it is within the needed bounds. */
  3104. if (mpz_sgn (SCM_I_BIG_MPZ (y)) > 0)
  3105. {
  3106. mpz_cdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  3107. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3108. mpz_neg (SCM_I_BIG_MPZ (min_r), SCM_I_BIG_MPZ (min_r));
  3109. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  3110. {
  3111. mpz_sub_ui (SCM_I_BIG_MPZ (q),
  3112. SCM_I_BIG_MPZ (q), 1);
  3113. mpz_add (SCM_I_BIG_MPZ (r),
  3114. SCM_I_BIG_MPZ (r),
  3115. SCM_I_BIG_MPZ (y));
  3116. }
  3117. }
  3118. else
  3119. {
  3120. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  3121. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3122. if (mpz_cmp (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (min_r)) < 0)
  3123. {
  3124. mpz_add_ui (SCM_I_BIG_MPZ (q),
  3125. SCM_I_BIG_MPZ (q), 1);
  3126. mpz_sub (SCM_I_BIG_MPZ (r),
  3127. SCM_I_BIG_MPZ (r),
  3128. SCM_I_BIG_MPZ (y));
  3129. }
  3130. }
  3131. scm_remember_upto_here_2 (x, y);
  3132. *qp = scm_i_normbig (q);
  3133. *rp = scm_i_normbig (r);
  3134. }
  3135. static void
  3136. scm_i_exact_rational_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  3137. {
  3138. SCM r1;
  3139. SCM xd = scm_denominator (x);
  3140. SCM yd = scm_denominator (y);
  3141. scm_centered_divide (scm_product (scm_numerator (x), yd),
  3142. scm_product (scm_numerator (y), xd),
  3143. qp, &r1);
  3144. *rp = scm_divide (r1, scm_product (xd, yd));
  3145. }
  3146. static SCM scm_i_inexact_round_quotient (double x, double y);
  3147. static SCM scm_i_bigint_round_quotient (SCM x, SCM y);
  3148. static SCM scm_i_exact_rational_round_quotient (SCM x, SCM y);
  3149. SCM_PRIMITIVE_GENERIC (scm_round_quotient, "round-quotient", 2, 0, 0,
  3150. (SCM x, SCM y),
  3151. "Return @math{@var{x} / @var{y}} to the nearest integer,\n"
  3152. "with ties going to the nearest even integer.\n"
  3153. "@lisp\n"
  3154. "(round-quotient 123 10) @result{} 12\n"
  3155. "(round-quotient 123 -10) @result{} -12\n"
  3156. "(round-quotient -123 10) @result{} -12\n"
  3157. "(round-quotient -123 -10) @result{} 12\n"
  3158. "(round-quotient 125 10) @result{} 12\n"
  3159. "(round-quotient 127 10) @result{} 13\n"
  3160. "(round-quotient 135 10) @result{} 14\n"
  3161. "(round-quotient -123.2 -63.5) @result{} 2.0\n"
  3162. "(round-quotient 16/3 -10/7) @result{} -4\n"
  3163. "@end lisp")
  3164. #define FUNC_NAME s_scm_round_quotient
  3165. {
  3166. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3167. {
  3168. scm_t_inum xx = SCM_I_INUM (x);
  3169. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3170. {
  3171. scm_t_inum yy = SCM_I_INUM (y);
  3172. if (SCM_UNLIKELY (yy == 0))
  3173. scm_num_overflow (s_scm_round_quotient);
  3174. else
  3175. {
  3176. scm_t_inum qq = xx / yy;
  3177. scm_t_inum rr = xx % yy;
  3178. scm_t_inum ay = yy;
  3179. scm_t_inum r2 = 2 * rr;
  3180. if (SCM_LIKELY (yy < 0))
  3181. {
  3182. ay = -ay;
  3183. r2 = -r2;
  3184. }
  3185. if (qq & 1L)
  3186. {
  3187. if (r2 >= ay)
  3188. qq++;
  3189. else if (r2 <= -ay)
  3190. qq--;
  3191. }
  3192. else
  3193. {
  3194. if (r2 > ay)
  3195. qq++;
  3196. else if (r2 < -ay)
  3197. qq--;
  3198. }
  3199. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  3200. return SCM_I_MAKINUM (qq);
  3201. else
  3202. return scm_i_inum2big (qq);
  3203. }
  3204. }
  3205. else if (SCM_BIGP (y))
  3206. {
  3207. /* Pass a denormalized bignum version of x (even though it
  3208. can fit in a fixnum) to scm_i_bigint_round_quotient */
  3209. return scm_i_bigint_round_quotient (scm_i_long2big (xx), y);
  3210. }
  3211. else if (SCM_REALP (y))
  3212. return scm_i_inexact_round_quotient (xx, SCM_REAL_VALUE (y));
  3213. else if (SCM_FRACTIONP (y))
  3214. return scm_i_exact_rational_round_quotient (x, y);
  3215. else
  3216. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  3217. s_scm_round_quotient);
  3218. }
  3219. else if (SCM_BIGP (x))
  3220. {
  3221. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3222. {
  3223. scm_t_inum yy = SCM_I_INUM (y);
  3224. if (SCM_UNLIKELY (yy == 0))
  3225. scm_num_overflow (s_scm_round_quotient);
  3226. else if (SCM_UNLIKELY (yy == 1))
  3227. return x;
  3228. else
  3229. {
  3230. SCM q = scm_i_mkbig ();
  3231. scm_t_inum rr;
  3232. int needs_adjustment;
  3233. if (yy > 0)
  3234. {
  3235. rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
  3236. SCM_I_BIG_MPZ (x), yy);
  3237. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3238. needs_adjustment = (2*rr >= yy);
  3239. else
  3240. needs_adjustment = (2*rr > yy);
  3241. }
  3242. else
  3243. {
  3244. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  3245. SCM_I_BIG_MPZ (x), -yy);
  3246. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  3247. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3248. needs_adjustment = (2*rr <= yy);
  3249. else
  3250. needs_adjustment = (2*rr < yy);
  3251. }
  3252. scm_remember_upto_here_1 (x);
  3253. if (needs_adjustment)
  3254. mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
  3255. return scm_i_normbig (q);
  3256. }
  3257. }
  3258. else if (SCM_BIGP (y))
  3259. return scm_i_bigint_round_quotient (x, y);
  3260. else if (SCM_REALP (y))
  3261. return scm_i_inexact_round_quotient
  3262. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  3263. else if (SCM_FRACTIONP (y))
  3264. return scm_i_exact_rational_round_quotient (x, y);
  3265. else
  3266. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  3267. s_scm_round_quotient);
  3268. }
  3269. else if (SCM_REALP (x))
  3270. {
  3271. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  3272. SCM_BIGP (y) || SCM_FRACTIONP (y))
  3273. return scm_i_inexact_round_quotient
  3274. (SCM_REAL_VALUE (x), scm_to_double (y));
  3275. else
  3276. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  3277. s_scm_round_quotient);
  3278. }
  3279. else if (SCM_FRACTIONP (x))
  3280. {
  3281. if (SCM_REALP (y))
  3282. return scm_i_inexact_round_quotient
  3283. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  3284. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  3285. return scm_i_exact_rational_round_quotient (x, y);
  3286. else
  3287. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  3288. s_scm_round_quotient);
  3289. }
  3290. else
  3291. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG1,
  3292. s_scm_round_quotient);
  3293. }
  3294. #undef FUNC_NAME
  3295. static SCM
  3296. scm_i_inexact_round_quotient (double x, double y)
  3297. {
  3298. if (SCM_UNLIKELY (y == 0))
  3299. scm_num_overflow (s_scm_round_quotient); /* or return a NaN? */
  3300. else
  3301. return scm_i_from_double (scm_c_round (x / y));
  3302. }
  3303. /* Assumes that both x and y are bigints, though
  3304. x might be able to fit into a fixnum. */
  3305. static SCM
  3306. scm_i_bigint_round_quotient (SCM x, SCM y)
  3307. {
  3308. SCM q, r, r2;
  3309. int cmp, needs_adjustment;
  3310. /* Note that x might be small enough to fit into a
  3311. fixnum, so we must not let it escape into the wild */
  3312. q = scm_i_mkbig ();
  3313. r = scm_i_mkbig ();
  3314. r2 = scm_i_mkbig ();
  3315. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  3316. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3317. mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
  3318. scm_remember_upto_here_2 (x, r);
  3319. cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
  3320. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3321. needs_adjustment = (cmp >= 0);
  3322. else
  3323. needs_adjustment = (cmp > 0);
  3324. scm_remember_upto_here_2 (r2, y);
  3325. if (needs_adjustment)
  3326. mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
  3327. return scm_i_normbig (q);
  3328. }
  3329. static SCM
  3330. scm_i_exact_rational_round_quotient (SCM x, SCM y)
  3331. {
  3332. return scm_round_quotient
  3333. (scm_product (scm_numerator (x), scm_denominator (y)),
  3334. scm_product (scm_numerator (y), scm_denominator (x)));
  3335. }
  3336. static SCM scm_i_inexact_round_remainder (double x, double y);
  3337. static SCM scm_i_bigint_round_remainder (SCM x, SCM y);
  3338. static SCM scm_i_exact_rational_round_remainder (SCM x, SCM y);
  3339. SCM_PRIMITIVE_GENERIC (scm_round_remainder, "round-remainder", 2, 0, 0,
  3340. (SCM x, SCM y),
  3341. "Return the real number @var{r} such that\n"
  3342. "@math{@var{x} = @var{q}*@var{y} + @var{r}}, where\n"
  3343. "@var{q} is @math{@var{x} / @var{y}} rounded to the\n"
  3344. "nearest integer, with ties going to the nearest\n"
  3345. "even integer.\n"
  3346. "@lisp\n"
  3347. "(round-remainder 123 10) @result{} 3\n"
  3348. "(round-remainder 123 -10) @result{} 3\n"
  3349. "(round-remainder -123 10) @result{} -3\n"
  3350. "(round-remainder -123 -10) @result{} -3\n"
  3351. "(round-remainder 125 10) @result{} 5\n"
  3352. "(round-remainder 127 10) @result{} -3\n"
  3353. "(round-remainder 135 10) @result{} -5\n"
  3354. "(round-remainder -123.2 -63.5) @result{} 3.8\n"
  3355. "(round-remainder 16/3 -10/7) @result{} -8/21\n"
  3356. "@end lisp")
  3357. #define FUNC_NAME s_scm_round_remainder
  3358. {
  3359. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3360. {
  3361. scm_t_inum xx = SCM_I_INUM (x);
  3362. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3363. {
  3364. scm_t_inum yy = SCM_I_INUM (y);
  3365. if (SCM_UNLIKELY (yy == 0))
  3366. scm_num_overflow (s_scm_round_remainder);
  3367. else
  3368. {
  3369. scm_t_inum qq = xx / yy;
  3370. scm_t_inum rr = xx % yy;
  3371. scm_t_inum ay = yy;
  3372. scm_t_inum r2 = 2 * rr;
  3373. if (SCM_LIKELY (yy < 0))
  3374. {
  3375. ay = -ay;
  3376. r2 = -r2;
  3377. }
  3378. if (qq & 1L)
  3379. {
  3380. if (r2 >= ay)
  3381. rr -= yy;
  3382. else if (r2 <= -ay)
  3383. rr += yy;
  3384. }
  3385. else
  3386. {
  3387. if (r2 > ay)
  3388. rr -= yy;
  3389. else if (r2 < -ay)
  3390. rr += yy;
  3391. }
  3392. return SCM_I_MAKINUM (rr);
  3393. }
  3394. }
  3395. else if (SCM_BIGP (y))
  3396. {
  3397. /* Pass a denormalized bignum version of x (even though it
  3398. can fit in a fixnum) to scm_i_bigint_round_remainder */
  3399. return scm_i_bigint_round_remainder
  3400. (scm_i_long2big (xx), y);
  3401. }
  3402. else if (SCM_REALP (y))
  3403. return scm_i_inexact_round_remainder (xx, SCM_REAL_VALUE (y));
  3404. else if (SCM_FRACTIONP (y))
  3405. return scm_i_exact_rational_round_remainder (x, y);
  3406. else
  3407. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  3408. s_scm_round_remainder);
  3409. }
  3410. else if (SCM_BIGP (x))
  3411. {
  3412. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3413. {
  3414. scm_t_inum yy = SCM_I_INUM (y);
  3415. if (SCM_UNLIKELY (yy == 0))
  3416. scm_num_overflow (s_scm_round_remainder);
  3417. else
  3418. {
  3419. SCM q = scm_i_mkbig ();
  3420. scm_t_inum rr;
  3421. int needs_adjustment;
  3422. if (yy > 0)
  3423. {
  3424. rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
  3425. SCM_I_BIG_MPZ (x), yy);
  3426. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3427. needs_adjustment = (2*rr >= yy);
  3428. else
  3429. needs_adjustment = (2*rr > yy);
  3430. }
  3431. else
  3432. {
  3433. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  3434. SCM_I_BIG_MPZ (x), -yy);
  3435. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3436. needs_adjustment = (2*rr <= yy);
  3437. else
  3438. needs_adjustment = (2*rr < yy);
  3439. }
  3440. scm_remember_upto_here_2 (x, q);
  3441. if (needs_adjustment)
  3442. rr -= yy;
  3443. return SCM_I_MAKINUM (rr);
  3444. }
  3445. }
  3446. else if (SCM_BIGP (y))
  3447. return scm_i_bigint_round_remainder (x, y);
  3448. else if (SCM_REALP (y))
  3449. return scm_i_inexact_round_remainder
  3450. (scm_i_big2dbl (x), SCM_REAL_VALUE (y));
  3451. else if (SCM_FRACTIONP (y))
  3452. return scm_i_exact_rational_round_remainder (x, y);
  3453. else
  3454. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  3455. s_scm_round_remainder);
  3456. }
  3457. else if (SCM_REALP (x))
  3458. {
  3459. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  3460. SCM_BIGP (y) || SCM_FRACTIONP (y))
  3461. return scm_i_inexact_round_remainder
  3462. (SCM_REAL_VALUE (x), scm_to_double (y));
  3463. else
  3464. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  3465. s_scm_round_remainder);
  3466. }
  3467. else if (SCM_FRACTIONP (x))
  3468. {
  3469. if (SCM_REALP (y))
  3470. return scm_i_inexact_round_remainder
  3471. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  3472. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  3473. return scm_i_exact_rational_round_remainder (x, y);
  3474. else
  3475. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  3476. s_scm_round_remainder);
  3477. }
  3478. else
  3479. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG1,
  3480. s_scm_round_remainder);
  3481. }
  3482. #undef FUNC_NAME
  3483. static SCM
  3484. scm_i_inexact_round_remainder (double x, double y)
  3485. {
  3486. /* Although it would be more efficient to use fmod here, we can't
  3487. because it would in some cases produce results inconsistent with
  3488. scm_i_inexact_round_quotient, such that x != r + q * y (not even
  3489. close). In particular, when x-y/2 is very close to a multiple of
  3490. y, then r might be either -abs(y/2) or abs(y/2), but those two
  3491. cases must correspond to different choices of q. If quotient
  3492. chooses one and remainder chooses the other, it would be bad. */
  3493. if (SCM_UNLIKELY (y == 0))
  3494. scm_num_overflow (s_scm_round_remainder); /* or return a NaN? */
  3495. else
  3496. {
  3497. double q = scm_c_round (x / y);
  3498. return scm_i_from_double (x - q * y);
  3499. }
  3500. }
  3501. /* Assumes that both x and y are bigints, though
  3502. x might be able to fit into a fixnum. */
  3503. static SCM
  3504. scm_i_bigint_round_remainder (SCM x, SCM y)
  3505. {
  3506. SCM q, r, r2;
  3507. int cmp, needs_adjustment;
  3508. /* Note that x might be small enough to fit into a
  3509. fixnum, so we must not let it escape into the wild */
  3510. q = scm_i_mkbig ();
  3511. r = scm_i_mkbig ();
  3512. r2 = scm_i_mkbig ();
  3513. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  3514. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3515. scm_remember_upto_here_1 (x);
  3516. mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
  3517. cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
  3518. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3519. needs_adjustment = (cmp >= 0);
  3520. else
  3521. needs_adjustment = (cmp > 0);
  3522. scm_remember_upto_here_2 (q, r2);
  3523. if (needs_adjustment)
  3524. mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y));
  3525. scm_remember_upto_here_1 (y);
  3526. return scm_i_normbig (r);
  3527. }
  3528. static SCM
  3529. scm_i_exact_rational_round_remainder (SCM x, SCM y)
  3530. {
  3531. SCM xd = scm_denominator (x);
  3532. SCM yd = scm_denominator (y);
  3533. SCM r1 = scm_round_remainder (scm_product (scm_numerator (x), yd),
  3534. scm_product (scm_numerator (y), xd));
  3535. return scm_divide (r1, scm_product (xd, yd));
  3536. }
  3537. static void scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp);
  3538. static void scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp);
  3539. static void scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp);
  3540. SCM_PRIMITIVE_GENERIC (scm_i_round_divide, "round/", 2, 0, 0,
  3541. (SCM x, SCM y),
  3542. "Return the integer @var{q} and the real number @var{r}\n"
  3543. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  3544. "and @var{q} is @math{@var{x} / @var{y}} rounded to the\n"
  3545. "nearest integer, with ties going to the nearest even integer.\n"
  3546. "@lisp\n"
  3547. "(round/ 123 10) @result{} 12 and 3\n"
  3548. "(round/ 123 -10) @result{} -12 and 3\n"
  3549. "(round/ -123 10) @result{} -12 and -3\n"
  3550. "(round/ -123 -10) @result{} 12 and -3\n"
  3551. "(round/ 125 10) @result{} 12 and 5\n"
  3552. "(round/ 127 10) @result{} 13 and -3\n"
  3553. "(round/ 135 10) @result{} 14 and -5\n"
  3554. "(round/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  3555. "(round/ 16/3 -10/7) @result{} -4 and -8/21\n"
  3556. "@end lisp")
  3557. #define FUNC_NAME s_scm_i_round_divide
  3558. {
  3559. SCM q, r;
  3560. scm_round_divide(x, y, &q, &r);
  3561. return scm_values_2 (q, r);
  3562. }
  3563. #undef FUNC_NAME
  3564. #define s_scm_round_divide s_scm_i_round_divide
  3565. #define g_scm_round_divide g_scm_i_round_divide
  3566. void
  3567. scm_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  3568. {
  3569. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3570. {
  3571. scm_t_inum xx = SCM_I_INUM (x);
  3572. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3573. {
  3574. scm_t_inum yy = SCM_I_INUM (y);
  3575. if (SCM_UNLIKELY (yy == 0))
  3576. scm_num_overflow (s_scm_round_divide);
  3577. else
  3578. {
  3579. scm_t_inum qq = xx / yy;
  3580. scm_t_inum rr = xx % yy;
  3581. scm_t_inum ay = yy;
  3582. scm_t_inum r2 = 2 * rr;
  3583. if (SCM_LIKELY (yy < 0))
  3584. {
  3585. ay = -ay;
  3586. r2 = -r2;
  3587. }
  3588. if (qq & 1L)
  3589. {
  3590. if (r2 >= ay)
  3591. { qq++; rr -= yy; }
  3592. else if (r2 <= -ay)
  3593. { qq--; rr += yy; }
  3594. }
  3595. else
  3596. {
  3597. if (r2 > ay)
  3598. { qq++; rr -= yy; }
  3599. else if (r2 < -ay)
  3600. { qq--; rr += yy; }
  3601. }
  3602. if (SCM_LIKELY (SCM_FIXABLE (qq)))
  3603. *qp = SCM_I_MAKINUM (qq);
  3604. else
  3605. *qp = scm_i_inum2big (qq);
  3606. *rp = SCM_I_MAKINUM (rr);
  3607. }
  3608. }
  3609. else if (SCM_BIGP (y))
  3610. /* Pass a denormalized bignum version of x (even though it
  3611. can fit in a fixnum) to scm_i_bigint_round_divide */
  3612. scm_i_bigint_round_divide (scm_i_long2big (SCM_I_INUM (x)), y, qp, rp);
  3613. else if (SCM_REALP (y))
  3614. scm_i_inexact_round_divide (xx, SCM_REAL_VALUE (y), qp, rp);
  3615. else if (SCM_FRACTIONP (y))
  3616. scm_i_exact_rational_round_divide (x, y, qp, rp);
  3617. else
  3618. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  3619. s_scm_round_divide, qp, rp);
  3620. }
  3621. else if (SCM_BIGP (x))
  3622. {
  3623. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3624. {
  3625. scm_t_inum yy = SCM_I_INUM (y);
  3626. if (SCM_UNLIKELY (yy == 0))
  3627. scm_num_overflow (s_scm_round_divide);
  3628. else
  3629. {
  3630. SCM q = scm_i_mkbig ();
  3631. scm_t_inum rr;
  3632. int needs_adjustment;
  3633. if (yy > 0)
  3634. {
  3635. rr = mpz_fdiv_q_ui (SCM_I_BIG_MPZ (q),
  3636. SCM_I_BIG_MPZ (x), yy);
  3637. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3638. needs_adjustment = (2*rr >= yy);
  3639. else
  3640. needs_adjustment = (2*rr > yy);
  3641. }
  3642. else
  3643. {
  3644. rr = - mpz_cdiv_q_ui (SCM_I_BIG_MPZ (q),
  3645. SCM_I_BIG_MPZ (x), -yy);
  3646. mpz_neg (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q));
  3647. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3648. needs_adjustment = (2*rr <= yy);
  3649. else
  3650. needs_adjustment = (2*rr < yy);
  3651. }
  3652. scm_remember_upto_here_1 (x);
  3653. if (needs_adjustment)
  3654. {
  3655. mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
  3656. rr -= yy;
  3657. }
  3658. *qp = scm_i_normbig (q);
  3659. *rp = SCM_I_MAKINUM (rr);
  3660. }
  3661. }
  3662. else if (SCM_BIGP (y))
  3663. scm_i_bigint_round_divide (x, y, qp, rp);
  3664. else if (SCM_REALP (y))
  3665. scm_i_inexact_round_divide (scm_i_big2dbl (x), SCM_REAL_VALUE (y),
  3666. qp, rp);
  3667. else if (SCM_FRACTIONP (y))
  3668. scm_i_exact_rational_round_divide (x, y, qp, rp);
  3669. else
  3670. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  3671. s_scm_round_divide, qp, rp);
  3672. }
  3673. else if (SCM_REALP (x))
  3674. {
  3675. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  3676. SCM_BIGP (y) || SCM_FRACTIONP (y))
  3677. scm_i_inexact_round_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  3678. qp, rp);
  3679. else
  3680. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  3681. s_scm_round_divide, qp, rp);
  3682. }
  3683. else if (SCM_FRACTIONP (x))
  3684. {
  3685. if (SCM_REALP (y))
  3686. scm_i_inexact_round_divide
  3687. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  3688. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  3689. scm_i_exact_rational_round_divide (x, y, qp, rp);
  3690. else
  3691. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  3692. s_scm_round_divide, qp, rp);
  3693. }
  3694. else
  3695. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG1,
  3696. s_scm_round_divide, qp, rp);
  3697. }
  3698. static void
  3699. scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp)
  3700. {
  3701. if (SCM_UNLIKELY (y == 0))
  3702. scm_num_overflow (s_scm_round_divide); /* or return a NaN? */
  3703. else
  3704. {
  3705. double q = scm_c_round (x / y);
  3706. double r = x - q * y;
  3707. *qp = scm_i_from_double (q);
  3708. *rp = scm_i_from_double (r);
  3709. }
  3710. }
  3711. /* Assumes that both x and y are bigints, though
  3712. x might be able to fit into a fixnum. */
  3713. static void
  3714. scm_i_bigint_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  3715. {
  3716. SCM q, r, r2;
  3717. int cmp, needs_adjustment;
  3718. /* Note that x might be small enough to fit into a
  3719. fixnum, so we must not let it escape into the wild */
  3720. q = scm_i_mkbig ();
  3721. r = scm_i_mkbig ();
  3722. r2 = scm_i_mkbig ();
  3723. mpz_fdiv_qr (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (r),
  3724. SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3725. scm_remember_upto_here_1 (x);
  3726. mpz_mul_2exp (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (r), 1); /* r2 = 2*r */
  3727. cmp = mpz_cmpabs (SCM_I_BIG_MPZ (r2), SCM_I_BIG_MPZ (y));
  3728. if (mpz_odd_p (SCM_I_BIG_MPZ (q)))
  3729. needs_adjustment = (cmp >= 0);
  3730. else
  3731. needs_adjustment = (cmp > 0);
  3732. if (needs_adjustment)
  3733. {
  3734. mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
  3735. mpz_sub (SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (y));
  3736. }
  3737. scm_remember_upto_here_2 (r2, y);
  3738. *qp = scm_i_normbig (q);
  3739. *rp = scm_i_normbig (r);
  3740. }
  3741. static void
  3742. scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  3743. {
  3744. SCM r1;
  3745. SCM xd = scm_denominator (x);
  3746. SCM yd = scm_denominator (y);
  3747. scm_round_divide (scm_product (scm_numerator (x), yd),
  3748. scm_product (scm_numerator (y), xd),
  3749. qp, &r1);
  3750. *rp = scm_divide (r1, scm_product (xd, yd));
  3751. }
  3752. SCM_PRIMITIVE_GENERIC (scm_i_gcd, "gcd", 0, 2, 1,
  3753. (SCM x, SCM y, SCM rest),
  3754. "Return the greatest common divisor of all parameter values.\n"
  3755. "If called without arguments, 0 is returned.")
  3756. #define FUNC_NAME s_scm_i_gcd
  3757. {
  3758. while (!scm_is_null (rest))
  3759. { x = scm_gcd (x, y);
  3760. y = scm_car (rest);
  3761. rest = scm_cdr (rest);
  3762. }
  3763. return scm_gcd (x, y);
  3764. }
  3765. #undef FUNC_NAME
  3766. #define s_gcd s_scm_i_gcd
  3767. #define g_gcd g_scm_i_gcd
  3768. SCM
  3769. scm_gcd (SCM x, SCM y)
  3770. {
  3771. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  3772. return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
  3773. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3774. {
  3775. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3776. {
  3777. scm_t_inum xx = SCM_I_INUM (x);
  3778. scm_t_inum yy = SCM_I_INUM (y);
  3779. scm_t_inum u = xx < 0 ? -xx : xx;
  3780. scm_t_inum v = yy < 0 ? -yy : yy;
  3781. scm_t_inum result;
  3782. if (SCM_UNLIKELY (xx == 0))
  3783. result = v;
  3784. else if (SCM_UNLIKELY (yy == 0))
  3785. result = u;
  3786. else
  3787. {
  3788. int k = 0;
  3789. /* Determine a common factor 2^k */
  3790. while (((u | v) & 1) == 0)
  3791. {
  3792. k++;
  3793. u >>= 1;
  3794. v >>= 1;
  3795. }
  3796. /* Now, any factor 2^n can be eliminated */
  3797. if ((u & 1) == 0)
  3798. while ((u & 1) == 0)
  3799. u >>= 1;
  3800. else
  3801. while ((v & 1) == 0)
  3802. v >>= 1;
  3803. /* Both u and v are now odd. Subtract the smaller one
  3804. from the larger one to produce an even number, remove
  3805. more factors of two, and repeat. */
  3806. while (u != v)
  3807. {
  3808. if (u > v)
  3809. {
  3810. u -= v;
  3811. while ((u & 1) == 0)
  3812. u >>= 1;
  3813. }
  3814. else
  3815. {
  3816. v -= u;
  3817. while ((v & 1) == 0)
  3818. v >>= 1;
  3819. }
  3820. }
  3821. result = u << k;
  3822. }
  3823. return (SCM_POSFIXABLE (result)
  3824. ? SCM_I_MAKINUM (result)
  3825. : scm_i_inum2big (result));
  3826. }
  3827. else if (SCM_BIGP (y))
  3828. {
  3829. SCM_SWAP (x, y);
  3830. goto big_inum;
  3831. }
  3832. else if (SCM_REALP (y) && scm_is_integer (y))
  3833. goto handle_inexacts;
  3834. else
  3835. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  3836. }
  3837. else if (SCM_BIGP (x))
  3838. {
  3839. if (SCM_I_INUMP (y))
  3840. {
  3841. scm_t_bits result;
  3842. scm_t_inum yy;
  3843. big_inum:
  3844. yy = SCM_I_INUM (y);
  3845. if (yy == 0)
  3846. return scm_abs (x);
  3847. if (yy < 0)
  3848. yy = -yy;
  3849. result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy);
  3850. scm_remember_upto_here_1 (x);
  3851. return (SCM_POSFIXABLE (result)
  3852. ? SCM_I_MAKINUM (result)
  3853. : scm_from_unsigned_integer (result));
  3854. }
  3855. else if (SCM_BIGP (y))
  3856. {
  3857. SCM result = scm_i_mkbig ();
  3858. mpz_gcd (SCM_I_BIG_MPZ (result),
  3859. SCM_I_BIG_MPZ (x),
  3860. SCM_I_BIG_MPZ (y));
  3861. scm_remember_upto_here_2 (x, y);
  3862. return scm_i_normbig (result);
  3863. }
  3864. else if (SCM_REALP (y) && scm_is_integer (y))
  3865. goto handle_inexacts;
  3866. else
  3867. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  3868. }
  3869. else if (SCM_REALP (x) && scm_is_integer (x))
  3870. {
  3871. if (SCM_I_INUMP (y) || SCM_BIGP (y)
  3872. || (SCM_REALP (y) && scm_is_integer (y)))
  3873. {
  3874. handle_inexacts:
  3875. return scm_exact_to_inexact (scm_gcd (scm_inexact_to_exact (x),
  3876. scm_inexact_to_exact (y)));
  3877. }
  3878. else
  3879. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  3880. }
  3881. else
  3882. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
  3883. }
  3884. SCM_PRIMITIVE_GENERIC (scm_i_lcm, "lcm", 0, 2, 1,
  3885. (SCM x, SCM y, SCM rest),
  3886. "Return the least common multiple of the arguments.\n"
  3887. "If called without arguments, 1 is returned.")
  3888. #define FUNC_NAME s_scm_i_lcm
  3889. {
  3890. while (!scm_is_null (rest))
  3891. { x = scm_lcm (x, y);
  3892. y = scm_car (rest);
  3893. rest = scm_cdr (rest);
  3894. }
  3895. return scm_lcm (x, y);
  3896. }
  3897. #undef FUNC_NAME
  3898. #define s_lcm s_scm_i_lcm
  3899. #define g_lcm g_scm_i_lcm
  3900. SCM
  3901. scm_lcm (SCM n1, SCM n2)
  3902. {
  3903. if (SCM_UNLIKELY (SCM_UNBNDP (n2)))
  3904. return SCM_UNBNDP (n1) ? SCM_INUM1 : scm_abs (n1);
  3905. if (SCM_LIKELY (SCM_I_INUMP (n1)))
  3906. {
  3907. if (SCM_LIKELY (SCM_I_INUMP (n2)))
  3908. {
  3909. SCM d = scm_gcd (n1, n2);
  3910. if (scm_is_eq (d, SCM_INUM0))
  3911. return d;
  3912. else
  3913. return scm_abs (scm_product (n1, scm_quotient (n2, d)));
  3914. }
  3915. else if (SCM_LIKELY (SCM_BIGP (n2)))
  3916. {
  3917. /* inum n1, big n2 */
  3918. inumbig:
  3919. {
  3920. SCM result = scm_i_mkbig ();
  3921. scm_t_inum nn1 = SCM_I_INUM (n1);
  3922. if (nn1 == 0) return SCM_INUM0;
  3923. if (nn1 < 0) nn1 = - nn1;
  3924. mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1);
  3925. scm_remember_upto_here_1 (n2);
  3926. return result;
  3927. }
  3928. }
  3929. else if (SCM_REALP (n2) && scm_is_integer (n2))
  3930. goto handle_inexacts;
  3931. else
  3932. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  3933. }
  3934. else if (SCM_LIKELY (SCM_BIGP (n1)))
  3935. {
  3936. /* big n1 */
  3937. if (SCM_I_INUMP (n2))
  3938. {
  3939. SCM_SWAP (n1, n2);
  3940. goto inumbig;
  3941. }
  3942. else if (SCM_LIKELY (SCM_BIGP (n2)))
  3943. {
  3944. SCM result = scm_i_mkbig ();
  3945. mpz_lcm(SCM_I_BIG_MPZ (result),
  3946. SCM_I_BIG_MPZ (n1),
  3947. SCM_I_BIG_MPZ (n2));
  3948. scm_remember_upto_here_2(n1, n2);
  3949. /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
  3950. return result;
  3951. }
  3952. else if (SCM_REALP (n2) && scm_is_integer (n2))
  3953. goto handle_inexacts;
  3954. else
  3955. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  3956. }
  3957. else if (SCM_REALP (n1) && scm_is_integer (n1))
  3958. {
  3959. if (SCM_I_INUMP (n2) || SCM_BIGP (n2)
  3960. || (SCM_REALP (n2) && scm_is_integer (n2)))
  3961. {
  3962. handle_inexacts:
  3963. return scm_exact_to_inexact (scm_lcm (scm_inexact_to_exact (n1),
  3964. scm_inexact_to_exact (n2)));
  3965. }
  3966. else
  3967. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  3968. }
  3969. else
  3970. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG1, s_lcm);
  3971. }
  3972. /* Emulating 2's complement bignums with sign magnitude arithmetic:
  3973. Logand:
  3974. X Y Result Method:
  3975. (len)
  3976. + + + x (map digit:logand X Y)
  3977. + - + x (map digit:logand X (lognot (+ -1 Y)))
  3978. - + + y (map digit:logand (lognot (+ -1 X)) Y)
  3979. - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
  3980. Logior:
  3981. X Y Result Method:
  3982. + + + (map digit:logior X Y)
  3983. + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
  3984. - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
  3985. - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
  3986. Logxor:
  3987. X Y Result Method:
  3988. + + + (map digit:logxor X Y)
  3989. + - - (+ 1 (map digit:logxor X (+ -1 Y)))
  3990. - + - (+ 1 (map digit:logxor (+ -1 X) Y))
  3991. - - + (map digit:logxor (+ -1 X) (+ -1 Y))
  3992. Logtest:
  3993. X Y Result
  3994. + + (any digit:logand X Y)
  3995. + - (any digit:logand X (lognot (+ -1 Y)))
  3996. - + (any digit:logand (lognot (+ -1 X)) Y)
  3997. - - #t
  3998. */
  3999. SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1,
  4000. (SCM x, SCM y, SCM rest),
  4001. "Return the bitwise AND of the integer arguments.\n\n"
  4002. "@lisp\n"
  4003. "(logand) @result{} -1\n"
  4004. "(logand 7) @result{} 7\n"
  4005. "(logand #b111 #b011 #b001) @result{} 1\n"
  4006. "@end lisp")
  4007. #define FUNC_NAME s_scm_i_logand
  4008. {
  4009. while (!scm_is_null (rest))
  4010. { x = scm_logand (x, y);
  4011. y = scm_car (rest);
  4012. rest = scm_cdr (rest);
  4013. }
  4014. return scm_logand (x, y);
  4015. }
  4016. #undef FUNC_NAME
  4017. #define s_scm_logand s_scm_i_logand
  4018. SCM scm_logand (SCM n1, SCM n2)
  4019. #define FUNC_NAME s_scm_logand
  4020. {
  4021. scm_t_inum nn1;
  4022. if (SCM_UNBNDP (n2))
  4023. {
  4024. if (SCM_UNBNDP (n1))
  4025. return SCM_I_MAKINUM (-1);
  4026. else if (!SCM_NUMBERP (n1))
  4027. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4028. else if (SCM_NUMBERP (n1))
  4029. return n1;
  4030. else
  4031. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4032. }
  4033. if (SCM_I_INUMP (n1))
  4034. {
  4035. nn1 = SCM_I_INUM (n1);
  4036. if (SCM_I_INUMP (n2))
  4037. {
  4038. scm_t_inum nn2 = SCM_I_INUM (n2);
  4039. return SCM_I_MAKINUM (nn1 & nn2);
  4040. }
  4041. else if SCM_BIGP (n2)
  4042. {
  4043. intbig:
  4044. if (nn1 == 0)
  4045. return SCM_INUM0;
  4046. {
  4047. SCM result_z = scm_i_mkbig ();
  4048. mpz_t nn1_z;
  4049. mpz_init_set_si (nn1_z, nn1);
  4050. mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  4051. scm_remember_upto_here_1 (n2);
  4052. mpz_clear (nn1_z);
  4053. return scm_i_normbig (result_z);
  4054. }
  4055. }
  4056. else
  4057. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4058. }
  4059. else if (SCM_BIGP (n1))
  4060. {
  4061. if (SCM_I_INUMP (n2))
  4062. {
  4063. SCM_SWAP (n1, n2);
  4064. nn1 = SCM_I_INUM (n1);
  4065. goto intbig;
  4066. }
  4067. else if (SCM_BIGP (n2))
  4068. {
  4069. SCM result_z = scm_i_mkbig ();
  4070. mpz_and (SCM_I_BIG_MPZ (result_z),
  4071. SCM_I_BIG_MPZ (n1),
  4072. SCM_I_BIG_MPZ (n2));
  4073. scm_remember_upto_here_2 (n1, n2);
  4074. return scm_i_normbig (result_z);
  4075. }
  4076. else
  4077. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4078. }
  4079. else
  4080. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4081. }
  4082. #undef FUNC_NAME
  4083. SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1,
  4084. (SCM x, SCM y, SCM rest),
  4085. "Return the bitwise OR of the integer arguments.\n\n"
  4086. "@lisp\n"
  4087. "(logior) @result{} 0\n"
  4088. "(logior 7) @result{} 7\n"
  4089. "(logior #b000 #b001 #b011) @result{} 3\n"
  4090. "@end lisp")
  4091. #define FUNC_NAME s_scm_i_logior
  4092. {
  4093. while (!scm_is_null (rest))
  4094. { x = scm_logior (x, y);
  4095. y = scm_car (rest);
  4096. rest = scm_cdr (rest);
  4097. }
  4098. return scm_logior (x, y);
  4099. }
  4100. #undef FUNC_NAME
  4101. #define s_scm_logior s_scm_i_logior
  4102. SCM scm_logior (SCM n1, SCM n2)
  4103. #define FUNC_NAME s_scm_logior
  4104. {
  4105. scm_t_inum nn1;
  4106. if (SCM_UNBNDP (n2))
  4107. {
  4108. if (SCM_UNBNDP (n1))
  4109. return SCM_INUM0;
  4110. else if (SCM_NUMBERP (n1))
  4111. return n1;
  4112. else
  4113. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4114. }
  4115. if (SCM_I_INUMP (n1))
  4116. {
  4117. nn1 = SCM_I_INUM (n1);
  4118. if (SCM_I_INUMP (n2))
  4119. {
  4120. long nn2 = SCM_I_INUM (n2);
  4121. return SCM_I_MAKINUM (nn1 | nn2);
  4122. }
  4123. else if (SCM_BIGP (n2))
  4124. {
  4125. intbig:
  4126. if (nn1 == 0)
  4127. return n2;
  4128. {
  4129. SCM result_z = scm_i_mkbig ();
  4130. mpz_t nn1_z;
  4131. mpz_init_set_si (nn1_z, nn1);
  4132. mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  4133. scm_remember_upto_here_1 (n2);
  4134. mpz_clear (nn1_z);
  4135. return scm_i_normbig (result_z);
  4136. }
  4137. }
  4138. else
  4139. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4140. }
  4141. else if (SCM_BIGP (n1))
  4142. {
  4143. if (SCM_I_INUMP (n2))
  4144. {
  4145. SCM_SWAP (n1, n2);
  4146. nn1 = SCM_I_INUM (n1);
  4147. goto intbig;
  4148. }
  4149. else if (SCM_BIGP (n2))
  4150. {
  4151. SCM result_z = scm_i_mkbig ();
  4152. mpz_ior (SCM_I_BIG_MPZ (result_z),
  4153. SCM_I_BIG_MPZ (n1),
  4154. SCM_I_BIG_MPZ (n2));
  4155. scm_remember_upto_here_2 (n1, n2);
  4156. return scm_i_normbig (result_z);
  4157. }
  4158. else
  4159. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4160. }
  4161. else
  4162. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4163. }
  4164. #undef FUNC_NAME
  4165. SCM_DEFINE (scm_i_logxor, "logxor", 0, 2, 1,
  4166. (SCM x, SCM y, SCM rest),
  4167. "Return the bitwise XOR of the integer arguments. A bit is\n"
  4168. "set in the result if it is set in an odd number of arguments.\n"
  4169. "@lisp\n"
  4170. "(logxor) @result{} 0\n"
  4171. "(logxor 7) @result{} 7\n"
  4172. "(logxor #b000 #b001 #b011) @result{} 2\n"
  4173. "(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
  4174. "@end lisp")
  4175. #define FUNC_NAME s_scm_i_logxor
  4176. {
  4177. while (!scm_is_null (rest))
  4178. { x = scm_logxor (x, y);
  4179. y = scm_car (rest);
  4180. rest = scm_cdr (rest);
  4181. }
  4182. return scm_logxor (x, y);
  4183. }
  4184. #undef FUNC_NAME
  4185. #define s_scm_logxor s_scm_i_logxor
  4186. SCM scm_logxor (SCM n1, SCM n2)
  4187. #define FUNC_NAME s_scm_logxor
  4188. {
  4189. scm_t_inum nn1;
  4190. if (SCM_UNBNDP (n2))
  4191. {
  4192. if (SCM_UNBNDP (n1))
  4193. return SCM_INUM0;
  4194. else if (SCM_NUMBERP (n1))
  4195. return n1;
  4196. else
  4197. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4198. }
  4199. if (SCM_I_INUMP (n1))
  4200. {
  4201. nn1 = SCM_I_INUM (n1);
  4202. if (SCM_I_INUMP (n2))
  4203. {
  4204. scm_t_inum nn2 = SCM_I_INUM (n2);
  4205. return SCM_I_MAKINUM (nn1 ^ nn2);
  4206. }
  4207. else if (SCM_BIGP (n2))
  4208. {
  4209. intbig:
  4210. {
  4211. SCM result_z = scm_i_mkbig ();
  4212. mpz_t nn1_z;
  4213. mpz_init_set_si (nn1_z, nn1);
  4214. mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  4215. scm_remember_upto_here_1 (n2);
  4216. mpz_clear (nn1_z);
  4217. return scm_i_normbig (result_z);
  4218. }
  4219. }
  4220. else
  4221. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4222. }
  4223. else if (SCM_BIGP (n1))
  4224. {
  4225. if (SCM_I_INUMP (n2))
  4226. {
  4227. SCM_SWAP (n1, n2);
  4228. nn1 = SCM_I_INUM (n1);
  4229. goto intbig;
  4230. }
  4231. else if (SCM_BIGP (n2))
  4232. {
  4233. SCM result_z = scm_i_mkbig ();
  4234. mpz_xor (SCM_I_BIG_MPZ (result_z),
  4235. SCM_I_BIG_MPZ (n1),
  4236. SCM_I_BIG_MPZ (n2));
  4237. scm_remember_upto_here_2 (n1, n2);
  4238. return scm_i_normbig (result_z);
  4239. }
  4240. else
  4241. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  4242. }
  4243. else
  4244. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  4245. }
  4246. #undef FUNC_NAME
  4247. SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
  4248. (SCM j, SCM k),
  4249. "Test whether @var{j} and @var{k} have any 1 bits in common.\n"
  4250. "This is equivalent to @code{(not (zero? (logand j k)))}, but\n"
  4251. "without actually calculating the @code{logand}, just testing\n"
  4252. "for non-zero.\n"
  4253. "\n"
  4254. "@lisp\n"
  4255. "(logtest #b0100 #b1011) @result{} #f\n"
  4256. "(logtest #b0100 #b0111) @result{} #t\n"
  4257. "@end lisp")
  4258. #define FUNC_NAME s_scm_logtest
  4259. {
  4260. scm_t_inum nj;
  4261. if (SCM_I_INUMP (j))
  4262. {
  4263. nj = SCM_I_INUM (j);
  4264. if (SCM_I_INUMP (k))
  4265. {
  4266. scm_t_inum nk = SCM_I_INUM (k);
  4267. return scm_from_bool (nj & nk);
  4268. }
  4269. else if (SCM_BIGP (k))
  4270. {
  4271. intbig:
  4272. if (nj == 0)
  4273. return SCM_BOOL_F;
  4274. {
  4275. SCM result;
  4276. mpz_t nj_z;
  4277. mpz_init_set_si (nj_z, nj);
  4278. mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k));
  4279. scm_remember_upto_here_1 (k);
  4280. result = scm_from_bool (mpz_sgn (nj_z) != 0);
  4281. mpz_clear (nj_z);
  4282. return result;
  4283. }
  4284. }
  4285. else
  4286. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  4287. }
  4288. else if (SCM_BIGP (j))
  4289. {
  4290. if (SCM_I_INUMP (k))
  4291. {
  4292. SCM_SWAP (j, k);
  4293. nj = SCM_I_INUM (j);
  4294. goto intbig;
  4295. }
  4296. else if (SCM_BIGP (k))
  4297. {
  4298. SCM result;
  4299. mpz_t result_z;
  4300. mpz_init (result_z);
  4301. mpz_and (result_z,
  4302. SCM_I_BIG_MPZ (j),
  4303. SCM_I_BIG_MPZ (k));
  4304. scm_remember_upto_here_2 (j, k);
  4305. result = scm_from_bool (mpz_sgn (result_z) != 0);
  4306. mpz_clear (result_z);
  4307. return result;
  4308. }
  4309. else
  4310. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  4311. }
  4312. else
  4313. SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
  4314. }
  4315. #undef FUNC_NAME
  4316. SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
  4317. (SCM index, SCM j),
  4318. "Test whether bit number @var{index} in @var{j} is set.\n"
  4319. "@var{index} starts from 0 for the least significant bit.\n"
  4320. "\n"
  4321. "@lisp\n"
  4322. "(logbit? 0 #b1101) @result{} #t\n"
  4323. "(logbit? 1 #b1101) @result{} #f\n"
  4324. "(logbit? 2 #b1101) @result{} #t\n"
  4325. "(logbit? 3 #b1101) @result{} #t\n"
  4326. "(logbit? 4 #b1101) @result{} #f\n"
  4327. "@end lisp")
  4328. #define FUNC_NAME s_scm_logbit_p
  4329. {
  4330. unsigned long int iindex;
  4331. iindex = scm_to_ulong (index);
  4332. if (SCM_I_INUMP (j))
  4333. {
  4334. if (iindex < SCM_LONG_BIT - 1)
  4335. /* Arrange for the number to be converted to unsigned before
  4336. checking the bit, to ensure that we're testing the bit in a
  4337. two's complement representation (regardless of the native
  4338. representation. */
  4339. return scm_from_bool ((1UL << iindex) & SCM_I_INUM (j));
  4340. else
  4341. /* Portably check the sign. */
  4342. return scm_from_bool (SCM_I_INUM (j) < 0);
  4343. }
  4344. else if (SCM_BIGP (j))
  4345. {
  4346. int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
  4347. scm_remember_upto_here_1 (j);
  4348. return scm_from_bool (val);
  4349. }
  4350. else
  4351. SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
  4352. }
  4353. #undef FUNC_NAME
  4354. SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
  4355. (SCM n),
  4356. "Return the integer which is the ones-complement of the integer\n"
  4357. "argument.\n"
  4358. "\n"
  4359. "@lisp\n"
  4360. "(number->string (lognot #b10000000) 2)\n"
  4361. " @result{} \"-10000001\"\n"
  4362. "(number->string (lognot #b0) 2)\n"
  4363. " @result{} \"-1\"\n"
  4364. "@end lisp")
  4365. #define FUNC_NAME s_scm_lognot
  4366. {
  4367. if (SCM_I_INUMP (n)) {
  4368. /* No overflow here, just need to toggle all the bits making up the inum.
  4369. Enhancement: No need to strip the tag and add it back, could just xor
  4370. a block of 1 bits, if that worked with the various debug versions of
  4371. the SCM typedef. */
  4372. return SCM_I_MAKINUM (~ SCM_I_INUM (n));
  4373. } else if (SCM_BIGP (n)) {
  4374. SCM result = scm_i_mkbig ();
  4375. mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
  4376. scm_remember_upto_here_1 (n);
  4377. return result;
  4378. } else {
  4379. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4380. }
  4381. }
  4382. #undef FUNC_NAME
  4383. /* returns 0 if IN is not an integer. OUT must already be
  4384. initialized. */
  4385. static int
  4386. coerce_to_big (SCM in, mpz_t out)
  4387. {
  4388. if (SCM_BIGP (in))
  4389. mpz_set (out, SCM_I_BIG_MPZ (in));
  4390. else if (SCM_I_INUMP (in))
  4391. mpz_set_si (out, SCM_I_INUM (in));
  4392. else
  4393. return 0;
  4394. return 1;
  4395. }
  4396. SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
  4397. (SCM n, SCM k, SCM m),
  4398. "Return @var{n} raised to the integer exponent\n"
  4399. "@var{k}, modulo @var{m}.\n"
  4400. "\n"
  4401. "@lisp\n"
  4402. "(modulo-expt 2 3 5)\n"
  4403. " @result{} 3\n"
  4404. "@end lisp")
  4405. #define FUNC_NAME s_scm_modulo_expt
  4406. {
  4407. mpz_t n_tmp;
  4408. mpz_t k_tmp;
  4409. mpz_t m_tmp;
  4410. /* There are two classes of error we might encounter --
  4411. 1) Math errors, which we'll report by calling scm_num_overflow,
  4412. and
  4413. 2) wrong-type errors, which of course we'll report by calling
  4414. SCM_WRONG_TYPE_ARG.
  4415. We don't report those errors immediately, however; instead we do
  4416. some cleanup first. These variables tell us which error (if
  4417. any) we should report after cleaning up.
  4418. */
  4419. int report_overflow = 0;
  4420. int position_of_wrong_type = 0;
  4421. SCM value_of_wrong_type = SCM_INUM0;
  4422. SCM result = SCM_UNDEFINED;
  4423. mpz_init (n_tmp);
  4424. mpz_init (k_tmp);
  4425. mpz_init (m_tmp);
  4426. if (scm_is_eq (m, SCM_INUM0))
  4427. {
  4428. report_overflow = 1;
  4429. goto cleanup;
  4430. }
  4431. if (!coerce_to_big (n, n_tmp))
  4432. {
  4433. value_of_wrong_type = n;
  4434. position_of_wrong_type = 1;
  4435. goto cleanup;
  4436. }
  4437. if (!coerce_to_big (k, k_tmp))
  4438. {
  4439. value_of_wrong_type = k;
  4440. position_of_wrong_type = 2;
  4441. goto cleanup;
  4442. }
  4443. if (!coerce_to_big (m, m_tmp))
  4444. {
  4445. value_of_wrong_type = m;
  4446. position_of_wrong_type = 3;
  4447. goto cleanup;
  4448. }
  4449. /* if the exponent K is negative, and we simply call mpz_powm, we
  4450. will get a divide-by-zero exception when an inverse 1/n mod m
  4451. doesn't exist (or is not unique). Since exceptions are hard to
  4452. handle, we'll attempt the inversion "by hand" -- that way, we get
  4453. a simple failure code, which is easy to handle. */
  4454. if (-1 == mpz_sgn (k_tmp))
  4455. {
  4456. if (!mpz_invert (n_tmp, n_tmp, m_tmp))
  4457. {
  4458. report_overflow = 1;
  4459. goto cleanup;
  4460. }
  4461. mpz_neg (k_tmp, k_tmp);
  4462. }
  4463. result = scm_i_mkbig ();
  4464. mpz_powm (SCM_I_BIG_MPZ (result),
  4465. n_tmp,
  4466. k_tmp,
  4467. m_tmp);
  4468. if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
  4469. mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp);
  4470. cleanup:
  4471. mpz_clear (m_tmp);
  4472. mpz_clear (k_tmp);
  4473. mpz_clear (n_tmp);
  4474. if (report_overflow)
  4475. scm_num_overflow (FUNC_NAME);
  4476. if (position_of_wrong_type)
  4477. SCM_WRONG_TYPE_ARG (position_of_wrong_type,
  4478. value_of_wrong_type);
  4479. return scm_i_normbig (result);
  4480. }
  4481. #undef FUNC_NAME
  4482. SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
  4483. (SCM n, SCM k),
  4484. "Return @var{n} raised to the power @var{k}. @var{k} must be an\n"
  4485. "exact integer, @var{n} can be any number.\n"
  4486. "\n"
  4487. "Negative @var{k} is supported, and results in\n"
  4488. "@math{1/@var{n}^abs(@var{k})} in the usual way.\n"
  4489. "@math{@var{n}^0} is 1, as usual, and that\n"
  4490. "includes @math{0^0} is 1.\n"
  4491. "\n"
  4492. "@lisp\n"
  4493. "(integer-expt 2 5) @result{} 32\n"
  4494. "(integer-expt -3 3) @result{} -27\n"
  4495. "(integer-expt 5 -3) @result{} 1/125\n"
  4496. "(integer-expt 0 0) @result{} 1\n"
  4497. "@end lisp")
  4498. #define FUNC_NAME s_scm_integer_expt
  4499. {
  4500. scm_t_inum i2 = 0;
  4501. SCM z_i2 = SCM_BOOL_F;
  4502. int i2_is_big = 0;
  4503. SCM acc = SCM_I_MAKINUM (1L);
  4504. /* Specifically refrain from checking the type of the first argument.
  4505. This allows us to exponentiate any object that can be multiplied.
  4506. If we must raise to a negative power, we must also be able to
  4507. take its reciprocal. */
  4508. if (!SCM_LIKELY (SCM_I_INUMP (k)) && !SCM_LIKELY (SCM_BIGP (k)))
  4509. SCM_WRONG_TYPE_ARG (2, k);
  4510. if (SCM_UNLIKELY (scm_is_eq (k, SCM_INUM0)))
  4511. return SCM_INUM1; /* n^(exact0) is exact 1, regardless of n */
  4512. else if (SCM_UNLIKELY (scm_is_eq (n, SCM_I_MAKINUM (-1L))))
  4513. return scm_is_false (scm_even_p (k)) ? n : SCM_INUM1;
  4514. /* The next check is necessary only because R6RS specifies different
  4515. behavior for 0^(-k) than for (/ 0). If n is not a scheme number,
  4516. we simply skip this case and move on. */
  4517. else if (SCM_NUMBERP (n) && scm_is_true (scm_zero_p (n)))
  4518. {
  4519. /* k cannot be 0 at this point, because we
  4520. have already checked for that case above */
  4521. if (scm_is_true (scm_positive_p (k)))
  4522. return n;
  4523. else /* return NaN for (0 ^ k) for negative k per R6RS */
  4524. return scm_nan ();
  4525. }
  4526. else if (SCM_FRACTIONP (n))
  4527. {
  4528. /* Optimize the fraction case by (a/b)^k ==> (a^k)/(b^k), to avoid
  4529. needless reduction of intermediate products to lowest terms.
  4530. If a and b have no common factors, then a^k and b^k have no
  4531. common factors. Use 'scm_i_make_ratio_already_reduced' to
  4532. construct the final result, so that no gcd computations are
  4533. needed to exponentiate a fraction. */
  4534. if (scm_is_true (scm_positive_p (k)))
  4535. return scm_i_make_ratio_already_reduced
  4536. (scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k),
  4537. scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k));
  4538. else
  4539. {
  4540. k = scm_difference (k, SCM_UNDEFINED);
  4541. return scm_i_make_ratio_already_reduced
  4542. (scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k),
  4543. scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k));
  4544. }
  4545. }
  4546. if (SCM_I_INUMP (k))
  4547. i2 = SCM_I_INUM (k);
  4548. else if (SCM_BIGP (k))
  4549. {
  4550. z_i2 = scm_i_clonebig (k, 1);
  4551. scm_remember_upto_here_1 (k);
  4552. i2_is_big = 1;
  4553. }
  4554. else
  4555. SCM_WRONG_TYPE_ARG (2, k);
  4556. if (i2_is_big)
  4557. {
  4558. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1)
  4559. {
  4560. mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2));
  4561. n = scm_divide (n, SCM_UNDEFINED);
  4562. }
  4563. while (1)
  4564. {
  4565. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0)
  4566. {
  4567. return acc;
  4568. }
  4569. if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0)
  4570. {
  4571. return scm_product (acc, n);
  4572. }
  4573. if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0))
  4574. acc = scm_product (acc, n);
  4575. n = scm_product (n, n);
  4576. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1);
  4577. }
  4578. }
  4579. else
  4580. {
  4581. if (i2 < 0)
  4582. {
  4583. i2 = -i2;
  4584. n = scm_divide (n, SCM_UNDEFINED);
  4585. }
  4586. while (1)
  4587. {
  4588. if (0 == i2)
  4589. return acc;
  4590. if (1 == i2)
  4591. return scm_product (acc, n);
  4592. if (i2 & 1)
  4593. acc = scm_product (acc, n);
  4594. n = scm_product (n, n);
  4595. i2 >>= 1;
  4596. }
  4597. }
  4598. }
  4599. #undef FUNC_NAME
  4600. /* Efficiently compute (N * 2^COUNT),
  4601. where N is an exact integer, and COUNT > 0. */
  4602. static SCM
  4603. left_shift_exact_integer (SCM n, long count)
  4604. {
  4605. if (SCM_I_INUMP (n))
  4606. {
  4607. scm_t_inum nn = SCM_I_INUM (n);
  4608. /* Left shift of count >= SCM_I_FIXNUM_BIT-1 will almost[*] always
  4609. overflow a non-zero fixnum. For smaller shifts we check the
  4610. bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
  4611. all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
  4612. Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 - count)".
  4613. [*] There's one exception:
  4614. (-1) << SCM_I_FIXNUM_BIT-1 == SCM_MOST_NEGATIVE_FIXNUM */
  4615. if (nn == 0)
  4616. return n;
  4617. else if (count < SCM_I_FIXNUM_BIT-1 &&
  4618. ((scm_t_bits) (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - count)) + 1)
  4619. <= 1))
  4620. return SCM_I_MAKINUM (nn < 0 ? -(-nn << count) : (nn << count));
  4621. else
  4622. {
  4623. SCM result = scm_i_inum2big (nn);
  4624. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  4625. count);
  4626. return scm_i_normbig (result);
  4627. }
  4628. }
  4629. else if (SCM_BIGP (n))
  4630. {
  4631. SCM result = scm_i_mkbig ();
  4632. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n), count);
  4633. scm_remember_upto_here_1 (n);
  4634. return result;
  4635. }
  4636. else
  4637. assert (0);
  4638. }
  4639. /* Efficiently compute floor (N / 2^COUNT),
  4640. where N is an exact integer and COUNT > 0. */
  4641. static SCM
  4642. floor_right_shift_exact_integer (SCM n, long count)
  4643. {
  4644. if (SCM_I_INUMP (n))
  4645. {
  4646. scm_t_inum nn = SCM_I_INUM (n);
  4647. if (count >= SCM_I_FIXNUM_BIT)
  4648. return (nn >= 0 ? SCM_INUM0 : SCM_I_MAKINUM (-1));
  4649. else
  4650. return SCM_I_MAKINUM (SCM_SRS (nn, count));
  4651. }
  4652. else if (SCM_BIGP (n))
  4653. {
  4654. SCM result = scm_i_mkbig ();
  4655. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
  4656. count);
  4657. scm_remember_upto_here_1 (n);
  4658. return scm_i_normbig (result);
  4659. }
  4660. else
  4661. assert (0);
  4662. }
  4663. /* Efficiently compute round (N / 2^COUNT),
  4664. where N is an exact integer and COUNT > 0. */
  4665. static SCM
  4666. round_right_shift_exact_integer (SCM n, long count)
  4667. {
  4668. if (SCM_I_INUMP (n))
  4669. {
  4670. if (count >= SCM_I_FIXNUM_BIT)
  4671. return SCM_INUM0;
  4672. else
  4673. {
  4674. scm_t_inum nn = SCM_I_INUM (n);
  4675. scm_t_inum qq = SCM_SRS (nn, count);
  4676. if (0 == (nn & (1L << (count-1))))
  4677. return SCM_I_MAKINUM (qq); /* round down */
  4678. else if (nn & ((1L << (count-1)) - 1))
  4679. return SCM_I_MAKINUM (qq + 1); /* round up */
  4680. else
  4681. return SCM_I_MAKINUM ((~1L) & (qq + 1)); /* round to even */
  4682. }
  4683. }
  4684. else if (SCM_BIGP (n))
  4685. {
  4686. SCM q = scm_i_mkbig ();
  4687. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (n), count);
  4688. if (mpz_tstbit (SCM_I_BIG_MPZ (n), count-1)
  4689. && (mpz_odd_p (SCM_I_BIG_MPZ (q))
  4690. || (mpz_scan1 (SCM_I_BIG_MPZ (n), 0) < count-1)))
  4691. mpz_add_ui (SCM_I_BIG_MPZ (q), SCM_I_BIG_MPZ (q), 1);
  4692. scm_remember_upto_here_1 (n);
  4693. return scm_i_normbig (q);
  4694. }
  4695. else
  4696. assert (0);
  4697. }
  4698. /* 'scm_ash' and 'scm_round_ash' assume that fixnums fit within a long,
  4699. and moreover that they can be negated without overflow. */
  4700. verify (SCM_MOST_NEGATIVE_FIXNUM >= LONG_MIN + 1
  4701. && SCM_MOST_POSITIVE_FIXNUM <= LONG_MAX);
  4702. SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
  4703. (SCM n, SCM count),
  4704. "Return @math{floor(@var{n} * 2^@var{count})}.\n"
  4705. "@var{n} and @var{count} must be exact integers.\n"
  4706. "\n"
  4707. "With @var{n} viewed as an infinite-precision twos-complement\n"
  4708. "integer, @code{ash} means a left shift introducing zero bits\n"
  4709. "when @var{count} is positive, or a right shift dropping bits\n"
  4710. "when @var{count} is negative. This is an ``arithmetic'' shift.\n"
  4711. "\n"
  4712. "@lisp\n"
  4713. "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
  4714. "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
  4715. "\n"
  4716. ";; -23 is bits ...11101001, -6 is bits ...111010\n"
  4717. "(ash -23 -2) @result{} -6\n"
  4718. "@end lisp")
  4719. #define FUNC_NAME s_scm_ash
  4720. {
  4721. if (SCM_I_INUMP (n) || SCM_BIGP (n))
  4722. {
  4723. long bits_to_shift;
  4724. if (SCM_I_INUMP (count)) /* fast path, not strictly needed */
  4725. bits_to_shift = SCM_I_INUM (count);
  4726. else if (scm_is_signed_integer (count, LONG_MIN + 1, LONG_MAX))
  4727. /* We exclude LONG_MIN to ensure that 'bits_to_shift' can be
  4728. negated without overflowing. */
  4729. bits_to_shift = scm_to_long (count);
  4730. else if (scm_is_false (scm_positive_p (scm_sum (scm_integer_length (n),
  4731. count))))
  4732. /* Huge right shift that eliminates all but the sign bit */
  4733. return scm_is_false (scm_negative_p (n))
  4734. ? SCM_INUM0 : SCM_I_MAKINUM (-1);
  4735. else if (scm_is_true (scm_zero_p (n)))
  4736. return SCM_INUM0;
  4737. else
  4738. scm_num_overflow ("ash");
  4739. if (bits_to_shift > 0)
  4740. return left_shift_exact_integer (n, bits_to_shift);
  4741. else if (SCM_LIKELY (bits_to_shift < 0))
  4742. return floor_right_shift_exact_integer (n, -bits_to_shift);
  4743. else
  4744. return n;
  4745. }
  4746. else
  4747. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4748. }
  4749. #undef FUNC_NAME
  4750. SCM_DEFINE (scm_round_ash, "round-ash", 2, 0, 0,
  4751. (SCM n, SCM count),
  4752. "Return @math{round(@var{n} * 2^@var{count})}.\n"
  4753. "@var{n} and @var{count} must be exact integers.\n"
  4754. "\n"
  4755. "With @var{n} viewed as an infinite-precision twos-complement\n"
  4756. "integer, @code{round-ash} means a left shift introducing zero\n"
  4757. "bits when @var{count} is positive, or a right shift rounding\n"
  4758. "to the nearest integer (with ties going to the nearest even\n"
  4759. "integer) when @var{count} is negative. This is a rounded\n"
  4760. "``arithmetic'' shift.\n"
  4761. "\n"
  4762. "@lisp\n"
  4763. "(number->string (round-ash #b1 3) 2) @result{} \"1000\"\n"
  4764. "(number->string (round-ash #b1010 -1) 2) @result{} \"101\"\n"
  4765. "(number->string (round-ash #b1010 -2) 2) @result{} \"10\"\n"
  4766. "(number->string (round-ash #b1011 -2) 2) @result{} \"11\"\n"
  4767. "(number->string (round-ash #b1101 -2) 2) @result{} \"11\"\n"
  4768. "(number->string (round-ash #b1110 -2) 2) @result{} \"100\"\n"
  4769. "@end lisp")
  4770. #define FUNC_NAME s_scm_round_ash
  4771. {
  4772. if (SCM_I_INUMP (n) || SCM_BIGP (n))
  4773. {
  4774. long bits_to_shift;
  4775. if (SCM_I_INUMP (count)) /* fast path, not strictly needed */
  4776. bits_to_shift = SCM_I_INUM (count);
  4777. else if (scm_is_signed_integer (count, LONG_MIN + 1, LONG_MAX))
  4778. /* We exclude LONG_MIN to ensure that 'bits_to_shift' can be
  4779. negated without overflowing. */
  4780. bits_to_shift = scm_to_long (count);
  4781. else if (scm_is_true (scm_negative_p (scm_sum (scm_integer_length (n),
  4782. count)))
  4783. || scm_is_true (scm_zero_p (n)))
  4784. /* If N is zero, or the right shift count exceeds the integer
  4785. length, the result is zero. */
  4786. return SCM_INUM0;
  4787. else
  4788. scm_num_overflow ("round-ash");
  4789. if (bits_to_shift > 0)
  4790. return left_shift_exact_integer (n, bits_to_shift);
  4791. else if (SCM_LIKELY (bits_to_shift < 0))
  4792. return round_right_shift_exact_integer (n, -bits_to_shift);
  4793. else
  4794. return n;
  4795. }
  4796. else
  4797. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4798. }
  4799. #undef FUNC_NAME
  4800. #define MIN(A, B) ((A) <= (B) ? (A) : (B))
  4801. SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
  4802. (SCM n, SCM start, SCM end),
  4803. "Return the integer composed of the @var{start} (inclusive)\n"
  4804. "through @var{end} (exclusive) bits of @var{n}. The\n"
  4805. "@var{start}th bit becomes the 0-th bit in the result.\n"
  4806. "\n"
  4807. "@lisp\n"
  4808. "(number->string (bit-extract #b1101101010 0 4) 2)\n"
  4809. " @result{} \"1010\"\n"
  4810. "(number->string (bit-extract #b1101101010 4 9) 2)\n"
  4811. " @result{} \"10110\"\n"
  4812. "@end lisp")
  4813. #define FUNC_NAME s_scm_bit_extract
  4814. {
  4815. unsigned long int istart, iend, bits;
  4816. istart = scm_to_ulong (start);
  4817. iend = scm_to_ulong (end);
  4818. SCM_ASSERT_RANGE (3, end, (iend >= istart));
  4819. /* how many bits to keep */
  4820. bits = iend - istart;
  4821. if (SCM_I_INUMP (n))
  4822. {
  4823. scm_t_inum in = SCM_I_INUM (n);
  4824. /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
  4825. SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
  4826. in = SCM_SRS (in, MIN (istart, SCM_I_FIXNUM_BIT-1));
  4827. if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
  4828. {
  4829. /* Since we emulate two's complement encoded numbers, this
  4830. * special case requires us to produce a result that has
  4831. * more bits than can be stored in a fixnum.
  4832. */
  4833. SCM result = scm_i_inum2big (in);
  4834. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  4835. bits);
  4836. return result;
  4837. }
  4838. /* mask down to requisite bits */
  4839. bits = MIN (bits, SCM_I_FIXNUM_BIT);
  4840. return SCM_I_MAKINUM (in & ((1L << bits) - 1));
  4841. }
  4842. else if (SCM_BIGP (n))
  4843. {
  4844. SCM result;
  4845. if (bits == 1)
  4846. {
  4847. result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
  4848. }
  4849. else
  4850. {
  4851. /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
  4852. bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
  4853. such bits into a ulong. */
  4854. result = scm_i_mkbig ();
  4855. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
  4856. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
  4857. result = scm_i_normbig (result);
  4858. }
  4859. scm_remember_upto_here_1 (n);
  4860. return result;
  4861. }
  4862. else
  4863. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4864. }
  4865. #undef FUNC_NAME
  4866. static const char scm_logtab[] = {
  4867. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  4868. };
  4869. SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
  4870. (SCM n),
  4871. "Return the number of bits in integer @var{n}. If integer is\n"
  4872. "positive, the 1-bits in its binary representation are counted.\n"
  4873. "If negative, the 0-bits in its two's-complement binary\n"
  4874. "representation are counted. If 0, 0 is returned.\n"
  4875. "\n"
  4876. "@lisp\n"
  4877. "(logcount #b10101010)\n"
  4878. " @result{} 4\n"
  4879. "(logcount 0)\n"
  4880. " @result{} 0\n"
  4881. "(logcount -2)\n"
  4882. " @result{} 1\n"
  4883. "@end lisp")
  4884. #define FUNC_NAME s_scm_logcount
  4885. {
  4886. if (SCM_I_INUMP (n))
  4887. {
  4888. unsigned long c = 0;
  4889. scm_t_inum nn = SCM_I_INUM (n);
  4890. if (nn < 0)
  4891. nn = -1 - nn;
  4892. while (nn)
  4893. {
  4894. c += scm_logtab[15 & nn];
  4895. nn >>= 4;
  4896. }
  4897. return SCM_I_MAKINUM (c);
  4898. }
  4899. else if (SCM_BIGP (n))
  4900. {
  4901. unsigned long count;
  4902. if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
  4903. count = mpz_popcount (SCM_I_BIG_MPZ (n));
  4904. else
  4905. count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
  4906. scm_remember_upto_here_1 (n);
  4907. return SCM_I_MAKINUM (count);
  4908. }
  4909. else
  4910. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4911. }
  4912. #undef FUNC_NAME
  4913. static const char scm_ilentab[] = {
  4914. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
  4915. };
  4916. SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
  4917. (SCM n),
  4918. "Return the number of bits necessary to represent @var{n}.\n"
  4919. "\n"
  4920. "@lisp\n"
  4921. "(integer-length #b10101010)\n"
  4922. " @result{} 8\n"
  4923. "(integer-length 0)\n"
  4924. " @result{} 0\n"
  4925. "(integer-length #b1111)\n"
  4926. " @result{} 4\n"
  4927. "@end lisp")
  4928. #define FUNC_NAME s_scm_integer_length
  4929. {
  4930. if (SCM_I_INUMP (n))
  4931. {
  4932. unsigned long c = 0;
  4933. unsigned int l = 4;
  4934. scm_t_inum nn = SCM_I_INUM (n);
  4935. if (nn < 0)
  4936. nn = -1 - nn;
  4937. while (nn)
  4938. {
  4939. c += 4;
  4940. l = scm_ilentab [15 & nn];
  4941. nn >>= 4;
  4942. }
  4943. return SCM_I_MAKINUM (c - 4 + l);
  4944. }
  4945. else if (SCM_BIGP (n))
  4946. {
  4947. /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
  4948. want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
  4949. 1 too big, so check for that and adjust. */
  4950. size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
  4951. if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
  4952. && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
  4953. mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
  4954. size--;
  4955. scm_remember_upto_here_1 (n);
  4956. return SCM_I_MAKINUM (size);
  4957. }
  4958. else
  4959. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4960. }
  4961. #undef FUNC_NAME
  4962. /*** NUMBERS -> STRINGS ***/
  4963. #define SCM_MAX_DBL_RADIX 36
  4964. /* use this array as a way to generate a single digit */
  4965. static const char number_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  4966. static mpz_t dbl_minimum_normal_mantissa;
  4967. static size_t
  4968. idbl2str (double dbl, char *a, int radix)
  4969. {
  4970. int ch = 0;
  4971. if (radix < 2 || radix > SCM_MAX_DBL_RADIX)
  4972. /* revert to existing behavior */
  4973. radix = 10;
  4974. if (isinf (dbl))
  4975. {
  4976. strcpy (a, (dbl > 0.0) ? "+inf.0" : "-inf.0");
  4977. return 6;
  4978. }
  4979. else if (dbl > 0.0)
  4980. ;
  4981. else if (dbl < 0.0)
  4982. {
  4983. dbl = -dbl;
  4984. a[ch++] = '-';
  4985. }
  4986. else if (dbl == 0.0)
  4987. {
  4988. if (copysign (1.0, dbl) < 0.0)
  4989. a[ch++] = '-';
  4990. strcpy (a + ch, "0.0");
  4991. return ch + 3;
  4992. }
  4993. else if (isnan (dbl))
  4994. {
  4995. strcpy (a, "+nan.0");
  4996. return 6;
  4997. }
  4998. /* Algorithm taken from "Printing Floating-Point Numbers Quickly and
  4999. Accurately" by Robert G. Burger and R. Kent Dybvig */
  5000. {
  5001. int e, k;
  5002. mpz_t f, r, s, mplus, mminus, hi, digit;
  5003. int f_is_even, f_is_odd;
  5004. int expon;
  5005. int show_exp = 0;
  5006. mpz_inits (f, r, s, mplus, mminus, hi, digit, NULL);
  5007. mpz_set_d (f, ldexp (frexp (dbl, &e), DBL_MANT_DIG));
  5008. if (e < DBL_MIN_EXP)
  5009. {
  5010. mpz_tdiv_q_2exp (f, f, DBL_MIN_EXP - e);
  5011. e = DBL_MIN_EXP;
  5012. }
  5013. e -= DBL_MANT_DIG;
  5014. f_is_even = !mpz_odd_p (f);
  5015. f_is_odd = !f_is_even;
  5016. /* Initialize r, s, mplus, and mminus according
  5017. to Table 1 from the paper. */
  5018. if (e < 0)
  5019. {
  5020. mpz_set_ui (mminus, 1);
  5021. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0
  5022. || e == DBL_MIN_EXP - DBL_MANT_DIG)
  5023. {
  5024. mpz_set_ui (mplus, 1);
  5025. mpz_mul_2exp (r, f, 1);
  5026. mpz_mul_2exp (s, mminus, 1 - e);
  5027. }
  5028. else
  5029. {
  5030. mpz_set_ui (mplus, 2);
  5031. mpz_mul_2exp (r, f, 2);
  5032. mpz_mul_2exp (s, mminus, 2 - e);
  5033. }
  5034. }
  5035. else
  5036. {
  5037. mpz_set_ui (mminus, 1);
  5038. mpz_mul_2exp (mminus, mminus, e);
  5039. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0)
  5040. {
  5041. mpz_set (mplus, mminus);
  5042. mpz_mul_2exp (r, f, 1 + e);
  5043. mpz_set_ui (s, 2);
  5044. }
  5045. else
  5046. {
  5047. mpz_mul_2exp (mplus, mminus, 1);
  5048. mpz_mul_2exp (r, f, 2 + e);
  5049. mpz_set_ui (s, 4);
  5050. }
  5051. }
  5052. /* Find the smallest k such that:
  5053. (r + mplus) / s < radix^k (if f is even)
  5054. (r + mplus) / s <= radix^k (if f is odd) */
  5055. {
  5056. /* IMPROVE-ME: Make an initial guess to speed this up */
  5057. mpz_add (hi, r, mplus);
  5058. k = 0;
  5059. while (mpz_cmp (hi, s) >= f_is_odd)
  5060. {
  5061. mpz_mul_ui (s, s, radix);
  5062. k++;
  5063. }
  5064. if (k == 0)
  5065. {
  5066. mpz_mul_ui (hi, hi, radix);
  5067. while (mpz_cmp (hi, s) < f_is_odd)
  5068. {
  5069. mpz_mul_ui (r, r, radix);
  5070. mpz_mul_ui (mplus, mplus, radix);
  5071. mpz_mul_ui (mminus, mminus, radix);
  5072. mpz_mul_ui (hi, hi, radix);
  5073. k--;
  5074. }
  5075. }
  5076. }
  5077. expon = k - 1;
  5078. if (k <= 0)
  5079. {
  5080. if (k <= -3)
  5081. {
  5082. /* Use scientific notation */
  5083. show_exp = 1;
  5084. k = 1;
  5085. }
  5086. else
  5087. {
  5088. int i;
  5089. /* Print leading zeroes */
  5090. a[ch++] = '0';
  5091. a[ch++] = '.';
  5092. for (i = 0; i > k; i--)
  5093. a[ch++] = '0';
  5094. }
  5095. }
  5096. for (;;)
  5097. {
  5098. int end_1_p, end_2_p;
  5099. int d;
  5100. mpz_mul_ui (mplus, mplus, radix);
  5101. mpz_mul_ui (mminus, mminus, radix);
  5102. mpz_mul_ui (r, r, radix);
  5103. mpz_fdiv_qr (digit, r, r, s);
  5104. d = mpz_get_ui (digit);
  5105. mpz_add (hi, r, mplus);
  5106. end_1_p = (mpz_cmp (r, mminus) < f_is_even);
  5107. end_2_p = (mpz_cmp (s, hi) < f_is_even);
  5108. if (end_1_p || end_2_p)
  5109. {
  5110. mpz_mul_2exp (r, r, 1);
  5111. if (!end_2_p)
  5112. ;
  5113. else if (!end_1_p)
  5114. d++;
  5115. else if (mpz_cmp (r, s) >= !(d & 1))
  5116. d++;
  5117. a[ch++] = number_chars[d];
  5118. if (--k == 0)
  5119. a[ch++] = '.';
  5120. break;
  5121. }
  5122. else
  5123. {
  5124. a[ch++] = number_chars[d];
  5125. if (--k == 0)
  5126. a[ch++] = '.';
  5127. }
  5128. }
  5129. if (k > 0)
  5130. {
  5131. if (expon >= 7 && k >= 4 && expon >= k)
  5132. {
  5133. /* Here we would have to print more than three zeroes
  5134. followed by a decimal point and another zero. It
  5135. makes more sense to use scientific notation. */
  5136. /* Adjust k to what it would have been if we had chosen
  5137. scientific notation from the beginning. */
  5138. k -= expon;
  5139. /* k will now be <= 0, with magnitude equal to the number of
  5140. digits that we printed which should now be put after the
  5141. decimal point. */
  5142. /* Insert a decimal point */
  5143. memmove (a + ch + k + 1, a + ch + k, -k);
  5144. a[ch + k] = '.';
  5145. ch++;
  5146. show_exp = 1;
  5147. }
  5148. else
  5149. {
  5150. for (; k > 0; k--)
  5151. a[ch++] = '0';
  5152. a[ch++] = '.';
  5153. }
  5154. }
  5155. if (k == 0)
  5156. a[ch++] = '0';
  5157. if (show_exp)
  5158. {
  5159. a[ch++] = 'e';
  5160. ch += scm_iint2str (expon, radix, a + ch);
  5161. }
  5162. mpz_clears (f, r, s, mplus, mminus, hi, digit, NULL);
  5163. }
  5164. return ch;
  5165. }
  5166. static size_t
  5167. icmplx2str (double real, double imag, char *str, int radix)
  5168. {
  5169. size_t i;
  5170. double sgn;
  5171. i = idbl2str (real, str, radix);
  5172. #ifdef HAVE_COPYSIGN
  5173. sgn = copysign (1.0, imag);
  5174. #else
  5175. sgn = imag;
  5176. #endif
  5177. /* Don't output a '+' for negative numbers or for Inf and
  5178. NaN. They will provide their own sign. */
  5179. if (sgn >= 0 && isfinite (imag))
  5180. str[i++] = '+';
  5181. i += idbl2str (imag, &str[i], radix);
  5182. str[i++] = 'i';
  5183. return i;
  5184. }
  5185. static size_t
  5186. iflo2str (SCM flt, char *str, int radix)
  5187. {
  5188. size_t i;
  5189. if (SCM_REALP (flt))
  5190. i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
  5191. else
  5192. i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
  5193. str, radix);
  5194. return i;
  5195. }
  5196. /* convert a intmax_t to a string (unterminated). returns the number of
  5197. characters in the result.
  5198. rad is output base
  5199. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  5200. size_t
  5201. scm_iint2str (intmax_t num, int rad, char *p)
  5202. {
  5203. if (num < 0)
  5204. {
  5205. *p++ = '-';
  5206. return scm_iuint2str (-num, rad, p) + 1;
  5207. }
  5208. else
  5209. return scm_iuint2str (num, rad, p);
  5210. }
  5211. /* convert a intmax_t to a string (unterminated). returns the number of
  5212. characters in the result.
  5213. rad is output base
  5214. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  5215. size_t
  5216. scm_iuint2str (uintmax_t num, int rad, char *p)
  5217. {
  5218. size_t j = 1;
  5219. size_t i;
  5220. uintmax_t n = num;
  5221. if (rad < 2 || rad > 36)
  5222. scm_out_of_range ("scm_iuint2str", scm_from_int (rad));
  5223. for (n /= rad; n > 0; n /= rad)
  5224. j++;
  5225. i = j;
  5226. n = num;
  5227. while (i--)
  5228. {
  5229. int d = n % rad;
  5230. n /= rad;
  5231. p[i] = number_chars[d];
  5232. }
  5233. return j;
  5234. }
  5235. SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
  5236. (SCM n, SCM radix),
  5237. "Return a string holding the external representation of the\n"
  5238. "number @var{n} in the given @var{radix}. If @var{n} is\n"
  5239. "inexact, a radix of 10 will be used.")
  5240. #define FUNC_NAME s_scm_number_to_string
  5241. {
  5242. int base;
  5243. if (SCM_UNBNDP (radix))
  5244. base = 10;
  5245. else
  5246. base = scm_to_signed_integer (radix, 2, 36);
  5247. if (SCM_I_INUMP (n))
  5248. {
  5249. char num_buf [SCM_INTBUFLEN];
  5250. size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
  5251. return scm_from_latin1_stringn (num_buf, length);
  5252. }
  5253. else if (SCM_BIGP (n))
  5254. {
  5255. char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
  5256. size_t len = strlen (str);
  5257. void (*freefunc) (void *, size_t);
  5258. SCM ret;
  5259. mp_get_memory_functions (NULL, NULL, &freefunc);
  5260. scm_remember_upto_here_1 (n);
  5261. ret = scm_from_latin1_stringn (str, len);
  5262. freefunc (str, len + 1);
  5263. return ret;
  5264. }
  5265. else if (SCM_FRACTIONP (n))
  5266. {
  5267. return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
  5268. scm_from_latin1_string ("/"),
  5269. scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
  5270. }
  5271. else if (SCM_INEXACTP (n))
  5272. {
  5273. char num_buf [FLOBUFLEN];
  5274. return scm_from_latin1_stringn (num_buf, iflo2str (n, num_buf, base));
  5275. }
  5276. else
  5277. SCM_WRONG_TYPE_ARG (1, n);
  5278. }
  5279. #undef FUNC_NAME
  5280. /* These print routines used to be stubbed here so that scm_repl.c
  5281. wouldn't need SCM_BIGDIG conditionals (pre GMP) */
  5282. int
  5283. scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5284. {
  5285. char num_buf[FLOBUFLEN];
  5286. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  5287. return !0;
  5288. }
  5289. void
  5290. scm_i_print_double (double val, SCM port)
  5291. {
  5292. char num_buf[FLOBUFLEN];
  5293. scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
  5294. }
  5295. int
  5296. scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5297. {
  5298. char num_buf[FLOBUFLEN];
  5299. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  5300. return !0;
  5301. }
  5302. void
  5303. scm_i_print_complex (double real, double imag, SCM port)
  5304. {
  5305. char num_buf[FLOBUFLEN];
  5306. scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
  5307. }
  5308. int
  5309. scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5310. {
  5311. SCM str;
  5312. str = scm_number_to_string (sexp, SCM_UNDEFINED);
  5313. scm_display (str, port);
  5314. scm_remember_upto_here_1 (str);
  5315. return !0;
  5316. }
  5317. int
  5318. scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5319. {
  5320. char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
  5321. size_t len = strlen (str);
  5322. void (*freefunc) (void *, size_t);
  5323. mp_get_memory_functions (NULL, NULL, &freefunc);
  5324. scm_remember_upto_here_1 (exp);
  5325. scm_lfwrite (str, len, port);
  5326. freefunc (str, len + 1);
  5327. return !0;
  5328. }
  5329. /*** END nums->strs ***/
  5330. /*** STRINGS -> NUMBERS ***/
  5331. /* The following functions implement the conversion from strings to numbers.
  5332. * The implementation somehow follows the grammar for numbers as it is given
  5333. * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
  5334. * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
  5335. * points should be noted about the implementation:
  5336. *
  5337. * * Each function keeps a local index variable 'idx' that points at the
  5338. * current position within the parsed string. The global index is only
  5339. * updated if the function could parse the corresponding syntactic unit
  5340. * successfully.
  5341. *
  5342. * * Similarly, the functions keep track of indicators of inexactness ('#',
  5343. * '.' or exponents) using local variables ('hash_seen', 'x').
  5344. *
  5345. * * Sequences of digits are parsed into temporary variables holding fixnums.
  5346. * Only if these fixnums would overflow, the result variables are updated
  5347. * using the standard functions scm_add, scm_product, scm_divide etc. Then,
  5348. * the temporary variables holding the fixnums are cleared, and the process
  5349. * starts over again. If for example fixnums were able to store five decimal
  5350. * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
  5351. * and the result was computed as 12345 * 100000 + 67890. In other words,
  5352. * only every five digits two bignum operations were performed.
  5353. *
  5354. * Notes on the handling of exactness specifiers:
  5355. *
  5356. * When parsing non-real complex numbers, we apply exactness specifiers on
  5357. * per-component basis, as is done in PLT Scheme. For complex numbers
  5358. * written in rectangular form, exactness specifiers are applied to the
  5359. * real and imaginary parts before calling scm_make_rectangular. For
  5360. * complex numbers written in polar form, exactness specifiers are applied
  5361. * to the magnitude and angle before calling scm_make_polar.
  5362. *
  5363. * There are two kinds of exactness specifiers: forced and implicit. A
  5364. * forced exactness specifier is a "#e" or "#i" prefix at the beginning of
  5365. * the entire number, and applies to both components of a complex number.
  5366. * "#e" causes each component to be made exact, and "#i" causes each
  5367. * component to be made inexact. If no forced exactness specifier is
  5368. * present, then the exactness of each component is determined
  5369. * independently by the presence or absence of a decimal point or hash mark
  5370. * within that component. If a decimal point or hash mark is present, the
  5371. * component is made inexact, otherwise it is made exact.
  5372. *
  5373. * After the exactness specifiers have been applied to each component, they
  5374. * are passed to either scm_make_rectangular or scm_make_polar to produce
  5375. * the final result. Note that this will result in a real number if the
  5376. * imaginary part, magnitude, or angle is an exact 0.
  5377. *
  5378. * For example, (string->number "#i5.0+0i") does the equivalent of:
  5379. *
  5380. * (make-rectangular (exact->inexact 5) (exact->inexact 0))
  5381. */
  5382. enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
  5383. /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
  5384. /* Caller is responsible for checking that the return value is in range
  5385. for the given radix, which should be <= 36. */
  5386. static unsigned int
  5387. char_decimal_value (uint32_t c)
  5388. {
  5389. if (c >= (uint32_t) '0' && c <= (uint32_t) '9')
  5390. return c - (uint32_t) '0';
  5391. else
  5392. {
  5393. /* uc_decimal_value returns -1 on error. When cast to an unsigned int,
  5394. that's certainly above any valid decimal, so we take advantage of
  5395. that to elide some tests. */
  5396. unsigned int d = (unsigned int) uc_decimal_value (c);
  5397. /* If that failed, try extended hexadecimals, then. Only accept ascii
  5398. hexadecimals. */
  5399. if (d >= 10U)
  5400. {
  5401. c = uc_tolower (c);
  5402. if (c >= (uint32_t) 'a')
  5403. d = c - (uint32_t)'a' + 10U;
  5404. }
  5405. return d;
  5406. }
  5407. }
  5408. /* Parse the substring of MEM starting at *P_IDX for an unsigned integer
  5409. in base RADIX. Upon success, return the unsigned integer and update
  5410. *P_IDX and *P_EXACTNESS accordingly. Return #f on failure. */
  5411. static SCM
  5412. mem2uinteger (SCM mem, unsigned int *p_idx,
  5413. unsigned int radix, enum t_exactness *p_exactness)
  5414. {
  5415. unsigned int idx = *p_idx;
  5416. unsigned int hash_seen = 0;
  5417. scm_t_bits shift = 1;
  5418. scm_t_bits add = 0;
  5419. unsigned int digit_value;
  5420. SCM result;
  5421. char c;
  5422. size_t len = scm_i_string_length (mem);
  5423. if (idx == len)
  5424. return SCM_BOOL_F;
  5425. c = scm_i_string_ref (mem, idx);
  5426. digit_value = char_decimal_value (c);
  5427. if (digit_value >= radix)
  5428. return SCM_BOOL_F;
  5429. idx++;
  5430. result = SCM_I_MAKINUM (digit_value);
  5431. while (idx != len)
  5432. {
  5433. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5434. if (c == '#')
  5435. {
  5436. hash_seen = 1;
  5437. digit_value = 0;
  5438. }
  5439. else if (hash_seen)
  5440. break;
  5441. else
  5442. {
  5443. digit_value = char_decimal_value (c);
  5444. /* This check catches non-decimals in addition to out-of-range
  5445. decimals. */
  5446. if (digit_value >= radix)
  5447. break;
  5448. }
  5449. idx++;
  5450. if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
  5451. {
  5452. result = scm_product (result, SCM_I_MAKINUM (shift));
  5453. if (add > 0)
  5454. result = scm_sum (result, SCM_I_MAKINUM (add));
  5455. shift = radix;
  5456. add = digit_value;
  5457. }
  5458. else
  5459. {
  5460. shift = shift * radix;
  5461. add = add * radix + digit_value;
  5462. }
  5463. };
  5464. if (shift > 1)
  5465. result = scm_product (result, SCM_I_MAKINUM (shift));
  5466. if (add > 0)
  5467. result = scm_sum (result, SCM_I_MAKINUM (add));
  5468. *p_idx = idx;
  5469. if (hash_seen)
  5470. *p_exactness = INEXACT;
  5471. return result;
  5472. }
  5473. /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
  5474. * covers the parts of the rules that start at a potential point. The value
  5475. * of the digits up to the point have been parsed by the caller and are given
  5476. * in variable result. The content of *p_exactness indicates, whether a hash
  5477. * has already been seen in the digits before the point.
  5478. */
  5479. #define DIGIT2UINT(d) (uc_numeric_value(d).numerator)
  5480. static SCM
  5481. mem2decimal_from_point (SCM result, SCM mem,
  5482. unsigned int *p_idx, enum t_exactness *p_exactness)
  5483. {
  5484. unsigned int idx = *p_idx;
  5485. enum t_exactness x = *p_exactness;
  5486. size_t len = scm_i_string_length (mem);
  5487. if (idx == len)
  5488. return result;
  5489. if (scm_i_string_ref (mem, idx) == '.')
  5490. {
  5491. scm_t_bits shift = 1;
  5492. scm_t_bits add = 0;
  5493. unsigned int digit_value;
  5494. SCM big_shift = SCM_INUM1;
  5495. idx++;
  5496. while (idx != len)
  5497. {
  5498. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5499. if (uc_is_property_decimal_digit ((uint32_t) c))
  5500. {
  5501. if (x == INEXACT)
  5502. return SCM_BOOL_F;
  5503. else
  5504. digit_value = DIGIT2UINT (c);
  5505. }
  5506. else if (c == '#')
  5507. {
  5508. x = INEXACT;
  5509. digit_value = 0;
  5510. }
  5511. else
  5512. break;
  5513. idx++;
  5514. if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
  5515. {
  5516. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  5517. result = scm_product (result, SCM_I_MAKINUM (shift));
  5518. if (add > 0)
  5519. result = scm_sum (result, SCM_I_MAKINUM (add));
  5520. shift = 10;
  5521. add = digit_value;
  5522. }
  5523. else
  5524. {
  5525. shift = shift * 10;
  5526. add = add * 10 + digit_value;
  5527. }
  5528. };
  5529. if (add > 0)
  5530. {
  5531. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  5532. result = scm_product (result, SCM_I_MAKINUM (shift));
  5533. result = scm_sum (result, SCM_I_MAKINUM (add));
  5534. }
  5535. result = scm_divide (result, big_shift);
  5536. /* We've seen a decimal point, thus the value is implicitly inexact. */
  5537. x = INEXACT;
  5538. }
  5539. if (idx != len)
  5540. {
  5541. int sign = 1;
  5542. unsigned int start;
  5543. scm_t_wchar c;
  5544. int exponent;
  5545. SCM e;
  5546. /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
  5547. switch (scm_i_string_ref (mem, idx))
  5548. {
  5549. case 'd': case 'D':
  5550. case 'e': case 'E':
  5551. case 'f': case 'F':
  5552. case 'l': case 'L':
  5553. case 's': case 'S':
  5554. idx++;
  5555. if (idx == len)
  5556. return SCM_BOOL_F;
  5557. start = idx;
  5558. c = scm_i_string_ref (mem, idx);
  5559. if (c == '-')
  5560. {
  5561. idx++;
  5562. if (idx == len)
  5563. return SCM_BOOL_F;
  5564. sign = -1;
  5565. c = scm_i_string_ref (mem, idx);
  5566. }
  5567. else if (c == '+')
  5568. {
  5569. idx++;
  5570. if (idx == len)
  5571. return SCM_BOOL_F;
  5572. sign = 1;
  5573. c = scm_i_string_ref (mem, idx);
  5574. }
  5575. else
  5576. sign = 1;
  5577. if (!uc_is_property_decimal_digit ((uint32_t) c))
  5578. return SCM_BOOL_F;
  5579. idx++;
  5580. exponent = DIGIT2UINT (c);
  5581. while (idx != len)
  5582. {
  5583. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5584. if (uc_is_property_decimal_digit ((uint32_t) c))
  5585. {
  5586. idx++;
  5587. if (exponent <= SCM_MAXEXP)
  5588. exponent = exponent * 10 + DIGIT2UINT (c);
  5589. }
  5590. else
  5591. break;
  5592. }
  5593. if (exponent > ((sign == 1) ? SCM_MAXEXP : SCM_MAXEXP + DBL_DIG + 1))
  5594. {
  5595. size_t exp_len = idx - start;
  5596. SCM exp_string = scm_i_substring_copy (mem, start, start + exp_len);
  5597. SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
  5598. scm_out_of_range ("string->number", exp_num);
  5599. }
  5600. e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
  5601. if (sign == 1)
  5602. result = scm_product (result, e);
  5603. else
  5604. result = scm_divide (result, e);
  5605. /* We've seen an exponent, thus the value is implicitly inexact. */
  5606. x = INEXACT;
  5607. break;
  5608. default:
  5609. break;
  5610. }
  5611. }
  5612. *p_idx = idx;
  5613. if (x == INEXACT)
  5614. *p_exactness = x;
  5615. return result;
  5616. }
  5617. /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
  5618. static SCM
  5619. mem2ureal (SCM mem, unsigned int *p_idx,
  5620. unsigned int radix, enum t_exactness forced_x,
  5621. int allow_inf_or_nan)
  5622. {
  5623. unsigned int idx = *p_idx;
  5624. SCM result;
  5625. size_t len = scm_i_string_length (mem);
  5626. /* Start off believing that the number will be exact. This changes
  5627. to INEXACT if we see a decimal point or a hash. */
  5628. enum t_exactness implicit_x = EXACT;
  5629. if (idx == len)
  5630. return SCM_BOOL_F;
  5631. if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len)
  5632. switch (scm_i_string_ref (mem, idx))
  5633. {
  5634. case 'i': case 'I':
  5635. switch (scm_i_string_ref (mem, idx + 1))
  5636. {
  5637. case 'n': case 'N':
  5638. switch (scm_i_string_ref (mem, idx + 2))
  5639. {
  5640. case 'f': case 'F':
  5641. if (scm_i_string_ref (mem, idx + 3) == '.'
  5642. && scm_i_string_ref (mem, idx + 4) == '0')
  5643. {
  5644. *p_idx = idx+5;
  5645. return scm_inf ();
  5646. }
  5647. }
  5648. }
  5649. case 'n': case 'N':
  5650. switch (scm_i_string_ref (mem, idx + 1))
  5651. {
  5652. case 'a': case 'A':
  5653. switch (scm_i_string_ref (mem, idx + 2))
  5654. {
  5655. case 'n': case 'N':
  5656. if (scm_i_string_ref (mem, idx + 3) == '.')
  5657. {
  5658. /* Cobble up the fractional part. We might want to
  5659. set the NaN's mantissa from it. */
  5660. idx += 4;
  5661. if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x),
  5662. SCM_INUM0))
  5663. return SCM_BOOL_F;
  5664. *p_idx = idx;
  5665. return scm_nan ();
  5666. }
  5667. }
  5668. }
  5669. }
  5670. if (scm_i_string_ref (mem, idx) == '.')
  5671. {
  5672. if (radix != 10)
  5673. return SCM_BOOL_F;
  5674. else if (idx + 1 == len)
  5675. return SCM_BOOL_F;
  5676. else if (!uc_is_property_decimal_digit ((uint32_t) scm_i_string_ref (mem, idx+1)))
  5677. return SCM_BOOL_F;
  5678. else
  5679. result = mem2decimal_from_point (SCM_INUM0, mem,
  5680. p_idx, &implicit_x);
  5681. }
  5682. else
  5683. {
  5684. SCM uinteger;
  5685. uinteger = mem2uinteger (mem, &idx, radix, &implicit_x);
  5686. if (scm_is_false (uinteger))
  5687. return SCM_BOOL_F;
  5688. if (idx == len)
  5689. result = uinteger;
  5690. else if (scm_i_string_ref (mem, idx) == '/')
  5691. {
  5692. SCM divisor;
  5693. idx++;
  5694. if (idx == len)
  5695. return SCM_BOOL_F;
  5696. divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
  5697. if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
  5698. return SCM_BOOL_F;
  5699. /* both are int/big here, I assume */
  5700. result = scm_i_make_ratio (uinteger, divisor);
  5701. }
  5702. else if (radix == 10)
  5703. {
  5704. result = mem2decimal_from_point (uinteger, mem, &idx, &implicit_x);
  5705. if (scm_is_false (result))
  5706. return SCM_BOOL_F;
  5707. }
  5708. else
  5709. result = uinteger;
  5710. *p_idx = idx;
  5711. }
  5712. switch (forced_x)
  5713. {
  5714. case EXACT:
  5715. if (SCM_INEXACTP (result))
  5716. return scm_inexact_to_exact (result);
  5717. else
  5718. return result;
  5719. case INEXACT:
  5720. if (SCM_INEXACTP (result))
  5721. return result;
  5722. else
  5723. return scm_exact_to_inexact (result);
  5724. case NO_EXACTNESS:
  5725. if (implicit_x == INEXACT)
  5726. {
  5727. if (SCM_INEXACTP (result))
  5728. return result;
  5729. else
  5730. return scm_exact_to_inexact (result);
  5731. }
  5732. else
  5733. return result;
  5734. }
  5735. /* We should never get here */
  5736. assert (0);
  5737. }
  5738. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  5739. static SCM
  5740. mem2complex (SCM mem, unsigned int idx,
  5741. unsigned int radix, enum t_exactness forced_x)
  5742. {
  5743. scm_t_wchar c;
  5744. int sign = 0;
  5745. SCM ureal;
  5746. size_t len = scm_i_string_length (mem);
  5747. if (idx == len)
  5748. return SCM_BOOL_F;
  5749. c = scm_i_string_ref (mem, idx);
  5750. if (c == '+')
  5751. {
  5752. idx++;
  5753. sign = 1;
  5754. }
  5755. else if (c == '-')
  5756. {
  5757. idx++;
  5758. sign = -1;
  5759. }
  5760. if (idx == len)
  5761. return SCM_BOOL_F;
  5762. ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5763. if (scm_is_false (ureal))
  5764. {
  5765. /* input must be either +i or -i */
  5766. if (sign == 0)
  5767. return SCM_BOOL_F;
  5768. if (scm_i_string_ref (mem, idx) == 'i'
  5769. || scm_i_string_ref (mem, idx) == 'I')
  5770. {
  5771. idx++;
  5772. if (idx != len)
  5773. return SCM_BOOL_F;
  5774. return scm_make_rectangular (SCM_INUM0, SCM_I_MAKINUM (sign));
  5775. }
  5776. else
  5777. return SCM_BOOL_F;
  5778. }
  5779. else
  5780. {
  5781. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  5782. ureal = scm_difference (ureal, SCM_UNDEFINED);
  5783. if (idx == len)
  5784. return ureal;
  5785. c = scm_i_string_ref (mem, idx);
  5786. switch (c)
  5787. {
  5788. case 'i': case 'I':
  5789. /* either +<ureal>i or -<ureal>i */
  5790. idx++;
  5791. if (sign == 0)
  5792. return SCM_BOOL_F;
  5793. if (idx != len)
  5794. return SCM_BOOL_F;
  5795. return scm_make_rectangular (SCM_INUM0, ureal);
  5796. case '@':
  5797. /* polar input: <real>@<real>. */
  5798. idx++;
  5799. if (idx == len)
  5800. return SCM_BOOL_F;
  5801. else
  5802. {
  5803. int sign;
  5804. SCM angle;
  5805. SCM result;
  5806. c = scm_i_string_ref (mem, idx);
  5807. if (c == '+')
  5808. {
  5809. idx++;
  5810. if (idx == len)
  5811. return SCM_BOOL_F;
  5812. sign = 1;
  5813. }
  5814. else if (c == '-')
  5815. {
  5816. idx++;
  5817. if (idx == len)
  5818. return SCM_BOOL_F;
  5819. sign = -1;
  5820. }
  5821. else
  5822. sign = 0;
  5823. angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5824. if (scm_is_false (angle))
  5825. return SCM_BOOL_F;
  5826. if (idx != len)
  5827. return SCM_BOOL_F;
  5828. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  5829. angle = scm_difference (angle, SCM_UNDEFINED);
  5830. result = scm_make_polar (ureal, angle);
  5831. return result;
  5832. }
  5833. case '+':
  5834. case '-':
  5835. /* expecting input matching <real>[+-]<ureal>?i */
  5836. idx++;
  5837. if (idx == len)
  5838. return SCM_BOOL_F;
  5839. else
  5840. {
  5841. int sign = (c == '+') ? 1 : -1;
  5842. SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5843. if (scm_is_false (imag))
  5844. imag = SCM_I_MAKINUM (sign);
  5845. else if (sign == -1 && scm_is_false (scm_nan_p (imag)))
  5846. imag = scm_difference (imag, SCM_UNDEFINED);
  5847. if (idx == len)
  5848. return SCM_BOOL_F;
  5849. if (scm_i_string_ref (mem, idx) != 'i'
  5850. && scm_i_string_ref (mem, idx) != 'I')
  5851. return SCM_BOOL_F;
  5852. idx++;
  5853. if (idx != len)
  5854. return SCM_BOOL_F;
  5855. return scm_make_rectangular (ureal, imag);
  5856. }
  5857. default:
  5858. return SCM_BOOL_F;
  5859. }
  5860. }
  5861. }
  5862. /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
  5863. enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
  5864. SCM
  5865. scm_i_string_to_number (SCM mem, unsigned int default_radix)
  5866. {
  5867. unsigned int idx = 0;
  5868. unsigned int radix = NO_RADIX;
  5869. enum t_exactness forced_x = NO_EXACTNESS;
  5870. size_t len = scm_i_string_length (mem);
  5871. /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
  5872. while (idx + 2 < len && scm_i_string_ref (mem, idx) == '#')
  5873. {
  5874. switch (scm_i_string_ref (mem, idx + 1))
  5875. {
  5876. case 'b': case 'B':
  5877. if (radix != NO_RADIX)
  5878. return SCM_BOOL_F;
  5879. radix = DUAL;
  5880. break;
  5881. case 'd': case 'D':
  5882. if (radix != NO_RADIX)
  5883. return SCM_BOOL_F;
  5884. radix = DEC;
  5885. break;
  5886. case 'i': case 'I':
  5887. if (forced_x != NO_EXACTNESS)
  5888. return SCM_BOOL_F;
  5889. forced_x = INEXACT;
  5890. break;
  5891. case 'e': case 'E':
  5892. if (forced_x != NO_EXACTNESS)
  5893. return SCM_BOOL_F;
  5894. forced_x = EXACT;
  5895. break;
  5896. case 'o': case 'O':
  5897. if (radix != NO_RADIX)
  5898. return SCM_BOOL_F;
  5899. radix = OCT;
  5900. break;
  5901. case 'x': case 'X':
  5902. if (radix != NO_RADIX)
  5903. return SCM_BOOL_F;
  5904. radix = HEX;
  5905. break;
  5906. default:
  5907. return SCM_BOOL_F;
  5908. }
  5909. idx += 2;
  5910. }
  5911. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  5912. if (radix == NO_RADIX)
  5913. radix = default_radix;
  5914. return mem2complex (mem, idx, radix, forced_x);
  5915. }
  5916. SCM
  5917. scm_c_locale_stringn_to_number (const char* mem, size_t len,
  5918. unsigned int default_radix)
  5919. {
  5920. SCM str = scm_from_locale_stringn (mem, len);
  5921. return scm_i_string_to_number (str, default_radix);
  5922. }
  5923. SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
  5924. (SCM string, SCM radix),
  5925. "Return a number of the maximally precise representation\n"
  5926. "expressed by the given @var{string}. @var{radix} must be an\n"
  5927. "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
  5928. "is a default radix that may be overridden by an explicit radix\n"
  5929. "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
  5930. "supplied, then the default radix is 10. If string is not a\n"
  5931. "syntactically valid notation for a number, then\n"
  5932. "@code{string->number} returns @code{#f}.")
  5933. #define FUNC_NAME s_scm_string_to_number
  5934. {
  5935. SCM answer;
  5936. unsigned int base;
  5937. SCM_VALIDATE_STRING (1, string);
  5938. if (SCM_UNBNDP (radix))
  5939. base = 10;
  5940. else
  5941. base = scm_to_unsigned_integer (radix, 2, INT_MAX);
  5942. answer = scm_i_string_to_number (string, base);
  5943. scm_remember_upto_here_1 (string);
  5944. return answer;
  5945. }
  5946. #undef FUNC_NAME
  5947. /*** END strs->nums ***/
  5948. SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
  5949. (SCM x),
  5950. "Return @code{#t} if @var{x} is a number, @code{#f}\n"
  5951. "otherwise.")
  5952. #define FUNC_NAME s_scm_number_p
  5953. {
  5954. return scm_from_bool (SCM_NUMBERP (x));
  5955. }
  5956. #undef FUNC_NAME
  5957. SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
  5958. (SCM x),
  5959. "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
  5960. "otherwise. Note that the sets of real, rational and integer\n"
  5961. "values form subsets of the set of complex numbers, i. e. the\n"
  5962. "predicate will also be fulfilled if @var{x} is a real,\n"
  5963. "rational or integer number.")
  5964. #define FUNC_NAME s_scm_complex_p
  5965. {
  5966. /* all numbers are complex. */
  5967. return scm_number_p (x);
  5968. }
  5969. #undef FUNC_NAME
  5970. SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
  5971. (SCM x),
  5972. "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
  5973. "otherwise. Note that the set of integer values forms a subset of\n"
  5974. "the set of real numbers, i. e. the predicate will also be\n"
  5975. "fulfilled if @var{x} is an integer number.")
  5976. #define FUNC_NAME s_scm_real_p
  5977. {
  5978. return scm_from_bool
  5979. (SCM_I_INUMP (x) || SCM_REALP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x));
  5980. }
  5981. #undef FUNC_NAME
  5982. SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
  5983. (SCM x),
  5984. "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
  5985. "otherwise. Note that the set of integer values forms a subset of\n"
  5986. "the set of rational numbers, i. e. the predicate will also be\n"
  5987. "fulfilled if @var{x} is an integer number.")
  5988. #define FUNC_NAME s_scm_rational_p
  5989. {
  5990. if (SCM_I_INUMP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x))
  5991. return SCM_BOOL_T;
  5992. else if (SCM_REALP (x))
  5993. /* due to their limited precision, finite floating point numbers are
  5994. rational as well. (finite means neither infinity nor a NaN) */
  5995. return scm_from_bool (isfinite (SCM_REAL_VALUE (x)));
  5996. else
  5997. return SCM_BOOL_F;
  5998. }
  5999. #undef FUNC_NAME
  6000. SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
  6001. (SCM x),
  6002. "Return @code{#t} if @var{x} is an integer number,\n"
  6003. "else return @code{#f}.")
  6004. #define FUNC_NAME s_scm_integer_p
  6005. {
  6006. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  6007. return SCM_BOOL_T;
  6008. else if (SCM_REALP (x))
  6009. {
  6010. double val = SCM_REAL_VALUE (x);
  6011. return scm_from_bool (!isinf (val) && (val == floor (val)));
  6012. }
  6013. else
  6014. return SCM_BOOL_F;
  6015. }
  6016. #undef FUNC_NAME
  6017. SCM_DEFINE (scm_exact_integer_p, "exact-integer?", 1, 0, 0,
  6018. (SCM x),
  6019. "Return @code{#t} if @var{x} is an exact integer number,\n"
  6020. "else return @code{#f}.")
  6021. #define FUNC_NAME s_scm_exact_integer_p
  6022. {
  6023. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  6024. return SCM_BOOL_T;
  6025. else
  6026. return SCM_BOOL_F;
  6027. }
  6028. #undef FUNC_NAME
  6029. SCM scm_i_num_eq_p (SCM, SCM, SCM);
  6030. SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1,
  6031. (SCM x, SCM y, SCM rest),
  6032. "Return @code{#t} if all parameters are numerically equal.")
  6033. #define FUNC_NAME s_scm_i_num_eq_p
  6034. {
  6035. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6036. return SCM_BOOL_T;
  6037. while (!scm_is_null (rest))
  6038. {
  6039. if (scm_is_false (scm_num_eq_p (x, y)))
  6040. return SCM_BOOL_F;
  6041. x = y;
  6042. y = scm_car (rest);
  6043. rest = scm_cdr (rest);
  6044. }
  6045. return scm_num_eq_p (x, y);
  6046. }
  6047. #undef FUNC_NAME
  6048. SCM
  6049. scm_num_eq_p (SCM x, SCM y)
  6050. {
  6051. again:
  6052. if (SCM_I_INUMP (x))
  6053. {
  6054. scm_t_signed_bits xx = SCM_I_INUM (x);
  6055. if (SCM_I_INUMP (y))
  6056. {
  6057. scm_t_signed_bits yy = SCM_I_INUM (y);
  6058. return scm_from_bool (xx == yy);
  6059. }
  6060. else if (SCM_BIGP (y))
  6061. return SCM_BOOL_F;
  6062. else if (SCM_REALP (y))
  6063. {
  6064. /* On a 32-bit system an inum fits a double, we can cast the inum
  6065. to a double and compare.
  6066. But on a 64-bit system an inum is bigger than a double and
  6067. casting it to a double (call that dxx) will round.
  6068. Although dxx will not in general be equal to xx, dxx will
  6069. always be an integer and within a factor of 2 of xx, so if
  6070. dxx==yy, we know that yy is an integer and fits in
  6071. scm_t_signed_bits. So we cast yy to scm_t_signed_bits and
  6072. compare with plain xx.
  6073. An alternative (for any size system actually) would be to check
  6074. yy is an integer (with floor) and is in range of an inum
  6075. (compare against appropriate powers of 2) then test
  6076. xx==(scm_t_signed_bits)yy. It's just a matter of which
  6077. casts/comparisons might be fastest or easiest for the cpu. */
  6078. double yy = SCM_REAL_VALUE (y);
  6079. return scm_from_bool ((double) xx == yy
  6080. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6081. || xx == (scm_t_signed_bits) yy));
  6082. }
  6083. else if (SCM_COMPLEXP (y))
  6084. {
  6085. /* see comments with inum/real above */
  6086. double ry = SCM_COMPLEX_REAL (y);
  6087. return scm_from_bool ((double) xx == ry
  6088. && 0.0 == SCM_COMPLEX_IMAG (y)
  6089. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6090. || xx == (scm_t_signed_bits) ry));
  6091. }
  6092. else if (SCM_FRACTIONP (y))
  6093. return SCM_BOOL_F;
  6094. else
  6095. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6096. s_scm_i_num_eq_p);
  6097. }
  6098. else if (SCM_BIGP (x))
  6099. {
  6100. if (SCM_I_INUMP (y))
  6101. return SCM_BOOL_F;
  6102. else if (SCM_BIGP (y))
  6103. {
  6104. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6105. scm_remember_upto_here_2 (x, y);
  6106. return scm_from_bool (0 == cmp);
  6107. }
  6108. else if (SCM_REALP (y))
  6109. {
  6110. int cmp;
  6111. if (isnan (SCM_REAL_VALUE (y)))
  6112. return SCM_BOOL_F;
  6113. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  6114. scm_remember_upto_here_1 (x);
  6115. return scm_from_bool (0 == cmp);
  6116. }
  6117. else if (SCM_COMPLEXP (y))
  6118. {
  6119. int cmp;
  6120. if (0.0 != SCM_COMPLEX_IMAG (y))
  6121. return SCM_BOOL_F;
  6122. if (isnan (SCM_COMPLEX_REAL (y)))
  6123. return SCM_BOOL_F;
  6124. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
  6125. scm_remember_upto_here_1 (x);
  6126. return scm_from_bool (0 == cmp);
  6127. }
  6128. else if (SCM_FRACTIONP (y))
  6129. return SCM_BOOL_F;
  6130. else
  6131. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6132. s_scm_i_num_eq_p);
  6133. }
  6134. else if (SCM_REALP (x))
  6135. {
  6136. double xx = SCM_REAL_VALUE (x);
  6137. if (SCM_I_INUMP (y))
  6138. {
  6139. /* see comments with inum/real above */
  6140. scm_t_signed_bits yy = SCM_I_INUM (y);
  6141. return scm_from_bool (xx == (double) yy
  6142. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6143. || (scm_t_signed_bits) xx == yy));
  6144. }
  6145. else if (SCM_BIGP (y))
  6146. {
  6147. int cmp;
  6148. if (isnan (xx))
  6149. return SCM_BOOL_F;
  6150. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), xx);
  6151. scm_remember_upto_here_1 (y);
  6152. return scm_from_bool (0 == cmp);
  6153. }
  6154. else if (SCM_REALP (y))
  6155. return scm_from_bool (xx == SCM_REAL_VALUE (y));
  6156. else if (SCM_COMPLEXP (y))
  6157. return scm_from_bool ((xx == SCM_COMPLEX_REAL (y))
  6158. && (0.0 == SCM_COMPLEX_IMAG (y)));
  6159. else if (SCM_FRACTIONP (y))
  6160. {
  6161. if (isnan (xx) || isinf (xx))
  6162. return SCM_BOOL_F;
  6163. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6164. goto again;
  6165. }
  6166. else
  6167. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6168. s_scm_i_num_eq_p);
  6169. }
  6170. else if (SCM_COMPLEXP (x))
  6171. {
  6172. if (SCM_I_INUMP (y))
  6173. {
  6174. /* see comments with inum/real above */
  6175. double rx = SCM_COMPLEX_REAL (x);
  6176. scm_t_signed_bits yy = SCM_I_INUM (y);
  6177. return scm_from_bool (rx == (double) yy
  6178. && 0.0 == SCM_COMPLEX_IMAG (x)
  6179. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6180. || (scm_t_signed_bits) rx == yy));
  6181. }
  6182. else if (SCM_BIGP (y))
  6183. {
  6184. int cmp;
  6185. if (0.0 != SCM_COMPLEX_IMAG (x))
  6186. return SCM_BOOL_F;
  6187. if (isnan (SCM_COMPLEX_REAL (x)))
  6188. return SCM_BOOL_F;
  6189. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
  6190. scm_remember_upto_here_1 (y);
  6191. return scm_from_bool (0 == cmp);
  6192. }
  6193. else if (SCM_REALP (y))
  6194. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
  6195. && (SCM_COMPLEX_IMAG (x) == 0.0));
  6196. else if (SCM_COMPLEXP (y))
  6197. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
  6198. && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
  6199. else if (SCM_FRACTIONP (y))
  6200. {
  6201. double xx;
  6202. if (SCM_COMPLEX_IMAG (x) != 0.0)
  6203. return SCM_BOOL_F;
  6204. xx = SCM_COMPLEX_REAL (x);
  6205. if (isnan (xx) || isinf (xx))
  6206. return SCM_BOOL_F;
  6207. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6208. goto again;
  6209. }
  6210. else
  6211. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6212. s_scm_i_num_eq_p);
  6213. }
  6214. else if (SCM_FRACTIONP (x))
  6215. {
  6216. if (SCM_I_INUMP (y))
  6217. return SCM_BOOL_F;
  6218. else if (SCM_BIGP (y))
  6219. return SCM_BOOL_F;
  6220. else if (SCM_REALP (y))
  6221. {
  6222. double yy = SCM_REAL_VALUE (y);
  6223. if (isnan (yy) || isinf (yy))
  6224. return SCM_BOOL_F;
  6225. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6226. goto again;
  6227. }
  6228. else if (SCM_COMPLEXP (y))
  6229. {
  6230. double yy;
  6231. if (SCM_COMPLEX_IMAG (y) != 0.0)
  6232. return SCM_BOOL_F;
  6233. yy = SCM_COMPLEX_REAL (y);
  6234. if (isnan (yy) || isinf(yy))
  6235. return SCM_BOOL_F;
  6236. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6237. goto again;
  6238. }
  6239. else if (SCM_FRACTIONP (y))
  6240. return scm_i_fraction_equalp (x, y);
  6241. else
  6242. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6243. s_scm_i_num_eq_p);
  6244. }
  6245. else
  6246. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARG1,
  6247. s_scm_i_num_eq_p);
  6248. }
  6249. /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
  6250. done are good for inums, but for bignums an answer can almost always be
  6251. had by just examining a few high bits of the operands, as done by GMP in
  6252. mpq_cmp. flonum/frac compares likewise, but with the slight complication
  6253. of the float exponent to take into account. */
  6254. SCM_INTERNAL SCM scm_i_num_less_p (SCM, SCM, SCM);
  6255. SCM_PRIMITIVE_GENERIC (scm_i_num_less_p, "<", 0, 2, 1,
  6256. (SCM x, SCM y, SCM rest),
  6257. "Return @code{#t} if the list of parameters is monotonically\n"
  6258. "increasing.")
  6259. #define FUNC_NAME s_scm_i_num_less_p
  6260. {
  6261. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6262. return SCM_BOOL_T;
  6263. while (!scm_is_null (rest))
  6264. {
  6265. if (scm_is_false (scm_less_p (x, y)))
  6266. return SCM_BOOL_F;
  6267. x = y;
  6268. y = scm_car (rest);
  6269. rest = scm_cdr (rest);
  6270. }
  6271. return scm_less_p (x, y);
  6272. }
  6273. #undef FUNC_NAME
  6274. SCM
  6275. scm_less_p (SCM x, SCM y)
  6276. {
  6277. again:
  6278. if (SCM_I_INUMP (x))
  6279. {
  6280. scm_t_inum xx = SCM_I_INUM (x);
  6281. if (SCM_I_INUMP (y))
  6282. {
  6283. scm_t_inum yy = SCM_I_INUM (y);
  6284. return scm_from_bool (xx < yy);
  6285. }
  6286. else if (SCM_BIGP (y))
  6287. {
  6288. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6289. scm_remember_upto_here_1 (y);
  6290. return scm_from_bool (sgn > 0);
  6291. }
  6292. else if (SCM_REALP (y))
  6293. {
  6294. /* We can safely take the ceiling of y without changing the
  6295. result of x<y, given that x is an integer. */
  6296. double yy = ceil (SCM_REAL_VALUE (y));
  6297. /* In the following comparisons, it's important that the right
  6298. hand side always be a power of 2, so that it can be
  6299. losslessly converted to a double even on 64-bit
  6300. machines. */
  6301. if (yy >= (double) (SCM_MOST_POSITIVE_FIXNUM+1))
  6302. return SCM_BOOL_T;
  6303. else if (!(yy > (double) SCM_MOST_NEGATIVE_FIXNUM))
  6304. /* The condition above is carefully written to include the
  6305. case where yy==NaN. */
  6306. return SCM_BOOL_F;
  6307. else
  6308. /* yy is a finite integer that fits in an inum. */
  6309. return scm_from_bool (xx < (scm_t_inum) yy);
  6310. }
  6311. else if (SCM_FRACTIONP (y))
  6312. {
  6313. /* "x < a/b" becomes "x*b < a" */
  6314. int_frac:
  6315. x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
  6316. y = SCM_FRACTION_NUMERATOR (y);
  6317. goto again;
  6318. }
  6319. else
  6320. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6321. s_scm_i_num_less_p);
  6322. }
  6323. else if (SCM_BIGP (x))
  6324. {
  6325. if (SCM_I_INUMP (y))
  6326. {
  6327. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6328. scm_remember_upto_here_1 (x);
  6329. return scm_from_bool (sgn < 0);
  6330. }
  6331. else if (SCM_BIGP (y))
  6332. {
  6333. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6334. scm_remember_upto_here_2 (x, y);
  6335. return scm_from_bool (cmp < 0);
  6336. }
  6337. else if (SCM_REALP (y))
  6338. {
  6339. int cmp;
  6340. if (isnan (SCM_REAL_VALUE (y)))
  6341. return SCM_BOOL_F;
  6342. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  6343. scm_remember_upto_here_1 (x);
  6344. return scm_from_bool (cmp < 0);
  6345. }
  6346. else if (SCM_FRACTIONP (y))
  6347. goto int_frac;
  6348. else
  6349. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6350. s_scm_i_num_less_p);
  6351. }
  6352. else if (SCM_REALP (x))
  6353. {
  6354. if (SCM_I_INUMP (y))
  6355. {
  6356. /* We can safely take the floor of x without changing the
  6357. result of x<y, given that y is an integer. */
  6358. double xx = floor (SCM_REAL_VALUE (x));
  6359. /* In the following comparisons, it's important that the right
  6360. hand side always be a power of 2, so that it can be
  6361. losslessly converted to a double even on 64-bit
  6362. machines. */
  6363. if (xx < (double) SCM_MOST_NEGATIVE_FIXNUM)
  6364. return SCM_BOOL_T;
  6365. else if (!(xx < (double) (SCM_MOST_POSITIVE_FIXNUM+1)))
  6366. /* The condition above is carefully written to include the
  6367. case where xx==NaN. */
  6368. return SCM_BOOL_F;
  6369. else
  6370. /* xx is a finite integer that fits in an inum. */
  6371. return scm_from_bool ((scm_t_inum) xx < SCM_I_INUM (y));
  6372. }
  6373. else if (SCM_BIGP (y))
  6374. {
  6375. int cmp;
  6376. if (isnan (SCM_REAL_VALUE (x)))
  6377. return SCM_BOOL_F;
  6378. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  6379. scm_remember_upto_here_1 (y);
  6380. return scm_from_bool (cmp > 0);
  6381. }
  6382. else if (SCM_REALP (y))
  6383. return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
  6384. else if (SCM_FRACTIONP (y))
  6385. {
  6386. double xx = SCM_REAL_VALUE (x);
  6387. if (isnan (xx))
  6388. return SCM_BOOL_F;
  6389. if (isinf (xx))
  6390. return scm_from_bool (xx < 0.0);
  6391. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6392. goto again;
  6393. }
  6394. else
  6395. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6396. s_scm_i_num_less_p);
  6397. }
  6398. else if (SCM_FRACTIONP (x))
  6399. {
  6400. if (SCM_I_INUMP (y) || SCM_BIGP (y))
  6401. {
  6402. /* "a/b < y" becomes "a < y*b" */
  6403. y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
  6404. x = SCM_FRACTION_NUMERATOR (x);
  6405. goto again;
  6406. }
  6407. else if (SCM_REALP (y))
  6408. {
  6409. double yy = SCM_REAL_VALUE (y);
  6410. if (isnan (yy))
  6411. return SCM_BOOL_F;
  6412. if (isinf (yy))
  6413. return scm_from_bool (0.0 < yy);
  6414. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6415. goto again;
  6416. }
  6417. else if (SCM_FRACTIONP (y))
  6418. {
  6419. /* "a/b < c/d" becomes "a*d < c*b" */
  6420. SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
  6421. SCM_FRACTION_DENOMINATOR (y));
  6422. SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
  6423. SCM_FRACTION_DENOMINATOR (x));
  6424. x = new_x;
  6425. y = new_y;
  6426. goto again;
  6427. }
  6428. else
  6429. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6430. s_scm_i_num_less_p);
  6431. }
  6432. else
  6433. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARG1,
  6434. s_scm_i_num_less_p);
  6435. }
  6436. SCM scm_i_num_gr_p (SCM, SCM, SCM);
  6437. SCM_PRIMITIVE_GENERIC (scm_i_num_gr_p, ">", 0, 2, 1,
  6438. (SCM x, SCM y, SCM rest),
  6439. "Return @code{#t} if the list of parameters is monotonically\n"
  6440. "decreasing.")
  6441. #define FUNC_NAME s_scm_i_num_gr_p
  6442. {
  6443. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6444. return SCM_BOOL_T;
  6445. while (!scm_is_null (rest))
  6446. {
  6447. if (scm_is_false (scm_gr_p (x, y)))
  6448. return SCM_BOOL_F;
  6449. x = y;
  6450. y = scm_car (rest);
  6451. rest = scm_cdr (rest);
  6452. }
  6453. return scm_gr_p (x, y);
  6454. }
  6455. #undef FUNC_NAME
  6456. #define FUNC_NAME s_scm_i_num_gr_p
  6457. SCM
  6458. scm_gr_p (SCM x, SCM y)
  6459. {
  6460. if (!SCM_NUMBERP (x))
  6461. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG1, FUNC_NAME);
  6462. else if (!SCM_NUMBERP (y))
  6463. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG2, FUNC_NAME);
  6464. else
  6465. return scm_less_p (y, x);
  6466. }
  6467. #undef FUNC_NAME
  6468. SCM scm_i_num_leq_p (SCM, SCM, SCM);
  6469. SCM_PRIMITIVE_GENERIC (scm_i_num_leq_p, "<=", 0, 2, 1,
  6470. (SCM x, SCM y, SCM rest),
  6471. "Return @code{#t} if the list of parameters is monotonically\n"
  6472. "non-decreasing.")
  6473. #define FUNC_NAME s_scm_i_num_leq_p
  6474. {
  6475. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6476. return SCM_BOOL_T;
  6477. while (!scm_is_null (rest))
  6478. {
  6479. if (scm_is_false (scm_leq_p (x, y)))
  6480. return SCM_BOOL_F;
  6481. x = y;
  6482. y = scm_car (rest);
  6483. rest = scm_cdr (rest);
  6484. }
  6485. return scm_leq_p (x, y);
  6486. }
  6487. #undef FUNC_NAME
  6488. #define FUNC_NAME s_scm_i_num_leq_p
  6489. SCM
  6490. scm_leq_p (SCM x, SCM y)
  6491. {
  6492. if (!SCM_NUMBERP (x))
  6493. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG1, FUNC_NAME);
  6494. else if (!SCM_NUMBERP (y))
  6495. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG2, FUNC_NAME);
  6496. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  6497. return SCM_BOOL_F;
  6498. else
  6499. return scm_not (scm_less_p (y, x));
  6500. }
  6501. #undef FUNC_NAME
  6502. SCM scm_i_num_geq_p (SCM, SCM, SCM);
  6503. SCM_PRIMITIVE_GENERIC (scm_i_num_geq_p, ">=", 0, 2, 1,
  6504. (SCM x, SCM y, SCM rest),
  6505. "Return @code{#t} if the list of parameters is monotonically\n"
  6506. "non-increasing.")
  6507. #define FUNC_NAME s_scm_i_num_geq_p
  6508. {
  6509. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6510. return SCM_BOOL_T;
  6511. while (!scm_is_null (rest))
  6512. {
  6513. if (scm_is_false (scm_geq_p (x, y)))
  6514. return SCM_BOOL_F;
  6515. x = y;
  6516. y = scm_car (rest);
  6517. rest = scm_cdr (rest);
  6518. }
  6519. return scm_geq_p (x, y);
  6520. }
  6521. #undef FUNC_NAME
  6522. #define FUNC_NAME s_scm_i_num_geq_p
  6523. SCM
  6524. scm_geq_p (SCM x, SCM y)
  6525. {
  6526. if (!SCM_NUMBERP (x))
  6527. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG1, FUNC_NAME);
  6528. else if (!SCM_NUMBERP (y))
  6529. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG2, FUNC_NAME);
  6530. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  6531. return SCM_BOOL_F;
  6532. else
  6533. return scm_not (scm_less_p (x, y));
  6534. }
  6535. #undef FUNC_NAME
  6536. SCM_PRIMITIVE_GENERIC (scm_zero_p, "zero?", 1, 0, 0,
  6537. (SCM z),
  6538. "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
  6539. "zero.")
  6540. #define FUNC_NAME s_scm_zero_p
  6541. {
  6542. if (SCM_I_INUMP (z))
  6543. return scm_from_bool (scm_is_eq (z, SCM_INUM0));
  6544. else if (SCM_BIGP (z))
  6545. return SCM_BOOL_F;
  6546. else if (SCM_REALP (z))
  6547. return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
  6548. else if (SCM_COMPLEXP (z))
  6549. return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
  6550. && SCM_COMPLEX_IMAG (z) == 0.0);
  6551. else if (SCM_FRACTIONP (z))
  6552. return SCM_BOOL_F;
  6553. else
  6554. return scm_wta_dispatch_1 (g_scm_zero_p, z, SCM_ARG1, s_scm_zero_p);
  6555. }
  6556. #undef FUNC_NAME
  6557. SCM_PRIMITIVE_GENERIC (scm_positive_p, "positive?", 1, 0, 0,
  6558. (SCM x),
  6559. "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
  6560. "zero.")
  6561. #define FUNC_NAME s_scm_positive_p
  6562. {
  6563. if (SCM_I_INUMP (x))
  6564. return scm_from_bool (SCM_I_INUM (x) > 0);
  6565. else if (SCM_BIGP (x))
  6566. {
  6567. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6568. scm_remember_upto_here_1 (x);
  6569. return scm_from_bool (sgn > 0);
  6570. }
  6571. else if (SCM_REALP (x))
  6572. return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
  6573. else if (SCM_FRACTIONP (x))
  6574. return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
  6575. else
  6576. return scm_wta_dispatch_1 (g_scm_positive_p, x, SCM_ARG1, s_scm_positive_p);
  6577. }
  6578. #undef FUNC_NAME
  6579. SCM_PRIMITIVE_GENERIC (scm_negative_p, "negative?", 1, 0, 0,
  6580. (SCM x),
  6581. "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
  6582. "zero.")
  6583. #define FUNC_NAME s_scm_negative_p
  6584. {
  6585. if (SCM_I_INUMP (x))
  6586. return scm_from_bool (SCM_I_INUM (x) < 0);
  6587. else if (SCM_BIGP (x))
  6588. {
  6589. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6590. scm_remember_upto_here_1 (x);
  6591. return scm_from_bool (sgn < 0);
  6592. }
  6593. else if (SCM_REALP (x))
  6594. return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
  6595. else if (SCM_FRACTIONP (x))
  6596. return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
  6597. else
  6598. return scm_wta_dispatch_1 (g_scm_negative_p, x, SCM_ARG1, s_scm_negative_p);
  6599. }
  6600. #undef FUNC_NAME
  6601. /* scm_min and scm_max return an inexact when either argument is inexact, as
  6602. required by r5rs. On that basis, for exact/inexact combinations the
  6603. exact is converted to inexact to compare and possibly return. This is
  6604. unlike scm_less_p above which takes some trouble to preserve all bits in
  6605. its test, such trouble is not required for min and max. */
  6606. SCM_PRIMITIVE_GENERIC (scm_i_max, "max", 0, 2, 1,
  6607. (SCM x, SCM y, SCM rest),
  6608. "Return the maximum of all parameter values.")
  6609. #define FUNC_NAME s_scm_i_max
  6610. {
  6611. while (!scm_is_null (rest))
  6612. { x = scm_max (x, y);
  6613. y = scm_car (rest);
  6614. rest = scm_cdr (rest);
  6615. }
  6616. return scm_max (x, y);
  6617. }
  6618. #undef FUNC_NAME
  6619. #define s_max s_scm_i_max
  6620. #define g_max g_scm_i_max
  6621. SCM
  6622. scm_max (SCM x, SCM y)
  6623. {
  6624. if (SCM_UNBNDP (y))
  6625. {
  6626. if (SCM_UNBNDP (x))
  6627. return scm_wta_dispatch_0 (g_max, s_max);
  6628. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  6629. return x;
  6630. else
  6631. return scm_wta_dispatch_1 (g_max, x, SCM_ARG1, s_max);
  6632. }
  6633. if (SCM_I_INUMP (x))
  6634. {
  6635. scm_t_inum xx = SCM_I_INUM (x);
  6636. if (SCM_I_INUMP (y))
  6637. {
  6638. scm_t_inum yy = SCM_I_INUM (y);
  6639. return (xx < yy) ? y : x;
  6640. }
  6641. else if (SCM_BIGP (y))
  6642. {
  6643. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6644. scm_remember_upto_here_1 (y);
  6645. return (sgn < 0) ? x : y;
  6646. }
  6647. else if (SCM_REALP (y))
  6648. {
  6649. double xxd = xx;
  6650. double yyd = SCM_REAL_VALUE (y);
  6651. if (xxd > yyd)
  6652. return scm_i_from_double (xxd);
  6653. /* If y is a NaN, then "==" is false and we return the NaN */
  6654. else if (SCM_LIKELY (!(xxd == yyd)))
  6655. return y;
  6656. /* Handle signed zeroes properly */
  6657. else if (xx == 0)
  6658. return flo0;
  6659. else
  6660. return y;
  6661. }
  6662. else if (SCM_FRACTIONP (y))
  6663. {
  6664. use_less:
  6665. return (scm_is_false (scm_less_p (x, y)) ? x : y);
  6666. }
  6667. else
  6668. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6669. }
  6670. else if (SCM_BIGP (x))
  6671. {
  6672. if (SCM_I_INUMP (y))
  6673. {
  6674. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6675. scm_remember_upto_here_1 (x);
  6676. return (sgn < 0) ? y : x;
  6677. }
  6678. else if (SCM_BIGP (y))
  6679. {
  6680. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6681. scm_remember_upto_here_2 (x, y);
  6682. return (cmp > 0) ? x : y;
  6683. }
  6684. else if (SCM_REALP (y))
  6685. {
  6686. /* if y==NaN then xx>yy is false, so we return the NaN y */
  6687. double xx, yy;
  6688. big_real:
  6689. xx = scm_i_big2dbl (x);
  6690. yy = SCM_REAL_VALUE (y);
  6691. return (xx > yy ? scm_i_from_double (xx) : y);
  6692. }
  6693. else if (SCM_FRACTIONP (y))
  6694. {
  6695. goto use_less;
  6696. }
  6697. else
  6698. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6699. }
  6700. else if (SCM_REALP (x))
  6701. {
  6702. if (SCM_I_INUMP (y))
  6703. {
  6704. scm_t_inum yy = SCM_I_INUM (y);
  6705. double xxd = SCM_REAL_VALUE (x);
  6706. double yyd = yy;
  6707. if (yyd > xxd)
  6708. return scm_i_from_double (yyd);
  6709. /* If x is a NaN, then "==" is false and we return the NaN */
  6710. else if (SCM_LIKELY (!(xxd == yyd)))
  6711. return x;
  6712. /* Handle signed zeroes properly */
  6713. else if (yy == 0)
  6714. return flo0;
  6715. else
  6716. return x;
  6717. }
  6718. else if (SCM_BIGP (y))
  6719. {
  6720. SCM_SWAP (x, y);
  6721. goto big_real;
  6722. }
  6723. else if (SCM_REALP (y))
  6724. {
  6725. double xx = SCM_REAL_VALUE (x);
  6726. double yy = SCM_REAL_VALUE (y);
  6727. /* For purposes of max: nan > +inf.0 > everything else,
  6728. per the R6RS errata */
  6729. if (xx > yy)
  6730. return x;
  6731. else if (SCM_LIKELY (xx < yy))
  6732. return y;
  6733. /* If neither (xx > yy) nor (xx < yy), then
  6734. either they're equal or one is a NaN */
  6735. else if (SCM_UNLIKELY (xx != yy))
  6736. return (xx != xx) ? x : y; /* Return the NaN */
  6737. /* xx == yy, but handle signed zeroes properly */
  6738. else if (copysign (1.0, yy) < 0.0)
  6739. return x;
  6740. else
  6741. return y;
  6742. }
  6743. else if (SCM_FRACTIONP (y))
  6744. {
  6745. double yy = scm_i_fraction2double (y);
  6746. double xx = SCM_REAL_VALUE (x);
  6747. return (xx < yy) ? scm_i_from_double (yy) : x;
  6748. }
  6749. else
  6750. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6751. }
  6752. else if (SCM_FRACTIONP (x))
  6753. {
  6754. if (SCM_I_INUMP (y))
  6755. {
  6756. goto use_less;
  6757. }
  6758. else if (SCM_BIGP (y))
  6759. {
  6760. goto use_less;
  6761. }
  6762. else if (SCM_REALP (y))
  6763. {
  6764. double xx = scm_i_fraction2double (x);
  6765. /* if y==NaN then ">" is false, so we return the NaN y */
  6766. return (xx > SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y;
  6767. }
  6768. else if (SCM_FRACTIONP (y))
  6769. {
  6770. goto use_less;
  6771. }
  6772. else
  6773. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6774. }
  6775. else
  6776. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARG1, s_max);
  6777. }
  6778. SCM_PRIMITIVE_GENERIC (scm_i_min, "min", 0, 2, 1,
  6779. (SCM x, SCM y, SCM rest),
  6780. "Return the minimum of all parameter values.")
  6781. #define FUNC_NAME s_scm_i_min
  6782. {
  6783. while (!scm_is_null (rest))
  6784. { x = scm_min (x, y);
  6785. y = scm_car (rest);
  6786. rest = scm_cdr (rest);
  6787. }
  6788. return scm_min (x, y);
  6789. }
  6790. #undef FUNC_NAME
  6791. #define s_min s_scm_i_min
  6792. #define g_min g_scm_i_min
  6793. SCM
  6794. scm_min (SCM x, SCM y)
  6795. {
  6796. if (SCM_UNBNDP (y))
  6797. {
  6798. if (SCM_UNBNDP (x))
  6799. return scm_wta_dispatch_0 (g_min, s_min);
  6800. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  6801. return x;
  6802. else
  6803. return scm_wta_dispatch_1 (g_min, x, SCM_ARG1, s_min);
  6804. }
  6805. if (SCM_I_INUMP (x))
  6806. {
  6807. scm_t_inum xx = SCM_I_INUM (x);
  6808. if (SCM_I_INUMP (y))
  6809. {
  6810. scm_t_inum yy = SCM_I_INUM (y);
  6811. return (xx < yy) ? x : y;
  6812. }
  6813. else if (SCM_BIGP (y))
  6814. {
  6815. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6816. scm_remember_upto_here_1 (y);
  6817. return (sgn < 0) ? y : x;
  6818. }
  6819. else if (SCM_REALP (y))
  6820. {
  6821. double z = xx;
  6822. /* if y==NaN then "<" is false and we return NaN */
  6823. return (z < SCM_REAL_VALUE (y)) ? scm_i_from_double (z) : y;
  6824. }
  6825. else if (SCM_FRACTIONP (y))
  6826. {
  6827. use_less:
  6828. return (scm_is_false (scm_less_p (x, y)) ? y : x);
  6829. }
  6830. else
  6831. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6832. }
  6833. else if (SCM_BIGP (x))
  6834. {
  6835. if (SCM_I_INUMP (y))
  6836. {
  6837. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6838. scm_remember_upto_here_1 (x);
  6839. return (sgn < 0) ? x : y;
  6840. }
  6841. else if (SCM_BIGP (y))
  6842. {
  6843. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6844. scm_remember_upto_here_2 (x, y);
  6845. return (cmp > 0) ? y : x;
  6846. }
  6847. else if (SCM_REALP (y))
  6848. {
  6849. /* if y==NaN then xx<yy is false, so we return the NaN y */
  6850. double xx, yy;
  6851. big_real:
  6852. xx = scm_i_big2dbl (x);
  6853. yy = SCM_REAL_VALUE (y);
  6854. return (xx < yy ? scm_i_from_double (xx) : y);
  6855. }
  6856. else if (SCM_FRACTIONP (y))
  6857. {
  6858. goto use_less;
  6859. }
  6860. else
  6861. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6862. }
  6863. else if (SCM_REALP (x))
  6864. {
  6865. if (SCM_I_INUMP (y))
  6866. {
  6867. double z = SCM_I_INUM (y);
  6868. /* if x==NaN then "<" is false and we return NaN */
  6869. return (z < SCM_REAL_VALUE (x)) ? scm_i_from_double (z) : x;
  6870. }
  6871. else if (SCM_BIGP (y))
  6872. {
  6873. SCM_SWAP (x, y);
  6874. goto big_real;
  6875. }
  6876. else if (SCM_REALP (y))
  6877. {
  6878. double xx = SCM_REAL_VALUE (x);
  6879. double yy = SCM_REAL_VALUE (y);
  6880. /* For purposes of min: nan < -inf.0 < everything else,
  6881. per the R6RS errata */
  6882. if (xx < yy)
  6883. return x;
  6884. else if (SCM_LIKELY (xx > yy))
  6885. return y;
  6886. /* If neither (xx < yy) nor (xx > yy), then
  6887. either they're equal or one is a NaN */
  6888. else if (SCM_UNLIKELY (xx != yy))
  6889. return (xx != xx) ? x : y; /* Return the NaN */
  6890. /* xx == yy, but handle signed zeroes properly */
  6891. else if (copysign (1.0, xx) < 0.0)
  6892. return x;
  6893. else
  6894. return y;
  6895. }
  6896. else if (SCM_FRACTIONP (y))
  6897. {
  6898. double yy = scm_i_fraction2double (y);
  6899. double xx = SCM_REAL_VALUE (x);
  6900. return (yy < xx) ? scm_i_from_double (yy) : x;
  6901. }
  6902. else
  6903. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6904. }
  6905. else if (SCM_FRACTIONP (x))
  6906. {
  6907. if (SCM_I_INUMP (y))
  6908. {
  6909. goto use_less;
  6910. }
  6911. else if (SCM_BIGP (y))
  6912. {
  6913. goto use_less;
  6914. }
  6915. else if (SCM_REALP (y))
  6916. {
  6917. double xx = scm_i_fraction2double (x);
  6918. /* if y==NaN then "<" is false, so we return the NaN y */
  6919. return (xx < SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y;
  6920. }
  6921. else if (SCM_FRACTIONP (y))
  6922. {
  6923. goto use_less;
  6924. }
  6925. else
  6926. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6927. }
  6928. else
  6929. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARG1, s_min);
  6930. }
  6931. SCM_PRIMITIVE_GENERIC (scm_i_sum, "+", 0, 2, 1,
  6932. (SCM x, SCM y, SCM rest),
  6933. "Return the sum of all parameter values. Return 0 if called without\n"
  6934. "any parameters." )
  6935. #define FUNC_NAME s_scm_i_sum
  6936. {
  6937. while (!scm_is_null (rest))
  6938. { x = scm_sum (x, y);
  6939. y = scm_car (rest);
  6940. rest = scm_cdr (rest);
  6941. }
  6942. return scm_sum (x, y);
  6943. }
  6944. #undef FUNC_NAME
  6945. #define s_sum s_scm_i_sum
  6946. #define g_sum g_scm_i_sum
  6947. SCM
  6948. scm_sum (SCM x, SCM y)
  6949. {
  6950. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  6951. {
  6952. if (SCM_NUMBERP (x)) return x;
  6953. if (SCM_UNBNDP (x)) return SCM_INUM0;
  6954. return scm_wta_dispatch_1 (g_sum, x, SCM_ARG1, s_sum);
  6955. }
  6956. if (SCM_LIKELY (SCM_I_INUMP (x)))
  6957. {
  6958. if (SCM_LIKELY (SCM_I_INUMP (y)))
  6959. {
  6960. scm_t_inum xx = SCM_I_INUM (x);
  6961. scm_t_inum yy = SCM_I_INUM (y);
  6962. scm_t_inum z = xx + yy;
  6963. return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_inum2big (z);
  6964. }
  6965. else if (SCM_BIGP (y))
  6966. {
  6967. SCM_SWAP (x, y);
  6968. goto add_big_inum;
  6969. }
  6970. else if (SCM_REALP (y))
  6971. {
  6972. scm_t_inum xx = SCM_I_INUM (x);
  6973. return scm_i_from_double (xx + SCM_REAL_VALUE (y));
  6974. }
  6975. else if (SCM_COMPLEXP (y))
  6976. {
  6977. scm_t_inum xx = SCM_I_INUM (x);
  6978. return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
  6979. SCM_COMPLEX_IMAG (y));
  6980. }
  6981. else if (SCM_FRACTIONP (y))
  6982. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  6983. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  6984. SCM_FRACTION_DENOMINATOR (y));
  6985. else
  6986. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  6987. }
  6988. else if (SCM_BIGP (x))
  6989. {
  6990. if (SCM_I_INUMP (y))
  6991. {
  6992. scm_t_inum inum;
  6993. int bigsgn;
  6994. add_big_inum:
  6995. inum = SCM_I_INUM (y);
  6996. if (inum == 0)
  6997. return x;
  6998. bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6999. if (inum < 0)
  7000. {
  7001. SCM result = scm_i_mkbig ();
  7002. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
  7003. scm_remember_upto_here_1 (x);
  7004. /* we know the result will have to be a bignum */
  7005. if (bigsgn == -1)
  7006. return result;
  7007. return scm_i_normbig (result);
  7008. }
  7009. else
  7010. {
  7011. SCM result = scm_i_mkbig ();
  7012. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
  7013. scm_remember_upto_here_1 (x);
  7014. /* we know the result will have to be a bignum */
  7015. if (bigsgn == 1)
  7016. return result;
  7017. return scm_i_normbig (result);
  7018. }
  7019. }
  7020. else if (SCM_BIGP (y))
  7021. {
  7022. SCM result = scm_i_mkbig ();
  7023. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7024. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7025. mpz_add (SCM_I_BIG_MPZ (result),
  7026. SCM_I_BIG_MPZ (x),
  7027. SCM_I_BIG_MPZ (y));
  7028. scm_remember_upto_here_2 (x, y);
  7029. /* we know the result will have to be a bignum */
  7030. if (sgn_x == sgn_y)
  7031. return result;
  7032. return scm_i_normbig (result);
  7033. }
  7034. else if (SCM_REALP (y))
  7035. {
  7036. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
  7037. scm_remember_upto_here_1 (x);
  7038. return scm_i_from_double (result);
  7039. }
  7040. else if (SCM_COMPLEXP (y))
  7041. {
  7042. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  7043. + SCM_COMPLEX_REAL (y));
  7044. scm_remember_upto_here_1 (x);
  7045. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  7046. }
  7047. else if (SCM_FRACTIONP (y))
  7048. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  7049. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  7050. SCM_FRACTION_DENOMINATOR (y));
  7051. else
  7052. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7053. }
  7054. else if (SCM_REALP (x))
  7055. {
  7056. if (SCM_I_INUMP (y))
  7057. return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
  7058. else if (SCM_BIGP (y))
  7059. {
  7060. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
  7061. scm_remember_upto_here_1 (y);
  7062. return scm_i_from_double (result);
  7063. }
  7064. else if (SCM_REALP (y))
  7065. return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
  7066. else if (SCM_COMPLEXP (y))
  7067. return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
  7068. SCM_COMPLEX_IMAG (y));
  7069. else if (SCM_FRACTIONP (y))
  7070. return scm_i_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
  7071. else
  7072. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7073. }
  7074. else if (SCM_COMPLEXP (x))
  7075. {
  7076. if (SCM_I_INUMP (y))
  7077. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
  7078. SCM_COMPLEX_IMAG (x));
  7079. else if (SCM_BIGP (y))
  7080. {
  7081. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
  7082. + SCM_COMPLEX_REAL (x));
  7083. scm_remember_upto_here_1 (y);
  7084. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
  7085. }
  7086. else if (SCM_REALP (y))
  7087. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
  7088. SCM_COMPLEX_IMAG (x));
  7089. else if (SCM_COMPLEXP (y))
  7090. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
  7091. SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
  7092. else if (SCM_FRACTIONP (y))
  7093. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
  7094. SCM_COMPLEX_IMAG (x));
  7095. else
  7096. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7097. }
  7098. else if (SCM_FRACTIONP (x))
  7099. {
  7100. if (SCM_I_INUMP (y))
  7101. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  7102. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  7103. SCM_FRACTION_DENOMINATOR (x));
  7104. else if (SCM_BIGP (y))
  7105. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  7106. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  7107. SCM_FRACTION_DENOMINATOR (x));
  7108. else if (SCM_REALP (y))
  7109. return scm_i_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
  7110. else if (SCM_COMPLEXP (y))
  7111. return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
  7112. SCM_COMPLEX_IMAG (y));
  7113. else if (SCM_FRACTIONP (y))
  7114. /* a/b + c/d = (ad + bc) / bd */
  7115. return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  7116. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  7117. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  7118. else
  7119. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7120. }
  7121. else
  7122. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARG1, s_sum);
  7123. }
  7124. SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
  7125. (SCM x),
  7126. "Return @math{@var{x}+1}.")
  7127. #define FUNC_NAME s_scm_oneplus
  7128. {
  7129. return scm_sum (x, SCM_INUM1);
  7130. }
  7131. #undef FUNC_NAME
  7132. SCM_PRIMITIVE_GENERIC (scm_i_difference, "-", 0, 2, 1,
  7133. (SCM x, SCM y, SCM rest),
  7134. "If called with one argument @var{z1}, -@var{z1} returned. Otherwise\n"
  7135. "the sum of all but the first argument are subtracted from the first\n"
  7136. "argument.")
  7137. #define FUNC_NAME s_scm_i_difference
  7138. {
  7139. while (!scm_is_null (rest))
  7140. { x = scm_difference (x, y);
  7141. y = scm_car (rest);
  7142. rest = scm_cdr (rest);
  7143. }
  7144. return scm_difference (x, y);
  7145. }
  7146. #undef FUNC_NAME
  7147. #define s_difference s_scm_i_difference
  7148. #define g_difference g_scm_i_difference
  7149. SCM
  7150. scm_difference (SCM x, SCM y)
  7151. #define FUNC_NAME s_difference
  7152. {
  7153. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7154. {
  7155. if (SCM_UNBNDP (x))
  7156. return scm_wta_dispatch_0 (g_difference, s_difference);
  7157. else
  7158. if (SCM_I_INUMP (x))
  7159. {
  7160. scm_t_inum xx = -SCM_I_INUM (x);
  7161. if (SCM_FIXABLE (xx))
  7162. return SCM_I_MAKINUM (xx);
  7163. else
  7164. return scm_i_inum2big (xx);
  7165. }
  7166. else if (SCM_BIGP (x))
  7167. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  7168. bignum, but negating that gives a fixnum. */
  7169. return scm_i_normbig (scm_i_clonebig (x, 0));
  7170. else if (SCM_REALP (x))
  7171. return scm_i_from_double (-SCM_REAL_VALUE (x));
  7172. else if (SCM_COMPLEXP (x))
  7173. return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
  7174. -SCM_COMPLEX_IMAG (x));
  7175. else if (SCM_FRACTIONP (x))
  7176. return scm_i_make_ratio_already_reduced
  7177. (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  7178. SCM_FRACTION_DENOMINATOR (x));
  7179. else
  7180. return scm_wta_dispatch_1 (g_difference, x, SCM_ARG1, s_difference);
  7181. }
  7182. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7183. {
  7184. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7185. {
  7186. scm_t_inum xx = SCM_I_INUM (x);
  7187. scm_t_inum yy = SCM_I_INUM (y);
  7188. scm_t_inum z = xx - yy;
  7189. if (SCM_FIXABLE (z))
  7190. return SCM_I_MAKINUM (z);
  7191. else
  7192. return scm_i_inum2big (z);
  7193. }
  7194. else if (SCM_BIGP (y))
  7195. {
  7196. /* inum-x - big-y */
  7197. scm_t_inum xx = SCM_I_INUM (x);
  7198. if (xx == 0)
  7199. {
  7200. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  7201. bignum, but negating that gives a fixnum. */
  7202. return scm_i_normbig (scm_i_clonebig (y, 0));
  7203. }
  7204. else
  7205. {
  7206. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7207. SCM result = scm_i_mkbig ();
  7208. if (xx >= 0)
  7209. mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
  7210. else
  7211. {
  7212. /* x - y == -(y + -x) */
  7213. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
  7214. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  7215. }
  7216. scm_remember_upto_here_1 (y);
  7217. if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
  7218. /* we know the result will have to be a bignum */
  7219. return result;
  7220. else
  7221. return scm_i_normbig (result);
  7222. }
  7223. }
  7224. else if (SCM_REALP (y))
  7225. {
  7226. scm_t_inum xx = SCM_I_INUM (x);
  7227. /*
  7228. * We need to handle x == exact 0
  7229. * specially because R6RS states that:
  7230. * (- 0.0) ==> -0.0 and
  7231. * (- 0.0 0.0) ==> 0.0
  7232. * and the scheme compiler changes
  7233. * (- 0.0) into (- 0 0.0)
  7234. * So we need to treat (- 0 0.0) like (- 0.0).
  7235. * At the C level, (-x) is different than (0.0 - x).
  7236. * (0.0 - 0.0) ==> 0.0, but (- 0.0) ==> -0.0.
  7237. */
  7238. if (xx == 0)
  7239. return scm_i_from_double (- SCM_REAL_VALUE (y));
  7240. else
  7241. return scm_i_from_double (xx - SCM_REAL_VALUE (y));
  7242. }
  7243. else if (SCM_COMPLEXP (y))
  7244. {
  7245. scm_t_inum xx = SCM_I_INUM (x);
  7246. /* We need to handle x == exact 0 specially.
  7247. See the comment above (for SCM_REALP (y)) */
  7248. if (xx == 0)
  7249. return scm_c_make_rectangular (- SCM_COMPLEX_REAL (y),
  7250. - SCM_COMPLEX_IMAG (y));
  7251. else
  7252. return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
  7253. - SCM_COMPLEX_IMAG (y));
  7254. }
  7255. else if (SCM_FRACTIONP (y))
  7256. /* a - b/c = (ac - b) / c */
  7257. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7258. SCM_FRACTION_NUMERATOR (y)),
  7259. SCM_FRACTION_DENOMINATOR (y));
  7260. else
  7261. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7262. }
  7263. else if (SCM_BIGP (x))
  7264. {
  7265. if (SCM_I_INUMP (y))
  7266. {
  7267. /* big-x - inum-y */
  7268. scm_t_inum yy = SCM_I_INUM (y);
  7269. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7270. scm_remember_upto_here_1 (x);
  7271. if (sgn_x == 0)
  7272. return (SCM_FIXABLE (-yy) ?
  7273. SCM_I_MAKINUM (-yy) : scm_from_inum (-yy));
  7274. else
  7275. {
  7276. SCM result = scm_i_mkbig ();
  7277. if (yy >= 0)
  7278. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  7279. else
  7280. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
  7281. scm_remember_upto_here_1 (x);
  7282. if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
  7283. /* we know the result will have to be a bignum */
  7284. return result;
  7285. else
  7286. return scm_i_normbig (result);
  7287. }
  7288. }
  7289. else if (SCM_BIGP (y))
  7290. {
  7291. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7292. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7293. SCM result = scm_i_mkbig ();
  7294. mpz_sub (SCM_I_BIG_MPZ (result),
  7295. SCM_I_BIG_MPZ (x),
  7296. SCM_I_BIG_MPZ (y));
  7297. scm_remember_upto_here_2 (x, y);
  7298. /* we know the result will have to be a bignum */
  7299. if ((sgn_x == 1) && (sgn_y == -1))
  7300. return result;
  7301. if ((sgn_x == -1) && (sgn_y == 1))
  7302. return result;
  7303. return scm_i_normbig (result);
  7304. }
  7305. else if (SCM_REALP (y))
  7306. {
  7307. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
  7308. scm_remember_upto_here_1 (x);
  7309. return scm_i_from_double (result);
  7310. }
  7311. else if (SCM_COMPLEXP (y))
  7312. {
  7313. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  7314. - SCM_COMPLEX_REAL (y));
  7315. scm_remember_upto_here_1 (x);
  7316. return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
  7317. }
  7318. else if (SCM_FRACTIONP (y))
  7319. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7320. SCM_FRACTION_NUMERATOR (y)),
  7321. SCM_FRACTION_DENOMINATOR (y));
  7322. else
  7323. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7324. }
  7325. else if (SCM_REALP (x))
  7326. {
  7327. if (SCM_I_INUMP (y))
  7328. return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
  7329. else if (SCM_BIGP (y))
  7330. {
  7331. double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
  7332. scm_remember_upto_here_1 (x);
  7333. return scm_i_from_double (result);
  7334. }
  7335. else if (SCM_REALP (y))
  7336. return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
  7337. else if (SCM_COMPLEXP (y))
  7338. return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
  7339. -SCM_COMPLEX_IMAG (y));
  7340. else if (SCM_FRACTIONP (y))
  7341. return scm_i_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
  7342. else
  7343. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7344. }
  7345. else if (SCM_COMPLEXP (x))
  7346. {
  7347. if (SCM_I_INUMP (y))
  7348. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
  7349. SCM_COMPLEX_IMAG (x));
  7350. else if (SCM_BIGP (y))
  7351. {
  7352. double real_part = (SCM_COMPLEX_REAL (x)
  7353. - mpz_get_d (SCM_I_BIG_MPZ (y)));
  7354. scm_remember_upto_here_1 (x);
  7355. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  7356. }
  7357. else if (SCM_REALP (y))
  7358. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
  7359. SCM_COMPLEX_IMAG (x));
  7360. else if (SCM_COMPLEXP (y))
  7361. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
  7362. SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
  7363. else if (SCM_FRACTIONP (y))
  7364. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
  7365. SCM_COMPLEX_IMAG (x));
  7366. else
  7367. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7368. }
  7369. else if (SCM_FRACTIONP (x))
  7370. {
  7371. if (SCM_I_INUMP (y))
  7372. /* a/b - c = (a - cb) / b */
  7373. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  7374. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  7375. SCM_FRACTION_DENOMINATOR (x));
  7376. else if (SCM_BIGP (y))
  7377. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  7378. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  7379. SCM_FRACTION_DENOMINATOR (x));
  7380. else if (SCM_REALP (y))
  7381. return scm_i_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
  7382. else if (SCM_COMPLEXP (y))
  7383. return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
  7384. -SCM_COMPLEX_IMAG (y));
  7385. else if (SCM_FRACTIONP (y))
  7386. /* a/b - c/d = (ad - bc) / bd */
  7387. return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  7388. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  7389. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  7390. else
  7391. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7392. }
  7393. else
  7394. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARG1, s_difference);
  7395. }
  7396. #undef FUNC_NAME
  7397. SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
  7398. (SCM x),
  7399. "Return @math{@var{x}-1}.")
  7400. #define FUNC_NAME s_scm_oneminus
  7401. {
  7402. return scm_difference (x, SCM_INUM1);
  7403. }
  7404. #undef FUNC_NAME
  7405. SCM_PRIMITIVE_GENERIC (scm_i_product, "*", 0, 2, 1,
  7406. (SCM x, SCM y, SCM rest),
  7407. "Return the product of all arguments. If called without arguments,\n"
  7408. "1 is returned.")
  7409. #define FUNC_NAME s_scm_i_product
  7410. {
  7411. while (!scm_is_null (rest))
  7412. { x = scm_product (x, y);
  7413. y = scm_car (rest);
  7414. rest = scm_cdr (rest);
  7415. }
  7416. return scm_product (x, y);
  7417. }
  7418. #undef FUNC_NAME
  7419. #define s_product s_scm_i_product
  7420. #define g_product g_scm_i_product
  7421. SCM
  7422. scm_product (SCM x, SCM y)
  7423. {
  7424. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7425. {
  7426. if (SCM_UNBNDP (x))
  7427. return SCM_I_MAKINUM (1L);
  7428. else if (SCM_NUMBERP (x))
  7429. return x;
  7430. else
  7431. return scm_wta_dispatch_1 (g_product, x, SCM_ARG1, s_product);
  7432. }
  7433. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7434. {
  7435. scm_t_inum xx;
  7436. xinum:
  7437. xx = SCM_I_INUM (x);
  7438. switch (xx)
  7439. {
  7440. case 1:
  7441. /* exact1 is the universal multiplicative identity */
  7442. return y;
  7443. break;
  7444. case 0:
  7445. /* exact0 times a fixnum is exact0: optimize this case */
  7446. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7447. return SCM_INUM0;
  7448. /* if the other argument is inexact, the result is inexact,
  7449. and we must do the multiplication in order to handle
  7450. infinities and NaNs properly. */
  7451. else if (SCM_REALP (y))
  7452. return scm_i_from_double (0.0 * SCM_REAL_VALUE (y));
  7453. else if (SCM_COMPLEXP (y))
  7454. return scm_c_make_rectangular (0.0 * SCM_COMPLEX_REAL (y),
  7455. 0.0 * SCM_COMPLEX_IMAG (y));
  7456. /* we've already handled inexact numbers,
  7457. so y must be exact, and we return exact0 */
  7458. else if (SCM_NUMP (y))
  7459. return SCM_INUM0;
  7460. else
  7461. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7462. break;
  7463. }
  7464. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7465. {
  7466. scm_t_inum yy = SCM_I_INUM (y);
  7467. #if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
  7468. int64_t kk = xx * (int64_t) yy;
  7469. if (SCM_FIXABLE (kk))
  7470. return SCM_I_MAKINUM (kk);
  7471. #else
  7472. scm_t_inum axx = (xx > 0) ? xx : -xx;
  7473. scm_t_inum ayy = (yy > 0) ? yy : -yy;
  7474. if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
  7475. return SCM_I_MAKINUM (xx * yy);
  7476. #endif
  7477. else
  7478. {
  7479. SCM result = scm_i_inum2big (xx);
  7480. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
  7481. return scm_i_normbig (result);
  7482. }
  7483. }
  7484. else if (SCM_BIGP (y))
  7485. {
  7486. /* There is one bignum which, when multiplied by negative one,
  7487. becomes a non-zero fixnum: (1+ most-positive-fixum). Since
  7488. we know the type of X and Y are numbers, delegate this
  7489. special case to scm_difference. */
  7490. if (xx == -1)
  7491. return scm_difference (y, SCM_UNDEFINED);
  7492. else
  7493. {
  7494. SCM result = scm_i_mkbig ();
  7495. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
  7496. scm_remember_upto_here_1 (y);
  7497. return result;
  7498. }
  7499. }
  7500. else if (SCM_REALP (y))
  7501. return scm_i_from_double (xx * SCM_REAL_VALUE (y));
  7502. else if (SCM_COMPLEXP (y))
  7503. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  7504. xx * SCM_COMPLEX_IMAG (y));
  7505. else if (SCM_FRACTIONP (y))
  7506. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  7507. SCM_FRACTION_DENOMINATOR (y));
  7508. else
  7509. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7510. }
  7511. else if (SCM_BIGP (x))
  7512. {
  7513. if (SCM_I_INUMP (y))
  7514. {
  7515. SCM_SWAP (x, y);
  7516. goto xinum;
  7517. }
  7518. else if (SCM_BIGP (y))
  7519. {
  7520. SCM result = scm_i_mkbig ();
  7521. mpz_mul (SCM_I_BIG_MPZ (result),
  7522. SCM_I_BIG_MPZ (x),
  7523. SCM_I_BIG_MPZ (y));
  7524. scm_remember_upto_here_2 (x, y);
  7525. return result;
  7526. }
  7527. else if (SCM_REALP (y))
  7528. {
  7529. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
  7530. scm_remember_upto_here_1 (x);
  7531. return scm_i_from_double (result);
  7532. }
  7533. else if (SCM_COMPLEXP (y))
  7534. {
  7535. double z = mpz_get_d (SCM_I_BIG_MPZ (x));
  7536. scm_remember_upto_here_1 (x);
  7537. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
  7538. z * SCM_COMPLEX_IMAG (y));
  7539. }
  7540. else if (SCM_FRACTIONP (y))
  7541. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  7542. SCM_FRACTION_DENOMINATOR (y));
  7543. else
  7544. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7545. }
  7546. else if (SCM_REALP (x))
  7547. {
  7548. if (SCM_I_INUMP (y))
  7549. {
  7550. SCM_SWAP (x, y);
  7551. goto xinum;
  7552. }
  7553. else if (SCM_BIGP (y))
  7554. {
  7555. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
  7556. scm_remember_upto_here_1 (y);
  7557. return scm_i_from_double (result);
  7558. }
  7559. else if (SCM_REALP (y))
  7560. return scm_i_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
  7561. else if (SCM_COMPLEXP (y))
  7562. return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
  7563. SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
  7564. else if (SCM_FRACTIONP (y))
  7565. return scm_i_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
  7566. else
  7567. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7568. }
  7569. else if (SCM_COMPLEXP (x))
  7570. {
  7571. if (SCM_I_INUMP (y))
  7572. {
  7573. SCM_SWAP (x, y);
  7574. goto xinum;
  7575. }
  7576. else if (SCM_BIGP (y))
  7577. {
  7578. double z = mpz_get_d (SCM_I_BIG_MPZ (y));
  7579. scm_remember_upto_here_1 (y);
  7580. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
  7581. z * SCM_COMPLEX_IMAG (x));
  7582. }
  7583. else if (SCM_REALP (y))
  7584. return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
  7585. SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
  7586. else if (SCM_COMPLEXP (y))
  7587. {
  7588. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
  7589. - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
  7590. SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
  7591. + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
  7592. }
  7593. else if (SCM_FRACTIONP (y))
  7594. {
  7595. double yy = scm_i_fraction2double (y);
  7596. return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
  7597. yy * SCM_COMPLEX_IMAG (x));
  7598. }
  7599. else
  7600. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7601. }
  7602. else if (SCM_FRACTIONP (x))
  7603. {
  7604. if (SCM_I_INUMP (y))
  7605. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  7606. SCM_FRACTION_DENOMINATOR (x));
  7607. else if (SCM_BIGP (y))
  7608. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  7609. SCM_FRACTION_DENOMINATOR (x));
  7610. else if (SCM_REALP (y))
  7611. return scm_i_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
  7612. else if (SCM_COMPLEXP (y))
  7613. {
  7614. double xx = scm_i_fraction2double (x);
  7615. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  7616. xx * SCM_COMPLEX_IMAG (y));
  7617. }
  7618. else if (SCM_FRACTIONP (y))
  7619. /* a/b * c/d = ac / bd */
  7620. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  7621. SCM_FRACTION_NUMERATOR (y)),
  7622. scm_product (SCM_FRACTION_DENOMINATOR (x),
  7623. SCM_FRACTION_DENOMINATOR (y)));
  7624. else
  7625. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7626. }
  7627. else
  7628. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARG1, s_product);
  7629. }
  7630. #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
  7631. || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
  7632. #define ALLOW_DIVIDE_BY_ZERO
  7633. /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
  7634. #endif
  7635. /* The code below for complex division is adapted from the GNU
  7636. libstdc++, which adapted it from f2c's libF77, and is subject to
  7637. this copyright: */
  7638. /****************************************************************
  7639. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  7640. Permission to use, copy, modify, and distribute this software
  7641. and its documentation for any purpose and without fee is hereby
  7642. granted, provided that the above copyright notice appear in all
  7643. copies and that both that the copyright notice and this
  7644. permission notice and warranty disclaimer appear in supporting
  7645. documentation, and that the names of AT&T Bell Laboratories or
  7646. Bellcore or any of their entities not be used in advertising or
  7647. publicity pertaining to distribution of the software without
  7648. specific, written prior permission.
  7649. AT&T and Bellcore disclaim all warranties with regard to this
  7650. software, including all implied warranties of merchantability
  7651. and fitness. In no event shall AT&T or Bellcore be liable for
  7652. any special, indirect or consequential damages or any damages
  7653. whatsoever resulting from loss of use, data or profits, whether
  7654. in an action of contract, negligence or other tortious action,
  7655. arising out of or in connection with the use or performance of
  7656. this software.
  7657. ****************************************************************/
  7658. SCM_PRIMITIVE_GENERIC (scm_i_divide, "/", 0, 2, 1,
  7659. (SCM x, SCM y, SCM rest),
  7660. "Divide the first argument by the product of the remaining\n"
  7661. "arguments. If called with one argument @var{z1}, 1/@var{z1} is\n"
  7662. "returned.")
  7663. #define FUNC_NAME s_scm_i_divide
  7664. {
  7665. while (!scm_is_null (rest))
  7666. { x = scm_divide (x, y);
  7667. y = scm_car (rest);
  7668. rest = scm_cdr (rest);
  7669. }
  7670. return scm_divide (x, y);
  7671. }
  7672. #undef FUNC_NAME
  7673. #define s_divide s_scm_i_divide
  7674. #define g_divide g_scm_i_divide
  7675. SCM
  7676. scm_divide (SCM x, SCM y)
  7677. #define FUNC_NAME s_divide
  7678. {
  7679. double a;
  7680. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7681. {
  7682. if (SCM_UNBNDP (x))
  7683. return scm_wta_dispatch_0 (g_divide, s_divide);
  7684. else if (SCM_I_INUMP (x))
  7685. {
  7686. scm_t_inum xx = SCM_I_INUM (x);
  7687. if (xx == 1 || xx == -1)
  7688. return x;
  7689. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7690. else if (xx == 0)
  7691. scm_num_overflow (s_divide);
  7692. #endif
  7693. else
  7694. return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  7695. }
  7696. else if (SCM_BIGP (x))
  7697. return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  7698. else if (SCM_REALP (x))
  7699. {
  7700. double xx = SCM_REAL_VALUE (x);
  7701. #ifndef ALLOW_DIVIDE_BY_ZERO
  7702. if (xx == 0.0)
  7703. scm_num_overflow (s_divide);
  7704. else
  7705. #endif
  7706. return scm_i_from_double (1.0 / xx);
  7707. }
  7708. else if (SCM_COMPLEXP (x))
  7709. {
  7710. double r = SCM_COMPLEX_REAL (x);
  7711. double i = SCM_COMPLEX_IMAG (x);
  7712. if (fabs(r) <= fabs(i))
  7713. {
  7714. double t = r / i;
  7715. double d = i * (1.0 + t * t);
  7716. return scm_c_make_rectangular (t / d, -1.0 / d);
  7717. }
  7718. else
  7719. {
  7720. double t = i / r;
  7721. double d = r * (1.0 + t * t);
  7722. return scm_c_make_rectangular (1.0 / d, -t / d);
  7723. }
  7724. }
  7725. else if (SCM_FRACTIONP (x))
  7726. return scm_i_make_ratio_already_reduced (SCM_FRACTION_DENOMINATOR (x),
  7727. SCM_FRACTION_NUMERATOR (x));
  7728. else
  7729. return scm_wta_dispatch_1 (g_divide, x, SCM_ARG1, s_divide);
  7730. }
  7731. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7732. {
  7733. scm_t_inum xx = SCM_I_INUM (x);
  7734. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7735. {
  7736. scm_t_inum yy = SCM_I_INUM (y);
  7737. if (yy == 0)
  7738. {
  7739. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7740. scm_num_overflow (s_divide);
  7741. #else
  7742. return scm_i_from_double ((double) xx / (double) yy);
  7743. #endif
  7744. }
  7745. else if (xx % yy != 0)
  7746. return scm_i_make_ratio (x, y);
  7747. else
  7748. {
  7749. scm_t_inum z = xx / yy;
  7750. if (SCM_FIXABLE (z))
  7751. return SCM_I_MAKINUM (z);
  7752. else
  7753. return scm_i_inum2big (z);
  7754. }
  7755. }
  7756. else if (SCM_BIGP (y))
  7757. return scm_i_make_ratio (x, y);
  7758. else if (SCM_REALP (y))
  7759. {
  7760. double yy = SCM_REAL_VALUE (y);
  7761. #ifndef ALLOW_DIVIDE_BY_ZERO
  7762. if (yy == 0.0)
  7763. scm_num_overflow (s_divide);
  7764. else
  7765. #endif
  7766. /* FIXME: Precision may be lost here due to:
  7767. (1) The cast from 'scm_t_inum' to 'double'
  7768. (2) Double rounding */
  7769. return scm_i_from_double ((double) xx / yy);
  7770. }
  7771. else if (SCM_COMPLEXP (y))
  7772. {
  7773. a = xx;
  7774. complex_div: /* y _must_ be a complex number */
  7775. {
  7776. double r = SCM_COMPLEX_REAL (y);
  7777. double i = SCM_COMPLEX_IMAG (y);
  7778. if (fabs(r) <= fabs(i))
  7779. {
  7780. double t = r / i;
  7781. double d = i * (1.0 + t * t);
  7782. return scm_c_make_rectangular ((a * t) / d, -a / d);
  7783. }
  7784. else
  7785. {
  7786. double t = i / r;
  7787. double d = r * (1.0 + t * t);
  7788. return scm_c_make_rectangular (a / d, -(a * t) / d);
  7789. }
  7790. }
  7791. }
  7792. else if (SCM_FRACTIONP (y))
  7793. /* a / b/c = ac / b */
  7794. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7795. SCM_FRACTION_NUMERATOR (y));
  7796. else
  7797. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7798. }
  7799. else if (SCM_BIGP (x))
  7800. {
  7801. if (SCM_I_INUMP (y))
  7802. {
  7803. scm_t_inum yy = SCM_I_INUM (y);
  7804. if (yy == 0)
  7805. {
  7806. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7807. scm_num_overflow (s_divide);
  7808. #else
  7809. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  7810. scm_remember_upto_here_1 (x);
  7811. return (sgn == 0) ? scm_nan () : scm_inf ();
  7812. #endif
  7813. }
  7814. else if (yy == 1)
  7815. return x;
  7816. else
  7817. {
  7818. /* FIXME: HMM, what are the relative performance issues here?
  7819. We need to test. Is it faster on average to test
  7820. divisible_p, then perform whichever operation, or is it
  7821. faster to perform the integer div opportunistically and
  7822. switch to real if there's a remainder? For now we take the
  7823. middle ground: test, then if divisible, use the faster div
  7824. func. */
  7825. scm_t_inum abs_yy = yy < 0 ? -yy : yy;
  7826. int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
  7827. if (divisible_p)
  7828. {
  7829. SCM result = scm_i_mkbig ();
  7830. mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
  7831. scm_remember_upto_here_1 (x);
  7832. if (yy < 0)
  7833. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  7834. return scm_i_normbig (result);
  7835. }
  7836. else
  7837. return scm_i_make_ratio (x, y);
  7838. }
  7839. }
  7840. else if (SCM_BIGP (y))
  7841. {
  7842. int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
  7843. SCM_I_BIG_MPZ (y));
  7844. if (divisible_p)
  7845. {
  7846. SCM result = scm_i_mkbig ();
  7847. mpz_divexact (SCM_I_BIG_MPZ (result),
  7848. SCM_I_BIG_MPZ (x),
  7849. SCM_I_BIG_MPZ (y));
  7850. scm_remember_upto_here_2 (x, y);
  7851. return scm_i_normbig (result);
  7852. }
  7853. else
  7854. return scm_i_make_ratio (x, y);
  7855. }
  7856. else if (SCM_REALP (y))
  7857. {
  7858. double yy = SCM_REAL_VALUE (y);
  7859. #ifndef ALLOW_DIVIDE_BY_ZERO
  7860. if (yy == 0.0)
  7861. scm_num_overflow (s_divide);
  7862. else
  7863. #endif
  7864. /* FIXME: Precision may be lost here due to:
  7865. (1) scm_i_big2dbl (2) Double rounding */
  7866. return scm_i_from_double (scm_i_big2dbl (x) / yy);
  7867. }
  7868. else if (SCM_COMPLEXP (y))
  7869. {
  7870. a = scm_i_big2dbl (x);
  7871. goto complex_div;
  7872. }
  7873. else if (SCM_FRACTIONP (y))
  7874. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7875. SCM_FRACTION_NUMERATOR (y));
  7876. else
  7877. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7878. }
  7879. else if (SCM_REALP (x))
  7880. {
  7881. double rx = SCM_REAL_VALUE (x);
  7882. if (SCM_I_INUMP (y))
  7883. {
  7884. scm_t_inum yy = SCM_I_INUM (y);
  7885. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7886. if (yy == 0)
  7887. scm_num_overflow (s_divide);
  7888. else
  7889. #endif
  7890. /* FIXME: Precision may be lost here due to:
  7891. (1) The cast from 'scm_t_inum' to 'double'
  7892. (2) Double rounding */
  7893. return scm_i_from_double (rx / (double) yy);
  7894. }
  7895. else if (SCM_BIGP (y))
  7896. {
  7897. /* FIXME: Precision may be lost here due to:
  7898. (1) The conversion from bignum to double
  7899. (2) Double rounding */
  7900. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  7901. scm_remember_upto_here_1 (y);
  7902. return scm_i_from_double (rx / dby);
  7903. }
  7904. else if (SCM_REALP (y))
  7905. {
  7906. double yy = SCM_REAL_VALUE (y);
  7907. #ifndef ALLOW_DIVIDE_BY_ZERO
  7908. if (yy == 0.0)
  7909. scm_num_overflow (s_divide);
  7910. else
  7911. #endif
  7912. return scm_i_from_double (rx / yy);
  7913. }
  7914. else if (SCM_COMPLEXP (y))
  7915. {
  7916. a = rx;
  7917. goto complex_div;
  7918. }
  7919. else if (SCM_FRACTIONP (y))
  7920. return scm_i_from_double (rx / scm_i_fraction2double (y));
  7921. else
  7922. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7923. }
  7924. else if (SCM_COMPLEXP (x))
  7925. {
  7926. double rx = SCM_COMPLEX_REAL (x);
  7927. double ix = SCM_COMPLEX_IMAG (x);
  7928. if (SCM_I_INUMP (y))
  7929. {
  7930. scm_t_inum yy = SCM_I_INUM (y);
  7931. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7932. if (yy == 0)
  7933. scm_num_overflow (s_divide);
  7934. else
  7935. #endif
  7936. {
  7937. /* FIXME: Precision may be lost here due to:
  7938. (1) The conversion from 'scm_t_inum' to double
  7939. (2) Double rounding */
  7940. double d = yy;
  7941. return scm_c_make_rectangular (rx / d, ix / d);
  7942. }
  7943. }
  7944. else if (SCM_BIGP (y))
  7945. {
  7946. /* FIXME: Precision may be lost here due to:
  7947. (1) The conversion from bignum to double
  7948. (2) Double rounding */
  7949. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  7950. scm_remember_upto_here_1 (y);
  7951. return scm_c_make_rectangular (rx / dby, ix / dby);
  7952. }
  7953. else if (SCM_REALP (y))
  7954. {
  7955. double yy = SCM_REAL_VALUE (y);
  7956. #ifndef ALLOW_DIVIDE_BY_ZERO
  7957. if (yy == 0.0)
  7958. scm_num_overflow (s_divide);
  7959. else
  7960. #endif
  7961. return scm_c_make_rectangular (rx / yy, ix / yy);
  7962. }
  7963. else if (SCM_COMPLEXP (y))
  7964. {
  7965. double ry = SCM_COMPLEX_REAL (y);
  7966. double iy = SCM_COMPLEX_IMAG (y);
  7967. if (fabs(ry) <= fabs(iy))
  7968. {
  7969. double t = ry / iy;
  7970. double d = iy * (1.0 + t * t);
  7971. return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
  7972. }
  7973. else
  7974. {
  7975. double t = iy / ry;
  7976. double d = ry * (1.0 + t * t);
  7977. return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
  7978. }
  7979. }
  7980. else if (SCM_FRACTIONP (y))
  7981. {
  7982. /* FIXME: Precision may be lost here due to:
  7983. (1) The conversion from fraction to double
  7984. (2) Double rounding */
  7985. double yy = scm_i_fraction2double (y);
  7986. return scm_c_make_rectangular (rx / yy, ix / yy);
  7987. }
  7988. else
  7989. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7990. }
  7991. else if (SCM_FRACTIONP (x))
  7992. {
  7993. if (SCM_I_INUMP (y))
  7994. {
  7995. scm_t_inum yy = SCM_I_INUM (y);
  7996. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7997. if (yy == 0)
  7998. scm_num_overflow (s_divide);
  7999. else
  8000. #endif
  8001. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  8002. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  8003. }
  8004. else if (SCM_BIGP (y))
  8005. {
  8006. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  8007. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  8008. }
  8009. else if (SCM_REALP (y))
  8010. {
  8011. double yy = SCM_REAL_VALUE (y);
  8012. #ifndef ALLOW_DIVIDE_BY_ZERO
  8013. if (yy == 0.0)
  8014. scm_num_overflow (s_divide);
  8015. else
  8016. #endif
  8017. /* FIXME: Precision may be lost here due to:
  8018. (1) The conversion from fraction to double
  8019. (2) Double rounding */
  8020. return scm_i_from_double (scm_i_fraction2double (x) / yy);
  8021. }
  8022. else if (SCM_COMPLEXP (y))
  8023. {
  8024. /* FIXME: Precision may be lost here due to:
  8025. (1) The conversion from fraction to double
  8026. (2) Double rounding */
  8027. a = scm_i_fraction2double (x);
  8028. goto complex_div;
  8029. }
  8030. else if (SCM_FRACTIONP (y))
  8031. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  8032. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
  8033. else
  8034. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  8035. }
  8036. else
  8037. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARG1, s_divide);
  8038. }
  8039. #undef FUNC_NAME
  8040. double
  8041. scm_c_truncate (double x)
  8042. {
  8043. return trunc (x);
  8044. }
  8045. /* scm_c_round is done using floor(x+0.5) to round to nearest and with
  8046. half-way case (ie. when x is an integer plus 0.5) going upwards.
  8047. Then half-way cases are identified and adjusted down if the
  8048. round-upwards didn't give the desired even integer.
  8049. "plus_half == result" identifies a half-way case. If plus_half, which is
  8050. x + 0.5, is an integer then x must be an integer plus 0.5.
  8051. An odd "result" value is identified with result/2 != floor(result/2).
  8052. This is done with plus_half, since that value is ready for use sooner in
  8053. a pipelined cpu, and we're already requiring plus_half == result.
  8054. Note however that we need to be careful when x is big and already an
  8055. integer. In that case "x+0.5" may round to an adjacent integer, causing
  8056. us to return such a value, incorrectly. For instance if the hardware is
  8057. in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
  8058. (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
  8059. returned. Or if the hardware is in round-upwards mode, then other bigger
  8060. values like say x == 2^128 will see x+0.5 rounding up to the next higher
  8061. representable value, 2^128+2^76 (or whatever), again incorrect.
  8062. These bad roundings of x+0.5 are avoided by testing at the start whether
  8063. x is already an integer. If it is then clearly that's the desired result
  8064. already. And if it's not then the exponent must be small enough to allow
  8065. an 0.5 to be represented, and hence added without a bad rounding. */
  8066. double
  8067. scm_c_round (double x)
  8068. {
  8069. double plus_half, result;
  8070. if (x == floor (x))
  8071. return x;
  8072. plus_half = x + 0.5;
  8073. result = floor (plus_half);
  8074. /* Adjust so that the rounding is towards even. */
  8075. return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
  8076. ? result - 1
  8077. : result);
  8078. }
  8079. SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0,
  8080. (SCM x),
  8081. "Round the number @var{x} towards zero.")
  8082. #define FUNC_NAME s_scm_truncate_number
  8083. {
  8084. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8085. return x;
  8086. else if (SCM_REALP (x))
  8087. return scm_i_from_double (trunc (SCM_REAL_VALUE (x)));
  8088. else if (SCM_FRACTIONP (x))
  8089. return scm_truncate_quotient (SCM_FRACTION_NUMERATOR (x),
  8090. SCM_FRACTION_DENOMINATOR (x));
  8091. else
  8092. return scm_wta_dispatch_1 (g_scm_truncate_number, x, SCM_ARG1,
  8093. s_scm_truncate_number);
  8094. }
  8095. #undef FUNC_NAME
  8096. SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0,
  8097. (SCM x),
  8098. "Round the number @var{x} towards the nearest integer. "
  8099. "When it is exactly halfway between two integers, "
  8100. "round towards the even one.")
  8101. #define FUNC_NAME s_scm_round_number
  8102. {
  8103. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8104. return x;
  8105. else if (SCM_REALP (x))
  8106. return scm_i_from_double (scm_c_round (SCM_REAL_VALUE (x)));
  8107. else if (SCM_FRACTIONP (x))
  8108. return scm_round_quotient (SCM_FRACTION_NUMERATOR (x),
  8109. SCM_FRACTION_DENOMINATOR (x));
  8110. else
  8111. return scm_wta_dispatch_1 (g_scm_round_number, x, SCM_ARG1,
  8112. s_scm_round_number);
  8113. }
  8114. #undef FUNC_NAME
  8115. SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
  8116. (SCM x),
  8117. "Round the number @var{x} towards minus infinity.")
  8118. #define FUNC_NAME s_scm_floor
  8119. {
  8120. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8121. return x;
  8122. else if (SCM_REALP (x))
  8123. return scm_i_from_double (floor (SCM_REAL_VALUE (x)));
  8124. else if (SCM_FRACTIONP (x))
  8125. return scm_floor_quotient (SCM_FRACTION_NUMERATOR (x),
  8126. SCM_FRACTION_DENOMINATOR (x));
  8127. else
  8128. return scm_wta_dispatch_1 (g_scm_floor, x, 1, s_scm_floor);
  8129. }
  8130. #undef FUNC_NAME
  8131. SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
  8132. (SCM x),
  8133. "Round the number @var{x} towards infinity.")
  8134. #define FUNC_NAME s_scm_ceiling
  8135. {
  8136. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8137. return x;
  8138. else if (SCM_REALP (x))
  8139. return scm_i_from_double (ceil (SCM_REAL_VALUE (x)));
  8140. else if (SCM_FRACTIONP (x))
  8141. return scm_ceiling_quotient (SCM_FRACTION_NUMERATOR (x),
  8142. SCM_FRACTION_DENOMINATOR (x));
  8143. else
  8144. return scm_wta_dispatch_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
  8145. }
  8146. #undef FUNC_NAME
  8147. SCM_PRIMITIVE_GENERIC (scm_expt, "expt", 2, 0, 0,
  8148. (SCM x, SCM y),
  8149. "Return @var{x} raised to the power of @var{y}.")
  8150. #define FUNC_NAME s_scm_expt
  8151. {
  8152. if (scm_is_integer (y))
  8153. {
  8154. if (scm_is_true (scm_exact_p (y)))
  8155. return scm_integer_expt (x, y);
  8156. else
  8157. {
  8158. /* Here we handle the case where the exponent is an inexact
  8159. integer. We make the exponent exact in order to use
  8160. scm_integer_expt, and thus avoid the spurious imaginary
  8161. parts that may result from round-off errors in the general
  8162. e^(y log x) method below (for example when squaring a large
  8163. negative number). In this case, we must return an inexact
  8164. result for correctness. We also make the base inexact so
  8165. that scm_integer_expt will use fast inexact arithmetic
  8166. internally. Note that making the base inexact is not
  8167. sufficient to guarantee an inexact result, because
  8168. scm_integer_expt will return an exact 1 when the exponent
  8169. is 0, even if the base is inexact. */
  8170. return scm_exact_to_inexact
  8171. (scm_integer_expt (scm_exact_to_inexact (x),
  8172. scm_inexact_to_exact (y)));
  8173. }
  8174. }
  8175. else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0)
  8176. {
  8177. return scm_i_from_double (pow (scm_to_double (x), scm_to_double (y)));
  8178. }
  8179. else if (scm_is_complex (x) && scm_is_complex (y))
  8180. return scm_exp (scm_product (scm_log (x), y));
  8181. else if (scm_is_complex (x))
  8182. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG2, s_scm_expt);
  8183. else
  8184. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG1, s_scm_expt);
  8185. }
  8186. #undef FUNC_NAME
  8187. /* sin/cos/tan/asin/acos/atan
  8188. sinh/cosh/tanh/asinh/acosh/atanh
  8189. Derived from "Transcen.scm", Complex trancendental functions for SCM.
  8190. Written by Jerry D. Hedden, (C) FSF.
  8191. See the file `COPYING' for terms applying to this program. */
  8192. SCM_PRIMITIVE_GENERIC (scm_sin, "sin", 1, 0, 0,
  8193. (SCM z),
  8194. "Compute the sine of @var{z}.")
  8195. #define FUNC_NAME s_scm_sin
  8196. {
  8197. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8198. return z; /* sin(exact0) = exact0 */
  8199. else if (scm_is_real (z))
  8200. return scm_i_from_double (sin (scm_to_double (z)));
  8201. else if (SCM_COMPLEXP (z))
  8202. { double x, y;
  8203. x = SCM_COMPLEX_REAL (z);
  8204. y = SCM_COMPLEX_IMAG (z);
  8205. return scm_c_make_rectangular (sin (x) * cosh (y),
  8206. cos (x) * sinh (y));
  8207. }
  8208. else
  8209. return scm_wta_dispatch_1 (g_scm_sin, z, 1, s_scm_sin);
  8210. }
  8211. #undef FUNC_NAME
  8212. SCM_PRIMITIVE_GENERIC (scm_cos, "cos", 1, 0, 0,
  8213. (SCM z),
  8214. "Compute the cosine of @var{z}.")
  8215. #define FUNC_NAME s_scm_cos
  8216. {
  8217. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8218. return SCM_INUM1; /* cos(exact0) = exact1 */
  8219. else if (scm_is_real (z))
  8220. return scm_i_from_double (cos (scm_to_double (z)));
  8221. else if (SCM_COMPLEXP (z))
  8222. { double x, y;
  8223. x = SCM_COMPLEX_REAL (z);
  8224. y = SCM_COMPLEX_IMAG (z);
  8225. return scm_c_make_rectangular (cos (x) * cosh (y),
  8226. -sin (x) * sinh (y));
  8227. }
  8228. else
  8229. return scm_wta_dispatch_1 (g_scm_cos, z, 1, s_scm_cos);
  8230. }
  8231. #undef FUNC_NAME
  8232. SCM_PRIMITIVE_GENERIC (scm_tan, "tan", 1, 0, 0,
  8233. (SCM z),
  8234. "Compute the tangent of @var{z}.")
  8235. #define FUNC_NAME s_scm_tan
  8236. {
  8237. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8238. return z; /* tan(exact0) = exact0 */
  8239. else if (scm_is_real (z))
  8240. return scm_i_from_double (tan (scm_to_double (z)));
  8241. else if (SCM_COMPLEXP (z))
  8242. { double x, y, w;
  8243. x = 2.0 * SCM_COMPLEX_REAL (z);
  8244. y = 2.0 * SCM_COMPLEX_IMAG (z);
  8245. w = cos (x) + cosh (y);
  8246. #ifndef ALLOW_DIVIDE_BY_ZERO
  8247. if (w == 0.0)
  8248. scm_num_overflow (s_scm_tan);
  8249. #endif
  8250. return scm_c_make_rectangular (sin (x) / w, sinh (y) / w);
  8251. }
  8252. else
  8253. return scm_wta_dispatch_1 (g_scm_tan, z, 1, s_scm_tan);
  8254. }
  8255. #undef FUNC_NAME
  8256. SCM_PRIMITIVE_GENERIC (scm_sinh, "sinh", 1, 0, 0,
  8257. (SCM z),
  8258. "Compute the hyperbolic sine of @var{z}.")
  8259. #define FUNC_NAME s_scm_sinh
  8260. {
  8261. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8262. return z; /* sinh(exact0) = exact0 */
  8263. else if (scm_is_real (z))
  8264. return scm_i_from_double (sinh (scm_to_double (z)));
  8265. else if (SCM_COMPLEXP (z))
  8266. { double x, y;
  8267. x = SCM_COMPLEX_REAL (z);
  8268. y = SCM_COMPLEX_IMAG (z);
  8269. return scm_c_make_rectangular (sinh (x) * cos (y),
  8270. cosh (x) * sin (y));
  8271. }
  8272. else
  8273. return scm_wta_dispatch_1 (g_scm_sinh, z, 1, s_scm_sinh);
  8274. }
  8275. #undef FUNC_NAME
  8276. SCM_PRIMITIVE_GENERIC (scm_cosh, "cosh", 1, 0, 0,
  8277. (SCM z),
  8278. "Compute the hyperbolic cosine of @var{z}.")
  8279. #define FUNC_NAME s_scm_cosh
  8280. {
  8281. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8282. return SCM_INUM1; /* cosh(exact0) = exact1 */
  8283. else if (scm_is_real (z))
  8284. return scm_i_from_double (cosh (scm_to_double (z)));
  8285. else if (SCM_COMPLEXP (z))
  8286. { double x, y;
  8287. x = SCM_COMPLEX_REAL (z);
  8288. y = SCM_COMPLEX_IMAG (z);
  8289. return scm_c_make_rectangular (cosh (x) * cos (y),
  8290. sinh (x) * sin (y));
  8291. }
  8292. else
  8293. return scm_wta_dispatch_1 (g_scm_cosh, z, 1, s_scm_cosh);
  8294. }
  8295. #undef FUNC_NAME
  8296. SCM_PRIMITIVE_GENERIC (scm_tanh, "tanh", 1, 0, 0,
  8297. (SCM z),
  8298. "Compute the hyperbolic tangent of @var{z}.")
  8299. #define FUNC_NAME s_scm_tanh
  8300. {
  8301. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8302. return z; /* tanh(exact0) = exact0 */
  8303. else if (scm_is_real (z))
  8304. return scm_i_from_double (tanh (scm_to_double (z)));
  8305. else if (SCM_COMPLEXP (z))
  8306. { double x, y, w;
  8307. x = 2.0 * SCM_COMPLEX_REAL (z);
  8308. y = 2.0 * SCM_COMPLEX_IMAG (z);
  8309. w = cosh (x) + cos (y);
  8310. #ifndef ALLOW_DIVIDE_BY_ZERO
  8311. if (w == 0.0)
  8312. scm_num_overflow (s_scm_tanh);
  8313. #endif
  8314. return scm_c_make_rectangular (sinh (x) / w, sin (y) / w);
  8315. }
  8316. else
  8317. return scm_wta_dispatch_1 (g_scm_tanh, z, 1, s_scm_tanh);
  8318. }
  8319. #undef FUNC_NAME
  8320. SCM_PRIMITIVE_GENERIC (scm_asin, "asin", 1, 0, 0,
  8321. (SCM z),
  8322. "Compute the arc sine of @var{z}.")
  8323. #define FUNC_NAME s_scm_asin
  8324. {
  8325. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8326. return z; /* asin(exact0) = exact0 */
  8327. else if (scm_is_real (z))
  8328. {
  8329. double w = scm_to_double (z);
  8330. if (w >= -1.0 && w <= 1.0)
  8331. return scm_i_from_double (asin (w));
  8332. else
  8333. return scm_product (scm_c_make_rectangular (0, -1),
  8334. scm_sys_asinh (scm_c_make_rectangular (0, w)));
  8335. }
  8336. else if (SCM_COMPLEXP (z))
  8337. { double x, y;
  8338. x = SCM_COMPLEX_REAL (z);
  8339. y = SCM_COMPLEX_IMAG (z);
  8340. return scm_product (scm_c_make_rectangular (0, -1),
  8341. scm_sys_asinh (scm_c_make_rectangular (-y, x)));
  8342. }
  8343. else
  8344. return scm_wta_dispatch_1 (g_scm_asin, z, 1, s_scm_asin);
  8345. }
  8346. #undef FUNC_NAME
  8347. SCM_PRIMITIVE_GENERIC (scm_acos, "acos", 1, 0, 0,
  8348. (SCM z),
  8349. "Compute the arc cosine of @var{z}.")
  8350. #define FUNC_NAME s_scm_acos
  8351. {
  8352. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  8353. return SCM_INUM0; /* acos(exact1) = exact0 */
  8354. else if (scm_is_real (z))
  8355. {
  8356. double w = scm_to_double (z);
  8357. if (w >= -1.0 && w <= 1.0)
  8358. return scm_i_from_double (acos (w));
  8359. else
  8360. return scm_sum (scm_i_from_double (acos (0.0)),
  8361. scm_product (scm_c_make_rectangular (0, 1),
  8362. scm_sys_asinh (scm_c_make_rectangular (0, w))));
  8363. }
  8364. else if (SCM_COMPLEXP (z))
  8365. { double x, y;
  8366. x = SCM_COMPLEX_REAL (z);
  8367. y = SCM_COMPLEX_IMAG (z);
  8368. return scm_sum (scm_i_from_double (acos (0.0)),
  8369. scm_product (scm_c_make_rectangular (0, 1),
  8370. scm_sys_asinh (scm_c_make_rectangular (-y, x))));
  8371. }
  8372. else
  8373. return scm_wta_dispatch_1 (g_scm_acos, z, 1, s_scm_acos);
  8374. }
  8375. #undef FUNC_NAME
  8376. SCM_PRIMITIVE_GENERIC (scm_atan, "atan", 1, 1, 0,
  8377. (SCM z, SCM y),
  8378. "With one argument, compute the arc tangent of @var{z}.\n"
  8379. "If @var{y} is present, compute the arc tangent of @var{z}/@var{y},\n"
  8380. "using the sign of @var{z} and @var{y} to determine the quadrant.")
  8381. #define FUNC_NAME s_scm_atan
  8382. {
  8383. if (SCM_UNBNDP (y))
  8384. {
  8385. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8386. return z; /* atan(exact0) = exact0 */
  8387. else if (scm_is_real (z))
  8388. return scm_i_from_double (atan (scm_to_double (z)));
  8389. else if (SCM_COMPLEXP (z))
  8390. {
  8391. double v, w;
  8392. v = SCM_COMPLEX_REAL (z);
  8393. w = SCM_COMPLEX_IMAG (z);
  8394. return scm_divide (scm_log (scm_divide (scm_c_make_rectangular (-v, 1.0 - w),
  8395. scm_c_make_rectangular ( v, 1.0 + w))),
  8396. scm_c_make_rectangular (0, 2));
  8397. }
  8398. else
  8399. return scm_wta_dispatch_1 (g_scm_atan, z, SCM_ARG1, s_scm_atan);
  8400. }
  8401. else if (scm_is_real (z))
  8402. {
  8403. if (scm_is_real (y))
  8404. return scm_i_from_double (atan2 (scm_to_double (z), scm_to_double (y)));
  8405. else
  8406. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG2, s_scm_atan);
  8407. }
  8408. else
  8409. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG1, s_scm_atan);
  8410. }
  8411. #undef FUNC_NAME
  8412. SCM_PRIMITIVE_GENERIC (scm_sys_asinh, "asinh", 1, 0, 0,
  8413. (SCM z),
  8414. "Compute the inverse hyperbolic sine of @var{z}.")
  8415. #define FUNC_NAME s_scm_sys_asinh
  8416. {
  8417. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8418. return z; /* asinh(exact0) = exact0 */
  8419. else if (scm_is_real (z))
  8420. return scm_i_from_double (asinh (scm_to_double (z)));
  8421. else if (scm_is_number (z))
  8422. return scm_log (scm_sum (z,
  8423. scm_sqrt (scm_sum (scm_product (z, z),
  8424. SCM_INUM1))));
  8425. else
  8426. return scm_wta_dispatch_1 (g_scm_sys_asinh, z, 1, s_scm_sys_asinh);
  8427. }
  8428. #undef FUNC_NAME
  8429. SCM_PRIMITIVE_GENERIC (scm_sys_acosh, "acosh", 1, 0, 0,
  8430. (SCM z),
  8431. "Compute the inverse hyperbolic cosine of @var{z}.")
  8432. #define FUNC_NAME s_scm_sys_acosh
  8433. {
  8434. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  8435. return SCM_INUM0; /* acosh(exact1) = exact0 */
  8436. else if (scm_is_real (z) && scm_to_double (z) >= 1.0)
  8437. return scm_i_from_double (acosh (scm_to_double (z)));
  8438. else if (scm_is_number (z))
  8439. return scm_log (scm_sum (z,
  8440. scm_sqrt (scm_difference (scm_product (z, z),
  8441. SCM_INUM1))));
  8442. else
  8443. return scm_wta_dispatch_1 (g_scm_sys_acosh, z, 1, s_scm_sys_acosh);
  8444. }
  8445. #undef FUNC_NAME
  8446. SCM_PRIMITIVE_GENERIC (scm_sys_atanh, "atanh", 1, 0, 0,
  8447. (SCM z),
  8448. "Compute the inverse hyperbolic tangent of @var{z}.")
  8449. #define FUNC_NAME s_scm_sys_atanh
  8450. {
  8451. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8452. return z; /* atanh(exact0) = exact0 */
  8453. else if (scm_is_real (z) && scm_to_double (z) >= -1.0 && scm_to_double (z) <= 1.0)
  8454. return scm_i_from_double (atanh (scm_to_double (z)));
  8455. else if (scm_is_number (z))
  8456. return scm_divide (scm_log (scm_divide (scm_sum (SCM_INUM1, z),
  8457. scm_difference (SCM_INUM1, z))),
  8458. SCM_I_MAKINUM (2));
  8459. else
  8460. return scm_wta_dispatch_1 (g_scm_sys_atanh, z, 1, s_scm_sys_atanh);
  8461. }
  8462. #undef FUNC_NAME
  8463. SCM
  8464. scm_c_make_rectangular (double re, double im)
  8465. {
  8466. SCM z;
  8467. z = SCM_PACK_POINTER (scm_gc_malloc_pointerless (sizeof (scm_t_complex),
  8468. "complex"));
  8469. SCM_SET_CELL_TYPE (z, scm_tc16_complex);
  8470. SCM_COMPLEX_REAL (z) = re;
  8471. SCM_COMPLEX_IMAG (z) = im;
  8472. return z;
  8473. }
  8474. SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
  8475. (SCM real_part, SCM imaginary_part),
  8476. "Return a complex number constructed of the given @var{real_part} "
  8477. "and @var{imaginary_part} parts.")
  8478. #define FUNC_NAME s_scm_make_rectangular
  8479. {
  8480. SCM_ASSERT_TYPE (scm_is_real (real_part), real_part,
  8481. SCM_ARG1, FUNC_NAME, "real");
  8482. SCM_ASSERT_TYPE (scm_is_real (imaginary_part), imaginary_part,
  8483. SCM_ARG2, FUNC_NAME, "real");
  8484. /* Return a real if and only if the imaginary_part is an _exact_ 0 */
  8485. if (scm_is_eq (imaginary_part, SCM_INUM0))
  8486. return real_part;
  8487. else
  8488. return scm_c_make_rectangular (scm_to_double (real_part),
  8489. scm_to_double (imaginary_part));
  8490. }
  8491. #undef FUNC_NAME
  8492. SCM
  8493. scm_c_make_polar (double mag, double ang)
  8494. {
  8495. double s, c;
  8496. /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
  8497. use it on Glibc-based systems that have it (it's a GNU extension). See
  8498. http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
  8499. details. */
  8500. #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
  8501. sincos (ang, &s, &c);
  8502. #elif (defined HAVE___SINCOS)
  8503. __sincos (ang, &s, &c);
  8504. #else
  8505. s = sin (ang);
  8506. c = cos (ang);
  8507. #endif
  8508. /* If s and c are NaNs, this indicates that the angle is a NaN,
  8509. infinite, or perhaps simply too large to determine its value
  8510. mod 2*pi. However, we know something that the floating-point
  8511. implementation doesn't know: We know that s and c are finite.
  8512. Therefore, if the magnitude is zero, return a complex zero.
  8513. The reason we check for the NaNs instead of using this case
  8514. whenever mag == 0.0 is because when the angle is known, we'd
  8515. like to return the correct kind of non-real complex zero:
  8516. +0.0+0.0i, -0.0+0.0i, -0.0-0.0i, or +0.0-0.0i, depending
  8517. on which quadrant the angle is in.
  8518. */
  8519. if (SCM_UNLIKELY (isnan(s)) && isnan(c) && (mag == 0.0))
  8520. return scm_c_make_rectangular (0.0, 0.0);
  8521. else
  8522. return scm_c_make_rectangular (mag * c, mag * s);
  8523. }
  8524. SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
  8525. (SCM mag, SCM ang),
  8526. "Return the complex number @var{mag} * e^(i * @var{ang}).")
  8527. #define FUNC_NAME s_scm_make_polar
  8528. {
  8529. SCM_ASSERT_TYPE (scm_is_real (mag), mag, SCM_ARG1, FUNC_NAME, "real");
  8530. SCM_ASSERT_TYPE (scm_is_real (ang), ang, SCM_ARG2, FUNC_NAME, "real");
  8531. /* If mag is exact0, return exact0 */
  8532. if (scm_is_eq (mag, SCM_INUM0))
  8533. return SCM_INUM0;
  8534. /* Return a real if ang is exact0 */
  8535. else if (scm_is_eq (ang, SCM_INUM0))
  8536. return mag;
  8537. else
  8538. return scm_c_make_polar (scm_to_double (mag), scm_to_double (ang));
  8539. }
  8540. #undef FUNC_NAME
  8541. SCM_PRIMITIVE_GENERIC (scm_real_part, "real-part", 1, 0, 0,
  8542. (SCM z),
  8543. "Return the real part of the number @var{z}.")
  8544. #define FUNC_NAME s_scm_real_part
  8545. {
  8546. if (SCM_COMPLEXP (z))
  8547. return scm_i_from_double (SCM_COMPLEX_REAL (z));
  8548. else if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_REALP (z) || SCM_FRACTIONP (z))
  8549. return z;
  8550. else
  8551. return scm_wta_dispatch_1 (g_scm_real_part, z, SCM_ARG1, s_scm_real_part);
  8552. }
  8553. #undef FUNC_NAME
  8554. SCM_PRIMITIVE_GENERIC (scm_imag_part, "imag-part", 1, 0, 0,
  8555. (SCM z),
  8556. "Return the imaginary part of the number @var{z}.")
  8557. #define FUNC_NAME s_scm_imag_part
  8558. {
  8559. if (SCM_COMPLEXP (z))
  8560. return scm_i_from_double (SCM_COMPLEX_IMAG (z));
  8561. else if (SCM_I_INUMP (z) || SCM_REALP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  8562. return SCM_INUM0;
  8563. else
  8564. return scm_wta_dispatch_1 (g_scm_imag_part, z, SCM_ARG1, s_scm_imag_part);
  8565. }
  8566. #undef FUNC_NAME
  8567. SCM_PRIMITIVE_GENERIC (scm_numerator, "numerator", 1, 0, 0,
  8568. (SCM z),
  8569. "Return the numerator of the number @var{z}.")
  8570. #define FUNC_NAME s_scm_numerator
  8571. {
  8572. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  8573. return z;
  8574. else if (SCM_FRACTIONP (z))
  8575. return SCM_FRACTION_NUMERATOR (z);
  8576. else if (SCM_REALP (z))
  8577. {
  8578. double zz = SCM_REAL_VALUE (z);
  8579. if (zz == floor (zz))
  8580. /* Handle -0.0 and infinities in accordance with R6RS
  8581. flnumerator, and optimize handling of integers. */
  8582. return z;
  8583. else
  8584. return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
  8585. }
  8586. else
  8587. return scm_wta_dispatch_1 (g_scm_numerator, z, SCM_ARG1, s_scm_numerator);
  8588. }
  8589. #undef FUNC_NAME
  8590. SCM_PRIMITIVE_GENERIC (scm_denominator, "denominator", 1, 0, 0,
  8591. (SCM z),
  8592. "Return the denominator of the number @var{z}.")
  8593. #define FUNC_NAME s_scm_denominator
  8594. {
  8595. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  8596. return SCM_INUM1;
  8597. else if (SCM_FRACTIONP (z))
  8598. return SCM_FRACTION_DENOMINATOR (z);
  8599. else if (SCM_REALP (z))
  8600. {
  8601. double zz = SCM_REAL_VALUE (z);
  8602. if (zz == floor (zz))
  8603. /* Handle infinities in accordance with R6RS fldenominator, and
  8604. optimize handling of integers. */
  8605. return scm_i_from_double (1.0);
  8606. else
  8607. return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
  8608. }
  8609. else
  8610. return scm_wta_dispatch_1 (g_scm_denominator, z, SCM_ARG1,
  8611. s_scm_denominator);
  8612. }
  8613. #undef FUNC_NAME
  8614. SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1, 0, 0,
  8615. (SCM z),
  8616. "Return the magnitude of the number @var{z}. This is the same as\n"
  8617. "@code{abs} for real arguments, but also allows complex numbers.")
  8618. #define FUNC_NAME s_scm_magnitude
  8619. {
  8620. if (SCM_I_INUMP (z))
  8621. {
  8622. scm_t_inum zz = SCM_I_INUM (z);
  8623. if (zz >= 0)
  8624. return z;
  8625. else if (SCM_POSFIXABLE (-zz))
  8626. return SCM_I_MAKINUM (-zz);
  8627. else
  8628. return scm_i_inum2big (-zz);
  8629. }
  8630. else if (SCM_BIGP (z))
  8631. {
  8632. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  8633. scm_remember_upto_here_1 (z);
  8634. if (sgn < 0)
  8635. return scm_i_clonebig (z, 0);
  8636. else
  8637. return z;
  8638. }
  8639. else if (SCM_REALP (z))
  8640. return scm_i_from_double (fabs (SCM_REAL_VALUE (z)));
  8641. else if (SCM_COMPLEXP (z))
  8642. return scm_i_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
  8643. else if (SCM_FRACTIONP (z))
  8644. {
  8645. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  8646. return z;
  8647. return scm_i_make_ratio_already_reduced
  8648. (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
  8649. SCM_FRACTION_DENOMINATOR (z));
  8650. }
  8651. else
  8652. return scm_wta_dispatch_1 (g_scm_magnitude, z, SCM_ARG1,
  8653. s_scm_magnitude);
  8654. }
  8655. #undef FUNC_NAME
  8656. SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0,
  8657. (SCM z),
  8658. "Return the angle of the complex number @var{z}.")
  8659. #define FUNC_NAME s_scm_angle
  8660. {
  8661. /* atan(0,-1) is pi and it'd be possible to have that as a constant like
  8662. flo0 to save allocating a new flonum with scm_i_from_double each time.
  8663. But if atan2 follows the floating point rounding mode, then the value
  8664. is not a constant. Maybe it'd be close enough though. */
  8665. if (SCM_I_INUMP (z))
  8666. {
  8667. if (SCM_I_INUM (z) >= 0)
  8668. return flo0;
  8669. else
  8670. return scm_i_from_double (atan2 (0.0, -1.0));
  8671. }
  8672. else if (SCM_BIGP (z))
  8673. {
  8674. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  8675. scm_remember_upto_here_1 (z);
  8676. if (sgn < 0)
  8677. return scm_i_from_double (atan2 (0.0, -1.0));
  8678. else
  8679. return flo0;
  8680. }
  8681. else if (SCM_REALP (z))
  8682. {
  8683. double x = SCM_REAL_VALUE (z);
  8684. if (copysign (1.0, x) > 0.0)
  8685. return flo0;
  8686. else
  8687. return scm_i_from_double (atan2 (0.0, -1.0));
  8688. }
  8689. else if (SCM_COMPLEXP (z))
  8690. return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
  8691. else if (SCM_FRACTIONP (z))
  8692. {
  8693. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  8694. return flo0;
  8695. else return scm_i_from_double (atan2 (0.0, -1.0));
  8696. }
  8697. else
  8698. return scm_wta_dispatch_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle);
  8699. }
  8700. #undef FUNC_NAME
  8701. SCM_PRIMITIVE_GENERIC (scm_exact_to_inexact, "exact->inexact", 1, 0, 0,
  8702. (SCM z),
  8703. "Convert the number @var{z} to its inexact representation.\n")
  8704. #define FUNC_NAME s_scm_exact_to_inexact
  8705. {
  8706. if (SCM_I_INUMP (z))
  8707. return scm_i_from_double ((double) SCM_I_INUM (z));
  8708. else if (SCM_BIGP (z))
  8709. return scm_i_from_double (scm_i_big2dbl (z));
  8710. else if (SCM_FRACTIONP (z))
  8711. return scm_i_from_double (scm_i_fraction2double (z));
  8712. else if (SCM_INEXACTP (z))
  8713. return z;
  8714. else
  8715. return scm_wta_dispatch_1 (g_scm_exact_to_inexact, z, 1,
  8716. s_scm_exact_to_inexact);
  8717. }
  8718. #undef FUNC_NAME
  8719. SCM_PRIMITIVE_GENERIC (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
  8720. (SCM z),
  8721. "Return an exact number that is numerically closest to @var{z}.")
  8722. #define FUNC_NAME s_scm_inexact_to_exact
  8723. {
  8724. if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  8725. return z;
  8726. else
  8727. {
  8728. double val;
  8729. if (SCM_REALP (z))
  8730. val = SCM_REAL_VALUE (z);
  8731. else if (SCM_COMPLEXP (z) && SCM_COMPLEX_IMAG (z) == 0.0)
  8732. val = SCM_COMPLEX_REAL (z);
  8733. else
  8734. return scm_wta_dispatch_1 (g_scm_inexact_to_exact, z, 1,
  8735. s_scm_inexact_to_exact);
  8736. if (!SCM_LIKELY (isfinite (val)))
  8737. SCM_OUT_OF_RANGE (1, z);
  8738. else if (val == 0.0)
  8739. return SCM_INUM0;
  8740. else
  8741. {
  8742. int expon;
  8743. SCM numerator;
  8744. numerator = scm_i_dbl2big (ldexp (frexp (val, &expon),
  8745. DBL_MANT_DIG));
  8746. expon -= DBL_MANT_DIG;
  8747. if (expon < 0)
  8748. {
  8749. int shift = mpz_scan1 (SCM_I_BIG_MPZ (numerator), 0);
  8750. if (shift > -expon)
  8751. shift = -expon;
  8752. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (numerator),
  8753. SCM_I_BIG_MPZ (numerator),
  8754. shift);
  8755. expon += shift;
  8756. }
  8757. numerator = scm_i_normbig (numerator);
  8758. if (expon < 0)
  8759. return scm_i_make_ratio_already_reduced
  8760. (numerator, left_shift_exact_integer (SCM_INUM1, -expon));
  8761. else if (expon > 0)
  8762. return left_shift_exact_integer (numerator, expon);
  8763. else
  8764. return numerator;
  8765. }
  8766. }
  8767. }
  8768. #undef FUNC_NAME
  8769. SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
  8770. (SCM x, SCM eps),
  8771. "Returns the @emph{simplest} rational number differing\n"
  8772. "from @var{x} by no more than @var{eps}.\n"
  8773. "\n"
  8774. "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
  8775. "exact result when both its arguments are exact. Thus, you might need\n"
  8776. "to use @code{inexact->exact} on the arguments.\n"
  8777. "\n"
  8778. "@lisp\n"
  8779. "(rationalize (inexact->exact 1.2) 1/100)\n"
  8780. "@result{} 6/5\n"
  8781. "@end lisp")
  8782. #define FUNC_NAME s_scm_rationalize
  8783. {
  8784. SCM_ASSERT_TYPE (scm_is_real (x), x, SCM_ARG1, FUNC_NAME, "real");
  8785. SCM_ASSERT_TYPE (scm_is_real (eps), eps, SCM_ARG2, FUNC_NAME, "real");
  8786. if (SCM_UNLIKELY (!scm_is_exact (eps) || !scm_is_exact (x)))
  8787. {
  8788. if (SCM_UNLIKELY (scm_is_false (scm_finite_p (eps))))
  8789. {
  8790. if (scm_is_false (scm_nan_p (eps)) && scm_is_true (scm_finite_p (x)))
  8791. return flo0;
  8792. else
  8793. return scm_nan ();
  8794. }
  8795. else if (SCM_UNLIKELY (scm_is_false (scm_finite_p (x))))
  8796. return x;
  8797. else
  8798. return scm_exact_to_inexact
  8799. (scm_rationalize (scm_inexact_to_exact (x),
  8800. scm_inexact_to_exact (eps)));
  8801. }
  8802. else
  8803. {
  8804. /* X and EPS are exact rationals.
  8805. The code that follows is equivalent to the following Scheme code:
  8806. (define (exact-rationalize x eps)
  8807. (let ((n1 (if (negative? x) -1 1))
  8808. (x (abs x))
  8809. (eps (abs eps)))
  8810. (let ((lo (- x eps))
  8811. (hi (+ x eps)))
  8812. (if (<= lo 0)
  8813. 0
  8814. (let loop ((nlo (numerator lo)) (dlo (denominator lo))
  8815. (nhi (numerator hi)) (dhi (denominator hi))
  8816. (n1 n1) (d1 0) (n2 0) (d2 1))
  8817. (let-values (((qlo rlo) (floor/ nlo dlo))
  8818. ((qhi rhi) (floor/ nhi dhi)))
  8819. (let ((n0 (+ n2 (* n1 qlo)))
  8820. (d0 (+ d2 (* d1 qlo))))
  8821. (cond ((zero? rlo) (/ n0 d0))
  8822. ((< qlo qhi) (/ (+ n0 n1) (+ d0 d1)))
  8823. (else (loop dhi rhi dlo rlo n0 d0 n1 d1))))))))))
  8824. */
  8825. int n1_init = 1;
  8826. SCM lo, hi;
  8827. eps = scm_abs (eps);
  8828. if (scm_is_true (scm_negative_p (x)))
  8829. {
  8830. n1_init = -1;
  8831. x = scm_difference (x, SCM_UNDEFINED);
  8832. }
  8833. /* X and EPS are non-negative exact rationals. */
  8834. lo = scm_difference (x, eps);
  8835. hi = scm_sum (x, eps);
  8836. if (scm_is_false (scm_positive_p (lo)))
  8837. /* If zero is included in the interval, return it.
  8838. It is the simplest rational of all. */
  8839. return SCM_INUM0;
  8840. else
  8841. {
  8842. SCM result;
  8843. mpz_t n0, d0, n1, d1, n2, d2;
  8844. mpz_t nlo, dlo, nhi, dhi;
  8845. mpz_t qlo, rlo, qhi, rhi;
  8846. /* LO and HI are positive exact rationals. */
  8847. /* Our approach here follows the method described by Alan
  8848. Bawden in a message entitled "(rationalize x y)" on the
  8849. rrrs-authors mailing list, dated 16 Feb 1988 14:08:28 EST:
  8850. http://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1988/msg00063.html
  8851. In brief, we compute the continued fractions of the two
  8852. endpoints of the interval (LO and HI). The continued
  8853. fraction of the result consists of the common prefix of the
  8854. continued fractions of LO and HI, plus one final term. The
  8855. final term of the result is the smallest integer contained
  8856. in the interval between the remainders of LO and HI after
  8857. the common prefix has been removed.
  8858. The following code lazily computes the continued fraction
  8859. representations of LO and HI, and simultaneously converts
  8860. the continued fraction of the result into a rational
  8861. number. We use MPZ functions directly to avoid type
  8862. dispatch and GC allocation during the loop. */
  8863. mpz_inits (n0, d0, n1, d1, n2, d2,
  8864. nlo, dlo, nhi, dhi,
  8865. qlo, rlo, qhi, rhi,
  8866. NULL);
  8867. /* The variables N1, D1, N2 and D2 are used to compute the
  8868. resulting rational from its continued fraction. At each
  8869. step, N2/D2 and N1/D1 are the last two convergents. They
  8870. are normally initialized to 0/1 and 1/0, respectively.
  8871. However, if we negated X then we must negate the result as
  8872. well, and we do that by initializing N1/D1 to -1/0. */
  8873. mpz_set_si (n1, n1_init);
  8874. mpz_set_ui (d1, 0);
  8875. mpz_set_ui (n2, 0);
  8876. mpz_set_ui (d2, 1);
  8877. /* The variables NLO, DLO, NHI, and DHI are used to lazily
  8878. compute the continued fraction representations of LO and HI
  8879. using Euclid's algorithm. Initially, NLO/DLO == LO and
  8880. NHI/DHI == HI. */
  8881. scm_to_mpz (scm_numerator (lo), nlo);
  8882. scm_to_mpz (scm_denominator (lo), dlo);
  8883. scm_to_mpz (scm_numerator (hi), nhi);
  8884. scm_to_mpz (scm_denominator (hi), dhi);
  8885. /* As long as we're using exact arithmetic, the following loop
  8886. is guaranteed to terminate. */
  8887. for (;;)
  8888. {
  8889. /* Compute the next terms (QLO and QHI) of the continued
  8890. fractions of LO and HI. */
  8891. mpz_fdiv_qr (qlo, rlo, nlo, dlo); /* QLO <-- floor (NLO/DLO), RLO <-- NLO - QLO * DLO */
  8892. mpz_fdiv_qr (qhi, rhi, nhi, dhi); /* QHI <-- floor (NHI/DHI), RHI <-- NHI - QHI * DHI */
  8893. /* The next term of the result will be either QLO or
  8894. QLO+1. Here we compute the next convergent of the
  8895. result based on the assumption that QLO is the next
  8896. term. If that turns out to be wrong, we'll adjust
  8897. these later by adding N1 to N0 and D1 to D0. */
  8898. mpz_set (n0, n2); mpz_addmul (n0, n1, qlo); /* N0 <-- N2 + (QLO * N1) */
  8899. mpz_set (d0, d2); mpz_addmul (d0, d1, qlo); /* D0 <-- D2 + (QLO * D1) */
  8900. /* We stop iterating when an integer is contained in the
  8901. interval between the remainders NLO/DLO and NHI/DHI.
  8902. There are two cases to consider: either NLO/DLO == QLO
  8903. is an integer (indicated by RLO == 0), or QLO < QHI. */
  8904. if (mpz_sgn (rlo) == 0 || mpz_cmp (qlo, qhi) != 0)
  8905. break;
  8906. /* Efficiently shuffle variables around for the next
  8907. iteration. First we shift the recent convergents. */
  8908. mpz_swap (n2, n1); mpz_swap (n1, n0); /* N2 <-- N1 <-- N0 */
  8909. mpz_swap (d2, d1); mpz_swap (d1, d0); /* D2 <-- D1 <-- D0 */
  8910. /* The following shuffling is a bit confusing, so some
  8911. explanation is in order. Conceptually, we're doing a
  8912. couple of things here. After substracting the floor of
  8913. NLO/DLO, the remainder is RLO/DLO. The rest of the
  8914. continued fraction will represent the remainder's
  8915. reciprocal DLO/RLO. Similarly for the HI endpoint.
  8916. So in the next iteration, the new endpoints will be
  8917. DLO/RLO and DHI/RHI. However, when we take the
  8918. reciprocals of these endpoints, their order is
  8919. switched. So in summary, we want NLO/DLO <-- DHI/RHI
  8920. and NHI/DHI <-- DLO/RLO. */
  8921. mpz_swap (nlo, dhi); mpz_swap (dhi, rlo); /* NLO <-- DHI <-- RLO */
  8922. mpz_swap (nhi, dlo); mpz_swap (dlo, rhi); /* NHI <-- DLO <-- RHI */
  8923. }
  8924. /* There is now an integer in the interval [NLO/DLO NHI/DHI].
  8925. The last term of the result will be the smallest integer in
  8926. that interval, which is ceiling(NLO/DLO). We have already
  8927. computed floor(NLO/DLO) in QLO, so now we adjust QLO to be
  8928. equal to the ceiling. */
  8929. if (mpz_sgn (rlo) != 0)
  8930. {
  8931. /* If RLO is non-zero, then NLO/DLO is not an integer and
  8932. the next term will be QLO+1. QLO was used in the
  8933. computation of N0 and D0 above. Here we adjust N0 and
  8934. D0 to be based on QLO+1 instead of QLO. */
  8935. mpz_add (n0, n0, n1); /* N0 <-- N0 + N1 */
  8936. mpz_add (d0, d0, d1); /* D0 <-- D0 + D1 */
  8937. }
  8938. /* The simplest rational in the interval is N0/D0 */
  8939. result = scm_i_make_ratio_already_reduced (scm_from_mpz (n0),
  8940. scm_from_mpz (d0));
  8941. mpz_clears (n0, d0, n1, d1, n2, d2,
  8942. nlo, dlo, nhi, dhi,
  8943. qlo, rlo, qhi, rhi,
  8944. NULL);
  8945. return result;
  8946. }
  8947. }
  8948. }
  8949. #undef FUNC_NAME
  8950. /* conversion functions */
  8951. int
  8952. scm_is_integer (SCM val)
  8953. {
  8954. return scm_is_true (scm_integer_p (val));
  8955. }
  8956. int
  8957. scm_is_exact_integer (SCM val)
  8958. {
  8959. return scm_is_true (scm_exact_integer_p (val));
  8960. }
  8961. int
  8962. scm_is_signed_integer (SCM val, intmax_t min, intmax_t max)
  8963. {
  8964. if (SCM_I_INUMP (val))
  8965. {
  8966. scm_t_signed_bits n = SCM_I_INUM (val);
  8967. return n >= min && n <= max;
  8968. }
  8969. else if (SCM_BIGP (val))
  8970. {
  8971. if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
  8972. return 0;
  8973. else if (min >= LONG_MIN && max <= LONG_MAX)
  8974. {
  8975. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
  8976. {
  8977. long n = mpz_get_si (SCM_I_BIG_MPZ (val));
  8978. return n >= min && n <= max;
  8979. }
  8980. else
  8981. return 0;
  8982. }
  8983. else
  8984. {
  8985. uintmax_t abs_n;
  8986. intmax_t n;
  8987. size_t count;
  8988. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  8989. > CHAR_BIT*sizeof (uintmax_t))
  8990. return 0;
  8991. mpz_export (&abs_n, &count, 1, sizeof (uintmax_t), 0, 0,
  8992. SCM_I_BIG_MPZ (val));
  8993. if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
  8994. {
  8995. if (abs_n <= max)
  8996. n = abs_n;
  8997. else
  8998. return 0;
  8999. }
  9000. else
  9001. {
  9002. /* Carefully avoid signed integer overflow. */
  9003. if (min < 0 && abs_n - 1 <= -(min + 1))
  9004. n = -1 - (intmax_t)(abs_n - 1);
  9005. else
  9006. return 0;
  9007. }
  9008. return n >= min && n <= max;
  9009. }
  9010. }
  9011. else
  9012. return 0;
  9013. }
  9014. int
  9015. scm_is_unsigned_integer (SCM val, uintmax_t min, uintmax_t max)
  9016. {
  9017. if (SCM_I_INUMP (val))
  9018. {
  9019. scm_t_signed_bits n = SCM_I_INUM (val);
  9020. return n >= 0 && ((uintmax_t)n) >= min && ((uintmax_t)n) <= max;
  9021. }
  9022. else if (SCM_BIGP (val))
  9023. {
  9024. if (max <= SCM_MOST_POSITIVE_FIXNUM)
  9025. return 0;
  9026. else if (max <= ULONG_MAX)
  9027. {
  9028. if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
  9029. {
  9030. unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
  9031. return n >= min && n <= max;
  9032. }
  9033. else
  9034. return 0;
  9035. }
  9036. else
  9037. {
  9038. uintmax_t n;
  9039. size_t count;
  9040. if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
  9041. return 0;
  9042. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  9043. > CHAR_BIT*sizeof (uintmax_t))
  9044. return 0;
  9045. mpz_export (&n, &count, 1, sizeof (uintmax_t), 0, 0,
  9046. SCM_I_BIG_MPZ (val));
  9047. return n >= min && n <= max;
  9048. }
  9049. }
  9050. else
  9051. return 0;
  9052. }
  9053. static void
  9054. scm_i_range_error (SCM bad_val, SCM min, SCM max)
  9055. {
  9056. scm_error (scm_out_of_range_key,
  9057. NULL,
  9058. "Value out of range ~S to ~S: ~S",
  9059. scm_list_3 (min, max, bad_val),
  9060. scm_list_1 (bad_val));
  9061. }
  9062. #define TYPE intmax_t
  9063. #define TYPE_MIN min
  9064. #define TYPE_MAX max
  9065. #define SIZEOF_TYPE 0
  9066. #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, intmax_t min, intmax_t max)
  9067. #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
  9068. #include "conv-integer.i.c"
  9069. #define TYPE uintmax_t
  9070. #define TYPE_MIN min
  9071. #define TYPE_MAX max
  9072. #define SIZEOF_TYPE 0
  9073. #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, uintmax_t min, uintmax_t max)
  9074. #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
  9075. #include "conv-uinteger.i.c"
  9076. #define TYPE int8_t
  9077. #define TYPE_MIN INT8_MIN
  9078. #define TYPE_MAX INT8_MAX
  9079. #define SIZEOF_TYPE 1
  9080. #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
  9081. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
  9082. #include "conv-integer.i.c"
  9083. #define TYPE uint8_t
  9084. #define TYPE_MIN 0
  9085. #define TYPE_MAX UINT8_MAX
  9086. #define SIZEOF_TYPE 1
  9087. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
  9088. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
  9089. #include "conv-uinteger.i.c"
  9090. #define TYPE int16_t
  9091. #define TYPE_MIN INT16_MIN
  9092. #define TYPE_MAX INT16_MAX
  9093. #define SIZEOF_TYPE 2
  9094. #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
  9095. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
  9096. #include "conv-integer.i.c"
  9097. #define TYPE uint16_t
  9098. #define TYPE_MIN 0
  9099. #define TYPE_MAX UINT16_MAX
  9100. #define SIZEOF_TYPE 2
  9101. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
  9102. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
  9103. #include "conv-uinteger.i.c"
  9104. #define TYPE int32_t
  9105. #define TYPE_MIN INT32_MIN
  9106. #define TYPE_MAX INT32_MAX
  9107. #define SIZEOF_TYPE 4
  9108. #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
  9109. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
  9110. #include "conv-integer.i.c"
  9111. #define TYPE uint32_t
  9112. #define TYPE_MIN 0
  9113. #define TYPE_MAX UINT32_MAX
  9114. #define SIZEOF_TYPE 4
  9115. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
  9116. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
  9117. #include "conv-uinteger.i.c"
  9118. #define TYPE scm_t_wchar
  9119. #define TYPE_MIN (int32_t)-1
  9120. #define TYPE_MAX (int32_t)0x10ffff
  9121. #define SIZEOF_TYPE 4
  9122. #define SCM_TO_TYPE_PROTO(arg) scm_to_wchar (arg)
  9123. #define SCM_FROM_TYPE_PROTO(arg) scm_from_wchar (arg)
  9124. #include "conv-integer.i.c"
  9125. #define TYPE int64_t
  9126. #define TYPE_MIN INT64_MIN
  9127. #define TYPE_MAX INT64_MAX
  9128. #define SIZEOF_TYPE 8
  9129. #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
  9130. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
  9131. #include "conv-integer.i.c"
  9132. #define TYPE uint64_t
  9133. #define TYPE_MIN 0
  9134. #define TYPE_MAX UINT64_MAX
  9135. #define SIZEOF_TYPE 8
  9136. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
  9137. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
  9138. #include "conv-uinteger.i.c"
  9139. void
  9140. scm_to_mpz (SCM val, mpz_t rop)
  9141. {
  9142. if (SCM_I_INUMP (val))
  9143. mpz_set_si (rop, SCM_I_INUM (val));
  9144. else if (SCM_BIGP (val))
  9145. mpz_set (rop, SCM_I_BIG_MPZ (val));
  9146. else
  9147. scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
  9148. }
  9149. SCM
  9150. scm_from_mpz (mpz_t val)
  9151. {
  9152. return scm_i_mpz2num (val);
  9153. }
  9154. int
  9155. scm_is_real (SCM val)
  9156. {
  9157. return scm_is_true (scm_real_p (val));
  9158. }
  9159. int
  9160. scm_is_rational (SCM val)
  9161. {
  9162. return scm_is_true (scm_rational_p (val));
  9163. }
  9164. double
  9165. scm_to_double (SCM val)
  9166. {
  9167. if (SCM_I_INUMP (val))
  9168. return SCM_I_INUM (val);
  9169. else if (SCM_BIGP (val))
  9170. return scm_i_big2dbl (val);
  9171. else if (SCM_FRACTIONP (val))
  9172. return scm_i_fraction2double (val);
  9173. else if (SCM_REALP (val))
  9174. return SCM_REAL_VALUE (val);
  9175. else
  9176. scm_wrong_type_arg_msg (NULL, 0, val, "real number");
  9177. }
  9178. SCM
  9179. scm_from_double (double val)
  9180. {
  9181. return scm_i_from_double (val);
  9182. }
  9183. int
  9184. scm_is_complex (SCM val)
  9185. {
  9186. return scm_is_true (scm_complex_p (val));
  9187. }
  9188. double
  9189. scm_c_real_part (SCM z)
  9190. {
  9191. if (SCM_COMPLEXP (z))
  9192. return SCM_COMPLEX_REAL (z);
  9193. else
  9194. {
  9195. /* Use the scm_real_part to get proper error checking and
  9196. dispatching.
  9197. */
  9198. return scm_to_double (scm_real_part (z));
  9199. }
  9200. }
  9201. double
  9202. scm_c_imag_part (SCM z)
  9203. {
  9204. if (SCM_COMPLEXP (z))
  9205. return SCM_COMPLEX_IMAG (z);
  9206. else
  9207. {
  9208. /* Use the scm_imag_part to get proper error checking and
  9209. dispatching. The result will almost always be 0.0, but not
  9210. always.
  9211. */
  9212. return scm_to_double (scm_imag_part (z));
  9213. }
  9214. }
  9215. double
  9216. scm_c_magnitude (SCM z)
  9217. {
  9218. return scm_to_double (scm_magnitude (z));
  9219. }
  9220. double
  9221. scm_c_angle (SCM z)
  9222. {
  9223. return scm_to_double (scm_angle (z));
  9224. }
  9225. int
  9226. scm_is_number (SCM z)
  9227. {
  9228. return scm_is_true (scm_number_p (z));
  9229. }
  9230. /* Returns log(x * 2^shift) */
  9231. static SCM
  9232. log_of_shifted_double (double x, long shift)
  9233. {
  9234. double ans = log (fabs (x)) + shift * M_LN2;
  9235. if (copysign (1.0, x) > 0.0)
  9236. return scm_i_from_double (ans);
  9237. else
  9238. return scm_c_make_rectangular (ans, M_PI);
  9239. }
  9240. /* Returns log(n), for exact integer n */
  9241. static SCM
  9242. log_of_exact_integer (SCM n)
  9243. {
  9244. if (SCM_I_INUMP (n))
  9245. return log_of_shifted_double (SCM_I_INUM (n), 0);
  9246. else if (SCM_BIGP (n))
  9247. {
  9248. long expon;
  9249. double signif = scm_i_big2dbl_2exp (n, &expon);
  9250. return log_of_shifted_double (signif, expon);
  9251. }
  9252. else
  9253. scm_wrong_type_arg ("log_of_exact_integer", SCM_ARG1, n);
  9254. }
  9255. /* Returns log(n/d), for exact non-zero integers n and d */
  9256. static SCM
  9257. log_of_fraction (SCM n, SCM d)
  9258. {
  9259. long n_size = scm_to_long (scm_integer_length (n));
  9260. long d_size = scm_to_long (scm_integer_length (d));
  9261. if (labs (n_size - d_size) > 1)
  9262. return (scm_difference (log_of_exact_integer (n),
  9263. log_of_exact_integer (d)));
  9264. else if (scm_is_false (scm_negative_p (n)))
  9265. return scm_i_from_double
  9266. (log1p (scm_i_divide2double (scm_difference (n, d), d)));
  9267. else
  9268. return scm_c_make_rectangular
  9269. (log1p (scm_i_divide2double (scm_difference (scm_abs (n), d),
  9270. d)),
  9271. M_PI);
  9272. }
  9273. /* In the following functions we dispatch to the real-arg funcs like log()
  9274. when we know the arg is real, instead of just handing everything to
  9275. clog() for instance. This is in case clog() doesn't optimize for a
  9276. real-only case, and because we have to test SCM_COMPLEXP anyway so may as
  9277. well use it to go straight to the applicable C func. */
  9278. SCM_PRIMITIVE_GENERIC (scm_log, "log", 1, 0, 0,
  9279. (SCM z),
  9280. "Return the natural logarithm of @var{z}.")
  9281. #define FUNC_NAME s_scm_log
  9282. {
  9283. if (SCM_COMPLEXP (z))
  9284. {
  9285. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG \
  9286. && defined (SCM_COMPLEX_VALUE)
  9287. return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
  9288. #else
  9289. double re = SCM_COMPLEX_REAL (z);
  9290. double im = SCM_COMPLEX_IMAG (z);
  9291. return scm_c_make_rectangular (log (hypot (re, im)),
  9292. atan2 (im, re));
  9293. #endif
  9294. }
  9295. else if (SCM_REALP (z))
  9296. return log_of_shifted_double (SCM_REAL_VALUE (z), 0);
  9297. else if (SCM_I_INUMP (z))
  9298. {
  9299. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  9300. if (scm_is_eq (z, SCM_INUM0))
  9301. scm_num_overflow (s_scm_log);
  9302. #endif
  9303. return log_of_shifted_double (SCM_I_INUM (z), 0);
  9304. }
  9305. else if (SCM_BIGP (z))
  9306. return log_of_exact_integer (z);
  9307. else if (SCM_FRACTIONP (z))
  9308. return log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  9309. SCM_FRACTION_DENOMINATOR (z));
  9310. else
  9311. return scm_wta_dispatch_1 (g_scm_log, z, 1, s_scm_log);
  9312. }
  9313. #undef FUNC_NAME
  9314. SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0,
  9315. (SCM z),
  9316. "Return the base 10 logarithm of @var{z}.")
  9317. #define FUNC_NAME s_scm_log10
  9318. {
  9319. if (SCM_COMPLEXP (z))
  9320. {
  9321. /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
  9322. clog() and a multiply by M_LOG10E, rather than the fallback
  9323. log10+hypot+atan2.) */
  9324. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG10 \
  9325. && defined SCM_COMPLEX_VALUE
  9326. return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
  9327. #else
  9328. double re = SCM_COMPLEX_REAL (z);
  9329. double im = SCM_COMPLEX_IMAG (z);
  9330. return scm_c_make_rectangular (log10 (hypot (re, im)),
  9331. M_LOG10E * atan2 (im, re));
  9332. #endif
  9333. }
  9334. else if (SCM_REALP (z) || SCM_I_INUMP (z))
  9335. {
  9336. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  9337. if (scm_is_eq (z, SCM_INUM0))
  9338. scm_num_overflow (s_scm_log10);
  9339. #endif
  9340. {
  9341. double re = scm_to_double (z);
  9342. double l = log10 (fabs (re));
  9343. if (copysign (1.0, re) > 0.0)
  9344. return scm_i_from_double (l);
  9345. else
  9346. return scm_c_make_rectangular (l, M_LOG10E * M_PI);
  9347. }
  9348. }
  9349. else if (SCM_BIGP (z))
  9350. return scm_product (flo_log10e, log_of_exact_integer (z));
  9351. else if (SCM_FRACTIONP (z))
  9352. return scm_product (flo_log10e,
  9353. log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  9354. SCM_FRACTION_DENOMINATOR (z)));
  9355. else
  9356. return scm_wta_dispatch_1 (g_scm_log10, z, 1, s_scm_log10);
  9357. }
  9358. #undef FUNC_NAME
  9359. SCM_PRIMITIVE_GENERIC (scm_exp, "exp", 1, 0, 0,
  9360. (SCM z),
  9361. "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
  9362. "base of natural logarithms (2.71828@dots{}).")
  9363. #define FUNC_NAME s_scm_exp
  9364. {
  9365. if (SCM_COMPLEXP (z))
  9366. {
  9367. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CEXP \
  9368. && defined (SCM_COMPLEX_VALUE)
  9369. return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
  9370. #else
  9371. return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
  9372. SCM_COMPLEX_IMAG (z));
  9373. #endif
  9374. }
  9375. else if (SCM_NUMBERP (z))
  9376. {
  9377. /* When z is a negative bignum the conversion to double overflows,
  9378. giving -infinity, but that's ok, the exp is still 0.0. */
  9379. return scm_i_from_double (exp (scm_to_double (z)));
  9380. }
  9381. else
  9382. return scm_wta_dispatch_1 (g_scm_exp, z, 1, s_scm_exp);
  9383. }
  9384. #undef FUNC_NAME
  9385. SCM_DEFINE (scm_i_exact_integer_sqrt, "exact-integer-sqrt", 1, 0, 0,
  9386. (SCM k),
  9387. "Return two exact non-negative integers @var{s} and @var{r}\n"
  9388. "such that @math{@var{k} = @var{s}^2 + @var{r}} and\n"
  9389. "@math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.\n"
  9390. "An error is raised if @var{k} is not an exact non-negative integer.\n"
  9391. "\n"
  9392. "@lisp\n"
  9393. "(exact-integer-sqrt 10) @result{} 3 and 1\n"
  9394. "@end lisp")
  9395. #define FUNC_NAME s_scm_i_exact_integer_sqrt
  9396. {
  9397. SCM s, r;
  9398. scm_exact_integer_sqrt (k, &s, &r);
  9399. return scm_values_2 (s, r);
  9400. }
  9401. #undef FUNC_NAME
  9402. void
  9403. scm_exact_integer_sqrt (SCM k, SCM *sp, SCM *rp)
  9404. {
  9405. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9406. {
  9407. if (SCM_I_INUM (k) > 0)
  9408. {
  9409. mp_limb_t kk, ss, rr;
  9410. kk = SCM_I_INUM (k);
  9411. if (mpn_sqrtrem (&ss, &rr, &kk, 1) == 0)
  9412. rr = 0;
  9413. *sp = SCM_I_MAKINUM (ss);
  9414. *rp = SCM_I_MAKINUM (rr);
  9415. }
  9416. else if (SCM_I_INUM (k) == 0)
  9417. *sp = *rp = SCM_INUM0;
  9418. else
  9419. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9420. "exact non-negative integer");
  9421. }
  9422. else if (SCM_LIKELY (SCM_BIGP (k)))
  9423. {
  9424. SCM s, r;
  9425. if (mpz_sgn (SCM_I_BIG_MPZ (k)) < 0)
  9426. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9427. "exact non-negative integer");
  9428. s = scm_i_mkbig ();
  9429. r = scm_i_mkbig ();
  9430. mpz_sqrtrem (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (k));
  9431. scm_remember_upto_here_1 (k);
  9432. *sp = scm_i_normbig (s);
  9433. *rp = scm_i_normbig (r);
  9434. }
  9435. else
  9436. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9437. "exact non-negative integer");
  9438. }
  9439. /* Return true iff K is a perfect square.
  9440. K must be an exact integer. */
  9441. static int
  9442. exact_integer_is_perfect_square (SCM k)
  9443. {
  9444. int result;
  9445. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9446. {
  9447. if (SCM_I_INUM (k) > 0)
  9448. {
  9449. mp_limb_t kk = SCM_I_INUM (k);
  9450. result = mpn_perfect_square_p (&kk, 1);
  9451. }
  9452. else
  9453. result = (SCM_I_INUM (k) == 0);
  9454. }
  9455. else
  9456. {
  9457. result = mpz_perfect_square_p (SCM_I_BIG_MPZ (k));
  9458. scm_remember_upto_here_1 (k);
  9459. }
  9460. return result;
  9461. }
  9462. /* Return the floor of the square root of K.
  9463. K must be an exact non-negative integer. */
  9464. static SCM
  9465. exact_integer_floor_square_root (SCM k)
  9466. {
  9467. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9468. {
  9469. if (SCM_I_INUM (k) > 0)
  9470. {
  9471. mp_limb_t kk, ss, rr;
  9472. kk = SCM_I_INUM (k);
  9473. mpn_sqrtrem (&ss, &rr, &kk, 1);
  9474. return SCM_I_MAKINUM (ss);
  9475. }
  9476. else
  9477. return SCM_INUM0;
  9478. }
  9479. else
  9480. {
  9481. SCM s;
  9482. s = scm_i_mkbig ();
  9483. mpz_sqrt (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (k));
  9484. scm_remember_upto_here_1 (k);
  9485. return scm_i_normbig (s);
  9486. }
  9487. }
  9488. SCM_PRIMITIVE_GENERIC (scm_sqrt, "sqrt", 1, 0, 0,
  9489. (SCM z),
  9490. "Return the square root of @var{z}. Of the two possible roots\n"
  9491. "(positive and negative), the one with positive real part\n"
  9492. "is returned, or if that's zero then a positive imaginary part.\n"
  9493. "Thus,\n"
  9494. "\n"
  9495. "@example\n"
  9496. "(sqrt 9.0) @result{} 3.0\n"
  9497. "(sqrt -9.0) @result{} 0.0+3.0i\n"
  9498. "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
  9499. "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
  9500. "@end example")
  9501. #define FUNC_NAME s_scm_sqrt
  9502. {
  9503. if (SCM_COMPLEXP (z))
  9504. {
  9505. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_USABLE_CSQRT \
  9506. && defined SCM_COMPLEX_VALUE
  9507. return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (z)));
  9508. #else
  9509. double re = SCM_COMPLEX_REAL (z);
  9510. double im = SCM_COMPLEX_IMAG (z);
  9511. return scm_c_make_polar (sqrt (hypot (re, im)),
  9512. 0.5 * atan2 (im, re));
  9513. #endif
  9514. }
  9515. else if (SCM_NUMBERP (z))
  9516. {
  9517. if (SCM_I_INUMP (z))
  9518. {
  9519. scm_t_inum x = SCM_I_INUM (z);
  9520. if (SCM_LIKELY (x >= 0))
  9521. {
  9522. if (SCM_LIKELY (SCM_I_FIXNUM_BIT < DBL_MANT_DIG
  9523. || x < (1L << (DBL_MANT_DIG - 1))))
  9524. {
  9525. double root = sqrt (x);
  9526. /* If 0 <= x < 2^(DBL_MANT_DIG-1) and sqrt(x) is an
  9527. integer, then the result is exact. */
  9528. if (root == floor (root))
  9529. return SCM_I_MAKINUM ((scm_t_inum) root);
  9530. else
  9531. return scm_i_from_double (root);
  9532. }
  9533. else
  9534. {
  9535. mp_limb_t xx, root, rem;
  9536. assert (x != 0);
  9537. xx = x;
  9538. if (mpn_perfect_square_p (&xx, 1))
  9539. {
  9540. mpn_sqrtrem (&root, &rem, &xx, 1);
  9541. return SCM_I_MAKINUM (root);
  9542. }
  9543. }
  9544. }
  9545. }
  9546. else if (SCM_BIGP (z))
  9547. {
  9548. if (mpz_perfect_square_p (SCM_I_BIG_MPZ (z)))
  9549. {
  9550. SCM root = scm_i_mkbig ();
  9551. mpz_sqrt (SCM_I_BIG_MPZ (root), SCM_I_BIG_MPZ (z));
  9552. scm_remember_upto_here_1 (z);
  9553. return scm_i_normbig (root);
  9554. }
  9555. else
  9556. {
  9557. long expon;
  9558. double signif = scm_i_big2dbl_2exp (z, &expon);
  9559. if (expon & 1)
  9560. {
  9561. signif *= 2;
  9562. expon--;
  9563. }
  9564. if (signif < 0)
  9565. return scm_c_make_rectangular
  9566. (0.0, ldexp (sqrt (-signif), expon / 2));
  9567. else
  9568. return scm_i_from_double (ldexp (sqrt (signif), expon / 2));
  9569. }
  9570. }
  9571. else if (SCM_FRACTIONP (z))
  9572. {
  9573. SCM n = SCM_FRACTION_NUMERATOR (z);
  9574. SCM d = SCM_FRACTION_DENOMINATOR (z);
  9575. if (exact_integer_is_perfect_square (n)
  9576. && exact_integer_is_perfect_square (d))
  9577. return scm_i_make_ratio_already_reduced
  9578. (exact_integer_floor_square_root (n),
  9579. exact_integer_floor_square_root (d));
  9580. else
  9581. {
  9582. double xx = scm_i_divide2double (n, d);
  9583. double abs_xx = fabs (xx);
  9584. long shift = 0;
  9585. if (SCM_UNLIKELY (abs_xx > DBL_MAX || abs_xx < DBL_MIN))
  9586. {
  9587. shift = (scm_to_long (scm_integer_length (n))
  9588. - scm_to_long (scm_integer_length (d))) / 2;
  9589. if (shift > 0)
  9590. d = left_shift_exact_integer (d, 2 * shift);
  9591. else
  9592. n = left_shift_exact_integer (n, -2 * shift);
  9593. xx = scm_i_divide2double (n, d);
  9594. }
  9595. if (xx < 0)
  9596. return scm_c_make_rectangular (0.0, ldexp (sqrt (-xx), shift));
  9597. else
  9598. return scm_i_from_double (ldexp (sqrt (xx), shift));
  9599. }
  9600. }
  9601. /* Fallback method, when the cases above do not apply. */
  9602. {
  9603. double xx = scm_to_double (z);
  9604. if (xx < 0)
  9605. return scm_c_make_rectangular (0.0, sqrt (-xx));
  9606. else
  9607. return scm_i_from_double (sqrt (xx));
  9608. }
  9609. }
  9610. else
  9611. return scm_wta_dispatch_1 (g_scm_sqrt, z, 1, s_scm_sqrt);
  9612. }
  9613. #undef FUNC_NAME
  9614. void
  9615. scm_init_numbers ()
  9616. {
  9617. if (scm_install_gmp_memory_functions)
  9618. mp_set_memory_functions (custom_gmp_malloc,
  9619. custom_gmp_realloc,
  9620. custom_gmp_free);
  9621. mpz_init_set_si (z_negative_one, -1);
  9622. /* It may be possible to tune the performance of some algorithms by using
  9623. * the following constants to avoid the creation of bignums. Please, before
  9624. * using these values, remember the two rules of program optimization:
  9625. * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
  9626. scm_c_define ("most-positive-fixnum",
  9627. SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
  9628. scm_c_define ("most-negative-fixnum",
  9629. SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
  9630. scm_add_feature ("complex");
  9631. scm_add_feature ("inexact");
  9632. flo0 = scm_i_from_double (0.0);
  9633. flo_log10e = scm_i_from_double (M_LOG10E);
  9634. exactly_one_half = scm_divide (SCM_INUM1, SCM_I_MAKINUM (2));
  9635. {
  9636. /* Set scm_i_divide2double_lo2b to (2 b^p - 1) */
  9637. mpz_init_set_ui (scm_i_divide2double_lo2b, 1);
  9638. mpz_mul_2exp (scm_i_divide2double_lo2b,
  9639. scm_i_divide2double_lo2b,
  9640. DBL_MANT_DIG + 1); /* 2 b^p */
  9641. mpz_sub_ui (scm_i_divide2double_lo2b, scm_i_divide2double_lo2b, 1);
  9642. }
  9643. {
  9644. /* Set dbl_minimum_normal_mantissa to b^{p-1} */
  9645. mpz_init_set_ui (dbl_minimum_normal_mantissa, 1);
  9646. mpz_mul_2exp (dbl_minimum_normal_mantissa,
  9647. dbl_minimum_normal_mantissa,
  9648. DBL_MANT_DIG - 1);
  9649. }
  9650. #include "numbers.x"
  9651. }