numbers.c 297 KB

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