numbers.c 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005, 2006, 2007, 2008, 2009, 2010 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
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but 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 02110-1301 USA
  20. */
  21. /* General assumptions:
  22. * All objects satisfying SCM_COMPLEXP() have a non-zero complex component.
  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. * All objects satisfying SCM_FRACTIONP are never an integer.
  27. */
  28. /* TODO:
  29. - see if special casing bignums and reals in integer-exponent when
  30. possible (to use mpz_pow and mpf_pow_ui) is faster.
  31. - look in to better short-circuiting of common cases in
  32. integer-expt and elsewhere.
  33. - see if direct mpz operations can help in ash and elsewhere.
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. # include <config.h>
  37. #endif
  38. #include <math.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #if HAVE_COMPLEX_H
  42. #include <complex.h>
  43. #endif
  44. #include "libguile/_scm.h"
  45. #include "libguile/feature.h"
  46. #include "libguile/ports.h"
  47. #include "libguile/root.h"
  48. #include "libguile/smob.h"
  49. #include "libguile/strings.h"
  50. #include "libguile/validate.h"
  51. #include "libguile/numbers.h"
  52. #include "libguile/deprecation.h"
  53. #include "libguile/eq.h"
  54. #include "libguile/discouraged.h"
  55. /* values per glibc, if not already defined */
  56. #ifndef M_LOG10E
  57. #define M_LOG10E 0.43429448190325182765
  58. #endif
  59. #ifndef M_PI
  60. #define M_PI 3.14159265358979323846
  61. #endif
  62. /*
  63. Wonder if this might be faster for some of our code? A switch on
  64. the numtag would jump directly to the right case, and the
  65. SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests...
  66. #define SCM_I_NUMTAG_NOTNUM 0
  67. #define SCM_I_NUMTAG_INUM 1
  68. #define SCM_I_NUMTAG_BIG scm_tc16_big
  69. #define SCM_I_NUMTAG_REAL scm_tc16_real
  70. #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex
  71. #define SCM_I_NUMTAG(x) \
  72. (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \
  73. : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \
  74. : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \
  75. : SCM_I_NUMTAG_NOTNUM)))
  76. */
  77. /* the macro above will not work as is with fractions */
  78. #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0)
  79. /* FLOBUFLEN is the maximum number of characters neccessary for the
  80. * printed or scm_string representation of an inexact number.
  81. */
  82. #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  83. #if defined (SCO)
  84. #if ! defined (HAVE_ISNAN)
  85. #define HAVE_ISNAN
  86. static int
  87. isnan (double x)
  88. {
  89. return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0;
  90. }
  91. #endif
  92. #if ! defined (HAVE_ISINF)
  93. #define HAVE_ISINF
  94. static int
  95. isinf (double x)
  96. {
  97. return (IsNANorINF (x) && IsINF (x)) ? 1 : 0;
  98. }
  99. #endif
  100. #endif
  101. /* mpz_cmp_d in gmp 4.1.3 doesn't recognise infinities, so xmpz_cmp_d uses
  102. an explicit check. In some future gmp (don't know what version number),
  103. mpz_cmp_d is supposed to do this itself. */
  104. #if 1
  105. #define xmpz_cmp_d(z, d) \
  106. (xisinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d))
  107. #else
  108. #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d)
  109. #endif
  110. /* For reference, sparc solaris 7 has infinities (IEEE) but doesn't have
  111. isinf. It does have finite and isnan though, hence the use of those.
  112. fpclass would be a possibility on that system too. */
  113. static int
  114. xisinf (double x)
  115. {
  116. #if defined (HAVE_ISINF)
  117. return isinf (x);
  118. #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN)
  119. return (! (finite (x) || isnan (x)));
  120. #else
  121. return 0;
  122. #endif
  123. }
  124. static int
  125. xisnan (double x)
  126. {
  127. #if defined (HAVE_ISNAN)
  128. return isnan (x);
  129. #else
  130. return 0;
  131. #endif
  132. }
  133. #if defined (GUILE_I)
  134. #if HAVE_COMPLEX_DOUBLE
  135. /* For an SCM object Z which is a complex number (ie. satisfies
  136. SCM_COMPLEXP), return its value as a C level "complex double". */
  137. #define SCM_COMPLEX_VALUE(z) \
  138. (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
  139. static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
  140. /* Convert a C "complex double" to an SCM value. */
  141. static inline SCM
  142. scm_from_complex_double (complex double z)
  143. {
  144. return scm_c_make_rectangular (creal (z), cimag (z));
  145. }
  146. #endif /* HAVE_COMPLEX_DOUBLE */
  147. #endif /* GUILE_I */
  148. static mpz_t z_negative_one;
  149. SCM
  150. scm_i_mkbig ()
  151. {
  152. /* Return a newly created bignum. */
  153. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  154. mpz_init (SCM_I_BIG_MPZ (z));
  155. return z;
  156. }
  157. SCM
  158. scm_i_long2big (long x)
  159. {
  160. /* Return a newly created bignum initialized to X. */
  161. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  162. mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
  163. return z;
  164. }
  165. SCM
  166. scm_i_ulong2big (unsigned long x)
  167. {
  168. /* Return a newly created bignum initialized to X. */
  169. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  170. mpz_init_set_ui (SCM_I_BIG_MPZ (z), x);
  171. return z;
  172. }
  173. SCM
  174. scm_i_clonebig (SCM src_big, int same_sign_p)
  175. {
  176. /* Copy src_big's value, negate it if same_sign_p is false, and return. */
  177. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  178. mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big));
  179. if (!same_sign_p)
  180. mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
  181. return z;
  182. }
  183. int
  184. scm_i_bigcmp (SCM x, SCM y)
  185. {
  186. /* Return neg if x < y, pos if x > y, and 0 if x == y */
  187. /* presume we already know x and y are bignums */
  188. int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  189. scm_remember_upto_here_2 (x, y);
  190. return result;
  191. }
  192. SCM
  193. scm_i_dbl2big (double d)
  194. {
  195. /* results are only defined if d is an integer */
  196. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  197. mpz_init_set_d (SCM_I_BIG_MPZ (z), d);
  198. return z;
  199. }
  200. /* Convert a integer in double representation to a SCM number. */
  201. SCM
  202. scm_i_dbl2num (double u)
  203. {
  204. /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both
  205. powers of 2, so there's no rounding when making "double" values
  206. from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could
  207. get rounded on a 64-bit machine, hence the "+1".
  208. The use of floor() to force to an integer value ensures we get a
  209. "numerically closest" value without depending on how a
  210. double->long cast or how mpz_set_d will round. For reference,
  211. double->long probably follows the hardware rounding mode,
  212. mpz_set_d truncates towards zero. */
  213. /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not
  214. representable as a double? */
  215. if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
  216. && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
  217. return SCM_I_MAKINUM ((long) u);
  218. else
  219. return scm_i_dbl2big (u);
  220. }
  221. /* scm_i_big2dbl() rounds to the closest representable double, in accordance
  222. with R5RS exact->inexact.
  223. The approach is to use mpz_get_d to pick out the high DBL_MANT_DIG bits
  224. (ie. truncate towards zero), then adjust to get the closest double by
  225. examining the next lower bit and adding 1 (to the absolute value) if
  226. necessary.
  227. Bignums exactly half way between representable doubles are rounded to the
  228. next higher absolute value (ie. away from zero). This seems like an
  229. adequate interpretation of R5RS "numerically closest", and it's easier
  230. and faster than a full "nearest-even" style.
  231. The bit test must be done on the absolute value of the mpz_t, which means
  232. we need to use mpz_getlimbn. mpz_tstbit is not right, it treats
  233. negatives as twos complement.
  234. In current gmp 4.1.3, mpz_get_d rounding is unspecified. It ends up
  235. following the hardware rounding mode, but applied to the absolute value
  236. of the mpz_t operand. This is not what we want so we put the high
  237. DBL_MANT_DIG bits into a temporary. In some future gmp, don't know when,
  238. mpz_get_d is supposed to always truncate towards zero.
  239. ENHANCE-ME: The temporary init+clear to force the rounding in gmp 4.1.3
  240. is a slowdown. It'd be faster to pick out the relevant high bits with
  241. mpz_getlimbn if we could be bothered coding that, and if the new
  242. truncating gmp doesn't come out. */
  243. double
  244. scm_i_big2dbl (SCM b)
  245. {
  246. double result;
  247. size_t bits;
  248. bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2);
  249. #if 1
  250. {
  251. /* Current GMP, eg. 4.1.3, force truncation towards zero */
  252. mpz_t tmp;
  253. if (bits > DBL_MANT_DIG)
  254. {
  255. size_t shift = bits - DBL_MANT_DIG;
  256. mpz_init2 (tmp, DBL_MANT_DIG);
  257. mpz_tdiv_q_2exp (tmp, SCM_I_BIG_MPZ (b), shift);
  258. result = ldexp (mpz_get_d (tmp), shift);
  259. mpz_clear (tmp);
  260. }
  261. else
  262. {
  263. result = mpz_get_d (SCM_I_BIG_MPZ (b));
  264. }
  265. }
  266. #else
  267. /* Future GMP */
  268. result = mpz_get_d (SCM_I_BIG_MPZ (b));
  269. #endif
  270. if (bits > DBL_MANT_DIG)
  271. {
  272. unsigned long pos = bits - DBL_MANT_DIG - 1;
  273. /* test bit number "pos" in absolute value */
  274. if (mpz_getlimbn (SCM_I_BIG_MPZ (b), pos / GMP_NUMB_BITS)
  275. & ((mp_limb_t) 1 << (pos % GMP_NUMB_BITS)))
  276. {
  277. result += ldexp ((double) mpz_sgn (SCM_I_BIG_MPZ (b)), pos + 1);
  278. }
  279. }
  280. scm_remember_upto_here_1 (b);
  281. return result;
  282. }
  283. SCM
  284. scm_i_normbig (SCM b)
  285. {
  286. /* convert a big back to a fixnum if it'll fit */
  287. /* presume b is a bignum */
  288. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b)))
  289. {
  290. long val = mpz_get_si (SCM_I_BIG_MPZ (b));
  291. if (SCM_FIXABLE (val))
  292. b = SCM_I_MAKINUM (val);
  293. }
  294. return b;
  295. }
  296. static SCM_C_INLINE_KEYWORD SCM
  297. scm_i_mpz2num (mpz_t b)
  298. {
  299. /* convert a mpz number to a SCM number. */
  300. if (mpz_fits_slong_p (b))
  301. {
  302. long val = mpz_get_si (b);
  303. if (SCM_FIXABLE (val))
  304. return SCM_I_MAKINUM (val);
  305. }
  306. {
  307. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  308. mpz_init_set (SCM_I_BIG_MPZ (z), b);
  309. return z;
  310. }
  311. }
  312. /* this is needed when we want scm_divide to make a float, not a ratio, even if passed two ints */
  313. static SCM scm_divide2real (SCM x, SCM y);
  314. static SCM
  315. scm_i_make_ratio (SCM numerator, SCM denominator)
  316. #define FUNC_NAME "make-ratio"
  317. {
  318. /* First make sure the arguments are proper.
  319. */
  320. if (SCM_I_INUMP (denominator))
  321. {
  322. if (scm_is_eq (denominator, SCM_INUM0))
  323. scm_num_overflow ("make-ratio");
  324. if (scm_is_eq (denominator, SCM_I_MAKINUM(1)))
  325. return numerator;
  326. }
  327. else
  328. {
  329. if (!(SCM_BIGP(denominator)))
  330. SCM_WRONG_TYPE_ARG (2, denominator);
  331. }
  332. if (!SCM_I_INUMP (numerator) && !SCM_BIGP (numerator))
  333. SCM_WRONG_TYPE_ARG (1, numerator);
  334. /* Then flip signs so that the denominator is positive.
  335. */
  336. if (scm_is_true (scm_negative_p (denominator)))
  337. {
  338. numerator = scm_difference (numerator, SCM_UNDEFINED);
  339. denominator = scm_difference (denominator, SCM_UNDEFINED);
  340. }
  341. /* Now consider for each of the four fixnum/bignum combinations
  342. whether the rational number is really an integer.
  343. */
  344. if (SCM_I_INUMP (numerator))
  345. {
  346. long x = SCM_I_INUM (numerator);
  347. if (scm_is_eq (numerator, SCM_INUM0))
  348. return SCM_INUM0;
  349. if (SCM_I_INUMP (denominator))
  350. {
  351. long y;
  352. y = SCM_I_INUM (denominator);
  353. if (x == y)
  354. return SCM_I_MAKINUM(1);
  355. if ((x % y) == 0)
  356. return SCM_I_MAKINUM (x / y);
  357. }
  358. else
  359. {
  360. /* When x == SCM_MOST_NEGATIVE_FIXNUM we could have the negative
  361. of that value for the denominator, as a bignum. Apart from
  362. that case, abs(bignum) > abs(inum) so inum/bignum is not an
  363. integer. */
  364. if (x == SCM_MOST_NEGATIVE_FIXNUM
  365. && mpz_cmp_ui (SCM_I_BIG_MPZ (denominator),
  366. - SCM_MOST_NEGATIVE_FIXNUM) == 0)
  367. return SCM_I_MAKINUM(-1);
  368. }
  369. }
  370. else if (SCM_BIGP (numerator))
  371. {
  372. if (SCM_I_INUMP (denominator))
  373. {
  374. long yy = SCM_I_INUM (denominator);
  375. if (mpz_divisible_ui_p (SCM_I_BIG_MPZ (numerator), yy))
  376. return scm_divide (numerator, denominator);
  377. }
  378. else
  379. {
  380. if (scm_is_eq (numerator, denominator))
  381. return SCM_I_MAKINUM(1);
  382. if (mpz_divisible_p (SCM_I_BIG_MPZ (numerator),
  383. SCM_I_BIG_MPZ (denominator)))
  384. return scm_divide(numerator, denominator);
  385. }
  386. }
  387. /* No, it's a proper fraction.
  388. */
  389. {
  390. SCM divisor = scm_gcd (numerator, denominator);
  391. if (!(scm_is_eq (divisor, SCM_I_MAKINUM(1))))
  392. {
  393. numerator = scm_divide (numerator, divisor);
  394. denominator = scm_divide (denominator, divisor);
  395. }
  396. return scm_double_cell (scm_tc16_fraction,
  397. SCM_UNPACK (numerator),
  398. SCM_UNPACK (denominator), 0);
  399. }
  400. }
  401. #undef FUNC_NAME
  402. double
  403. scm_i_fraction2double (SCM z)
  404. {
  405. return scm_to_double (scm_divide2real (SCM_FRACTION_NUMERATOR (z),
  406. SCM_FRACTION_DENOMINATOR (z)));
  407. }
  408. SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0,
  409. (SCM x),
  410. "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
  411. "otherwise.")
  412. #define FUNC_NAME s_scm_exact_p
  413. {
  414. if (SCM_I_INUMP (x))
  415. return SCM_BOOL_T;
  416. if (SCM_BIGP (x))
  417. return SCM_BOOL_T;
  418. if (SCM_FRACTIONP (x))
  419. return SCM_BOOL_T;
  420. if (SCM_NUMBERP (x))
  421. return SCM_BOOL_F;
  422. SCM_WRONG_TYPE_ARG (1, x);
  423. }
  424. #undef FUNC_NAME
  425. SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0,
  426. (SCM n),
  427. "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
  428. "otherwise.")
  429. #define FUNC_NAME s_scm_odd_p
  430. {
  431. if (SCM_I_INUMP (n))
  432. {
  433. long val = SCM_I_INUM (n);
  434. return scm_from_bool ((val & 1L) != 0);
  435. }
  436. else if (SCM_BIGP (n))
  437. {
  438. int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n));
  439. scm_remember_upto_here_1 (n);
  440. return scm_from_bool (odd_p);
  441. }
  442. else if (scm_is_true (scm_inf_p (n)))
  443. return SCM_BOOL_T;
  444. else if (SCM_REALP (n))
  445. {
  446. double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0));
  447. if (rem == 1.0)
  448. return SCM_BOOL_T;
  449. else if (rem == 0.0)
  450. return SCM_BOOL_F;
  451. else
  452. SCM_WRONG_TYPE_ARG (1, n);
  453. }
  454. else
  455. SCM_WRONG_TYPE_ARG (1, n);
  456. }
  457. #undef FUNC_NAME
  458. SCM_DEFINE (scm_even_p, "even?", 1, 0, 0,
  459. (SCM n),
  460. "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
  461. "otherwise.")
  462. #define FUNC_NAME s_scm_even_p
  463. {
  464. if (SCM_I_INUMP (n))
  465. {
  466. long val = SCM_I_INUM (n);
  467. return scm_from_bool ((val & 1L) == 0);
  468. }
  469. else if (SCM_BIGP (n))
  470. {
  471. int even_p = mpz_even_p (SCM_I_BIG_MPZ (n));
  472. scm_remember_upto_here_1 (n);
  473. return scm_from_bool (even_p);
  474. }
  475. else if (scm_is_true (scm_inf_p (n)))
  476. return SCM_BOOL_T;
  477. else if (SCM_REALP (n))
  478. {
  479. double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0));
  480. if (rem == 1.0)
  481. return SCM_BOOL_F;
  482. else if (rem == 0.0)
  483. return SCM_BOOL_T;
  484. else
  485. SCM_WRONG_TYPE_ARG (1, n);
  486. }
  487. else
  488. SCM_WRONG_TYPE_ARG (1, n);
  489. }
  490. #undef FUNC_NAME
  491. SCM_DEFINE (scm_inf_p, "inf?", 1, 0, 0,
  492. (SCM x),
  493. "Return @code{#t} if @var{x} is either @samp{+inf.0}\n"
  494. "or @samp{-inf.0}, @code{#f} otherwise.")
  495. #define FUNC_NAME s_scm_inf_p
  496. {
  497. if (SCM_REALP (x))
  498. return scm_from_bool (xisinf (SCM_REAL_VALUE (x)));
  499. else if (SCM_COMPLEXP (x))
  500. return scm_from_bool (xisinf (SCM_COMPLEX_REAL (x))
  501. || xisinf (SCM_COMPLEX_IMAG (x)));
  502. else
  503. return SCM_BOOL_F;
  504. }
  505. #undef FUNC_NAME
  506. SCM_DEFINE (scm_nan_p, "nan?", 1, 0, 0,
  507. (SCM n),
  508. "Return @code{#t} if @var{n} is a NaN, @code{#f}\n"
  509. "otherwise.")
  510. #define FUNC_NAME s_scm_nan_p
  511. {
  512. if (SCM_REALP (n))
  513. return scm_from_bool (xisnan (SCM_REAL_VALUE (n)));
  514. else if (SCM_COMPLEXP (n))
  515. return scm_from_bool (xisnan (SCM_COMPLEX_REAL (n))
  516. || xisnan (SCM_COMPLEX_IMAG (n)));
  517. else
  518. return SCM_BOOL_F;
  519. }
  520. #undef FUNC_NAME
  521. /* Guile's idea of infinity. */
  522. static double guile_Inf;
  523. /* Guile's idea of not a number. */
  524. static double guile_NaN;
  525. static void
  526. guile_ieee_init (void)
  527. {
  528. #if defined (HAVE_ISINF) || defined (HAVE_FINITE)
  529. /* Some version of gcc on some old version of Linux used to crash when
  530. trying to make Inf and NaN. */
  531. #ifdef INFINITY
  532. /* C99 INFINITY, when available.
  533. FIXME: The standard allows for INFINITY to be something that overflows
  534. at compile time. We ought to have a configure test to check for that
  535. before trying to use it. (But in practice we believe this is not a
  536. problem on any system guile is likely to target.) */
  537. guile_Inf = INFINITY;
  538. #elif HAVE_DINFINITY
  539. /* OSF */
  540. extern unsigned int DINFINITY[2];
  541. union
  542. {
  543. double d;
  544. int i[2];
  545. } alias;
  546. alias.i[0] = DINFINITY[0];
  547. alias.i[1] = DINFINITY[1];
  548. guile_Inf = alias.d;
  549. #else
  550. double tmp = 1e+10;
  551. guile_Inf = tmp;
  552. for (;;)
  553. {
  554. guile_Inf *= 1e+10;
  555. if (guile_Inf == tmp)
  556. break;
  557. tmp = guile_Inf;
  558. }
  559. #endif
  560. #endif
  561. #if defined (HAVE_ISNAN)
  562. #if defined __GNUC__ && defined __alpha__ && !defined _IEEE_FP
  563. /* On Alpha GCC must be passed `-mieee' to provide proper NaN handling.
  564. See http://lists.gnu.org/archive/html/bug-gnulib/2009-05/msg00010.html
  565. for more details. */
  566. # error NaN handling will not work when compiling without -mieee
  567. #endif
  568. #ifdef NAN
  569. /* C99 NAN, when available */
  570. guile_NaN = NAN;
  571. #elif HAVE_DQNAN
  572. {
  573. /* OSF */
  574. extern unsigned int DQNAN[2];
  575. union
  576. {
  577. double d;
  578. int i[2];
  579. } alias;
  580. alias.i[0] = DQNAN[0];
  581. alias.i[1] = DQNAN[1];
  582. guile_NaN = alias.d;
  583. }
  584. #else
  585. guile_NaN = guile_Inf / guile_Inf;
  586. #endif
  587. #endif
  588. }
  589. SCM_DEFINE (scm_inf, "inf", 0, 0, 0,
  590. (void),
  591. "Return Inf.")
  592. #define FUNC_NAME s_scm_inf
  593. {
  594. static int initialized = 0;
  595. if (! initialized)
  596. {
  597. guile_ieee_init ();
  598. initialized = 1;
  599. }
  600. return scm_from_double (guile_Inf);
  601. }
  602. #undef FUNC_NAME
  603. SCM_DEFINE (scm_nan, "nan", 0, 0, 0,
  604. (void),
  605. "Return NaN.")
  606. #define FUNC_NAME s_scm_nan
  607. {
  608. static int initialized = 0;
  609. if (!initialized)
  610. {
  611. guile_ieee_init ();
  612. initialized = 1;
  613. }
  614. return scm_from_double (guile_NaN);
  615. }
  616. #undef FUNC_NAME
  617. SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
  618. (SCM x),
  619. "Return the absolute value of @var{x}.")
  620. #define FUNC_NAME
  621. {
  622. if (SCM_I_INUMP (x))
  623. {
  624. long int xx = SCM_I_INUM (x);
  625. if (xx >= 0)
  626. return x;
  627. else if (SCM_POSFIXABLE (-xx))
  628. return SCM_I_MAKINUM (-xx);
  629. else
  630. return scm_i_long2big (-xx);
  631. }
  632. else if (SCM_BIGP (x))
  633. {
  634. const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  635. if (sgn < 0)
  636. return scm_i_clonebig (x, 0);
  637. else
  638. return x;
  639. }
  640. else if (SCM_REALP (x))
  641. {
  642. /* note that if x is a NaN then xx<0 is false so we return x unchanged */
  643. double xx = SCM_REAL_VALUE (x);
  644. if (xx < 0.0)
  645. return scm_from_double (-xx);
  646. else
  647. return x;
  648. }
  649. else if (SCM_FRACTIONP (x))
  650. {
  651. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
  652. return x;
  653. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  654. SCM_FRACTION_DENOMINATOR (x));
  655. }
  656. else
  657. SCM_WTA_DISPATCH_1 (g_scm_abs, x, 1, s_scm_abs);
  658. }
  659. #undef FUNC_NAME
  660. SCM_GPROC (s_quotient, "quotient", 2, 0, 0, scm_quotient, g_quotient);
  661. /* "Return the quotient of the numbers @var{x} and @var{y}."
  662. */
  663. SCM
  664. scm_quotient (SCM x, SCM y)
  665. {
  666. if (SCM_I_INUMP (x))
  667. {
  668. long xx = SCM_I_INUM (x);
  669. if (SCM_I_INUMP (y))
  670. {
  671. long yy = SCM_I_INUM (y);
  672. if (yy == 0)
  673. scm_num_overflow (s_quotient);
  674. else
  675. {
  676. long z = xx / yy;
  677. if (SCM_FIXABLE (z))
  678. return SCM_I_MAKINUM (z);
  679. else
  680. return scm_i_long2big (z);
  681. }
  682. }
  683. else if (SCM_BIGP (y))
  684. {
  685. if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM)
  686. && (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  687. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  688. {
  689. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  690. scm_remember_upto_here_1 (y);
  691. return SCM_I_MAKINUM (-1);
  692. }
  693. else
  694. return SCM_I_MAKINUM (0);
  695. }
  696. else
  697. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
  698. }
  699. else if (SCM_BIGP (x))
  700. {
  701. if (SCM_I_INUMP (y))
  702. {
  703. long yy = SCM_I_INUM (y);
  704. if (yy == 0)
  705. scm_num_overflow (s_quotient);
  706. else if (yy == 1)
  707. return x;
  708. else
  709. {
  710. SCM result = scm_i_mkbig ();
  711. if (yy < 0)
  712. {
  713. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result),
  714. SCM_I_BIG_MPZ (x),
  715. - yy);
  716. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  717. }
  718. else
  719. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  720. scm_remember_upto_here_1 (x);
  721. return scm_i_normbig (result);
  722. }
  723. }
  724. else if (SCM_BIGP (y))
  725. {
  726. SCM result = scm_i_mkbig ();
  727. mpz_tdiv_q (SCM_I_BIG_MPZ (result),
  728. SCM_I_BIG_MPZ (x),
  729. SCM_I_BIG_MPZ (y));
  730. scm_remember_upto_here_2 (x, y);
  731. return scm_i_normbig (result);
  732. }
  733. else
  734. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
  735. }
  736. else
  737. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG1, s_quotient);
  738. }
  739. SCM_GPROC (s_remainder, "remainder", 2, 0, 0, scm_remainder, g_remainder);
  740. /* "Return the remainder of the numbers @var{x} and @var{y}.\n"
  741. * "@lisp\n"
  742. * "(remainder 13 4) @result{} 1\n"
  743. * "(remainder -13 4) @result{} -1\n"
  744. * "@end lisp"
  745. */
  746. SCM
  747. scm_remainder (SCM x, SCM y)
  748. {
  749. if (SCM_I_INUMP (x))
  750. {
  751. if (SCM_I_INUMP (y))
  752. {
  753. long yy = SCM_I_INUM (y);
  754. if (yy == 0)
  755. scm_num_overflow (s_remainder);
  756. else
  757. {
  758. long z = SCM_I_INUM (x) % yy;
  759. return SCM_I_MAKINUM (z);
  760. }
  761. }
  762. else if (SCM_BIGP (y))
  763. {
  764. if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM)
  765. && (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  766. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  767. {
  768. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  769. scm_remember_upto_here_1 (y);
  770. return SCM_I_MAKINUM (0);
  771. }
  772. else
  773. return x;
  774. }
  775. else
  776. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
  777. }
  778. else if (SCM_BIGP (x))
  779. {
  780. if (SCM_I_INUMP (y))
  781. {
  782. long yy = SCM_I_INUM (y);
  783. if (yy == 0)
  784. scm_num_overflow (s_remainder);
  785. else
  786. {
  787. SCM result = scm_i_mkbig ();
  788. if (yy < 0)
  789. yy = - yy;
  790. mpz_tdiv_r_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ(x), yy);
  791. scm_remember_upto_here_1 (x);
  792. return scm_i_normbig (result);
  793. }
  794. }
  795. else if (SCM_BIGP (y))
  796. {
  797. SCM result = scm_i_mkbig ();
  798. mpz_tdiv_r (SCM_I_BIG_MPZ (result),
  799. SCM_I_BIG_MPZ (x),
  800. SCM_I_BIG_MPZ (y));
  801. scm_remember_upto_here_2 (x, y);
  802. return scm_i_normbig (result);
  803. }
  804. else
  805. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
  806. }
  807. else
  808. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG1, s_remainder);
  809. }
  810. SCM_GPROC (s_modulo, "modulo", 2, 0, 0, scm_modulo, g_modulo);
  811. /* "Return the modulo of the numbers @var{x} and @var{y}.\n"
  812. * "@lisp\n"
  813. * "(modulo 13 4) @result{} 1\n"
  814. * "(modulo -13 4) @result{} 3\n"
  815. * "@end lisp"
  816. */
  817. SCM
  818. scm_modulo (SCM x, SCM y)
  819. {
  820. if (SCM_I_INUMP (x))
  821. {
  822. long xx = SCM_I_INUM (x);
  823. if (SCM_I_INUMP (y))
  824. {
  825. long yy = SCM_I_INUM (y);
  826. if (yy == 0)
  827. scm_num_overflow (s_modulo);
  828. else
  829. {
  830. /* C99 specifies that "%" is the remainder corresponding to a
  831. quotient rounded towards zero, and that's also traditional
  832. for machine division, so z here should be well defined. */
  833. long z = xx % yy;
  834. long result;
  835. if (yy < 0)
  836. {
  837. if (z > 0)
  838. result = z + yy;
  839. else
  840. result = z;
  841. }
  842. else
  843. {
  844. if (z < 0)
  845. result = z + yy;
  846. else
  847. result = z;
  848. }
  849. return SCM_I_MAKINUM (result);
  850. }
  851. }
  852. else if (SCM_BIGP (y))
  853. {
  854. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  855. {
  856. mpz_t z_x;
  857. SCM result;
  858. if (sgn_y < 0)
  859. {
  860. SCM pos_y = scm_i_clonebig (y, 0);
  861. /* do this after the last scm_op */
  862. mpz_init_set_si (z_x, xx);
  863. result = pos_y; /* re-use this bignum */
  864. mpz_mod (SCM_I_BIG_MPZ (result),
  865. z_x,
  866. SCM_I_BIG_MPZ (pos_y));
  867. scm_remember_upto_here_1 (pos_y);
  868. }
  869. else
  870. {
  871. result = scm_i_mkbig ();
  872. /* do this after the last scm_op */
  873. mpz_init_set_si (z_x, xx);
  874. mpz_mod (SCM_I_BIG_MPZ (result),
  875. z_x,
  876. SCM_I_BIG_MPZ (y));
  877. scm_remember_upto_here_1 (y);
  878. }
  879. if ((sgn_y < 0) && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
  880. mpz_add (SCM_I_BIG_MPZ (result),
  881. SCM_I_BIG_MPZ (y),
  882. SCM_I_BIG_MPZ (result));
  883. scm_remember_upto_here_1 (y);
  884. /* and do this before the next one */
  885. mpz_clear (z_x);
  886. return scm_i_normbig (result);
  887. }
  888. }
  889. else
  890. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
  891. }
  892. else if (SCM_BIGP (x))
  893. {
  894. if (SCM_I_INUMP (y))
  895. {
  896. long yy = SCM_I_INUM (y);
  897. if (yy == 0)
  898. scm_num_overflow (s_modulo);
  899. else
  900. {
  901. SCM result = scm_i_mkbig ();
  902. mpz_mod_ui (SCM_I_BIG_MPZ (result),
  903. SCM_I_BIG_MPZ (x),
  904. (yy < 0) ? - yy : yy);
  905. scm_remember_upto_here_1 (x);
  906. if ((yy < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0))
  907. mpz_sub_ui (SCM_I_BIG_MPZ (result),
  908. SCM_I_BIG_MPZ (result),
  909. - yy);
  910. return scm_i_normbig (result);
  911. }
  912. }
  913. else if (SCM_BIGP (y))
  914. {
  915. {
  916. SCM result = scm_i_mkbig ();
  917. int y_sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  918. SCM pos_y = scm_i_clonebig (y, y_sgn >= 0);
  919. mpz_mod (SCM_I_BIG_MPZ (result),
  920. SCM_I_BIG_MPZ (x),
  921. SCM_I_BIG_MPZ (pos_y));
  922. scm_remember_upto_here_1 (x);
  923. if ((y_sgn < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0))
  924. mpz_add (SCM_I_BIG_MPZ (result),
  925. SCM_I_BIG_MPZ (y),
  926. SCM_I_BIG_MPZ (result));
  927. scm_remember_upto_here_2 (y, pos_y);
  928. return scm_i_normbig (result);
  929. }
  930. }
  931. else
  932. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
  933. }
  934. else
  935. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG1, s_modulo);
  936. }
  937. SCM_GPROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd, g_gcd);
  938. /* "Return the greatest common divisor of all arguments.\n"
  939. * "If called without arguments, 0 is returned."
  940. */
  941. SCM
  942. scm_gcd (SCM x, SCM y)
  943. {
  944. if (SCM_UNBNDP (y))
  945. return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
  946. if (SCM_I_INUMP (x))
  947. {
  948. if (SCM_I_INUMP (y))
  949. {
  950. long xx = SCM_I_INUM (x);
  951. long yy = SCM_I_INUM (y);
  952. long u = xx < 0 ? -xx : xx;
  953. long v = yy < 0 ? -yy : yy;
  954. long result;
  955. if (xx == 0)
  956. result = v;
  957. else if (yy == 0)
  958. result = u;
  959. else
  960. {
  961. long k = 1;
  962. long t;
  963. /* Determine a common factor 2^k */
  964. while (!(1 & (u | v)))
  965. {
  966. k <<= 1;
  967. u >>= 1;
  968. v >>= 1;
  969. }
  970. /* Now, any factor 2^n can be eliminated */
  971. if (u & 1)
  972. t = -v;
  973. else
  974. {
  975. t = u;
  976. b3:
  977. t = SCM_SRS (t, 1);
  978. }
  979. if (!(1 & t))
  980. goto b3;
  981. if (t > 0)
  982. u = t;
  983. else
  984. v = -t;
  985. t = u - v;
  986. if (t != 0)
  987. goto b3;
  988. result = u * k;
  989. }
  990. return (SCM_POSFIXABLE (result)
  991. ? SCM_I_MAKINUM (result)
  992. : scm_i_long2big (result));
  993. }
  994. else if (SCM_BIGP (y))
  995. {
  996. SCM_SWAP (x, y);
  997. goto big_inum;
  998. }
  999. else
  1000. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  1001. }
  1002. else if (SCM_BIGP (x))
  1003. {
  1004. if (SCM_I_INUMP (y))
  1005. {
  1006. unsigned long result;
  1007. long yy;
  1008. big_inum:
  1009. yy = SCM_I_INUM (y);
  1010. if (yy == 0)
  1011. return scm_abs (x);
  1012. if (yy < 0)
  1013. yy = -yy;
  1014. result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy);
  1015. scm_remember_upto_here_1 (x);
  1016. return (SCM_POSFIXABLE (result)
  1017. ? SCM_I_MAKINUM (result)
  1018. : scm_from_ulong (result));
  1019. }
  1020. else if (SCM_BIGP (y))
  1021. {
  1022. SCM result = scm_i_mkbig ();
  1023. mpz_gcd (SCM_I_BIG_MPZ (result),
  1024. SCM_I_BIG_MPZ (x),
  1025. SCM_I_BIG_MPZ (y));
  1026. scm_remember_upto_here_2 (x, y);
  1027. return scm_i_normbig (result);
  1028. }
  1029. else
  1030. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  1031. }
  1032. else
  1033. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
  1034. }
  1035. SCM_GPROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm, g_lcm);
  1036. /* "Return the least common multiple of the arguments.\n"
  1037. * "If called without arguments, 1 is returned."
  1038. */
  1039. SCM
  1040. scm_lcm (SCM n1, SCM n2)
  1041. {
  1042. if (SCM_UNBNDP (n2))
  1043. {
  1044. if (SCM_UNBNDP (n1))
  1045. return SCM_I_MAKINUM (1L);
  1046. n2 = SCM_I_MAKINUM (1L);
  1047. }
  1048. SCM_GASSERT2 (SCM_I_INUMP (n1) || SCM_BIGP (n1),
  1049. g_lcm, n1, n2, SCM_ARG1, s_lcm);
  1050. SCM_GASSERT2 (SCM_I_INUMP (n2) || SCM_BIGP (n2),
  1051. g_lcm, n1, n2, SCM_ARGn, s_lcm);
  1052. if (SCM_I_INUMP (n1))
  1053. {
  1054. if (SCM_I_INUMP (n2))
  1055. {
  1056. SCM d = scm_gcd (n1, n2);
  1057. if (scm_is_eq (d, SCM_INUM0))
  1058. return d;
  1059. else
  1060. return scm_abs (scm_product (n1, scm_quotient (n2, d)));
  1061. }
  1062. else
  1063. {
  1064. /* inum n1, big n2 */
  1065. inumbig:
  1066. {
  1067. SCM result = scm_i_mkbig ();
  1068. long nn1 = SCM_I_INUM (n1);
  1069. if (nn1 == 0) return SCM_INUM0;
  1070. if (nn1 < 0) nn1 = - nn1;
  1071. mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1);
  1072. scm_remember_upto_here_1 (n2);
  1073. return result;
  1074. }
  1075. }
  1076. }
  1077. else
  1078. {
  1079. /* big n1 */
  1080. if (SCM_I_INUMP (n2))
  1081. {
  1082. SCM_SWAP (n1, n2);
  1083. goto inumbig;
  1084. }
  1085. else
  1086. {
  1087. SCM result = scm_i_mkbig ();
  1088. mpz_lcm(SCM_I_BIG_MPZ (result),
  1089. SCM_I_BIG_MPZ (n1),
  1090. SCM_I_BIG_MPZ (n2));
  1091. scm_remember_upto_here_2(n1, n2);
  1092. /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
  1093. return result;
  1094. }
  1095. }
  1096. }
  1097. /* Emulating 2's complement bignums with sign magnitude arithmetic:
  1098. Logand:
  1099. X Y Result Method:
  1100. (len)
  1101. + + + x (map digit:logand X Y)
  1102. + - + x (map digit:logand X (lognot (+ -1 Y)))
  1103. - + + y (map digit:logand (lognot (+ -1 X)) Y)
  1104. - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
  1105. Logior:
  1106. X Y Result Method:
  1107. + + + (map digit:logior X Y)
  1108. + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
  1109. - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
  1110. - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
  1111. Logxor:
  1112. X Y Result Method:
  1113. + + + (map digit:logxor X Y)
  1114. + - - (+ 1 (map digit:logxor X (+ -1 Y)))
  1115. - + - (+ 1 (map digit:logxor (+ -1 X) Y))
  1116. - - + (map digit:logxor (+ -1 X) (+ -1 Y))
  1117. Logtest:
  1118. X Y Result
  1119. + + (any digit:logand X Y)
  1120. + - (any digit:logand X (lognot (+ -1 Y)))
  1121. - + (any digit:logand (lognot (+ -1 X)) Y)
  1122. - - #t
  1123. */
  1124. SCM_DEFINE1 (scm_logand, "logand", scm_tc7_asubr,
  1125. (SCM n1, SCM n2),
  1126. "Return the bitwise AND of the integer arguments.\n\n"
  1127. "@lisp\n"
  1128. "(logand) @result{} -1\n"
  1129. "(logand 7) @result{} 7\n"
  1130. "(logand #b111 #b011 #b001) @result{} 1\n"
  1131. "@end lisp")
  1132. #define FUNC_NAME s_scm_logand
  1133. {
  1134. long int nn1;
  1135. if (SCM_UNBNDP (n2))
  1136. {
  1137. if (SCM_UNBNDP (n1))
  1138. return SCM_I_MAKINUM (-1);
  1139. else if (!SCM_NUMBERP (n1))
  1140. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1141. else if (SCM_NUMBERP (n1))
  1142. return n1;
  1143. else
  1144. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1145. }
  1146. if (SCM_I_INUMP (n1))
  1147. {
  1148. nn1 = SCM_I_INUM (n1);
  1149. if (SCM_I_INUMP (n2))
  1150. {
  1151. long nn2 = SCM_I_INUM (n2);
  1152. return SCM_I_MAKINUM (nn1 & nn2);
  1153. }
  1154. else if SCM_BIGP (n2)
  1155. {
  1156. intbig:
  1157. if (n1 == 0)
  1158. return SCM_INUM0;
  1159. {
  1160. SCM result_z = scm_i_mkbig ();
  1161. mpz_t nn1_z;
  1162. mpz_init_set_si (nn1_z, nn1);
  1163. mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1164. scm_remember_upto_here_1 (n2);
  1165. mpz_clear (nn1_z);
  1166. return scm_i_normbig (result_z);
  1167. }
  1168. }
  1169. else
  1170. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1171. }
  1172. else if (SCM_BIGP (n1))
  1173. {
  1174. if (SCM_I_INUMP (n2))
  1175. {
  1176. SCM_SWAP (n1, n2);
  1177. nn1 = SCM_I_INUM (n1);
  1178. goto intbig;
  1179. }
  1180. else if (SCM_BIGP (n2))
  1181. {
  1182. SCM result_z = scm_i_mkbig ();
  1183. mpz_and (SCM_I_BIG_MPZ (result_z),
  1184. SCM_I_BIG_MPZ (n1),
  1185. SCM_I_BIG_MPZ (n2));
  1186. scm_remember_upto_here_2 (n1, n2);
  1187. return scm_i_normbig (result_z);
  1188. }
  1189. else
  1190. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1191. }
  1192. else
  1193. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1194. }
  1195. #undef FUNC_NAME
  1196. SCM_DEFINE1 (scm_logior, "logior", scm_tc7_asubr,
  1197. (SCM n1, SCM n2),
  1198. "Return the bitwise OR of the integer arguments.\n\n"
  1199. "@lisp\n"
  1200. "(logior) @result{} 0\n"
  1201. "(logior 7) @result{} 7\n"
  1202. "(logior #b000 #b001 #b011) @result{} 3\n"
  1203. "@end lisp")
  1204. #define FUNC_NAME s_scm_logior
  1205. {
  1206. long int nn1;
  1207. if (SCM_UNBNDP (n2))
  1208. {
  1209. if (SCM_UNBNDP (n1))
  1210. return SCM_INUM0;
  1211. else if (SCM_NUMBERP (n1))
  1212. return n1;
  1213. else
  1214. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1215. }
  1216. if (SCM_I_INUMP (n1))
  1217. {
  1218. nn1 = SCM_I_INUM (n1);
  1219. if (SCM_I_INUMP (n2))
  1220. {
  1221. long nn2 = SCM_I_INUM (n2);
  1222. return SCM_I_MAKINUM (nn1 | nn2);
  1223. }
  1224. else if (SCM_BIGP (n2))
  1225. {
  1226. intbig:
  1227. if (nn1 == 0)
  1228. return n2;
  1229. {
  1230. SCM result_z = scm_i_mkbig ();
  1231. mpz_t nn1_z;
  1232. mpz_init_set_si (nn1_z, nn1);
  1233. mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1234. scm_remember_upto_here_1 (n2);
  1235. mpz_clear (nn1_z);
  1236. return scm_i_normbig (result_z);
  1237. }
  1238. }
  1239. else
  1240. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1241. }
  1242. else if (SCM_BIGP (n1))
  1243. {
  1244. if (SCM_I_INUMP (n2))
  1245. {
  1246. SCM_SWAP (n1, n2);
  1247. nn1 = SCM_I_INUM (n1);
  1248. goto intbig;
  1249. }
  1250. else if (SCM_BIGP (n2))
  1251. {
  1252. SCM result_z = scm_i_mkbig ();
  1253. mpz_ior (SCM_I_BIG_MPZ (result_z),
  1254. SCM_I_BIG_MPZ (n1),
  1255. SCM_I_BIG_MPZ (n2));
  1256. scm_remember_upto_here_2 (n1, n2);
  1257. return scm_i_normbig (result_z);
  1258. }
  1259. else
  1260. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1261. }
  1262. else
  1263. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1264. }
  1265. #undef FUNC_NAME
  1266. SCM_DEFINE1 (scm_logxor, "logxor", scm_tc7_asubr,
  1267. (SCM n1, SCM n2),
  1268. "Return the bitwise XOR of the integer arguments. A bit is\n"
  1269. "set in the result if it is set in an odd number of arguments.\n"
  1270. "@lisp\n"
  1271. "(logxor) @result{} 0\n"
  1272. "(logxor 7) @result{} 7\n"
  1273. "(logxor #b000 #b001 #b011) @result{} 2\n"
  1274. "(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
  1275. "@end lisp")
  1276. #define FUNC_NAME s_scm_logxor
  1277. {
  1278. long int nn1;
  1279. if (SCM_UNBNDP (n2))
  1280. {
  1281. if (SCM_UNBNDP (n1))
  1282. return SCM_INUM0;
  1283. else if (SCM_NUMBERP (n1))
  1284. return n1;
  1285. else
  1286. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1287. }
  1288. if (SCM_I_INUMP (n1))
  1289. {
  1290. nn1 = SCM_I_INUM (n1);
  1291. if (SCM_I_INUMP (n2))
  1292. {
  1293. long nn2 = SCM_I_INUM (n2);
  1294. return SCM_I_MAKINUM (nn1 ^ nn2);
  1295. }
  1296. else if (SCM_BIGP (n2))
  1297. {
  1298. intbig:
  1299. {
  1300. SCM result_z = scm_i_mkbig ();
  1301. mpz_t nn1_z;
  1302. mpz_init_set_si (nn1_z, nn1);
  1303. mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1304. scm_remember_upto_here_1 (n2);
  1305. mpz_clear (nn1_z);
  1306. return scm_i_normbig (result_z);
  1307. }
  1308. }
  1309. else
  1310. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1311. }
  1312. else if (SCM_BIGP (n1))
  1313. {
  1314. if (SCM_I_INUMP (n2))
  1315. {
  1316. SCM_SWAP (n1, n2);
  1317. nn1 = SCM_I_INUM (n1);
  1318. goto intbig;
  1319. }
  1320. else if (SCM_BIGP (n2))
  1321. {
  1322. SCM result_z = scm_i_mkbig ();
  1323. mpz_xor (SCM_I_BIG_MPZ (result_z),
  1324. SCM_I_BIG_MPZ (n1),
  1325. SCM_I_BIG_MPZ (n2));
  1326. scm_remember_upto_here_2 (n1, n2);
  1327. return scm_i_normbig (result_z);
  1328. }
  1329. else
  1330. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1331. }
  1332. else
  1333. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1334. }
  1335. #undef FUNC_NAME
  1336. SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
  1337. (SCM j, SCM k),
  1338. "Test whether @var{j} and @var{k} have any 1 bits in common.\n"
  1339. "This is equivalent to @code{(not (zero? (logand j k)))}, but\n"
  1340. "without actually calculating the @code{logand}, just testing\n"
  1341. "for non-zero.\n"
  1342. "\n"
  1343. "@lisp\n"
  1344. "(logtest #b0100 #b1011) @result{} #f\n"
  1345. "(logtest #b0100 #b0111) @result{} #t\n"
  1346. "@end lisp")
  1347. #define FUNC_NAME s_scm_logtest
  1348. {
  1349. long int nj;
  1350. if (SCM_I_INUMP (j))
  1351. {
  1352. nj = SCM_I_INUM (j);
  1353. if (SCM_I_INUMP (k))
  1354. {
  1355. long nk = SCM_I_INUM (k);
  1356. return scm_from_bool (nj & nk);
  1357. }
  1358. else if (SCM_BIGP (k))
  1359. {
  1360. intbig:
  1361. if (nj == 0)
  1362. return SCM_BOOL_F;
  1363. {
  1364. SCM result;
  1365. mpz_t nj_z;
  1366. mpz_init_set_si (nj_z, nj);
  1367. mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k));
  1368. scm_remember_upto_here_1 (k);
  1369. result = scm_from_bool (mpz_sgn (nj_z) != 0);
  1370. mpz_clear (nj_z);
  1371. return result;
  1372. }
  1373. }
  1374. else
  1375. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  1376. }
  1377. else if (SCM_BIGP (j))
  1378. {
  1379. if (SCM_I_INUMP (k))
  1380. {
  1381. SCM_SWAP (j, k);
  1382. nj = SCM_I_INUM (j);
  1383. goto intbig;
  1384. }
  1385. else if (SCM_BIGP (k))
  1386. {
  1387. SCM result;
  1388. mpz_t result_z;
  1389. mpz_init (result_z);
  1390. mpz_and (result_z,
  1391. SCM_I_BIG_MPZ (j),
  1392. SCM_I_BIG_MPZ (k));
  1393. scm_remember_upto_here_2 (j, k);
  1394. result = scm_from_bool (mpz_sgn (result_z) != 0);
  1395. mpz_clear (result_z);
  1396. return result;
  1397. }
  1398. else
  1399. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  1400. }
  1401. else
  1402. SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
  1403. }
  1404. #undef FUNC_NAME
  1405. SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
  1406. (SCM index, SCM j),
  1407. "Test whether bit number @var{index} in @var{j} is set.\n"
  1408. "@var{index} starts from 0 for the least significant bit.\n"
  1409. "\n"
  1410. "@lisp\n"
  1411. "(logbit? 0 #b1101) @result{} #t\n"
  1412. "(logbit? 1 #b1101) @result{} #f\n"
  1413. "(logbit? 2 #b1101) @result{} #t\n"
  1414. "(logbit? 3 #b1101) @result{} #t\n"
  1415. "(logbit? 4 #b1101) @result{} #f\n"
  1416. "@end lisp")
  1417. #define FUNC_NAME s_scm_logbit_p
  1418. {
  1419. unsigned long int iindex;
  1420. iindex = scm_to_ulong (index);
  1421. if (SCM_I_INUMP (j))
  1422. {
  1423. /* bits above what's in an inum follow the sign bit */
  1424. iindex = min (iindex, SCM_LONG_BIT - 1);
  1425. return scm_from_bool ((1L << iindex) & SCM_I_INUM (j));
  1426. }
  1427. else if (SCM_BIGP (j))
  1428. {
  1429. int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
  1430. scm_remember_upto_here_1 (j);
  1431. return scm_from_bool (val);
  1432. }
  1433. else
  1434. SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
  1435. }
  1436. #undef FUNC_NAME
  1437. SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
  1438. (SCM n),
  1439. "Return the integer which is the ones-complement of the integer\n"
  1440. "argument.\n"
  1441. "\n"
  1442. "@lisp\n"
  1443. "(number->string (lognot #b10000000) 2)\n"
  1444. " @result{} \"-10000001\"\n"
  1445. "(number->string (lognot #b0) 2)\n"
  1446. " @result{} \"-1\"\n"
  1447. "@end lisp")
  1448. #define FUNC_NAME s_scm_lognot
  1449. {
  1450. if (SCM_I_INUMP (n)) {
  1451. /* No overflow here, just need to toggle all the bits making up the inum.
  1452. Enhancement: No need to strip the tag and add it back, could just xor
  1453. a block of 1 bits, if that worked with the various debug versions of
  1454. the SCM typedef. */
  1455. return SCM_I_MAKINUM (~ SCM_I_INUM (n));
  1456. } else if (SCM_BIGP (n)) {
  1457. SCM result = scm_i_mkbig ();
  1458. mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
  1459. scm_remember_upto_here_1 (n);
  1460. return result;
  1461. } else {
  1462. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1463. }
  1464. }
  1465. #undef FUNC_NAME
  1466. /* returns 0 if IN is not an integer. OUT must already be
  1467. initialized. */
  1468. static int
  1469. coerce_to_big (SCM in, mpz_t out)
  1470. {
  1471. if (SCM_BIGP (in))
  1472. mpz_set (out, SCM_I_BIG_MPZ (in));
  1473. else if (SCM_I_INUMP (in))
  1474. mpz_set_si (out, SCM_I_INUM (in));
  1475. else
  1476. return 0;
  1477. return 1;
  1478. }
  1479. SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
  1480. (SCM n, SCM k, SCM m),
  1481. "Return @var{n} raised to the integer exponent\n"
  1482. "@var{k}, modulo @var{m}.\n"
  1483. "\n"
  1484. "@lisp\n"
  1485. "(modulo-expt 2 3 5)\n"
  1486. " @result{} 3\n"
  1487. "@end lisp")
  1488. #define FUNC_NAME s_scm_modulo_expt
  1489. {
  1490. mpz_t n_tmp;
  1491. mpz_t k_tmp;
  1492. mpz_t m_tmp;
  1493. /* There are two classes of error we might encounter --
  1494. 1) Math errors, which we'll report by calling scm_num_overflow,
  1495. and
  1496. 2) wrong-type errors, which of course we'll report by calling
  1497. SCM_WRONG_TYPE_ARG.
  1498. We don't report those errors immediately, however; instead we do
  1499. some cleanup first. These variables tell us which error (if
  1500. any) we should report after cleaning up.
  1501. */
  1502. int report_overflow = 0;
  1503. int position_of_wrong_type = 0;
  1504. SCM value_of_wrong_type = SCM_INUM0;
  1505. SCM result = SCM_UNDEFINED;
  1506. mpz_init (n_tmp);
  1507. mpz_init (k_tmp);
  1508. mpz_init (m_tmp);
  1509. if (scm_is_eq (m, SCM_INUM0))
  1510. {
  1511. report_overflow = 1;
  1512. goto cleanup;
  1513. }
  1514. if (!coerce_to_big (n, n_tmp))
  1515. {
  1516. value_of_wrong_type = n;
  1517. position_of_wrong_type = 1;
  1518. goto cleanup;
  1519. }
  1520. if (!coerce_to_big (k, k_tmp))
  1521. {
  1522. value_of_wrong_type = k;
  1523. position_of_wrong_type = 2;
  1524. goto cleanup;
  1525. }
  1526. if (!coerce_to_big (m, m_tmp))
  1527. {
  1528. value_of_wrong_type = m;
  1529. position_of_wrong_type = 3;
  1530. goto cleanup;
  1531. }
  1532. /* if the exponent K is negative, and we simply call mpz_powm, we
  1533. will get a divide-by-zero exception when an inverse 1/n mod m
  1534. doesn't exist (or is not unique). Since exceptions are hard to
  1535. handle, we'll attempt the inversion "by hand" -- that way, we get
  1536. a simple failure code, which is easy to handle. */
  1537. if (-1 == mpz_sgn (k_tmp))
  1538. {
  1539. if (!mpz_invert (n_tmp, n_tmp, m_tmp))
  1540. {
  1541. report_overflow = 1;
  1542. goto cleanup;
  1543. }
  1544. mpz_neg (k_tmp, k_tmp);
  1545. }
  1546. result = scm_i_mkbig ();
  1547. mpz_powm (SCM_I_BIG_MPZ (result),
  1548. n_tmp,
  1549. k_tmp,
  1550. m_tmp);
  1551. if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
  1552. mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp);
  1553. cleanup:
  1554. mpz_clear (m_tmp);
  1555. mpz_clear (k_tmp);
  1556. mpz_clear (n_tmp);
  1557. if (report_overflow)
  1558. scm_num_overflow (FUNC_NAME);
  1559. if (position_of_wrong_type)
  1560. SCM_WRONG_TYPE_ARG (position_of_wrong_type,
  1561. value_of_wrong_type);
  1562. return scm_i_normbig (result);
  1563. }
  1564. #undef FUNC_NAME
  1565. SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
  1566. (SCM n, SCM k),
  1567. "Return @var{n} raised to the power @var{k}. @var{k} must be an\n"
  1568. "exact integer, @var{n} can be any number.\n"
  1569. "\n"
  1570. "Negative @var{k} is supported, and results in @math{1/n^abs(k)}\n"
  1571. "in the usual way. @math{@var{n}^0} is 1, as usual, and that\n"
  1572. "includes @math{0^0} is 1.\n"
  1573. "\n"
  1574. "@lisp\n"
  1575. "(integer-expt 2 5) @result{} 32\n"
  1576. "(integer-expt -3 3) @result{} -27\n"
  1577. "(integer-expt 5 -3) @result{} 1/125\n"
  1578. "(integer-expt 0 0) @result{} 1\n"
  1579. "@end lisp")
  1580. #define FUNC_NAME s_scm_integer_expt
  1581. {
  1582. long i2 = 0;
  1583. SCM z_i2 = SCM_BOOL_F;
  1584. int i2_is_big = 0;
  1585. SCM acc = SCM_I_MAKINUM (1L);
  1586. /* 0^0 == 1 according to R5RS */
  1587. if (scm_is_eq (n, SCM_INUM0) || scm_is_eq (n, acc))
  1588. return scm_is_false (scm_zero_p(k)) ? n : acc;
  1589. else if (scm_is_eq (n, SCM_I_MAKINUM (-1L)))
  1590. return scm_is_false (scm_even_p (k)) ? n : acc;
  1591. if (SCM_I_INUMP (k))
  1592. i2 = SCM_I_INUM (k);
  1593. else if (SCM_BIGP (k))
  1594. {
  1595. z_i2 = scm_i_clonebig (k, 1);
  1596. scm_remember_upto_here_1 (k);
  1597. i2_is_big = 1;
  1598. }
  1599. else
  1600. SCM_WRONG_TYPE_ARG (2, k);
  1601. if (i2_is_big)
  1602. {
  1603. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1)
  1604. {
  1605. mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2));
  1606. n = scm_divide (n, SCM_UNDEFINED);
  1607. }
  1608. while (1)
  1609. {
  1610. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0)
  1611. {
  1612. return acc;
  1613. }
  1614. if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0)
  1615. {
  1616. return scm_product (acc, n);
  1617. }
  1618. if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0))
  1619. acc = scm_product (acc, n);
  1620. n = scm_product (n, n);
  1621. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1);
  1622. }
  1623. }
  1624. else
  1625. {
  1626. if (i2 < 0)
  1627. {
  1628. i2 = -i2;
  1629. n = scm_divide (n, SCM_UNDEFINED);
  1630. }
  1631. while (1)
  1632. {
  1633. if (0 == i2)
  1634. return acc;
  1635. if (1 == i2)
  1636. return scm_product (acc, n);
  1637. if (i2 & 1)
  1638. acc = scm_product (acc, n);
  1639. n = scm_product (n, n);
  1640. i2 >>= 1;
  1641. }
  1642. }
  1643. }
  1644. #undef FUNC_NAME
  1645. SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
  1646. (SCM n, SCM cnt),
  1647. "Return @var{n} shifted left by @var{cnt} bits, or shifted right\n"
  1648. "if @var{cnt} is negative. This is an ``arithmetic'' shift.\n"
  1649. "\n"
  1650. "This is effectively a multiplication by 2^@var{cnt}, and when\n"
  1651. "@var{cnt} is negative it's a division, rounded towards negative\n"
  1652. "infinity. (Note that this is not the same rounding as\n"
  1653. "@code{quotient} does.)\n"
  1654. "\n"
  1655. "With @var{n} viewed as an infinite precision twos complement,\n"
  1656. "@code{ash} means a left shift introducing zero bits, or a right\n"
  1657. "shift dropping bits.\n"
  1658. "\n"
  1659. "@lisp\n"
  1660. "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
  1661. "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
  1662. "\n"
  1663. ";; -23 is bits ...11101001, -6 is bits ...111010\n"
  1664. "(ash -23 -2) @result{} -6\n"
  1665. "@end lisp")
  1666. #define FUNC_NAME s_scm_ash
  1667. {
  1668. long bits_to_shift;
  1669. bits_to_shift = scm_to_long (cnt);
  1670. if (SCM_I_INUMP (n))
  1671. {
  1672. long nn = SCM_I_INUM (n);
  1673. if (bits_to_shift > 0)
  1674. {
  1675. /* Left shift of bits_to_shift >= SCM_I_FIXNUM_BIT-1 will always
  1676. overflow a non-zero fixnum. For smaller shifts we check the
  1677. bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
  1678. all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
  1679. Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 -
  1680. bits_to_shift)". */
  1681. if (nn == 0)
  1682. return n;
  1683. if (bits_to_shift < SCM_I_FIXNUM_BIT-1
  1684. && ((unsigned long)
  1685. (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
  1686. <= 1))
  1687. {
  1688. return SCM_I_MAKINUM (nn << bits_to_shift);
  1689. }
  1690. else
  1691. {
  1692. SCM result = scm_i_long2big (nn);
  1693. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  1694. bits_to_shift);
  1695. return result;
  1696. }
  1697. }
  1698. else
  1699. {
  1700. bits_to_shift = -bits_to_shift;
  1701. if (bits_to_shift >= SCM_LONG_BIT)
  1702. return (nn >= 0 ? SCM_I_MAKINUM (0) : SCM_I_MAKINUM(-1));
  1703. else
  1704. return SCM_I_MAKINUM (SCM_SRS (nn, bits_to_shift));
  1705. }
  1706. }
  1707. else if (SCM_BIGP (n))
  1708. {
  1709. SCM result;
  1710. if (bits_to_shift == 0)
  1711. return n;
  1712. result = scm_i_mkbig ();
  1713. if (bits_to_shift >= 0)
  1714. {
  1715. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
  1716. bits_to_shift);
  1717. return result;
  1718. }
  1719. else
  1720. {
  1721. /* GMP doesn't have an fdiv_q_2exp variant returning just a long, so
  1722. we have to allocate a bignum even if the result is going to be a
  1723. fixnum. */
  1724. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
  1725. -bits_to_shift);
  1726. return scm_i_normbig (result);
  1727. }
  1728. }
  1729. else
  1730. {
  1731. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1732. }
  1733. }
  1734. #undef FUNC_NAME
  1735. SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
  1736. (SCM n, SCM start, SCM end),
  1737. "Return the integer composed of the @var{start} (inclusive)\n"
  1738. "through @var{end} (exclusive) bits of @var{n}. The\n"
  1739. "@var{start}th bit becomes the 0-th bit in the result.\n"
  1740. "\n"
  1741. "@lisp\n"
  1742. "(number->string (bit-extract #b1101101010 0 4) 2)\n"
  1743. " @result{} \"1010\"\n"
  1744. "(number->string (bit-extract #b1101101010 4 9) 2)\n"
  1745. " @result{} \"10110\"\n"
  1746. "@end lisp")
  1747. #define FUNC_NAME s_scm_bit_extract
  1748. {
  1749. unsigned long int istart, iend, bits;
  1750. istart = scm_to_ulong (start);
  1751. iend = scm_to_ulong (end);
  1752. SCM_ASSERT_RANGE (3, end, (iend >= istart));
  1753. /* how many bits to keep */
  1754. bits = iend - istart;
  1755. if (SCM_I_INUMP (n))
  1756. {
  1757. long int in = SCM_I_INUM (n);
  1758. /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
  1759. SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
  1760. in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1));
  1761. if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
  1762. {
  1763. /* Since we emulate two's complement encoded numbers, this
  1764. * special case requires us to produce a result that has
  1765. * more bits than can be stored in a fixnum.
  1766. */
  1767. SCM result = scm_i_long2big (in);
  1768. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  1769. bits);
  1770. return result;
  1771. }
  1772. /* mask down to requisite bits */
  1773. bits = min (bits, SCM_I_FIXNUM_BIT);
  1774. return SCM_I_MAKINUM (in & ((1L << bits) - 1));
  1775. }
  1776. else if (SCM_BIGP (n))
  1777. {
  1778. SCM result;
  1779. if (bits == 1)
  1780. {
  1781. result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
  1782. }
  1783. else
  1784. {
  1785. /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
  1786. bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
  1787. such bits into a ulong. */
  1788. result = scm_i_mkbig ();
  1789. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
  1790. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
  1791. result = scm_i_normbig (result);
  1792. }
  1793. scm_remember_upto_here_1 (n);
  1794. return result;
  1795. }
  1796. else
  1797. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1798. }
  1799. #undef FUNC_NAME
  1800. static const char scm_logtab[] = {
  1801. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  1802. };
  1803. SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
  1804. (SCM n),
  1805. "Return the number of bits in integer @var{n}. If integer is\n"
  1806. "positive, the 1-bits in its binary representation are counted.\n"
  1807. "If negative, the 0-bits in its two's-complement binary\n"
  1808. "representation are counted. If 0, 0 is returned.\n"
  1809. "\n"
  1810. "@lisp\n"
  1811. "(logcount #b10101010)\n"
  1812. " @result{} 4\n"
  1813. "(logcount 0)\n"
  1814. " @result{} 0\n"
  1815. "(logcount -2)\n"
  1816. " @result{} 1\n"
  1817. "@end lisp")
  1818. #define FUNC_NAME s_scm_logcount
  1819. {
  1820. if (SCM_I_INUMP (n))
  1821. {
  1822. unsigned long int c = 0;
  1823. long int nn = SCM_I_INUM (n);
  1824. if (nn < 0)
  1825. nn = -1 - nn;
  1826. while (nn)
  1827. {
  1828. c += scm_logtab[15 & nn];
  1829. nn >>= 4;
  1830. }
  1831. return SCM_I_MAKINUM (c);
  1832. }
  1833. else if (SCM_BIGP (n))
  1834. {
  1835. unsigned long count;
  1836. if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
  1837. count = mpz_popcount (SCM_I_BIG_MPZ (n));
  1838. else
  1839. count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
  1840. scm_remember_upto_here_1 (n);
  1841. return SCM_I_MAKINUM (count);
  1842. }
  1843. else
  1844. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1845. }
  1846. #undef FUNC_NAME
  1847. static const char scm_ilentab[] = {
  1848. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
  1849. };
  1850. SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
  1851. (SCM n),
  1852. "Return the number of bits necessary to represent @var{n}.\n"
  1853. "\n"
  1854. "@lisp\n"
  1855. "(integer-length #b10101010)\n"
  1856. " @result{} 8\n"
  1857. "(integer-length 0)\n"
  1858. " @result{} 0\n"
  1859. "(integer-length #b1111)\n"
  1860. " @result{} 4\n"
  1861. "@end lisp")
  1862. #define FUNC_NAME s_scm_integer_length
  1863. {
  1864. if (SCM_I_INUMP (n))
  1865. {
  1866. unsigned long int c = 0;
  1867. unsigned int l = 4;
  1868. long int nn = SCM_I_INUM (n);
  1869. if (nn < 0)
  1870. nn = -1 - nn;
  1871. while (nn)
  1872. {
  1873. c += 4;
  1874. l = scm_ilentab [15 & nn];
  1875. nn >>= 4;
  1876. }
  1877. return SCM_I_MAKINUM (c - 4 + l);
  1878. }
  1879. else if (SCM_BIGP (n))
  1880. {
  1881. /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
  1882. want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
  1883. 1 too big, so check for that and adjust. */
  1884. size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
  1885. if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
  1886. && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
  1887. mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
  1888. size--;
  1889. scm_remember_upto_here_1 (n);
  1890. return SCM_I_MAKINUM (size);
  1891. }
  1892. else
  1893. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1894. }
  1895. #undef FUNC_NAME
  1896. /*** NUMBERS -> STRINGS ***/
  1897. #define SCM_MAX_DBL_PREC 60
  1898. #define SCM_MAX_DBL_RADIX 36
  1899. /* this is an array starting with radix 2, and ending with radix SCM_MAX_DBL_RADIX */
  1900. static int scm_dblprec[SCM_MAX_DBL_RADIX - 1];
  1901. static double fx_per_radix[SCM_MAX_DBL_RADIX - 1][SCM_MAX_DBL_PREC];
  1902. static
  1903. void init_dblprec(int *prec, int radix) {
  1904. /* determine floating point precision by adding successively
  1905. smaller increments to 1.0 until it is considered == 1.0 */
  1906. double f = ((double)1.0)/radix;
  1907. double fsum = 1.0 + f;
  1908. *prec = 0;
  1909. while (fsum != 1.0)
  1910. {
  1911. if (++(*prec) > SCM_MAX_DBL_PREC)
  1912. fsum = 1.0;
  1913. else
  1914. {
  1915. f /= radix;
  1916. fsum = f + 1.0;
  1917. }
  1918. }
  1919. (*prec) -= 1;
  1920. }
  1921. static
  1922. void init_fx_radix(double *fx_list, int radix)
  1923. {
  1924. /* initialize a per-radix list of tolerances. When added
  1925. to a number < 1.0, we can determine if we should raund
  1926. up and quit converting a number to a string. */
  1927. int i;
  1928. fx_list[0] = 0.0;
  1929. fx_list[1] = 0.5;
  1930. for( i = 2 ; i < SCM_MAX_DBL_PREC; ++i )
  1931. fx_list[i] = (fx_list[i-1] / radix);
  1932. }
  1933. /* use this array as a way to generate a single digit */
  1934. static const char*number_chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  1935. static size_t
  1936. idbl2str (double f, char *a, int radix)
  1937. {
  1938. int efmt, dpt, d, i, wp;
  1939. double *fx;
  1940. #ifdef DBL_MIN_10_EXP
  1941. double f_cpy;
  1942. int exp_cpy;
  1943. #endif /* DBL_MIN_10_EXP */
  1944. size_t ch = 0;
  1945. int exp = 0;
  1946. if(radix < 2 ||
  1947. radix > SCM_MAX_DBL_RADIX)
  1948. {
  1949. /* revert to existing behavior */
  1950. radix = 10;
  1951. }
  1952. wp = scm_dblprec[radix-2];
  1953. fx = fx_per_radix[radix-2];
  1954. if (f == 0.0)
  1955. {
  1956. #ifdef HAVE_COPYSIGN
  1957. double sgn = copysign (1.0, f);
  1958. if (sgn < 0.0)
  1959. a[ch++] = '-';
  1960. #endif
  1961. goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */
  1962. }
  1963. if (xisinf (f))
  1964. {
  1965. if (f < 0)
  1966. strcpy (a, "-inf.0");
  1967. else
  1968. strcpy (a, "+inf.0");
  1969. return ch+6;
  1970. }
  1971. else if (xisnan (f))
  1972. {
  1973. strcpy (a, "+nan.0");
  1974. return ch+6;
  1975. }
  1976. if (f < 0.0)
  1977. {
  1978. f = -f;
  1979. a[ch++] = '-';
  1980. }
  1981. #ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
  1982. make-uniform-vector, from causing infinite loops. */
  1983. /* just do the checking...if it passes, we do the conversion for our
  1984. radix again below */
  1985. f_cpy = f;
  1986. exp_cpy = exp;
  1987. while (f_cpy < 1.0)
  1988. {
  1989. f_cpy *= 10.0;
  1990. if (exp_cpy-- < DBL_MIN_10_EXP)
  1991. {
  1992. a[ch++] = '#';
  1993. a[ch++] = '.';
  1994. a[ch++] = '#';
  1995. return ch;
  1996. }
  1997. }
  1998. while (f_cpy > 10.0)
  1999. {
  2000. f_cpy *= 0.10;
  2001. if (exp_cpy++ > DBL_MAX_10_EXP)
  2002. {
  2003. a[ch++] = '#';
  2004. a[ch++] = '.';
  2005. a[ch++] = '#';
  2006. return ch;
  2007. }
  2008. }
  2009. #endif
  2010. while (f < 1.0)
  2011. {
  2012. f *= radix;
  2013. exp--;
  2014. }
  2015. while (f > radix)
  2016. {
  2017. f /= radix;
  2018. exp++;
  2019. }
  2020. if (f + fx[wp] >= radix)
  2021. {
  2022. f = 1.0;
  2023. exp++;
  2024. }
  2025. zero:
  2026. #ifdef ENGNOT
  2027. /* adding 9999 makes this equivalent to abs(x) % 3 */
  2028. dpt = (exp + 9999) % 3;
  2029. exp -= dpt++;
  2030. efmt = 1;
  2031. #else
  2032. efmt = (exp < -3) || (exp > wp + 2);
  2033. if (!efmt)
  2034. {
  2035. if (exp < 0)
  2036. {
  2037. a[ch++] = '0';
  2038. a[ch++] = '.';
  2039. dpt = exp;
  2040. while (++dpt)
  2041. a[ch++] = '0';
  2042. }
  2043. else
  2044. dpt = exp + 1;
  2045. }
  2046. else
  2047. dpt = 1;
  2048. #endif
  2049. do
  2050. {
  2051. d = f;
  2052. f -= d;
  2053. a[ch++] = number_chars[d];
  2054. if (f < fx[wp])
  2055. break;
  2056. if (f + fx[wp] >= 1.0)
  2057. {
  2058. a[ch - 1] = number_chars[d+1];
  2059. break;
  2060. }
  2061. f *= radix;
  2062. if (!(--dpt))
  2063. a[ch++] = '.';
  2064. }
  2065. while (wp--);
  2066. if (dpt > 0)
  2067. {
  2068. #ifndef ENGNOT
  2069. if ((dpt > 4) && (exp > 6))
  2070. {
  2071. d = (a[0] == '-' ? 2 : 1);
  2072. for (i = ch++; i > d; i--)
  2073. a[i] = a[i - 1];
  2074. a[d] = '.';
  2075. efmt = 1;
  2076. }
  2077. else
  2078. #endif
  2079. {
  2080. while (--dpt)
  2081. a[ch++] = '0';
  2082. a[ch++] = '.';
  2083. }
  2084. }
  2085. if (a[ch - 1] == '.')
  2086. a[ch++] = '0'; /* trailing zero */
  2087. if (efmt && exp)
  2088. {
  2089. a[ch++] = 'e';
  2090. if (exp < 0)
  2091. {
  2092. exp = -exp;
  2093. a[ch++] = '-';
  2094. }
  2095. for (i = radix; i <= exp; i *= radix);
  2096. for (i /= radix; i; i /= radix)
  2097. {
  2098. a[ch++] = number_chars[exp / i];
  2099. exp %= i;
  2100. }
  2101. }
  2102. return ch;
  2103. }
  2104. static size_t
  2105. icmplx2str (double real, double imag, char *str, int radix)
  2106. {
  2107. size_t i;
  2108. i = idbl2str (real, str, radix);
  2109. if (imag != 0.0)
  2110. {
  2111. /* Don't output a '+' for negative numbers or for Inf and
  2112. NaN. They will provide their own sign. */
  2113. if (0 <= imag && !xisinf (imag) && !xisnan (imag))
  2114. str[i++] = '+';
  2115. i += idbl2str (imag, &str[i], radix);
  2116. str[i++] = 'i';
  2117. }
  2118. return i;
  2119. }
  2120. static size_t
  2121. iflo2str (SCM flt, char *str, int radix)
  2122. {
  2123. size_t i;
  2124. if (SCM_REALP (flt))
  2125. i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
  2126. else
  2127. i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
  2128. str, radix);
  2129. return i;
  2130. }
  2131. /* convert a scm_t_intmax to a string (unterminated). returns the number of
  2132. characters in the result.
  2133. rad is output base
  2134. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  2135. size_t
  2136. scm_iint2str (scm_t_intmax num, int rad, char *p)
  2137. {
  2138. if (num < 0)
  2139. {
  2140. *p++ = '-';
  2141. return scm_iuint2str (-num, rad, p) + 1;
  2142. }
  2143. else
  2144. return scm_iuint2str (num, rad, p);
  2145. }
  2146. /* convert a scm_t_intmax to a string (unterminated). returns the number of
  2147. characters in the result.
  2148. rad is output base
  2149. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  2150. size_t
  2151. scm_iuint2str (scm_t_uintmax num, int rad, char *p)
  2152. {
  2153. size_t j = 1;
  2154. size_t i;
  2155. scm_t_uintmax n = num;
  2156. for (n /= rad; n > 0; n /= rad)
  2157. j++;
  2158. i = j;
  2159. n = num;
  2160. while (i--)
  2161. {
  2162. int d = n % rad;
  2163. n /= rad;
  2164. p[i] = d + ((d < 10) ? '0' : 'a' - 10);
  2165. }
  2166. return j;
  2167. }
  2168. SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
  2169. (SCM n, SCM radix),
  2170. "Return a string holding the external representation of the\n"
  2171. "number @var{n} in the given @var{radix}. If @var{n} is\n"
  2172. "inexact, a radix of 10 will be used.")
  2173. #define FUNC_NAME s_scm_number_to_string
  2174. {
  2175. int base;
  2176. if (SCM_UNBNDP (radix))
  2177. base = 10;
  2178. else
  2179. base = scm_to_signed_integer (radix, 2, 36);
  2180. if (SCM_I_INUMP (n))
  2181. {
  2182. char num_buf [SCM_INTBUFLEN];
  2183. size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
  2184. return scm_from_locale_stringn (num_buf, length);
  2185. }
  2186. else if (SCM_BIGP (n))
  2187. {
  2188. char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
  2189. scm_remember_upto_here_1 (n);
  2190. return scm_take_locale_string (str);
  2191. }
  2192. else if (SCM_FRACTIONP (n))
  2193. {
  2194. return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
  2195. scm_from_locale_string ("/"),
  2196. scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
  2197. }
  2198. else if (SCM_INEXACTP (n))
  2199. {
  2200. char num_buf [FLOBUFLEN];
  2201. return scm_from_locale_stringn (num_buf, iflo2str (n, num_buf, base));
  2202. }
  2203. else
  2204. SCM_WRONG_TYPE_ARG (1, n);
  2205. }
  2206. #undef FUNC_NAME
  2207. /* These print routines used to be stubbed here so that scm_repl.c
  2208. wouldn't need SCM_BIGDIG conditionals (pre GMP) */
  2209. int
  2210. scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2211. {
  2212. char num_buf[FLOBUFLEN];
  2213. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  2214. return !0;
  2215. }
  2216. void
  2217. scm_i_print_double (double val, SCM port)
  2218. {
  2219. char num_buf[FLOBUFLEN];
  2220. scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
  2221. }
  2222. int
  2223. scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2224. {
  2225. char num_buf[FLOBUFLEN];
  2226. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  2227. return !0;
  2228. }
  2229. void
  2230. scm_i_print_complex (double real, double imag, SCM port)
  2231. {
  2232. char num_buf[FLOBUFLEN];
  2233. scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
  2234. }
  2235. int
  2236. scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2237. {
  2238. SCM str;
  2239. str = scm_number_to_string (sexp, SCM_UNDEFINED);
  2240. scm_lfwrite (scm_i_string_chars (str), scm_i_string_length (str), port);
  2241. scm_remember_upto_here_1 (str);
  2242. return !0;
  2243. }
  2244. int
  2245. scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2246. {
  2247. char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
  2248. scm_remember_upto_here_1 (exp);
  2249. scm_lfwrite (str, (size_t) strlen (str), port);
  2250. free (str);
  2251. return !0;
  2252. }
  2253. /*** END nums->strs ***/
  2254. /*** STRINGS -> NUMBERS ***/
  2255. /* The following functions implement the conversion from strings to numbers.
  2256. * The implementation somehow follows the grammar for numbers as it is given
  2257. * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
  2258. * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
  2259. * points should be noted about the implementation:
  2260. * * Each function keeps a local index variable 'idx' that points at the
  2261. * current position within the parsed string. The global index is only
  2262. * updated if the function could parse the corresponding syntactic unit
  2263. * successfully.
  2264. * * Similarly, the functions keep track of indicators of inexactness ('#',
  2265. * '.' or exponents) using local variables ('hash_seen', 'x'). Again, the
  2266. * global exactness information is only updated after each part has been
  2267. * successfully parsed.
  2268. * * Sequences of digits are parsed into temporary variables holding fixnums.
  2269. * Only if these fixnums would overflow, the result variables are updated
  2270. * using the standard functions scm_add, scm_product, scm_divide etc. Then,
  2271. * the temporary variables holding the fixnums are cleared, and the process
  2272. * starts over again. If for example fixnums were able to store five decimal
  2273. * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
  2274. * and the result was computed as 12345 * 100000 + 67890. In other words,
  2275. * only every five digits two bignum operations were performed.
  2276. */
  2277. enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
  2278. /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
  2279. /* In non ASCII-style encodings the following macro might not work. */
  2280. #define XDIGIT2UINT(d) \
  2281. (isdigit ((int) (unsigned char) d) \
  2282. ? (d) - '0' \
  2283. : tolower ((int) (unsigned char) d) - 'a' + 10)
  2284. static SCM
  2285. mem2uinteger (const char* mem, size_t len, unsigned int *p_idx,
  2286. unsigned int radix, enum t_exactness *p_exactness)
  2287. {
  2288. unsigned int idx = *p_idx;
  2289. unsigned int hash_seen = 0;
  2290. scm_t_bits shift = 1;
  2291. scm_t_bits add = 0;
  2292. unsigned int digit_value;
  2293. SCM result;
  2294. char c;
  2295. if (idx == len)
  2296. return SCM_BOOL_F;
  2297. c = mem[idx];
  2298. if (!isxdigit ((int) (unsigned char) c))
  2299. return SCM_BOOL_F;
  2300. digit_value = XDIGIT2UINT (c);
  2301. if (digit_value >= radix)
  2302. return SCM_BOOL_F;
  2303. idx++;
  2304. result = SCM_I_MAKINUM (digit_value);
  2305. while (idx != len)
  2306. {
  2307. char c = mem[idx];
  2308. if (isxdigit ((int) (unsigned char) c))
  2309. {
  2310. if (hash_seen)
  2311. break;
  2312. digit_value = XDIGIT2UINT (c);
  2313. if (digit_value >= radix)
  2314. break;
  2315. }
  2316. else if (c == '#')
  2317. {
  2318. hash_seen = 1;
  2319. digit_value = 0;
  2320. }
  2321. else
  2322. break;
  2323. idx++;
  2324. if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
  2325. {
  2326. result = scm_product (result, SCM_I_MAKINUM (shift));
  2327. if (add > 0)
  2328. result = scm_sum (result, SCM_I_MAKINUM (add));
  2329. shift = radix;
  2330. add = digit_value;
  2331. }
  2332. else
  2333. {
  2334. shift = shift * radix;
  2335. add = add * radix + digit_value;
  2336. }
  2337. };
  2338. if (shift > 1)
  2339. result = scm_product (result, SCM_I_MAKINUM (shift));
  2340. if (add > 0)
  2341. result = scm_sum (result, SCM_I_MAKINUM (add));
  2342. *p_idx = idx;
  2343. if (hash_seen)
  2344. *p_exactness = INEXACT;
  2345. return result;
  2346. }
  2347. /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
  2348. * covers the parts of the rules that start at a potential point. The value
  2349. * of the digits up to the point have been parsed by the caller and are given
  2350. * in variable result. The content of *p_exactness indicates, whether a hash
  2351. * has already been seen in the digits before the point.
  2352. */
  2353. /* In non ASCII-style encodings the following macro might not work. */
  2354. #define DIGIT2UINT(d) ((d) - '0')
  2355. static SCM
  2356. mem2decimal_from_point (SCM result, const char* mem, size_t len,
  2357. unsigned int *p_idx, enum t_exactness *p_exactness)
  2358. {
  2359. unsigned int idx = *p_idx;
  2360. enum t_exactness x = *p_exactness;
  2361. if (idx == len)
  2362. return result;
  2363. if (mem[idx] == '.')
  2364. {
  2365. scm_t_bits shift = 1;
  2366. scm_t_bits add = 0;
  2367. unsigned int digit_value;
  2368. SCM big_shift = SCM_I_MAKINUM (1);
  2369. idx++;
  2370. while (idx != len)
  2371. {
  2372. char c = mem[idx];
  2373. if (isdigit ((int) (unsigned char) c))
  2374. {
  2375. if (x == INEXACT)
  2376. return SCM_BOOL_F;
  2377. else
  2378. digit_value = DIGIT2UINT (c);
  2379. }
  2380. else if (c == '#')
  2381. {
  2382. x = INEXACT;
  2383. digit_value = 0;
  2384. }
  2385. else
  2386. break;
  2387. idx++;
  2388. if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
  2389. {
  2390. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  2391. result = scm_product (result, SCM_I_MAKINUM (shift));
  2392. if (add > 0)
  2393. result = scm_sum (result, SCM_I_MAKINUM (add));
  2394. shift = 10;
  2395. add = digit_value;
  2396. }
  2397. else
  2398. {
  2399. shift = shift * 10;
  2400. add = add * 10 + digit_value;
  2401. }
  2402. };
  2403. if (add > 0)
  2404. {
  2405. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  2406. result = scm_product (result, SCM_I_MAKINUM (shift));
  2407. result = scm_sum (result, SCM_I_MAKINUM (add));
  2408. }
  2409. result = scm_divide (result, big_shift);
  2410. /* We've seen a decimal point, thus the value is implicitly inexact. */
  2411. x = INEXACT;
  2412. }
  2413. if (idx != len)
  2414. {
  2415. int sign = 1;
  2416. unsigned int start;
  2417. char c;
  2418. int exponent;
  2419. SCM e;
  2420. /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
  2421. switch (mem[idx])
  2422. {
  2423. case 'd': case 'D':
  2424. case 'e': case 'E':
  2425. case 'f': case 'F':
  2426. case 'l': case 'L':
  2427. case 's': case 'S':
  2428. idx++;
  2429. if (idx == len)
  2430. return SCM_BOOL_F;
  2431. start = idx;
  2432. c = mem[idx];
  2433. if (c == '-')
  2434. {
  2435. idx++;
  2436. if (idx == len)
  2437. return SCM_BOOL_F;
  2438. sign = -1;
  2439. c = mem[idx];
  2440. }
  2441. else if (c == '+')
  2442. {
  2443. idx++;
  2444. if (idx == len)
  2445. return SCM_BOOL_F;
  2446. sign = 1;
  2447. c = mem[idx];
  2448. }
  2449. else
  2450. sign = 1;
  2451. if (!isdigit ((int) (unsigned char) c))
  2452. return SCM_BOOL_F;
  2453. idx++;
  2454. exponent = DIGIT2UINT (c);
  2455. while (idx != len)
  2456. {
  2457. char c = mem[idx];
  2458. if (isdigit ((int) (unsigned char) c))
  2459. {
  2460. idx++;
  2461. if (exponent <= SCM_MAXEXP)
  2462. exponent = exponent * 10 + DIGIT2UINT (c);
  2463. }
  2464. else
  2465. break;
  2466. }
  2467. if (exponent > SCM_MAXEXP)
  2468. {
  2469. size_t exp_len = idx - start;
  2470. SCM exp_string = scm_from_locale_stringn (&mem[start], exp_len);
  2471. SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
  2472. scm_out_of_range ("string->number", exp_num);
  2473. }
  2474. e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
  2475. if (sign == 1)
  2476. result = scm_product (result, e);
  2477. else
  2478. result = scm_divide2real (result, e);
  2479. /* We've seen an exponent, thus the value is implicitly inexact. */
  2480. x = INEXACT;
  2481. break;
  2482. default:
  2483. break;
  2484. }
  2485. }
  2486. *p_idx = idx;
  2487. if (x == INEXACT)
  2488. *p_exactness = x;
  2489. return result;
  2490. }
  2491. /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
  2492. static SCM
  2493. mem2ureal (const char* mem, size_t len, unsigned int *p_idx,
  2494. unsigned int radix, enum t_exactness *p_exactness)
  2495. {
  2496. unsigned int idx = *p_idx;
  2497. SCM result;
  2498. /* Start off believing that the number will be exact. This changes
  2499. to INEXACT if we see a decimal point or a hash. */
  2500. enum t_exactness x = EXACT;
  2501. if (idx == len)
  2502. return SCM_BOOL_F;
  2503. if (idx+5 <= len && !strncmp (mem+idx, "inf.0", 5))
  2504. {
  2505. *p_idx = idx+5;
  2506. return scm_inf ();
  2507. }
  2508. if (idx+4 < len && !strncmp (mem+idx, "nan.", 4))
  2509. {
  2510. /* Cobble up the fractional part. We might want to set the
  2511. NaN's mantissa from it. */
  2512. idx += 4;
  2513. mem2uinteger (mem, len, &idx, 10, &x);
  2514. *p_idx = idx;
  2515. return scm_nan ();
  2516. }
  2517. if (mem[idx] == '.')
  2518. {
  2519. if (radix != 10)
  2520. return SCM_BOOL_F;
  2521. else if (idx + 1 == len)
  2522. return SCM_BOOL_F;
  2523. else if (!isdigit ((int) (unsigned char) mem[idx + 1]))
  2524. return SCM_BOOL_F;
  2525. else
  2526. result = mem2decimal_from_point (SCM_I_MAKINUM (0), mem, len,
  2527. p_idx, &x);
  2528. }
  2529. else
  2530. {
  2531. SCM uinteger;
  2532. uinteger = mem2uinteger (mem, len, &idx, radix, &x);
  2533. if (scm_is_false (uinteger))
  2534. return SCM_BOOL_F;
  2535. if (idx == len)
  2536. result = uinteger;
  2537. else if (mem[idx] == '/')
  2538. {
  2539. SCM divisor;
  2540. idx++;
  2541. if (idx == len)
  2542. return SCM_BOOL_F;
  2543. divisor = mem2uinteger (mem, len, &idx, radix, &x);
  2544. if (scm_is_false (divisor))
  2545. return SCM_BOOL_F;
  2546. /* both are int/big here, I assume */
  2547. result = scm_i_make_ratio (uinteger, divisor);
  2548. }
  2549. else if (radix == 10)
  2550. {
  2551. result = mem2decimal_from_point (uinteger, mem, len, &idx, &x);
  2552. if (scm_is_false (result))
  2553. return SCM_BOOL_F;
  2554. }
  2555. else
  2556. result = uinteger;
  2557. *p_idx = idx;
  2558. }
  2559. /* Update *p_exactness if the number just read was inexact. This is
  2560. important for complex numbers, so that a complex number is
  2561. treated as inexact overall if either its real or imaginary part
  2562. is inexact.
  2563. */
  2564. if (x == INEXACT)
  2565. *p_exactness = x;
  2566. /* When returning an inexact zero, make sure it is represented as a
  2567. floating point value so that we can change its sign.
  2568. */
  2569. if (scm_is_eq (result, SCM_I_MAKINUM(0)) && *p_exactness == INEXACT)
  2570. result = scm_from_double (0.0);
  2571. return result;
  2572. }
  2573. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  2574. static SCM
  2575. mem2complex (const char* mem, size_t len, unsigned int idx,
  2576. unsigned int radix, enum t_exactness *p_exactness)
  2577. {
  2578. char c;
  2579. int sign = 0;
  2580. SCM ureal;
  2581. if (idx == len)
  2582. return SCM_BOOL_F;
  2583. c = mem[idx];
  2584. if (c == '+')
  2585. {
  2586. idx++;
  2587. sign = 1;
  2588. }
  2589. else if (c == '-')
  2590. {
  2591. idx++;
  2592. sign = -1;
  2593. }
  2594. if (idx == len)
  2595. return SCM_BOOL_F;
  2596. ureal = mem2ureal (mem, len, &idx, radix, p_exactness);
  2597. if (scm_is_false (ureal))
  2598. {
  2599. /* input must be either +i or -i */
  2600. if (sign == 0)
  2601. return SCM_BOOL_F;
  2602. if (mem[idx] == 'i' || mem[idx] == 'I')
  2603. {
  2604. idx++;
  2605. if (idx != len)
  2606. return SCM_BOOL_F;
  2607. return scm_make_rectangular (SCM_I_MAKINUM (0), SCM_I_MAKINUM (sign));
  2608. }
  2609. else
  2610. return SCM_BOOL_F;
  2611. }
  2612. else
  2613. {
  2614. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2615. ureal = scm_difference (ureal, SCM_UNDEFINED);
  2616. if (idx == len)
  2617. return ureal;
  2618. c = mem[idx];
  2619. switch (c)
  2620. {
  2621. case 'i': case 'I':
  2622. /* either +<ureal>i or -<ureal>i */
  2623. idx++;
  2624. if (sign == 0)
  2625. return SCM_BOOL_F;
  2626. if (idx != len)
  2627. return SCM_BOOL_F;
  2628. return scm_make_rectangular (SCM_I_MAKINUM (0), ureal);
  2629. case '@':
  2630. /* polar input: <real>@<real>. */
  2631. idx++;
  2632. if (idx == len)
  2633. return SCM_BOOL_F;
  2634. else
  2635. {
  2636. int sign;
  2637. SCM angle;
  2638. SCM result;
  2639. c = mem[idx];
  2640. if (c == '+')
  2641. {
  2642. idx++;
  2643. if (idx == len)
  2644. return SCM_BOOL_F;
  2645. sign = 1;
  2646. }
  2647. else if (c == '-')
  2648. {
  2649. idx++;
  2650. if (idx == len)
  2651. return SCM_BOOL_F;
  2652. sign = -1;
  2653. }
  2654. else
  2655. sign = 1;
  2656. angle = mem2ureal (mem, len, &idx, radix, p_exactness);
  2657. if (scm_is_false (angle))
  2658. return SCM_BOOL_F;
  2659. if (idx != len)
  2660. return SCM_BOOL_F;
  2661. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2662. angle = scm_difference (angle, SCM_UNDEFINED);
  2663. result = scm_make_polar (ureal, angle);
  2664. return result;
  2665. }
  2666. case '+':
  2667. case '-':
  2668. /* expecting input matching <real>[+-]<ureal>?i */
  2669. idx++;
  2670. if (idx == len)
  2671. return SCM_BOOL_F;
  2672. else
  2673. {
  2674. int sign = (c == '+') ? 1 : -1;
  2675. SCM imag = mem2ureal (mem, len, &idx, radix, p_exactness);
  2676. if (scm_is_false (imag))
  2677. imag = SCM_I_MAKINUM (sign);
  2678. else if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2679. imag = scm_difference (imag, SCM_UNDEFINED);
  2680. if (idx == len)
  2681. return SCM_BOOL_F;
  2682. if (mem[idx] != 'i' && mem[idx] != 'I')
  2683. return SCM_BOOL_F;
  2684. idx++;
  2685. if (idx != len)
  2686. return SCM_BOOL_F;
  2687. return scm_make_rectangular (ureal, imag);
  2688. }
  2689. default:
  2690. return SCM_BOOL_F;
  2691. }
  2692. }
  2693. }
  2694. /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
  2695. enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
  2696. SCM
  2697. scm_c_locale_stringn_to_number (const char* mem, size_t len,
  2698. unsigned int default_radix)
  2699. {
  2700. unsigned int idx = 0;
  2701. unsigned int radix = NO_RADIX;
  2702. enum t_exactness forced_x = NO_EXACTNESS;
  2703. enum t_exactness implicit_x = EXACT;
  2704. SCM result;
  2705. /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
  2706. while (idx + 2 < len && mem[idx] == '#')
  2707. {
  2708. switch (mem[idx + 1])
  2709. {
  2710. case 'b': case 'B':
  2711. if (radix != NO_RADIX)
  2712. return SCM_BOOL_F;
  2713. radix = DUAL;
  2714. break;
  2715. case 'd': case 'D':
  2716. if (radix != NO_RADIX)
  2717. return SCM_BOOL_F;
  2718. radix = DEC;
  2719. break;
  2720. case 'i': case 'I':
  2721. if (forced_x != NO_EXACTNESS)
  2722. return SCM_BOOL_F;
  2723. forced_x = INEXACT;
  2724. break;
  2725. case 'e': case 'E':
  2726. if (forced_x != NO_EXACTNESS)
  2727. return SCM_BOOL_F;
  2728. forced_x = EXACT;
  2729. break;
  2730. case 'o': case 'O':
  2731. if (radix != NO_RADIX)
  2732. return SCM_BOOL_F;
  2733. radix = OCT;
  2734. break;
  2735. case 'x': case 'X':
  2736. if (radix != NO_RADIX)
  2737. return SCM_BOOL_F;
  2738. radix = HEX;
  2739. break;
  2740. default:
  2741. return SCM_BOOL_F;
  2742. }
  2743. idx += 2;
  2744. }
  2745. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  2746. if (radix == NO_RADIX)
  2747. result = mem2complex (mem, len, idx, default_radix, &implicit_x);
  2748. else
  2749. result = mem2complex (mem, len, idx, (unsigned int) radix, &implicit_x);
  2750. if (scm_is_false (result))
  2751. return SCM_BOOL_F;
  2752. switch (forced_x)
  2753. {
  2754. case EXACT:
  2755. if (SCM_INEXACTP (result))
  2756. return scm_inexact_to_exact (result);
  2757. else
  2758. return result;
  2759. case INEXACT:
  2760. if (SCM_INEXACTP (result))
  2761. return result;
  2762. else
  2763. return scm_exact_to_inexact (result);
  2764. case NO_EXACTNESS:
  2765. default:
  2766. if (implicit_x == INEXACT)
  2767. {
  2768. if (SCM_INEXACTP (result))
  2769. return result;
  2770. else
  2771. return scm_exact_to_inexact (result);
  2772. }
  2773. else
  2774. return result;
  2775. }
  2776. }
  2777. SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
  2778. (SCM string, SCM radix),
  2779. "Return a number of the maximally precise representation\n"
  2780. "expressed by the given @var{string}. @var{radix} must be an\n"
  2781. "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
  2782. "is a default radix that may be overridden by an explicit radix\n"
  2783. "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
  2784. "supplied, then the default radix is 10. If string is not a\n"
  2785. "syntactically valid notation for a number, then\n"
  2786. "@code{string->number} returns @code{#f}.")
  2787. #define FUNC_NAME s_scm_string_to_number
  2788. {
  2789. SCM answer;
  2790. unsigned int base;
  2791. SCM_VALIDATE_STRING (1, string);
  2792. if (SCM_UNBNDP (radix))
  2793. base = 10;
  2794. else
  2795. base = scm_to_unsigned_integer (radix, 2, INT_MAX);
  2796. answer = scm_c_locale_stringn_to_number (scm_i_string_chars (string),
  2797. scm_i_string_length (string),
  2798. base);
  2799. scm_remember_upto_here_1 (string);
  2800. return answer;
  2801. }
  2802. #undef FUNC_NAME
  2803. /*** END strs->nums ***/
  2804. SCM
  2805. scm_bigequal (SCM x, SCM y)
  2806. {
  2807. int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2808. scm_remember_upto_here_2 (x, y);
  2809. return scm_from_bool (0 == result);
  2810. }
  2811. SCM
  2812. scm_real_equalp (SCM x, SCM y)
  2813. {
  2814. return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  2815. }
  2816. SCM
  2817. scm_complex_equalp (SCM x, SCM y)
  2818. {
  2819. return scm_from_bool (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
  2820. && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
  2821. }
  2822. SCM
  2823. scm_i_fraction_equalp (SCM x, SCM y)
  2824. {
  2825. if (scm_is_false (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
  2826. SCM_FRACTION_NUMERATOR (y)))
  2827. || scm_is_false (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
  2828. SCM_FRACTION_DENOMINATOR (y))))
  2829. return SCM_BOOL_F;
  2830. else
  2831. return SCM_BOOL_T;
  2832. }
  2833. SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
  2834. (SCM x),
  2835. "Return @code{#t} if @var{x} is a number, @code{#f}\n"
  2836. "otherwise.")
  2837. #define FUNC_NAME s_scm_number_p
  2838. {
  2839. return scm_from_bool (SCM_NUMBERP (x));
  2840. }
  2841. #undef FUNC_NAME
  2842. SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
  2843. (SCM x),
  2844. "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
  2845. "otherwise. Note that the sets of real, rational and integer\n"
  2846. "values form subsets of the set of complex numbers, i. e. the\n"
  2847. "predicate will also be fulfilled if @var{x} is a real,\n"
  2848. "rational or integer number.")
  2849. #define FUNC_NAME s_scm_complex_p
  2850. {
  2851. /* all numbers are complex. */
  2852. return scm_number_p (x);
  2853. }
  2854. #undef FUNC_NAME
  2855. SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
  2856. (SCM x),
  2857. "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
  2858. "otherwise. Note that the set of integer values forms a subset of\n"
  2859. "the set of real numbers, i. e. the predicate will also be\n"
  2860. "fulfilled if @var{x} is an integer number.")
  2861. #define FUNC_NAME s_scm_real_p
  2862. {
  2863. /* we can't represent irrational numbers. */
  2864. return scm_rational_p (x);
  2865. }
  2866. #undef FUNC_NAME
  2867. SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
  2868. (SCM x),
  2869. "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
  2870. "otherwise. Note that the set of integer values forms a subset of\n"
  2871. "the set of rational numbers, i. e. the predicate will also be\n"
  2872. "fulfilled if @var{x} is an integer number.")
  2873. #define FUNC_NAME s_scm_rational_p
  2874. {
  2875. if (SCM_I_INUMP (x))
  2876. return SCM_BOOL_T;
  2877. else if (SCM_IMP (x))
  2878. return SCM_BOOL_F;
  2879. else if (SCM_BIGP (x))
  2880. return SCM_BOOL_T;
  2881. else if (SCM_FRACTIONP (x))
  2882. return SCM_BOOL_T;
  2883. else if (SCM_REALP (x))
  2884. /* due to their limited precision, all floating point numbers are
  2885. rational as well. */
  2886. return SCM_BOOL_T;
  2887. else
  2888. return SCM_BOOL_F;
  2889. }
  2890. #undef FUNC_NAME
  2891. SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
  2892. (SCM x),
  2893. "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
  2894. "else.")
  2895. #define FUNC_NAME s_scm_integer_p
  2896. {
  2897. double r;
  2898. if (SCM_I_INUMP (x))
  2899. return SCM_BOOL_T;
  2900. if (SCM_IMP (x))
  2901. return SCM_BOOL_F;
  2902. if (SCM_BIGP (x))
  2903. return SCM_BOOL_T;
  2904. if (!SCM_INEXACTP (x))
  2905. return SCM_BOOL_F;
  2906. if (SCM_COMPLEXP (x))
  2907. return SCM_BOOL_F;
  2908. r = SCM_REAL_VALUE (x);
  2909. /* +/-inf passes r==floor(r), making those #t */
  2910. if (r == floor (r))
  2911. return SCM_BOOL_T;
  2912. return SCM_BOOL_F;
  2913. }
  2914. #undef FUNC_NAME
  2915. SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
  2916. (SCM x),
  2917. "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
  2918. "else.")
  2919. #define FUNC_NAME s_scm_inexact_p
  2920. {
  2921. if (SCM_INEXACTP (x))
  2922. return SCM_BOOL_T;
  2923. if (SCM_NUMBERP (x))
  2924. return SCM_BOOL_F;
  2925. SCM_WRONG_TYPE_ARG (1, x);
  2926. }
  2927. #undef FUNC_NAME
  2928. SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
  2929. /* "Return @code{#t} if all parameters are numerically equal." */
  2930. SCM
  2931. scm_num_eq_p (SCM x, SCM y)
  2932. {
  2933. again:
  2934. if (SCM_I_INUMP (x))
  2935. {
  2936. long xx = SCM_I_INUM (x);
  2937. if (SCM_I_INUMP (y))
  2938. {
  2939. long yy = SCM_I_INUM (y);
  2940. return scm_from_bool (xx == yy);
  2941. }
  2942. else if (SCM_BIGP (y))
  2943. return SCM_BOOL_F;
  2944. else if (SCM_REALP (y))
  2945. {
  2946. /* On a 32-bit system an inum fits a double, we can cast the inum
  2947. to a double and compare.
  2948. But on a 64-bit system an inum is bigger than a double and
  2949. casting it to a double (call that dxx) will round. dxx is at
  2950. worst 1 bigger or smaller than xx, so if dxx==yy we know yy is
  2951. an integer and fits a long. So we cast yy to a long and
  2952. compare with plain xx.
  2953. An alternative (for any size system actually) would be to check
  2954. yy is an integer (with floor) and is in range of an inum
  2955. (compare against appropriate powers of 2) then test
  2956. xx==(long)yy. It's just a matter of which casts/comparisons
  2957. might be fastest or easiest for the cpu. */
  2958. double yy = SCM_REAL_VALUE (y);
  2959. return scm_from_bool ((double) xx == yy
  2960. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  2961. || xx == (long) yy));
  2962. }
  2963. else if (SCM_COMPLEXP (y))
  2964. return scm_from_bool (((double) xx == SCM_COMPLEX_REAL (y))
  2965. && (0.0 == SCM_COMPLEX_IMAG (y)));
  2966. else if (SCM_FRACTIONP (y))
  2967. return SCM_BOOL_F;
  2968. else
  2969. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  2970. }
  2971. else if (SCM_BIGP (x))
  2972. {
  2973. if (SCM_I_INUMP (y))
  2974. return SCM_BOOL_F;
  2975. else if (SCM_BIGP (y))
  2976. {
  2977. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2978. scm_remember_upto_here_2 (x, y);
  2979. return scm_from_bool (0 == cmp);
  2980. }
  2981. else if (SCM_REALP (y))
  2982. {
  2983. int cmp;
  2984. if (xisnan (SCM_REAL_VALUE (y)))
  2985. return SCM_BOOL_F;
  2986. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  2987. scm_remember_upto_here_1 (x);
  2988. return scm_from_bool (0 == cmp);
  2989. }
  2990. else if (SCM_COMPLEXP (y))
  2991. {
  2992. int cmp;
  2993. if (0.0 != SCM_COMPLEX_IMAG (y))
  2994. return SCM_BOOL_F;
  2995. if (xisnan (SCM_COMPLEX_REAL (y)))
  2996. return SCM_BOOL_F;
  2997. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
  2998. scm_remember_upto_here_1 (x);
  2999. return scm_from_bool (0 == cmp);
  3000. }
  3001. else if (SCM_FRACTIONP (y))
  3002. return SCM_BOOL_F;
  3003. else
  3004. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3005. }
  3006. else if (SCM_REALP (x))
  3007. {
  3008. double xx = SCM_REAL_VALUE (x);
  3009. if (SCM_I_INUMP (y))
  3010. {
  3011. /* see comments with inum/real above */
  3012. long yy = SCM_I_INUM (y);
  3013. return scm_from_bool (xx == (double) yy
  3014. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  3015. || (long) xx == yy));
  3016. }
  3017. else if (SCM_BIGP (y))
  3018. {
  3019. int cmp;
  3020. if (xisnan (SCM_REAL_VALUE (x)))
  3021. return SCM_BOOL_F;
  3022. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  3023. scm_remember_upto_here_1 (y);
  3024. return scm_from_bool (0 == cmp);
  3025. }
  3026. else if (SCM_REALP (y))
  3027. return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  3028. else if (SCM_COMPLEXP (y))
  3029. return scm_from_bool ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
  3030. && (0.0 == SCM_COMPLEX_IMAG (y)));
  3031. else if (SCM_FRACTIONP (y))
  3032. {
  3033. double xx = SCM_REAL_VALUE (x);
  3034. if (xisnan (xx))
  3035. return SCM_BOOL_F;
  3036. if (xisinf (xx))
  3037. return scm_from_bool (xx < 0.0);
  3038. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3039. goto again;
  3040. }
  3041. else
  3042. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3043. }
  3044. else if (SCM_COMPLEXP (x))
  3045. {
  3046. if (SCM_I_INUMP (y))
  3047. return scm_from_bool ((SCM_COMPLEX_REAL (x) == (double) SCM_I_INUM (y))
  3048. && (SCM_COMPLEX_IMAG (x) == 0.0));
  3049. else if (SCM_BIGP (y))
  3050. {
  3051. int cmp;
  3052. if (0.0 != SCM_COMPLEX_IMAG (x))
  3053. return SCM_BOOL_F;
  3054. if (xisnan (SCM_COMPLEX_REAL (x)))
  3055. return SCM_BOOL_F;
  3056. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
  3057. scm_remember_upto_here_1 (y);
  3058. return scm_from_bool (0 == cmp);
  3059. }
  3060. else if (SCM_REALP (y))
  3061. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
  3062. && (SCM_COMPLEX_IMAG (x) == 0.0));
  3063. else if (SCM_COMPLEXP (y))
  3064. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
  3065. && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
  3066. else if (SCM_FRACTIONP (y))
  3067. {
  3068. double xx;
  3069. if (SCM_COMPLEX_IMAG (x) != 0.0)
  3070. return SCM_BOOL_F;
  3071. xx = SCM_COMPLEX_REAL (x);
  3072. if (xisnan (xx))
  3073. return SCM_BOOL_F;
  3074. if (xisinf (xx))
  3075. return scm_from_bool (xx < 0.0);
  3076. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3077. goto again;
  3078. }
  3079. else
  3080. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3081. }
  3082. else if (SCM_FRACTIONP (x))
  3083. {
  3084. if (SCM_I_INUMP (y))
  3085. return SCM_BOOL_F;
  3086. else if (SCM_BIGP (y))
  3087. return SCM_BOOL_F;
  3088. else if (SCM_REALP (y))
  3089. {
  3090. double yy = SCM_REAL_VALUE (y);
  3091. if (xisnan (yy))
  3092. return SCM_BOOL_F;
  3093. if (xisinf (yy))
  3094. return scm_from_bool (0.0 < yy);
  3095. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3096. goto again;
  3097. }
  3098. else if (SCM_COMPLEXP (y))
  3099. {
  3100. double yy;
  3101. if (SCM_COMPLEX_IMAG (y) != 0.0)
  3102. return SCM_BOOL_F;
  3103. yy = SCM_COMPLEX_REAL (y);
  3104. if (xisnan (yy))
  3105. return SCM_BOOL_F;
  3106. if (xisinf (yy))
  3107. return scm_from_bool (0.0 < yy);
  3108. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3109. goto again;
  3110. }
  3111. else if (SCM_FRACTIONP (y))
  3112. return scm_i_fraction_equalp (x, y);
  3113. else
  3114. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3115. }
  3116. else
  3117. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p);
  3118. }
  3119. /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
  3120. done are good for inums, but for bignums an answer can almost always be
  3121. had by just examining a few high bits of the operands, as done by GMP in
  3122. mpq_cmp. flonum/frac compares likewise, but with the slight complication
  3123. of the float exponent to take into account. */
  3124. SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
  3125. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3126. * "increasing."
  3127. */
  3128. SCM
  3129. scm_less_p (SCM x, SCM y)
  3130. {
  3131. again:
  3132. if (SCM_I_INUMP (x))
  3133. {
  3134. long xx = SCM_I_INUM (x);
  3135. if (SCM_I_INUMP (y))
  3136. {
  3137. long yy = SCM_I_INUM (y);
  3138. return scm_from_bool (xx < yy);
  3139. }
  3140. else if (SCM_BIGP (y))
  3141. {
  3142. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3143. scm_remember_upto_here_1 (y);
  3144. return scm_from_bool (sgn > 0);
  3145. }
  3146. else if (SCM_REALP (y))
  3147. return scm_from_bool ((double) xx < SCM_REAL_VALUE (y));
  3148. else if (SCM_FRACTIONP (y))
  3149. {
  3150. /* "x < a/b" becomes "x*b < a" */
  3151. int_frac:
  3152. x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
  3153. y = SCM_FRACTION_NUMERATOR (y);
  3154. goto again;
  3155. }
  3156. else
  3157. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3158. }
  3159. else if (SCM_BIGP (x))
  3160. {
  3161. if (SCM_I_INUMP (y))
  3162. {
  3163. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3164. scm_remember_upto_here_1 (x);
  3165. return scm_from_bool (sgn < 0);
  3166. }
  3167. else if (SCM_BIGP (y))
  3168. {
  3169. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3170. scm_remember_upto_here_2 (x, y);
  3171. return scm_from_bool (cmp < 0);
  3172. }
  3173. else if (SCM_REALP (y))
  3174. {
  3175. int cmp;
  3176. if (xisnan (SCM_REAL_VALUE (y)))
  3177. return SCM_BOOL_F;
  3178. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  3179. scm_remember_upto_here_1 (x);
  3180. return scm_from_bool (cmp < 0);
  3181. }
  3182. else if (SCM_FRACTIONP (y))
  3183. goto int_frac;
  3184. else
  3185. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3186. }
  3187. else if (SCM_REALP (x))
  3188. {
  3189. if (SCM_I_INUMP (y))
  3190. return scm_from_bool (SCM_REAL_VALUE (x) < (double) SCM_I_INUM (y));
  3191. else if (SCM_BIGP (y))
  3192. {
  3193. int cmp;
  3194. if (xisnan (SCM_REAL_VALUE (x)))
  3195. return SCM_BOOL_F;
  3196. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  3197. scm_remember_upto_here_1 (y);
  3198. return scm_from_bool (cmp > 0);
  3199. }
  3200. else if (SCM_REALP (y))
  3201. return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
  3202. else if (SCM_FRACTIONP (y))
  3203. {
  3204. double xx = SCM_REAL_VALUE (x);
  3205. if (xisnan (xx))
  3206. return SCM_BOOL_F;
  3207. if (xisinf (xx))
  3208. return scm_from_bool (xx < 0.0);
  3209. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3210. goto again;
  3211. }
  3212. else
  3213. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3214. }
  3215. else if (SCM_FRACTIONP (x))
  3216. {
  3217. if (SCM_I_INUMP (y) || SCM_BIGP (y))
  3218. {
  3219. /* "a/b < y" becomes "a < y*b" */
  3220. y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
  3221. x = SCM_FRACTION_NUMERATOR (x);
  3222. goto again;
  3223. }
  3224. else if (SCM_REALP (y))
  3225. {
  3226. double yy = SCM_REAL_VALUE (y);
  3227. if (xisnan (yy))
  3228. return SCM_BOOL_F;
  3229. if (xisinf (yy))
  3230. return scm_from_bool (0.0 < yy);
  3231. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3232. goto again;
  3233. }
  3234. else if (SCM_FRACTIONP (y))
  3235. {
  3236. /* "a/b < c/d" becomes "a*d < c*b" */
  3237. SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
  3238. SCM_FRACTION_DENOMINATOR (y));
  3239. SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
  3240. SCM_FRACTION_DENOMINATOR (x));
  3241. x = new_x;
  3242. y = new_y;
  3243. goto again;
  3244. }
  3245. else
  3246. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3247. }
  3248. else
  3249. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p);
  3250. }
  3251. SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
  3252. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3253. * "decreasing."
  3254. */
  3255. #define FUNC_NAME s_scm_gr_p
  3256. SCM
  3257. scm_gr_p (SCM x, SCM y)
  3258. {
  3259. if (!SCM_NUMBERP (x))
  3260. SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
  3261. else if (!SCM_NUMBERP (y))
  3262. SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
  3263. else
  3264. return scm_less_p (y, x);
  3265. }
  3266. #undef FUNC_NAME
  3267. SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
  3268. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3269. * "non-decreasing."
  3270. */
  3271. #define FUNC_NAME s_scm_leq_p
  3272. SCM
  3273. scm_leq_p (SCM x, SCM y)
  3274. {
  3275. if (!SCM_NUMBERP (x))
  3276. SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
  3277. else if (!SCM_NUMBERP (y))
  3278. SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
  3279. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  3280. return SCM_BOOL_F;
  3281. else
  3282. return scm_not (scm_less_p (y, x));
  3283. }
  3284. #undef FUNC_NAME
  3285. SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
  3286. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3287. * "non-increasing."
  3288. */
  3289. #define FUNC_NAME s_scm_geq_p
  3290. SCM
  3291. scm_geq_p (SCM x, SCM y)
  3292. {
  3293. if (!SCM_NUMBERP (x))
  3294. SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
  3295. else if (!SCM_NUMBERP (y))
  3296. SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
  3297. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  3298. return SCM_BOOL_F;
  3299. else
  3300. return scm_not (scm_less_p (x, y));
  3301. }
  3302. #undef FUNC_NAME
  3303. SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
  3304. /* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
  3305. * "zero."
  3306. */
  3307. SCM
  3308. scm_zero_p (SCM z)
  3309. {
  3310. if (SCM_I_INUMP (z))
  3311. return scm_from_bool (scm_is_eq (z, SCM_INUM0));
  3312. else if (SCM_BIGP (z))
  3313. return SCM_BOOL_F;
  3314. else if (SCM_REALP (z))
  3315. return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
  3316. else if (SCM_COMPLEXP (z))
  3317. return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
  3318. && SCM_COMPLEX_IMAG (z) == 0.0);
  3319. else if (SCM_FRACTIONP (z))
  3320. return SCM_BOOL_F;
  3321. else
  3322. SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p);
  3323. }
  3324. SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
  3325. /* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
  3326. * "zero."
  3327. */
  3328. SCM
  3329. scm_positive_p (SCM x)
  3330. {
  3331. if (SCM_I_INUMP (x))
  3332. return scm_from_bool (SCM_I_INUM (x) > 0);
  3333. else if (SCM_BIGP (x))
  3334. {
  3335. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3336. scm_remember_upto_here_1 (x);
  3337. return scm_from_bool (sgn > 0);
  3338. }
  3339. else if (SCM_REALP (x))
  3340. return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
  3341. else if (SCM_FRACTIONP (x))
  3342. return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
  3343. else
  3344. SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p);
  3345. }
  3346. SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
  3347. /* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
  3348. * "zero."
  3349. */
  3350. SCM
  3351. scm_negative_p (SCM x)
  3352. {
  3353. if (SCM_I_INUMP (x))
  3354. return scm_from_bool (SCM_I_INUM (x) < 0);
  3355. else if (SCM_BIGP (x))
  3356. {
  3357. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3358. scm_remember_upto_here_1 (x);
  3359. return scm_from_bool (sgn < 0);
  3360. }
  3361. else if (SCM_REALP (x))
  3362. return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
  3363. else if (SCM_FRACTIONP (x))
  3364. return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
  3365. else
  3366. SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p);
  3367. }
  3368. /* scm_min and scm_max return an inexact when either argument is inexact, as
  3369. required by r5rs. On that basis, for exact/inexact combinations the
  3370. exact is converted to inexact to compare and possibly return. This is
  3371. unlike scm_less_p above which takes some trouble to preserve all bits in
  3372. its test, such trouble is not required for min and max. */
  3373. SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
  3374. /* "Return the maximum of all parameter values."
  3375. */
  3376. SCM
  3377. scm_max (SCM x, SCM y)
  3378. {
  3379. if (SCM_UNBNDP (y))
  3380. {
  3381. if (SCM_UNBNDP (x))
  3382. SCM_WTA_DISPATCH_0 (g_max, s_max);
  3383. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  3384. return x;
  3385. else
  3386. SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
  3387. }
  3388. if (SCM_I_INUMP (x))
  3389. {
  3390. long xx = SCM_I_INUM (x);
  3391. if (SCM_I_INUMP (y))
  3392. {
  3393. long yy = SCM_I_INUM (y);
  3394. return (xx < yy) ? y : x;
  3395. }
  3396. else if (SCM_BIGP (y))
  3397. {
  3398. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3399. scm_remember_upto_here_1 (y);
  3400. return (sgn < 0) ? x : y;
  3401. }
  3402. else if (SCM_REALP (y))
  3403. {
  3404. double z = xx;
  3405. /* if y==NaN then ">" is false and we return NaN */
  3406. return (z > SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
  3407. }
  3408. else if (SCM_FRACTIONP (y))
  3409. {
  3410. use_less:
  3411. return (scm_is_false (scm_less_p (x, y)) ? x : y);
  3412. }
  3413. else
  3414. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3415. }
  3416. else if (SCM_BIGP (x))
  3417. {
  3418. if (SCM_I_INUMP (y))
  3419. {
  3420. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3421. scm_remember_upto_here_1 (x);
  3422. return (sgn < 0) ? y : x;
  3423. }
  3424. else if (SCM_BIGP (y))
  3425. {
  3426. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3427. scm_remember_upto_here_2 (x, y);
  3428. return (cmp > 0) ? x : y;
  3429. }
  3430. else if (SCM_REALP (y))
  3431. {
  3432. /* if y==NaN then xx>yy is false, so we return the NaN y */
  3433. double xx, yy;
  3434. big_real:
  3435. xx = scm_i_big2dbl (x);
  3436. yy = SCM_REAL_VALUE (y);
  3437. return (xx > yy ? scm_from_double (xx) : y);
  3438. }
  3439. else if (SCM_FRACTIONP (y))
  3440. {
  3441. goto use_less;
  3442. }
  3443. else
  3444. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3445. }
  3446. else if (SCM_REALP (x))
  3447. {
  3448. if (SCM_I_INUMP (y))
  3449. {
  3450. double z = SCM_I_INUM (y);
  3451. /* if x==NaN then "<" is false and we return NaN */
  3452. return (SCM_REAL_VALUE (x) < z) ? scm_from_double (z) : x;
  3453. }
  3454. else if (SCM_BIGP (y))
  3455. {
  3456. SCM_SWAP (x, y);
  3457. goto big_real;
  3458. }
  3459. else if (SCM_REALP (y))
  3460. {
  3461. /* if x==NaN then our explicit check means we return NaN
  3462. if y==NaN then ">" is false and we return NaN
  3463. calling isnan is unavoidable, since it's the only way to know
  3464. which of x or y causes any compares to be false */
  3465. double xx = SCM_REAL_VALUE (x);
  3466. return (xisnan (xx) || xx > SCM_REAL_VALUE (y)) ? x : y;
  3467. }
  3468. else if (SCM_FRACTIONP (y))
  3469. {
  3470. double yy = scm_i_fraction2double (y);
  3471. double xx = SCM_REAL_VALUE (x);
  3472. return (xx < yy) ? scm_from_double (yy) : x;
  3473. }
  3474. else
  3475. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3476. }
  3477. else if (SCM_FRACTIONP (x))
  3478. {
  3479. if (SCM_I_INUMP (y))
  3480. {
  3481. goto use_less;
  3482. }
  3483. else if (SCM_BIGP (y))
  3484. {
  3485. goto use_less;
  3486. }
  3487. else if (SCM_REALP (y))
  3488. {
  3489. double xx = scm_i_fraction2double (x);
  3490. return (xx < SCM_REAL_VALUE (y)) ? y : scm_from_double (xx);
  3491. }
  3492. else if (SCM_FRACTIONP (y))
  3493. {
  3494. goto use_less;
  3495. }
  3496. else
  3497. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3498. }
  3499. else
  3500. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
  3501. }
  3502. SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
  3503. /* "Return the minium of all parameter values."
  3504. */
  3505. SCM
  3506. scm_min (SCM x, SCM y)
  3507. {
  3508. if (SCM_UNBNDP (y))
  3509. {
  3510. if (SCM_UNBNDP (x))
  3511. SCM_WTA_DISPATCH_0 (g_min, s_min);
  3512. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  3513. return x;
  3514. else
  3515. SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
  3516. }
  3517. if (SCM_I_INUMP (x))
  3518. {
  3519. long xx = SCM_I_INUM (x);
  3520. if (SCM_I_INUMP (y))
  3521. {
  3522. long yy = SCM_I_INUM (y);
  3523. return (xx < yy) ? x : y;
  3524. }
  3525. else if (SCM_BIGP (y))
  3526. {
  3527. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3528. scm_remember_upto_here_1 (y);
  3529. return (sgn < 0) ? y : x;
  3530. }
  3531. else if (SCM_REALP (y))
  3532. {
  3533. double z = xx;
  3534. /* if y==NaN then "<" is false and we return NaN */
  3535. return (z < SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
  3536. }
  3537. else if (SCM_FRACTIONP (y))
  3538. {
  3539. use_less:
  3540. return (scm_is_false (scm_less_p (x, y)) ? y : x);
  3541. }
  3542. else
  3543. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3544. }
  3545. else if (SCM_BIGP (x))
  3546. {
  3547. if (SCM_I_INUMP (y))
  3548. {
  3549. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3550. scm_remember_upto_here_1 (x);
  3551. return (sgn < 0) ? x : y;
  3552. }
  3553. else if (SCM_BIGP (y))
  3554. {
  3555. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3556. scm_remember_upto_here_2 (x, y);
  3557. return (cmp > 0) ? y : x;
  3558. }
  3559. else if (SCM_REALP (y))
  3560. {
  3561. /* if y==NaN then xx<yy is false, so we return the NaN y */
  3562. double xx, yy;
  3563. big_real:
  3564. xx = scm_i_big2dbl (x);
  3565. yy = SCM_REAL_VALUE (y);
  3566. return (xx < yy ? scm_from_double (xx) : y);
  3567. }
  3568. else if (SCM_FRACTIONP (y))
  3569. {
  3570. goto use_less;
  3571. }
  3572. else
  3573. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3574. }
  3575. else if (SCM_REALP (x))
  3576. {
  3577. if (SCM_I_INUMP (y))
  3578. {
  3579. double z = SCM_I_INUM (y);
  3580. /* if x==NaN then "<" is false and we return NaN */
  3581. return (z < SCM_REAL_VALUE (x)) ? scm_from_double (z) : x;
  3582. }
  3583. else if (SCM_BIGP (y))
  3584. {
  3585. SCM_SWAP (x, y);
  3586. goto big_real;
  3587. }
  3588. else if (SCM_REALP (y))
  3589. {
  3590. /* if x==NaN then our explicit check means we return NaN
  3591. if y==NaN then "<" is false and we return NaN
  3592. calling isnan is unavoidable, since it's the only way to know
  3593. which of x or y causes any compares to be false */
  3594. double xx = SCM_REAL_VALUE (x);
  3595. return (xisnan (xx) || xx < SCM_REAL_VALUE (y)) ? x : y;
  3596. }
  3597. else if (SCM_FRACTIONP (y))
  3598. {
  3599. double yy = scm_i_fraction2double (y);
  3600. double xx = SCM_REAL_VALUE (x);
  3601. return (yy < xx) ? scm_from_double (yy) : x;
  3602. }
  3603. else
  3604. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3605. }
  3606. else if (SCM_FRACTIONP (x))
  3607. {
  3608. if (SCM_I_INUMP (y))
  3609. {
  3610. goto use_less;
  3611. }
  3612. else if (SCM_BIGP (y))
  3613. {
  3614. goto use_less;
  3615. }
  3616. else if (SCM_REALP (y))
  3617. {
  3618. double xx = scm_i_fraction2double (x);
  3619. return (SCM_REAL_VALUE (y) < xx) ? y : scm_from_double (xx);
  3620. }
  3621. else if (SCM_FRACTIONP (y))
  3622. {
  3623. goto use_less;
  3624. }
  3625. else
  3626. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3627. }
  3628. else
  3629. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
  3630. }
  3631. SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
  3632. /* "Return the sum of all parameter values. Return 0 if called without\n"
  3633. * "any parameters."
  3634. */
  3635. SCM
  3636. scm_sum (SCM x, SCM y)
  3637. {
  3638. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  3639. {
  3640. if (SCM_NUMBERP (x)) return x;
  3641. if (SCM_UNBNDP (x)) return SCM_INUM0;
  3642. SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
  3643. }
  3644. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3645. {
  3646. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3647. {
  3648. long xx = SCM_I_INUM (x);
  3649. long yy = SCM_I_INUM (y);
  3650. long int z = xx + yy;
  3651. return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_long2big (z);
  3652. }
  3653. else if (SCM_BIGP (y))
  3654. {
  3655. SCM_SWAP (x, y);
  3656. goto add_big_inum;
  3657. }
  3658. else if (SCM_REALP (y))
  3659. {
  3660. long int xx = SCM_I_INUM (x);
  3661. return scm_from_double (xx + SCM_REAL_VALUE (y));
  3662. }
  3663. else if (SCM_COMPLEXP (y))
  3664. {
  3665. long int xx = SCM_I_INUM (x);
  3666. return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
  3667. SCM_COMPLEX_IMAG (y));
  3668. }
  3669. else if (SCM_FRACTIONP (y))
  3670. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  3671. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  3672. SCM_FRACTION_DENOMINATOR (y));
  3673. else
  3674. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3675. } else if (SCM_BIGP (x))
  3676. {
  3677. if (SCM_I_INUMP (y))
  3678. {
  3679. long int inum;
  3680. int bigsgn;
  3681. add_big_inum:
  3682. inum = SCM_I_INUM (y);
  3683. if (inum == 0)
  3684. return x;
  3685. bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3686. if (inum < 0)
  3687. {
  3688. SCM result = scm_i_mkbig ();
  3689. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
  3690. scm_remember_upto_here_1 (x);
  3691. /* we know the result will have to be a bignum */
  3692. if (bigsgn == -1)
  3693. return result;
  3694. return scm_i_normbig (result);
  3695. }
  3696. else
  3697. {
  3698. SCM result = scm_i_mkbig ();
  3699. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
  3700. scm_remember_upto_here_1 (x);
  3701. /* we know the result will have to be a bignum */
  3702. if (bigsgn == 1)
  3703. return result;
  3704. return scm_i_normbig (result);
  3705. }
  3706. }
  3707. else if (SCM_BIGP (y))
  3708. {
  3709. SCM result = scm_i_mkbig ();
  3710. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3711. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3712. mpz_add (SCM_I_BIG_MPZ (result),
  3713. SCM_I_BIG_MPZ (x),
  3714. SCM_I_BIG_MPZ (y));
  3715. scm_remember_upto_here_2 (x, y);
  3716. /* we know the result will have to be a bignum */
  3717. if (sgn_x == sgn_y)
  3718. return result;
  3719. return scm_i_normbig (result);
  3720. }
  3721. else if (SCM_REALP (y))
  3722. {
  3723. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
  3724. scm_remember_upto_here_1 (x);
  3725. return scm_from_double (result);
  3726. }
  3727. else if (SCM_COMPLEXP (y))
  3728. {
  3729. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  3730. + SCM_COMPLEX_REAL (y));
  3731. scm_remember_upto_here_1 (x);
  3732. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  3733. }
  3734. else if (SCM_FRACTIONP (y))
  3735. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  3736. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  3737. SCM_FRACTION_DENOMINATOR (y));
  3738. else
  3739. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3740. }
  3741. else if (SCM_REALP (x))
  3742. {
  3743. if (SCM_I_INUMP (y))
  3744. return scm_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
  3745. else if (SCM_BIGP (y))
  3746. {
  3747. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
  3748. scm_remember_upto_here_1 (y);
  3749. return scm_from_double (result);
  3750. }
  3751. else if (SCM_REALP (y))
  3752. return scm_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
  3753. else if (SCM_COMPLEXP (y))
  3754. return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
  3755. SCM_COMPLEX_IMAG (y));
  3756. else if (SCM_FRACTIONP (y))
  3757. return scm_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
  3758. else
  3759. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3760. }
  3761. else if (SCM_COMPLEXP (x))
  3762. {
  3763. if (SCM_I_INUMP (y))
  3764. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
  3765. SCM_COMPLEX_IMAG (x));
  3766. else if (SCM_BIGP (y))
  3767. {
  3768. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
  3769. + SCM_COMPLEX_REAL (x));
  3770. scm_remember_upto_here_1 (y);
  3771. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
  3772. }
  3773. else if (SCM_REALP (y))
  3774. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
  3775. SCM_COMPLEX_IMAG (x));
  3776. else if (SCM_COMPLEXP (y))
  3777. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
  3778. SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
  3779. else if (SCM_FRACTIONP (y))
  3780. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
  3781. SCM_COMPLEX_IMAG (x));
  3782. else
  3783. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3784. }
  3785. else if (SCM_FRACTIONP (x))
  3786. {
  3787. if (SCM_I_INUMP (y))
  3788. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  3789. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  3790. SCM_FRACTION_DENOMINATOR (x));
  3791. else if (SCM_BIGP (y))
  3792. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  3793. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  3794. SCM_FRACTION_DENOMINATOR (x));
  3795. else if (SCM_REALP (y))
  3796. return scm_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
  3797. else if (SCM_COMPLEXP (y))
  3798. return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
  3799. SCM_COMPLEX_IMAG (y));
  3800. else if (SCM_FRACTIONP (y))
  3801. /* a/b + c/d = (ad + bc) / bd */
  3802. return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  3803. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  3804. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  3805. else
  3806. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3807. }
  3808. else
  3809. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
  3810. }
  3811. SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
  3812. (SCM x),
  3813. "Return @math{@var{x}+1}.")
  3814. #define FUNC_NAME s_scm_oneplus
  3815. {
  3816. return scm_sum (x, SCM_I_MAKINUM (1));
  3817. }
  3818. #undef FUNC_NAME
  3819. SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
  3820. /* If called with one argument @var{z1}, -@var{z1} returned. Otherwise
  3821. * the sum of all but the first argument are subtracted from the first
  3822. * argument. */
  3823. #define FUNC_NAME s_difference
  3824. SCM
  3825. scm_difference (SCM x, SCM y)
  3826. {
  3827. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  3828. {
  3829. if (SCM_UNBNDP (x))
  3830. SCM_WTA_DISPATCH_0 (g_difference, s_difference);
  3831. else
  3832. if (SCM_I_INUMP (x))
  3833. {
  3834. long xx = -SCM_I_INUM (x);
  3835. if (SCM_FIXABLE (xx))
  3836. return SCM_I_MAKINUM (xx);
  3837. else
  3838. return scm_i_long2big (xx);
  3839. }
  3840. else if (SCM_BIGP (x))
  3841. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  3842. bignum, but negating that gives a fixnum. */
  3843. return scm_i_normbig (scm_i_clonebig (x, 0));
  3844. else if (SCM_REALP (x))
  3845. return scm_from_double (-SCM_REAL_VALUE (x));
  3846. else if (SCM_COMPLEXP (x))
  3847. return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
  3848. -SCM_COMPLEX_IMAG (x));
  3849. else if (SCM_FRACTIONP (x))
  3850. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  3851. SCM_FRACTION_DENOMINATOR (x));
  3852. else
  3853. SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
  3854. }
  3855. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3856. {
  3857. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3858. {
  3859. long int xx = SCM_I_INUM (x);
  3860. long int yy = SCM_I_INUM (y);
  3861. long int z = xx - yy;
  3862. if (SCM_FIXABLE (z))
  3863. return SCM_I_MAKINUM (z);
  3864. else
  3865. return scm_i_long2big (z);
  3866. }
  3867. else if (SCM_BIGP (y))
  3868. {
  3869. /* inum-x - big-y */
  3870. long xx = SCM_I_INUM (x);
  3871. if (xx == 0)
  3872. return scm_i_clonebig (y, 0);
  3873. else
  3874. {
  3875. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3876. SCM result = scm_i_mkbig ();
  3877. if (xx >= 0)
  3878. mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
  3879. else
  3880. {
  3881. /* x - y == -(y + -x) */
  3882. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
  3883. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  3884. }
  3885. scm_remember_upto_here_1 (y);
  3886. if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
  3887. /* we know the result will have to be a bignum */
  3888. return result;
  3889. else
  3890. return scm_i_normbig (result);
  3891. }
  3892. }
  3893. else if (SCM_REALP (y))
  3894. {
  3895. long int xx = SCM_I_INUM (x);
  3896. return scm_from_double (xx - SCM_REAL_VALUE (y));
  3897. }
  3898. else if (SCM_COMPLEXP (y))
  3899. {
  3900. long int xx = SCM_I_INUM (x);
  3901. return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
  3902. - SCM_COMPLEX_IMAG (y));
  3903. }
  3904. else if (SCM_FRACTIONP (y))
  3905. /* a - b/c = (ac - b) / c */
  3906. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  3907. SCM_FRACTION_NUMERATOR (y)),
  3908. SCM_FRACTION_DENOMINATOR (y));
  3909. else
  3910. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3911. }
  3912. else if (SCM_BIGP (x))
  3913. {
  3914. if (SCM_I_INUMP (y))
  3915. {
  3916. /* big-x - inum-y */
  3917. long yy = SCM_I_INUM (y);
  3918. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3919. scm_remember_upto_here_1 (x);
  3920. if (sgn_x == 0)
  3921. return (SCM_FIXABLE (-yy) ?
  3922. SCM_I_MAKINUM (-yy) : scm_from_long (-yy));
  3923. else
  3924. {
  3925. SCM result = scm_i_mkbig ();
  3926. if (yy >= 0)
  3927. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  3928. else
  3929. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
  3930. scm_remember_upto_here_1 (x);
  3931. if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
  3932. /* we know the result will have to be a bignum */
  3933. return result;
  3934. else
  3935. return scm_i_normbig (result);
  3936. }
  3937. }
  3938. else if (SCM_BIGP (y))
  3939. {
  3940. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3941. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3942. SCM result = scm_i_mkbig ();
  3943. mpz_sub (SCM_I_BIG_MPZ (result),
  3944. SCM_I_BIG_MPZ (x),
  3945. SCM_I_BIG_MPZ (y));
  3946. scm_remember_upto_here_2 (x, y);
  3947. /* we know the result will have to be a bignum */
  3948. if ((sgn_x == 1) && (sgn_y == -1))
  3949. return result;
  3950. if ((sgn_x == -1) && (sgn_y == 1))
  3951. return result;
  3952. return scm_i_normbig (result);
  3953. }
  3954. else if (SCM_REALP (y))
  3955. {
  3956. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
  3957. scm_remember_upto_here_1 (x);
  3958. return scm_from_double (result);
  3959. }
  3960. else if (SCM_COMPLEXP (y))
  3961. {
  3962. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  3963. - SCM_COMPLEX_REAL (y));
  3964. scm_remember_upto_here_1 (x);
  3965. return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
  3966. }
  3967. else if (SCM_FRACTIONP (y))
  3968. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  3969. SCM_FRACTION_NUMERATOR (y)),
  3970. SCM_FRACTION_DENOMINATOR (y));
  3971. else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3972. }
  3973. else if (SCM_REALP (x))
  3974. {
  3975. if (SCM_I_INUMP (y))
  3976. return scm_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
  3977. else if (SCM_BIGP (y))
  3978. {
  3979. double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
  3980. scm_remember_upto_here_1 (x);
  3981. return scm_from_double (result);
  3982. }
  3983. else if (SCM_REALP (y))
  3984. return scm_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
  3985. else if (SCM_COMPLEXP (y))
  3986. return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
  3987. -SCM_COMPLEX_IMAG (y));
  3988. else if (SCM_FRACTIONP (y))
  3989. return scm_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
  3990. else
  3991. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3992. }
  3993. else if (SCM_COMPLEXP (x))
  3994. {
  3995. if (SCM_I_INUMP (y))
  3996. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
  3997. SCM_COMPLEX_IMAG (x));
  3998. else if (SCM_BIGP (y))
  3999. {
  4000. double real_part = (SCM_COMPLEX_REAL (x)
  4001. - mpz_get_d (SCM_I_BIG_MPZ (y)));
  4002. scm_remember_upto_here_1 (x);
  4003. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  4004. }
  4005. else if (SCM_REALP (y))
  4006. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
  4007. SCM_COMPLEX_IMAG (x));
  4008. else if (SCM_COMPLEXP (y))
  4009. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
  4010. SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
  4011. else if (SCM_FRACTIONP (y))
  4012. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
  4013. SCM_COMPLEX_IMAG (x));
  4014. else
  4015. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  4016. }
  4017. else if (SCM_FRACTIONP (x))
  4018. {
  4019. if (SCM_I_INUMP (y))
  4020. /* a/b - c = (a - cb) / b */
  4021. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  4022. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  4023. SCM_FRACTION_DENOMINATOR (x));
  4024. else if (SCM_BIGP (y))
  4025. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  4026. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  4027. SCM_FRACTION_DENOMINATOR (x));
  4028. else if (SCM_REALP (y))
  4029. return scm_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
  4030. else if (SCM_COMPLEXP (y))
  4031. return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
  4032. -SCM_COMPLEX_IMAG (y));
  4033. else if (SCM_FRACTIONP (y))
  4034. /* a/b - c/d = (ad - bc) / bd */
  4035. return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  4036. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  4037. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  4038. else
  4039. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  4040. }
  4041. else
  4042. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
  4043. }
  4044. #undef FUNC_NAME
  4045. SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
  4046. (SCM x),
  4047. "Return @math{@var{x}-1}.")
  4048. #define FUNC_NAME s_scm_oneminus
  4049. {
  4050. return scm_difference (x, SCM_I_MAKINUM (1));
  4051. }
  4052. #undef FUNC_NAME
  4053. SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
  4054. /* "Return the product of all arguments. If called without arguments,\n"
  4055. * "1 is returned."
  4056. */
  4057. SCM
  4058. scm_product (SCM x, SCM y)
  4059. {
  4060. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  4061. {
  4062. if (SCM_UNBNDP (x))
  4063. return SCM_I_MAKINUM (1L);
  4064. else if (SCM_NUMBERP (x))
  4065. return x;
  4066. else
  4067. SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
  4068. }
  4069. if (SCM_LIKELY (SCM_I_INUMP (x)))
  4070. {
  4071. long xx;
  4072. intbig:
  4073. xx = SCM_I_INUM (x);
  4074. switch (xx)
  4075. {
  4076. case 0: return x; break;
  4077. case 1: return y; break;
  4078. }
  4079. if (SCM_LIKELY (SCM_I_INUMP (y)))
  4080. {
  4081. long yy = SCM_I_INUM (y);
  4082. long kk = xx * yy;
  4083. SCM k = SCM_I_MAKINUM (kk);
  4084. if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
  4085. return k;
  4086. else
  4087. {
  4088. SCM result = scm_i_long2big (xx);
  4089. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
  4090. return scm_i_normbig (result);
  4091. }
  4092. }
  4093. else if (SCM_BIGP (y))
  4094. {
  4095. SCM result = scm_i_mkbig ();
  4096. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
  4097. scm_remember_upto_here_1 (y);
  4098. return result;
  4099. }
  4100. else if (SCM_REALP (y))
  4101. return scm_from_double (xx * SCM_REAL_VALUE (y));
  4102. else if (SCM_COMPLEXP (y))
  4103. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  4104. xx * SCM_COMPLEX_IMAG (y));
  4105. else if (SCM_FRACTIONP (y))
  4106. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  4107. SCM_FRACTION_DENOMINATOR (y));
  4108. else
  4109. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4110. }
  4111. else if (SCM_BIGP (x))
  4112. {
  4113. if (SCM_I_INUMP (y))
  4114. {
  4115. SCM_SWAP (x, y);
  4116. goto intbig;
  4117. }
  4118. else if (SCM_BIGP (y))
  4119. {
  4120. SCM result = scm_i_mkbig ();
  4121. mpz_mul (SCM_I_BIG_MPZ (result),
  4122. SCM_I_BIG_MPZ (x),
  4123. SCM_I_BIG_MPZ (y));
  4124. scm_remember_upto_here_2 (x, y);
  4125. return result;
  4126. }
  4127. else if (SCM_REALP (y))
  4128. {
  4129. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
  4130. scm_remember_upto_here_1 (x);
  4131. return scm_from_double (result);
  4132. }
  4133. else if (SCM_COMPLEXP (y))
  4134. {
  4135. double z = mpz_get_d (SCM_I_BIG_MPZ (x));
  4136. scm_remember_upto_here_1 (x);
  4137. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
  4138. z * SCM_COMPLEX_IMAG (y));
  4139. }
  4140. else if (SCM_FRACTIONP (y))
  4141. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  4142. SCM_FRACTION_DENOMINATOR (y));
  4143. else
  4144. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4145. }
  4146. else if (SCM_REALP (x))
  4147. {
  4148. if (SCM_I_INUMP (y))
  4149. {
  4150. /* inexact*exact0 => exact 0, per R5RS "Exactness" section */
  4151. if (scm_is_eq (y, SCM_INUM0))
  4152. return y;
  4153. return scm_from_double (SCM_I_INUM (y) * SCM_REAL_VALUE (x));
  4154. }
  4155. else if (SCM_BIGP (y))
  4156. {
  4157. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
  4158. scm_remember_upto_here_1 (y);
  4159. return scm_from_double (result);
  4160. }
  4161. else if (SCM_REALP (y))
  4162. return scm_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
  4163. else if (SCM_COMPLEXP (y))
  4164. return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
  4165. SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
  4166. else if (SCM_FRACTIONP (y))
  4167. return scm_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
  4168. else
  4169. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4170. }
  4171. else if (SCM_COMPLEXP (x))
  4172. {
  4173. if (SCM_I_INUMP (y))
  4174. {
  4175. /* inexact*exact0 => exact 0, per R5RS "Exactness" section */
  4176. if (scm_is_eq (y, SCM_INUM0))
  4177. return y;
  4178. return scm_c_make_rectangular (SCM_I_INUM (y) * SCM_COMPLEX_REAL (x),
  4179. SCM_I_INUM (y) * SCM_COMPLEX_IMAG (x));
  4180. }
  4181. else if (SCM_BIGP (y))
  4182. {
  4183. double z = mpz_get_d (SCM_I_BIG_MPZ (y));
  4184. scm_remember_upto_here_1 (y);
  4185. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
  4186. z * SCM_COMPLEX_IMAG (x));
  4187. }
  4188. else if (SCM_REALP (y))
  4189. return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
  4190. SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
  4191. else if (SCM_COMPLEXP (y))
  4192. {
  4193. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
  4194. - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
  4195. SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
  4196. + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
  4197. }
  4198. else if (SCM_FRACTIONP (y))
  4199. {
  4200. double yy = scm_i_fraction2double (y);
  4201. return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
  4202. yy * SCM_COMPLEX_IMAG (x));
  4203. }
  4204. else
  4205. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4206. }
  4207. else if (SCM_FRACTIONP (x))
  4208. {
  4209. if (SCM_I_INUMP (y))
  4210. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  4211. SCM_FRACTION_DENOMINATOR (x));
  4212. else if (SCM_BIGP (y))
  4213. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  4214. SCM_FRACTION_DENOMINATOR (x));
  4215. else if (SCM_REALP (y))
  4216. return scm_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
  4217. else if (SCM_COMPLEXP (y))
  4218. {
  4219. double xx = scm_i_fraction2double (x);
  4220. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  4221. xx * SCM_COMPLEX_IMAG (y));
  4222. }
  4223. else if (SCM_FRACTIONP (y))
  4224. /* a/b * c/d = ac / bd */
  4225. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  4226. SCM_FRACTION_NUMERATOR (y)),
  4227. scm_product (SCM_FRACTION_DENOMINATOR (x),
  4228. SCM_FRACTION_DENOMINATOR (y)));
  4229. else
  4230. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4231. }
  4232. else
  4233. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
  4234. }
  4235. #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
  4236. || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
  4237. #define ALLOW_DIVIDE_BY_ZERO
  4238. /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
  4239. #endif
  4240. /* The code below for complex division is adapted from the GNU
  4241. libstdc++, which adapted it from f2c's libF77, and is subject to
  4242. this copyright: */
  4243. /****************************************************************
  4244. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  4245. Permission to use, copy, modify, and distribute this software
  4246. and its documentation for any purpose and without fee is hereby
  4247. granted, provided that the above copyright notice appear in all
  4248. copies and that both that the copyright notice and this
  4249. permission notice and warranty disclaimer appear in supporting
  4250. documentation, and that the names of AT&T Bell Laboratories or
  4251. Bellcore or any of their entities not be used in advertising or
  4252. publicity pertaining to distribution of the software without
  4253. specific, written prior permission.
  4254. AT&T and Bellcore disclaim all warranties with regard to this
  4255. software, including all implied warranties of merchantability
  4256. and fitness. In no event shall AT&T or Bellcore be liable for
  4257. any special, indirect or consequential damages or any damages
  4258. whatsoever resulting from loss of use, data or profits, whether
  4259. in an action of contract, negligence or other tortious action,
  4260. arising out of or in connection with the use or performance of
  4261. this software.
  4262. ****************************************************************/
  4263. SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
  4264. /* Divide the first argument by the product of the remaining
  4265. arguments. If called with one argument @var{z1}, 1/@var{z1} is
  4266. returned. */
  4267. #define FUNC_NAME s_divide
  4268. static SCM
  4269. scm_i_divide (SCM x, SCM y, int inexact)
  4270. {
  4271. double a;
  4272. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  4273. {
  4274. if (SCM_UNBNDP (x))
  4275. SCM_WTA_DISPATCH_0 (g_divide, s_divide);
  4276. else if (SCM_I_INUMP (x))
  4277. {
  4278. long xx = SCM_I_INUM (x);
  4279. if (xx == 1 || xx == -1)
  4280. return x;
  4281. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4282. else if (xx == 0)
  4283. scm_num_overflow (s_divide);
  4284. #endif
  4285. else
  4286. {
  4287. if (inexact)
  4288. return scm_from_double (1.0 / (double) xx);
  4289. else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
  4290. }
  4291. }
  4292. else if (SCM_BIGP (x))
  4293. {
  4294. if (inexact)
  4295. return scm_from_double (1.0 / scm_i_big2dbl (x));
  4296. else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
  4297. }
  4298. else if (SCM_REALP (x))
  4299. {
  4300. double xx = SCM_REAL_VALUE (x);
  4301. #ifndef ALLOW_DIVIDE_BY_ZERO
  4302. if (xx == 0.0)
  4303. scm_num_overflow (s_divide);
  4304. else
  4305. #endif
  4306. return scm_from_double (1.0 / xx);
  4307. }
  4308. else if (SCM_COMPLEXP (x))
  4309. {
  4310. double r = SCM_COMPLEX_REAL (x);
  4311. double i = SCM_COMPLEX_IMAG (x);
  4312. if (fabs(r) <= fabs(i))
  4313. {
  4314. double t = r / i;
  4315. double d = i * (1.0 + t * t);
  4316. return scm_c_make_rectangular (t / d, -1.0 / d);
  4317. }
  4318. else
  4319. {
  4320. double t = i / r;
  4321. double d = r * (1.0 + t * t);
  4322. return scm_c_make_rectangular (1.0 / d, -t / d);
  4323. }
  4324. }
  4325. else if (SCM_FRACTIONP (x))
  4326. return scm_i_make_ratio (SCM_FRACTION_DENOMINATOR (x),
  4327. SCM_FRACTION_NUMERATOR (x));
  4328. else
  4329. SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
  4330. }
  4331. if (SCM_LIKELY (SCM_I_INUMP (x)))
  4332. {
  4333. long xx = SCM_I_INUM (x);
  4334. if (SCM_LIKELY (SCM_I_INUMP (y)))
  4335. {
  4336. long yy = SCM_I_INUM (y);
  4337. if (yy == 0)
  4338. {
  4339. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4340. scm_num_overflow (s_divide);
  4341. #else
  4342. return scm_from_double ((double) xx / (double) yy);
  4343. #endif
  4344. }
  4345. else if (xx % yy != 0)
  4346. {
  4347. if (inexact)
  4348. return scm_from_double ((double) xx / (double) yy);
  4349. else return scm_i_make_ratio (x, y);
  4350. }
  4351. else
  4352. {
  4353. long z = xx / yy;
  4354. if (SCM_FIXABLE (z))
  4355. return SCM_I_MAKINUM (z);
  4356. else
  4357. return scm_i_long2big (z);
  4358. }
  4359. }
  4360. else if (SCM_BIGP (y))
  4361. {
  4362. if (inexact)
  4363. return scm_from_double ((double) xx / scm_i_big2dbl (y));
  4364. else return scm_i_make_ratio (x, y);
  4365. }
  4366. else if (SCM_REALP (y))
  4367. {
  4368. double yy = SCM_REAL_VALUE (y);
  4369. #ifndef ALLOW_DIVIDE_BY_ZERO
  4370. if (yy == 0.0)
  4371. scm_num_overflow (s_divide);
  4372. else
  4373. #endif
  4374. return scm_from_double ((double) xx / yy);
  4375. }
  4376. else if (SCM_COMPLEXP (y))
  4377. {
  4378. a = xx;
  4379. complex_div: /* y _must_ be a complex number */
  4380. {
  4381. double r = SCM_COMPLEX_REAL (y);
  4382. double i = SCM_COMPLEX_IMAG (y);
  4383. if (fabs(r) <= fabs(i))
  4384. {
  4385. double t = r / i;
  4386. double d = i * (1.0 + t * t);
  4387. return scm_c_make_rectangular ((a * t) / d, -a / d);
  4388. }
  4389. else
  4390. {
  4391. double t = i / r;
  4392. double d = r * (1.0 + t * t);
  4393. return scm_c_make_rectangular (a / d, -(a * t) / d);
  4394. }
  4395. }
  4396. }
  4397. else if (SCM_FRACTIONP (y))
  4398. /* a / b/c = ac / b */
  4399. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4400. SCM_FRACTION_NUMERATOR (y));
  4401. else
  4402. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4403. }
  4404. else if (SCM_BIGP (x))
  4405. {
  4406. if (SCM_I_INUMP (y))
  4407. {
  4408. long int yy = SCM_I_INUM (y);
  4409. if (yy == 0)
  4410. {
  4411. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4412. scm_num_overflow (s_divide);
  4413. #else
  4414. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  4415. scm_remember_upto_here_1 (x);
  4416. return (sgn == 0) ? scm_nan () : scm_inf ();
  4417. #endif
  4418. }
  4419. else if (yy == 1)
  4420. return x;
  4421. else
  4422. {
  4423. /* FIXME: HMM, what are the relative performance issues here?
  4424. We need to test. Is it faster on average to test
  4425. divisible_p, then perform whichever operation, or is it
  4426. faster to perform the integer div opportunistically and
  4427. switch to real if there's a remainder? For now we take the
  4428. middle ground: test, then if divisible, use the faster div
  4429. func. */
  4430. long abs_yy = yy < 0 ? -yy : yy;
  4431. int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
  4432. if (divisible_p)
  4433. {
  4434. SCM result = scm_i_mkbig ();
  4435. mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
  4436. scm_remember_upto_here_1 (x);
  4437. if (yy < 0)
  4438. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  4439. return scm_i_normbig (result);
  4440. }
  4441. else
  4442. {
  4443. if (inexact)
  4444. return scm_from_double (scm_i_big2dbl (x) / (double) yy);
  4445. else return scm_i_make_ratio (x, y);
  4446. }
  4447. }
  4448. }
  4449. else if (SCM_BIGP (y))
  4450. {
  4451. int y_is_zero = (mpz_sgn (SCM_I_BIG_MPZ (y)) == 0);
  4452. if (y_is_zero)
  4453. {
  4454. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4455. scm_num_overflow (s_divide);
  4456. #else
  4457. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  4458. scm_remember_upto_here_1 (x);
  4459. return (sgn == 0) ? scm_nan () : scm_inf ();
  4460. #endif
  4461. }
  4462. else
  4463. {
  4464. /* big_x / big_y */
  4465. if (inexact)
  4466. {
  4467. /* It's easily possible for the ratio x/y to fit a double
  4468. but one or both x and y be too big to fit a double,
  4469. hence the use of mpq_get_d rather than converting and
  4470. dividing. */
  4471. mpq_t q;
  4472. *mpq_numref(q) = *SCM_I_BIG_MPZ (x);
  4473. *mpq_denref(q) = *SCM_I_BIG_MPZ (y);
  4474. return scm_from_double (mpq_get_d (q));
  4475. }
  4476. else
  4477. {
  4478. int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
  4479. SCM_I_BIG_MPZ (y));
  4480. if (divisible_p)
  4481. {
  4482. SCM result = scm_i_mkbig ();
  4483. mpz_divexact (SCM_I_BIG_MPZ (result),
  4484. SCM_I_BIG_MPZ (x),
  4485. SCM_I_BIG_MPZ (y));
  4486. scm_remember_upto_here_2 (x, y);
  4487. return scm_i_normbig (result);
  4488. }
  4489. else
  4490. return scm_i_make_ratio (x, y);
  4491. }
  4492. }
  4493. }
  4494. else if (SCM_REALP (y))
  4495. {
  4496. double yy = SCM_REAL_VALUE (y);
  4497. #ifndef ALLOW_DIVIDE_BY_ZERO
  4498. if (yy == 0.0)
  4499. scm_num_overflow (s_divide);
  4500. else
  4501. #endif
  4502. return scm_from_double (scm_i_big2dbl (x) / yy);
  4503. }
  4504. else if (SCM_COMPLEXP (y))
  4505. {
  4506. a = scm_i_big2dbl (x);
  4507. goto complex_div;
  4508. }
  4509. else if (SCM_FRACTIONP (y))
  4510. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4511. SCM_FRACTION_NUMERATOR (y));
  4512. else
  4513. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4514. }
  4515. else if (SCM_REALP (x))
  4516. {
  4517. double rx = SCM_REAL_VALUE (x);
  4518. if (SCM_I_INUMP (y))
  4519. {
  4520. long int yy = SCM_I_INUM (y);
  4521. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4522. if (yy == 0)
  4523. scm_num_overflow (s_divide);
  4524. else
  4525. #endif
  4526. return scm_from_double (rx / (double) yy);
  4527. }
  4528. else if (SCM_BIGP (y))
  4529. {
  4530. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  4531. scm_remember_upto_here_1 (y);
  4532. return scm_from_double (rx / dby);
  4533. }
  4534. else if (SCM_REALP (y))
  4535. {
  4536. double yy = SCM_REAL_VALUE (y);
  4537. #ifndef ALLOW_DIVIDE_BY_ZERO
  4538. if (yy == 0.0)
  4539. scm_num_overflow (s_divide);
  4540. else
  4541. #endif
  4542. return scm_from_double (rx / yy);
  4543. }
  4544. else if (SCM_COMPLEXP (y))
  4545. {
  4546. a = rx;
  4547. goto complex_div;
  4548. }
  4549. else if (SCM_FRACTIONP (y))
  4550. return scm_from_double (rx / scm_i_fraction2double (y));
  4551. else
  4552. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4553. }
  4554. else if (SCM_COMPLEXP (x))
  4555. {
  4556. double rx = SCM_COMPLEX_REAL (x);
  4557. double ix = SCM_COMPLEX_IMAG (x);
  4558. if (SCM_I_INUMP (y))
  4559. {
  4560. long int yy = SCM_I_INUM (y);
  4561. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4562. if (yy == 0)
  4563. scm_num_overflow (s_divide);
  4564. else
  4565. #endif
  4566. {
  4567. double d = yy;
  4568. return scm_c_make_rectangular (rx / d, ix / d);
  4569. }
  4570. }
  4571. else if (SCM_BIGP (y))
  4572. {
  4573. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  4574. scm_remember_upto_here_1 (y);
  4575. return scm_c_make_rectangular (rx / dby, ix / dby);
  4576. }
  4577. else if (SCM_REALP (y))
  4578. {
  4579. double yy = SCM_REAL_VALUE (y);
  4580. #ifndef ALLOW_DIVIDE_BY_ZERO
  4581. if (yy == 0.0)
  4582. scm_num_overflow (s_divide);
  4583. else
  4584. #endif
  4585. return scm_c_make_rectangular (rx / yy, ix / yy);
  4586. }
  4587. else if (SCM_COMPLEXP (y))
  4588. {
  4589. double ry = SCM_COMPLEX_REAL (y);
  4590. double iy = SCM_COMPLEX_IMAG (y);
  4591. if (fabs(ry) <= fabs(iy))
  4592. {
  4593. double t = ry / iy;
  4594. double d = iy * (1.0 + t * t);
  4595. return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
  4596. }
  4597. else
  4598. {
  4599. double t = iy / ry;
  4600. double d = ry * (1.0 + t * t);
  4601. return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
  4602. }
  4603. }
  4604. else if (SCM_FRACTIONP (y))
  4605. {
  4606. double yy = scm_i_fraction2double (y);
  4607. return scm_c_make_rectangular (rx / yy, ix / yy);
  4608. }
  4609. else
  4610. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4611. }
  4612. else if (SCM_FRACTIONP (x))
  4613. {
  4614. if (SCM_I_INUMP (y))
  4615. {
  4616. long int yy = SCM_I_INUM (y);
  4617. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4618. if (yy == 0)
  4619. scm_num_overflow (s_divide);
  4620. else
  4621. #endif
  4622. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  4623. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  4624. }
  4625. else if (SCM_BIGP (y))
  4626. {
  4627. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  4628. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  4629. }
  4630. else if (SCM_REALP (y))
  4631. {
  4632. double yy = SCM_REAL_VALUE (y);
  4633. #ifndef ALLOW_DIVIDE_BY_ZERO
  4634. if (yy == 0.0)
  4635. scm_num_overflow (s_divide);
  4636. else
  4637. #endif
  4638. return scm_from_double (scm_i_fraction2double (x) / yy);
  4639. }
  4640. else if (SCM_COMPLEXP (y))
  4641. {
  4642. a = scm_i_fraction2double (x);
  4643. goto complex_div;
  4644. }
  4645. else if (SCM_FRACTIONP (y))
  4646. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  4647. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
  4648. else
  4649. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4650. }
  4651. else
  4652. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
  4653. }
  4654. SCM
  4655. scm_divide (SCM x, SCM y)
  4656. {
  4657. return scm_i_divide (x, y, 0);
  4658. }
  4659. static SCM scm_divide2real (SCM x, SCM y)
  4660. {
  4661. return scm_i_divide (x, y, 1);
  4662. }
  4663. #undef FUNC_NAME
  4664. double
  4665. scm_asinh (double x)
  4666. {
  4667. #if HAVE_ASINH
  4668. return asinh (x);
  4669. #else
  4670. #define asinh scm_asinh
  4671. return log (x + sqrt (x * x + 1));
  4672. #endif
  4673. }
  4674. SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_dsubr, (SCM (*)()) asinh, g_asinh);
  4675. /* "Return the inverse hyperbolic sine of @var{x}."
  4676. */
  4677. double
  4678. scm_acosh (double x)
  4679. {
  4680. #if HAVE_ACOSH
  4681. return acosh (x);
  4682. #else
  4683. #define acosh scm_acosh
  4684. return log (x + sqrt (x * x - 1));
  4685. #endif
  4686. }
  4687. SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_dsubr, (SCM (*)()) acosh, g_acosh);
  4688. /* "Return the inverse hyperbolic cosine of @var{x}."
  4689. */
  4690. double
  4691. scm_atanh (double x)
  4692. {
  4693. #if HAVE_ATANH
  4694. return atanh (x);
  4695. #else
  4696. #define atanh scm_atanh
  4697. return 0.5 * log ((1 + x) / (1 - x));
  4698. #endif
  4699. }
  4700. SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_dsubr, (SCM (*)()) atanh, g_atanh);
  4701. /* "Return the inverse hyperbolic tangent of @var{x}."
  4702. */
  4703. double
  4704. scm_c_truncate (double x)
  4705. {
  4706. #if HAVE_TRUNC
  4707. return trunc (x);
  4708. #else
  4709. if (x < 0.0)
  4710. return -floor (-x);
  4711. return floor (x);
  4712. #endif
  4713. }
  4714. /* scm_c_round is done using floor(x+0.5) to round to nearest and with
  4715. half-way case (ie. when x is an integer plus 0.5) going upwards.
  4716. Then half-way cases are identified and adjusted down if the
  4717. round-upwards didn't give the desired even integer.
  4718. "plus_half == result" identifies a half-way case. If plus_half, which is
  4719. x + 0.5, is an integer then x must be an integer plus 0.5.
  4720. An odd "result" value is identified with result/2 != floor(result/2).
  4721. This is done with plus_half, since that value is ready for use sooner in
  4722. a pipelined cpu, and we're already requiring plus_half == result.
  4723. Note however that we need to be careful when x is big and already an
  4724. integer. In that case "x+0.5" may round to an adjacent integer, causing
  4725. us to return such a value, incorrectly. For instance if the hardware is
  4726. in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
  4727. (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
  4728. returned. Or if the hardware is in round-upwards mode, then other bigger
  4729. values like say x == 2^128 will see x+0.5 rounding up to the next higher
  4730. representable value, 2^128+2^76 (or whatever), again incorrect.
  4731. These bad roundings of x+0.5 are avoided by testing at the start whether
  4732. x is already an integer. If it is then clearly that's the desired result
  4733. already. And if it's not then the exponent must be small enough to allow
  4734. an 0.5 to be represented, and hence added without a bad rounding. */
  4735. double
  4736. scm_c_round (double x)
  4737. {
  4738. double plus_half, result;
  4739. if (x == floor (x))
  4740. return x;
  4741. plus_half = x + 0.5;
  4742. result = floor (plus_half);
  4743. /* Adjust so that the rounding is towards even. */
  4744. return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
  4745. ? result - 1
  4746. : result);
  4747. }
  4748. SCM_DEFINE (scm_truncate_number, "truncate", 1, 0, 0,
  4749. (SCM x),
  4750. "Round the number @var{x} towards zero.")
  4751. #define FUNC_NAME s_scm_truncate_number
  4752. {
  4753. if (scm_is_false (scm_negative_p (x)))
  4754. return scm_floor (x);
  4755. else
  4756. return scm_ceiling (x);
  4757. }
  4758. #undef FUNC_NAME
  4759. static SCM exactly_one_half;
  4760. SCM_DEFINE (scm_round_number, "round", 1, 0, 0,
  4761. (SCM x),
  4762. "Round the number @var{x} towards the nearest integer. "
  4763. "When it is exactly halfway between two integers, "
  4764. "round towards the even one.")
  4765. #define FUNC_NAME s_scm_round_number
  4766. {
  4767. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4768. return x;
  4769. else if (SCM_REALP (x))
  4770. return scm_from_double (scm_c_round (SCM_REAL_VALUE (x)));
  4771. else
  4772. {
  4773. /* OPTIMIZE-ME: Fraction case could be done more efficiently by a
  4774. single quotient+remainder division then examining to see which way
  4775. the rounding should go. */
  4776. SCM plus_half = scm_sum (x, exactly_one_half);
  4777. SCM result = scm_floor (plus_half);
  4778. /* Adjust so that the rounding is towards even. */
  4779. if (scm_is_true (scm_num_eq_p (plus_half, result))
  4780. && scm_is_true (scm_odd_p (result)))
  4781. return scm_difference (result, SCM_I_MAKINUM (1));
  4782. else
  4783. return result;
  4784. }
  4785. }
  4786. #undef FUNC_NAME
  4787. SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
  4788. (SCM x),
  4789. "Round the number @var{x} towards minus infinity.")
  4790. #define FUNC_NAME s_scm_floor
  4791. {
  4792. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4793. return x;
  4794. else if (SCM_REALP (x))
  4795. return scm_from_double (floor (SCM_REAL_VALUE (x)));
  4796. else if (SCM_FRACTIONP (x))
  4797. {
  4798. SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
  4799. SCM_FRACTION_DENOMINATOR (x));
  4800. if (scm_is_false (scm_negative_p (x)))
  4801. {
  4802. /* For positive x, rounding towards zero is correct. */
  4803. return q;
  4804. }
  4805. else
  4806. {
  4807. /* For negative x, we need to return q-1 unless x is an
  4808. integer. But fractions are never integer, per our
  4809. assumptions. */
  4810. return scm_difference (q, SCM_I_MAKINUM (1));
  4811. }
  4812. }
  4813. else
  4814. SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
  4815. }
  4816. #undef FUNC_NAME
  4817. SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
  4818. (SCM x),
  4819. "Round the number @var{x} towards infinity.")
  4820. #define FUNC_NAME s_scm_ceiling
  4821. {
  4822. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4823. return x;
  4824. else if (SCM_REALP (x))
  4825. return scm_from_double (ceil (SCM_REAL_VALUE (x)));
  4826. else if (SCM_FRACTIONP (x))
  4827. {
  4828. SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
  4829. SCM_FRACTION_DENOMINATOR (x));
  4830. if (scm_is_false (scm_positive_p (x)))
  4831. {
  4832. /* For negative x, rounding towards zero is correct. */
  4833. return q;
  4834. }
  4835. else
  4836. {
  4837. /* For positive x, we need to return q+1 unless x is an
  4838. integer. But fractions are never integer, per our
  4839. assumptions. */
  4840. return scm_sum (q, SCM_I_MAKINUM (1));
  4841. }
  4842. }
  4843. else
  4844. SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
  4845. }
  4846. #undef FUNC_NAME
  4847. SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_dsubr, (SCM (*)()) sqrt, g_i_sqrt);
  4848. /* "Return the square root of the real number @var{x}."
  4849. */
  4850. SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_dsubr, (SCM (*)()) fabs, g_i_abs);
  4851. /* "Return the absolute value of the real number @var{x}."
  4852. */
  4853. SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_dsubr, (SCM (*)()) exp, g_i_exp);
  4854. /* "Return the @var{x}th power of e."
  4855. */
  4856. SCM_GPROC1 (s_i_log, "$log", scm_tc7_dsubr, (SCM (*)()) log, g_i_log);
  4857. /* "Return the natural logarithm of the real number @var{x}."
  4858. */
  4859. SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_dsubr, (SCM (*)()) sin, g_i_sin);
  4860. /* "Return the sine of the real number @var{x}."
  4861. */
  4862. SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_dsubr, (SCM (*)()) cos, g_i_cos);
  4863. /* "Return the cosine of the real number @var{x}."
  4864. */
  4865. SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_dsubr, (SCM (*)()) tan, g_i_tan);
  4866. /* "Return the tangent of the real number @var{x}."
  4867. */
  4868. SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_dsubr, (SCM (*)()) asin, g_i_asin);
  4869. /* "Return the arc sine of the real number @var{x}."
  4870. */
  4871. SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_dsubr, (SCM (*)()) acos, g_i_acos);
  4872. /* "Return the arc cosine of the real number @var{x}."
  4873. */
  4874. SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_dsubr, (SCM (*)()) atan, g_i_atan);
  4875. /* "Return the arc tangent of the real number @var{x}."
  4876. */
  4877. SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_dsubr, (SCM (*)()) sinh, g_i_sinh);
  4878. /* "Return the hyperbolic sine of the real number @var{x}."
  4879. */
  4880. SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_dsubr, (SCM (*)()) cosh, g_i_cosh);
  4881. /* "Return the hyperbolic cosine of the real number @var{x}."
  4882. */
  4883. SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_dsubr, (SCM (*)()) tanh, g_i_tanh);
  4884. /* "Return the hyperbolic tangent of the real number @var{x}."
  4885. */
  4886. struct dpair
  4887. {
  4888. double x, y;
  4889. };
  4890. static void scm_two_doubles (SCM x,
  4891. SCM y,
  4892. const char *sstring,
  4893. struct dpair * xy);
  4894. static void
  4895. scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
  4896. {
  4897. if (SCM_I_INUMP (x))
  4898. xy->x = SCM_I_INUM (x);
  4899. else if (SCM_BIGP (x))
  4900. xy->x = scm_i_big2dbl (x);
  4901. else if (SCM_REALP (x))
  4902. xy->x = SCM_REAL_VALUE (x);
  4903. else if (SCM_FRACTIONP (x))
  4904. xy->x = scm_i_fraction2double (x);
  4905. else
  4906. scm_wrong_type_arg (sstring, SCM_ARG1, x);
  4907. if (SCM_I_INUMP (y))
  4908. xy->y = SCM_I_INUM (y);
  4909. else if (SCM_BIGP (y))
  4910. xy->y = scm_i_big2dbl (y);
  4911. else if (SCM_REALP (y))
  4912. xy->y = SCM_REAL_VALUE (y);
  4913. else if (SCM_FRACTIONP (y))
  4914. xy->y = scm_i_fraction2double (y);
  4915. else
  4916. scm_wrong_type_arg (sstring, SCM_ARG2, y);
  4917. }
  4918. SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
  4919. (SCM x, SCM y),
  4920. "Return @var{x} raised to the power of @var{y}. This\n"
  4921. "procedure does not accept complex arguments.")
  4922. #define FUNC_NAME s_scm_sys_expt
  4923. {
  4924. struct dpair xy;
  4925. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4926. return scm_from_double (pow (xy.x, xy.y));
  4927. }
  4928. #undef FUNC_NAME
  4929. SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
  4930. (SCM x, SCM y),
  4931. "Return the arc tangent of the two arguments @var{x} and\n"
  4932. "@var{y}. This is similar to calculating the arc tangent of\n"
  4933. "@var{x} / @var{y}, except that the signs of both arguments\n"
  4934. "are used to determine the quadrant of the result. This\n"
  4935. "procedure does not accept complex arguments.")
  4936. #define FUNC_NAME s_scm_sys_atan2
  4937. {
  4938. struct dpair xy;
  4939. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4940. return scm_from_double (atan2 (xy.x, xy.y));
  4941. }
  4942. #undef FUNC_NAME
  4943. SCM
  4944. scm_c_make_rectangular (double re, double im)
  4945. {
  4946. if (im == 0.0)
  4947. return scm_from_double (re);
  4948. else
  4949. {
  4950. SCM z;
  4951. SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex),
  4952. "complex"));
  4953. SCM_COMPLEX_REAL (z) = re;
  4954. SCM_COMPLEX_IMAG (z) = im;
  4955. return z;
  4956. }
  4957. }
  4958. SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
  4959. (SCM real_part, SCM imaginary_part),
  4960. "Return a complex number constructed of the given @var{real-part} "
  4961. "and @var{imaginary-part} parts.")
  4962. #define FUNC_NAME s_scm_make_rectangular
  4963. {
  4964. struct dpair xy;
  4965. scm_two_doubles (real_part, imaginary_part, FUNC_NAME, &xy);
  4966. return scm_c_make_rectangular (xy.x, xy.y);
  4967. }
  4968. #undef FUNC_NAME
  4969. SCM
  4970. scm_c_make_polar (double mag, double ang)
  4971. {
  4972. double s, c;
  4973. /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
  4974. use it on Glibc-based systems that have it (it's a GNU extension). See
  4975. http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
  4976. details. */
  4977. #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
  4978. sincos (ang, &s, &c);
  4979. #else
  4980. s = sin (ang);
  4981. c = cos (ang);
  4982. #endif
  4983. return scm_c_make_rectangular (mag * c, mag * s);
  4984. }
  4985. SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
  4986. (SCM x, SCM y),
  4987. "Return the complex number @var{x} * e^(i * @var{y}).")
  4988. #define FUNC_NAME s_scm_make_polar
  4989. {
  4990. struct dpair xy;
  4991. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4992. return scm_c_make_polar (xy.x, xy.y);
  4993. }
  4994. #undef FUNC_NAME
  4995. SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
  4996. /* "Return the real part of the number @var{z}."
  4997. */
  4998. SCM
  4999. scm_real_part (SCM z)
  5000. {
  5001. if (SCM_I_INUMP (z))
  5002. return z;
  5003. else if (SCM_BIGP (z))
  5004. return z;
  5005. else if (SCM_REALP (z))
  5006. return z;
  5007. else if (SCM_COMPLEXP (z))
  5008. return scm_from_double (SCM_COMPLEX_REAL (z));
  5009. else if (SCM_FRACTIONP (z))
  5010. return z;
  5011. else
  5012. SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part);
  5013. }
  5014. SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
  5015. /* "Return the imaginary part of the number @var{z}."
  5016. */
  5017. SCM
  5018. scm_imag_part (SCM z)
  5019. {
  5020. if (SCM_I_INUMP (z))
  5021. return SCM_INUM0;
  5022. else if (SCM_BIGP (z))
  5023. return SCM_INUM0;
  5024. else if (SCM_REALP (z))
  5025. return scm_flo0;
  5026. else if (SCM_COMPLEXP (z))
  5027. return scm_from_double (SCM_COMPLEX_IMAG (z));
  5028. else if (SCM_FRACTIONP (z))
  5029. return SCM_INUM0;
  5030. else
  5031. SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part);
  5032. }
  5033. SCM_GPROC (s_numerator, "numerator", 1, 0, 0, scm_numerator, g_numerator);
  5034. /* "Return the numerator of the number @var{z}."
  5035. */
  5036. SCM
  5037. scm_numerator (SCM z)
  5038. {
  5039. if (SCM_I_INUMP (z))
  5040. return z;
  5041. else if (SCM_BIGP (z))
  5042. return z;
  5043. else if (SCM_FRACTIONP (z))
  5044. return SCM_FRACTION_NUMERATOR (z);
  5045. else if (SCM_REALP (z))
  5046. return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
  5047. else
  5048. SCM_WTA_DISPATCH_1 (g_numerator, z, SCM_ARG1, s_numerator);
  5049. }
  5050. SCM_GPROC (s_denominator, "denominator", 1, 0, 0, scm_denominator, g_denominator);
  5051. /* "Return the denominator of the number @var{z}."
  5052. */
  5053. SCM
  5054. scm_denominator (SCM z)
  5055. {
  5056. if (SCM_I_INUMP (z))
  5057. return SCM_I_MAKINUM (1);
  5058. else if (SCM_BIGP (z))
  5059. return SCM_I_MAKINUM (1);
  5060. else if (SCM_FRACTIONP (z))
  5061. return SCM_FRACTION_DENOMINATOR (z);
  5062. else if (SCM_REALP (z))
  5063. return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
  5064. else
  5065. SCM_WTA_DISPATCH_1 (g_denominator, z, SCM_ARG1, s_denominator);
  5066. }
  5067. SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
  5068. /* "Return the magnitude of the number @var{z}. This is the same as\n"
  5069. * "@code{abs} for real arguments, but also allows complex numbers."
  5070. */
  5071. SCM
  5072. scm_magnitude (SCM z)
  5073. {
  5074. if (SCM_I_INUMP (z))
  5075. {
  5076. long int zz = SCM_I_INUM (z);
  5077. if (zz >= 0)
  5078. return z;
  5079. else if (SCM_POSFIXABLE (-zz))
  5080. return SCM_I_MAKINUM (-zz);
  5081. else
  5082. return scm_i_long2big (-zz);
  5083. }
  5084. else if (SCM_BIGP (z))
  5085. {
  5086. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  5087. scm_remember_upto_here_1 (z);
  5088. if (sgn < 0)
  5089. return scm_i_clonebig (z, 0);
  5090. else
  5091. return z;
  5092. }
  5093. else if (SCM_REALP (z))
  5094. return scm_from_double (fabs (SCM_REAL_VALUE (z)));
  5095. else if (SCM_COMPLEXP (z))
  5096. return scm_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
  5097. else if (SCM_FRACTIONP (z))
  5098. {
  5099. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  5100. return z;
  5101. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
  5102. SCM_FRACTION_DENOMINATOR (z));
  5103. }
  5104. else
  5105. SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
  5106. }
  5107. SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
  5108. /* "Return the angle of the complex number @var{z}."
  5109. */
  5110. SCM
  5111. scm_angle (SCM z)
  5112. {
  5113. /* atan(0,-1) is pi and it'd be possible to have that as a constant like
  5114. scm_flo0 to save allocating a new flonum with scm_from_double each time.
  5115. But if atan2 follows the floating point rounding mode, then the value
  5116. is not a constant. Maybe it'd be close enough though. */
  5117. if (SCM_I_INUMP (z))
  5118. {
  5119. if (SCM_I_INUM (z) >= 0)
  5120. return scm_flo0;
  5121. else
  5122. return scm_from_double (atan2 (0.0, -1.0));
  5123. }
  5124. else if (SCM_BIGP (z))
  5125. {
  5126. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  5127. scm_remember_upto_here_1 (z);
  5128. if (sgn < 0)
  5129. return scm_from_double (atan2 (0.0, -1.0));
  5130. else
  5131. return scm_flo0;
  5132. }
  5133. else if (SCM_REALP (z))
  5134. {
  5135. if (SCM_REAL_VALUE (z) >= 0)
  5136. return scm_flo0;
  5137. else
  5138. return scm_from_double (atan2 (0.0, -1.0));
  5139. }
  5140. else if (SCM_COMPLEXP (z))
  5141. return scm_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
  5142. else if (SCM_FRACTIONP (z))
  5143. {
  5144. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  5145. return scm_flo0;
  5146. else return scm_from_double (atan2 (0.0, -1.0));
  5147. }
  5148. else
  5149. SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle);
  5150. }
  5151. SCM_GPROC (s_exact_to_inexact, "exact->inexact", 1, 0, 0, scm_exact_to_inexact, g_exact_to_inexact);
  5152. /* Convert the number @var{x} to its inexact representation.\n"
  5153. */
  5154. SCM
  5155. scm_exact_to_inexact (SCM z)
  5156. {
  5157. if (SCM_I_INUMP (z))
  5158. return scm_from_double ((double) SCM_I_INUM (z));
  5159. else if (SCM_BIGP (z))
  5160. return scm_from_double (scm_i_big2dbl (z));
  5161. else if (SCM_FRACTIONP (z))
  5162. return scm_from_double (scm_i_fraction2double (z));
  5163. else if (SCM_INEXACTP (z))
  5164. return z;
  5165. else
  5166. SCM_WTA_DISPATCH_1 (g_exact_to_inexact, z, 1, s_exact_to_inexact);
  5167. }
  5168. SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
  5169. (SCM z),
  5170. "Return an exact number that is numerically closest to @var{z}.")
  5171. #define FUNC_NAME s_scm_inexact_to_exact
  5172. {
  5173. if (SCM_I_INUMP (z))
  5174. return z;
  5175. else if (SCM_BIGP (z))
  5176. return z;
  5177. else if (SCM_REALP (z))
  5178. {
  5179. if (xisinf (SCM_REAL_VALUE (z)) || xisnan (SCM_REAL_VALUE (z)))
  5180. SCM_OUT_OF_RANGE (1, z);
  5181. else
  5182. {
  5183. mpq_t frac;
  5184. SCM q;
  5185. mpq_init (frac);
  5186. mpq_set_d (frac, SCM_REAL_VALUE (z));
  5187. q = scm_i_make_ratio (scm_i_mpz2num (mpq_numref (frac)),
  5188. scm_i_mpz2num (mpq_denref (frac)));
  5189. /* When scm_i_make_ratio throws, we leak the memory allocated
  5190. for frac...
  5191. */
  5192. mpq_clear (frac);
  5193. return q;
  5194. }
  5195. }
  5196. else if (SCM_FRACTIONP (z))
  5197. return z;
  5198. else
  5199. SCM_WRONG_TYPE_ARG (1, z);
  5200. }
  5201. #undef FUNC_NAME
  5202. SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
  5203. (SCM x, SCM eps),
  5204. "Returns the @emph{simplest} rational number differing\n"
  5205. "from @var{x} by no more than @var{eps}.\n"
  5206. "\n"
  5207. "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
  5208. "exact result when both its arguments are exact. Thus, you might need\n"
  5209. "to use @code{inexact->exact} on the arguments.\n"
  5210. "\n"
  5211. "@lisp\n"
  5212. "(rationalize (inexact->exact 1.2) 1/100)\n"
  5213. "@result{} 6/5\n"
  5214. "@end lisp")
  5215. #define FUNC_NAME s_scm_rationalize
  5216. {
  5217. if (SCM_I_INUMP (x))
  5218. return x;
  5219. else if (SCM_BIGP (x))
  5220. return x;
  5221. else if ((SCM_REALP (x)) || SCM_FRACTIONP (x))
  5222. {
  5223. /* Use continued fractions to find closest ratio. All
  5224. arithmetic is done with exact numbers.
  5225. */
  5226. SCM ex = scm_inexact_to_exact (x);
  5227. SCM int_part = scm_floor (ex);
  5228. SCM tt = SCM_I_MAKINUM (1);
  5229. SCM a1 = SCM_I_MAKINUM (0), a2 = SCM_I_MAKINUM (1), a = SCM_I_MAKINUM (0);
  5230. SCM b1 = SCM_I_MAKINUM (1), b2 = SCM_I_MAKINUM (0), b = SCM_I_MAKINUM (0);
  5231. SCM rx;
  5232. int i = 0;
  5233. if (scm_is_true (scm_num_eq_p (ex, int_part)))
  5234. return ex;
  5235. ex = scm_difference (ex, int_part); /* x = x-int_part */
  5236. rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */
  5237. /* We stop after a million iterations just to be absolutely sure
  5238. that we don't go into an infinite loop. The process normally
  5239. converges after less than a dozen iterations.
  5240. */
  5241. eps = scm_abs (eps);
  5242. while (++i < 1000000)
  5243. {
  5244. a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */
  5245. b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */
  5246. if (scm_is_false (scm_zero_p (b)) && /* b != 0 */
  5247. scm_is_false
  5248. (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
  5249. eps))) /* abs(x-a/b) <= eps */
  5250. {
  5251. SCM res = scm_sum (int_part, scm_divide (a, b));
  5252. if (scm_is_false (scm_exact_p (x))
  5253. || scm_is_false (scm_exact_p (eps)))
  5254. return scm_exact_to_inexact (res);
  5255. else
  5256. return res;
  5257. }
  5258. rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */
  5259. SCM_UNDEFINED);
  5260. tt = scm_floor (rx); /* tt = floor (rx) */
  5261. a2 = a1;
  5262. b2 = b1;
  5263. a1 = a;
  5264. b1 = b;
  5265. }
  5266. scm_num_overflow (s_scm_rationalize);
  5267. }
  5268. else
  5269. SCM_WRONG_TYPE_ARG (1, x);
  5270. }
  5271. #undef FUNC_NAME
  5272. /* conversion functions */
  5273. int
  5274. scm_is_integer (SCM val)
  5275. {
  5276. return scm_is_true (scm_integer_p (val));
  5277. }
  5278. int
  5279. scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max)
  5280. {
  5281. if (SCM_I_INUMP (val))
  5282. {
  5283. scm_t_signed_bits n = SCM_I_INUM (val);
  5284. return n >= min && n <= max;
  5285. }
  5286. else if (SCM_BIGP (val))
  5287. {
  5288. if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
  5289. return 0;
  5290. else if (min >= LONG_MIN && max <= LONG_MAX)
  5291. {
  5292. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
  5293. {
  5294. long n = mpz_get_si (SCM_I_BIG_MPZ (val));
  5295. return n >= min && n <= max;
  5296. }
  5297. else
  5298. return 0;
  5299. }
  5300. else
  5301. {
  5302. scm_t_intmax n;
  5303. size_t count;
  5304. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  5305. > CHAR_BIT*sizeof (scm_t_uintmax))
  5306. return 0;
  5307. mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
  5308. SCM_I_BIG_MPZ (val));
  5309. if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
  5310. {
  5311. if (n < 0)
  5312. return 0;
  5313. }
  5314. else
  5315. {
  5316. n = -n;
  5317. if (n >= 0)
  5318. return 0;
  5319. }
  5320. return n >= min && n <= max;
  5321. }
  5322. }
  5323. else
  5324. return 0;
  5325. }
  5326. int
  5327. scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max)
  5328. {
  5329. if (SCM_I_INUMP (val))
  5330. {
  5331. scm_t_signed_bits n = SCM_I_INUM (val);
  5332. return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max;
  5333. }
  5334. else if (SCM_BIGP (val))
  5335. {
  5336. if (max <= SCM_MOST_POSITIVE_FIXNUM)
  5337. return 0;
  5338. else if (max <= ULONG_MAX)
  5339. {
  5340. if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
  5341. {
  5342. unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
  5343. return n >= min && n <= max;
  5344. }
  5345. else
  5346. return 0;
  5347. }
  5348. else
  5349. {
  5350. scm_t_uintmax n;
  5351. size_t count;
  5352. if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
  5353. return 0;
  5354. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  5355. > CHAR_BIT*sizeof (scm_t_uintmax))
  5356. return 0;
  5357. mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
  5358. SCM_I_BIG_MPZ (val));
  5359. return n >= min && n <= max;
  5360. }
  5361. }
  5362. else
  5363. return 0;
  5364. }
  5365. static void
  5366. scm_i_range_error (SCM bad_val, SCM min, SCM max)
  5367. {
  5368. scm_error (scm_out_of_range_key,
  5369. NULL,
  5370. "Value out of range ~S to ~S: ~S",
  5371. scm_list_3 (min, max, bad_val),
  5372. scm_list_1 (bad_val));
  5373. }
  5374. #define TYPE scm_t_intmax
  5375. #define TYPE_MIN min
  5376. #define TYPE_MAX max
  5377. #define SIZEOF_TYPE 0
  5378. #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max)
  5379. #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
  5380. #include "libguile/conv-integer.i.c"
  5381. #define TYPE scm_t_uintmax
  5382. #define TYPE_MIN min
  5383. #define TYPE_MAX max
  5384. #define SIZEOF_TYPE 0
  5385. #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max)
  5386. #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
  5387. #include "libguile/conv-uinteger.i.c"
  5388. #define TYPE scm_t_int8
  5389. #define TYPE_MIN SCM_T_INT8_MIN
  5390. #define TYPE_MAX SCM_T_INT8_MAX
  5391. #define SIZEOF_TYPE 1
  5392. #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
  5393. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
  5394. #include "libguile/conv-integer.i.c"
  5395. #define TYPE scm_t_uint8
  5396. #define TYPE_MIN 0
  5397. #define TYPE_MAX SCM_T_UINT8_MAX
  5398. #define SIZEOF_TYPE 1
  5399. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
  5400. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
  5401. #include "libguile/conv-uinteger.i.c"
  5402. #define TYPE scm_t_int16
  5403. #define TYPE_MIN SCM_T_INT16_MIN
  5404. #define TYPE_MAX SCM_T_INT16_MAX
  5405. #define SIZEOF_TYPE 2
  5406. #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
  5407. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
  5408. #include "libguile/conv-integer.i.c"
  5409. #define TYPE scm_t_uint16
  5410. #define TYPE_MIN 0
  5411. #define TYPE_MAX SCM_T_UINT16_MAX
  5412. #define SIZEOF_TYPE 2
  5413. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
  5414. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
  5415. #include "libguile/conv-uinteger.i.c"
  5416. #define TYPE scm_t_int32
  5417. #define TYPE_MIN SCM_T_INT32_MIN
  5418. #define TYPE_MAX SCM_T_INT32_MAX
  5419. #define SIZEOF_TYPE 4
  5420. #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
  5421. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
  5422. #include "libguile/conv-integer.i.c"
  5423. #define TYPE scm_t_uint32
  5424. #define TYPE_MIN 0
  5425. #define TYPE_MAX SCM_T_UINT32_MAX
  5426. #define SIZEOF_TYPE 4
  5427. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
  5428. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
  5429. #include "libguile/conv-uinteger.i.c"
  5430. #define TYPE scm_t_int64
  5431. #define TYPE_MIN SCM_T_INT64_MIN
  5432. #define TYPE_MAX SCM_T_INT64_MAX
  5433. #define SIZEOF_TYPE 8
  5434. #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
  5435. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
  5436. #include "libguile/conv-integer.i.c"
  5437. #define TYPE scm_t_uint64
  5438. #define TYPE_MIN 0
  5439. #define TYPE_MAX SCM_T_UINT64_MAX
  5440. #define SIZEOF_TYPE 8
  5441. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
  5442. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
  5443. #include "libguile/conv-uinteger.i.c"
  5444. void
  5445. scm_to_mpz (SCM val, mpz_t rop)
  5446. {
  5447. if (SCM_I_INUMP (val))
  5448. mpz_set_si (rop, SCM_I_INUM (val));
  5449. else if (SCM_BIGP (val))
  5450. mpz_set (rop, SCM_I_BIG_MPZ (val));
  5451. else
  5452. scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
  5453. }
  5454. SCM
  5455. scm_from_mpz (mpz_t val)
  5456. {
  5457. return scm_i_mpz2num (val);
  5458. }
  5459. int
  5460. scm_is_real (SCM val)
  5461. {
  5462. return scm_is_true (scm_real_p (val));
  5463. }
  5464. int
  5465. scm_is_rational (SCM val)
  5466. {
  5467. return scm_is_true (scm_rational_p (val));
  5468. }
  5469. double
  5470. scm_to_double (SCM val)
  5471. {
  5472. if (SCM_I_INUMP (val))
  5473. return SCM_I_INUM (val);
  5474. else if (SCM_BIGP (val))
  5475. return scm_i_big2dbl (val);
  5476. else if (SCM_FRACTIONP (val))
  5477. return scm_i_fraction2double (val);
  5478. else if (SCM_REALP (val))
  5479. return SCM_REAL_VALUE (val);
  5480. else
  5481. scm_wrong_type_arg_msg (NULL, 0, val, "real number");
  5482. }
  5483. SCM
  5484. scm_from_double (double val)
  5485. {
  5486. SCM z = scm_double_cell (scm_tc16_real, 0, 0, 0);
  5487. SCM_REAL_VALUE (z) = val;
  5488. return z;
  5489. }
  5490. #if SCM_ENABLE_DISCOURAGED == 1
  5491. float
  5492. scm_num2float (SCM num, unsigned long int pos, const char *s_caller)
  5493. {
  5494. if (SCM_BIGP (num))
  5495. {
  5496. float res = mpz_get_d (SCM_I_BIG_MPZ (num));
  5497. if (!xisinf (res))
  5498. return res;
  5499. else
  5500. scm_out_of_range (NULL, num);
  5501. }
  5502. else
  5503. return scm_to_double (num);
  5504. }
  5505. double
  5506. scm_num2double (SCM num, unsigned long int pos, const char *s_caller)
  5507. {
  5508. if (SCM_BIGP (num))
  5509. {
  5510. double res = mpz_get_d (SCM_I_BIG_MPZ (num));
  5511. if (!xisinf (res))
  5512. return res;
  5513. else
  5514. scm_out_of_range (NULL, num);
  5515. }
  5516. else
  5517. return scm_to_double (num);
  5518. }
  5519. #endif
  5520. int
  5521. scm_is_complex (SCM val)
  5522. {
  5523. return scm_is_true (scm_complex_p (val));
  5524. }
  5525. double
  5526. scm_c_real_part (SCM z)
  5527. {
  5528. if (SCM_COMPLEXP (z))
  5529. return SCM_COMPLEX_REAL (z);
  5530. else
  5531. {
  5532. /* Use the scm_real_part to get proper error checking and
  5533. dispatching.
  5534. */
  5535. return scm_to_double (scm_real_part (z));
  5536. }
  5537. }
  5538. double
  5539. scm_c_imag_part (SCM z)
  5540. {
  5541. if (SCM_COMPLEXP (z))
  5542. return SCM_COMPLEX_IMAG (z);
  5543. else
  5544. {
  5545. /* Use the scm_imag_part to get proper error checking and
  5546. dispatching. The result will almost always be 0.0, but not
  5547. always.
  5548. */
  5549. return scm_to_double (scm_imag_part (z));
  5550. }
  5551. }
  5552. double
  5553. scm_c_magnitude (SCM z)
  5554. {
  5555. return scm_to_double (scm_magnitude (z));
  5556. }
  5557. double
  5558. scm_c_angle (SCM z)
  5559. {
  5560. return scm_to_double (scm_angle (z));
  5561. }
  5562. int
  5563. scm_is_number (SCM z)
  5564. {
  5565. return scm_is_true (scm_number_p (z));
  5566. }
  5567. /* In the following functions we dispatch to the real-arg funcs like log()
  5568. when we know the arg is real, instead of just handing everything to
  5569. clog() for instance. This is in case clog() doesn't optimize for a
  5570. real-only case, and because we have to test SCM_COMPLEXP anyway so may as
  5571. well use it to go straight to the applicable C func. */
  5572. SCM_DEFINE (scm_log, "log", 1, 0, 0,
  5573. (SCM z),
  5574. "Return the natural logarithm of @var{z}.")
  5575. #define FUNC_NAME s_scm_log
  5576. {
  5577. if (SCM_COMPLEXP (z))
  5578. {
  5579. #if HAVE_COMPLEX_DOUBLE && HAVE_CLOG && defined (SCM_COMPLEX_VALUE)
  5580. return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
  5581. #else
  5582. double re = SCM_COMPLEX_REAL (z);
  5583. double im = SCM_COMPLEX_IMAG (z);
  5584. return scm_c_make_rectangular (log (hypot (re, im)),
  5585. atan2 (im, re));
  5586. #endif
  5587. }
  5588. else
  5589. {
  5590. /* ENHANCE-ME: When z is a bignum the logarithm will fit a double
  5591. although the value itself overflows. */
  5592. double re = scm_to_double (z);
  5593. double l = log (fabs (re));
  5594. if (re >= 0.0)
  5595. return scm_from_double (l);
  5596. else
  5597. return scm_c_make_rectangular (l, M_PI);
  5598. }
  5599. }
  5600. #undef FUNC_NAME
  5601. SCM_DEFINE (scm_log10, "log10", 1, 0, 0,
  5602. (SCM z),
  5603. "Return the base 10 logarithm of @var{z}.")
  5604. #define FUNC_NAME s_scm_log10
  5605. {
  5606. if (SCM_COMPLEXP (z))
  5607. {
  5608. /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
  5609. clog() and a multiply by M_LOG10E, rather than the fallback
  5610. log10+hypot+atan2.) */
  5611. #if HAVE_COMPLEX_DOUBLE && HAVE_CLOG10 && defined (SCM_COMPLEX_VALUE)
  5612. return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
  5613. #else
  5614. double re = SCM_COMPLEX_REAL (z);
  5615. double im = SCM_COMPLEX_IMAG (z);
  5616. return scm_c_make_rectangular (log10 (hypot (re, im)),
  5617. M_LOG10E * atan2 (im, re));
  5618. #endif
  5619. }
  5620. else
  5621. {
  5622. /* ENHANCE-ME: When z is a bignum the logarithm will fit a double
  5623. although the value itself overflows. */
  5624. double re = scm_to_double (z);
  5625. double l = log10 (fabs (re));
  5626. if (re >= 0.0)
  5627. return scm_from_double (l);
  5628. else
  5629. return scm_c_make_rectangular (l, M_LOG10E * M_PI);
  5630. }
  5631. }
  5632. #undef FUNC_NAME
  5633. SCM_DEFINE (scm_exp, "exp", 1, 0, 0,
  5634. (SCM z),
  5635. "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
  5636. "base of natural logarithms (2.71828@dots{}).")
  5637. #define FUNC_NAME s_scm_exp
  5638. {
  5639. if (SCM_COMPLEXP (z))
  5640. {
  5641. #if HAVE_COMPLEX_DOUBLE && HAVE_CEXP && defined (SCM_COMPLEX_VALUE)
  5642. return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
  5643. #else
  5644. return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
  5645. SCM_COMPLEX_IMAG (z));
  5646. #endif
  5647. }
  5648. else
  5649. {
  5650. /* When z is a negative bignum the conversion to double overflows,
  5651. giving -infinity, but that's ok, the exp is still 0.0. */
  5652. return scm_from_double (exp (scm_to_double (z)));
  5653. }
  5654. }
  5655. #undef FUNC_NAME
  5656. SCM_DEFINE (scm_sqrt, "sqrt", 1, 0, 0,
  5657. (SCM x),
  5658. "Return the square root of @var{z}. Of the two possible roots\n"
  5659. "(positive and negative), the one with the a positive real part\n"
  5660. "is returned, or if that's zero then a positive imaginary part.\n"
  5661. "Thus,\n"
  5662. "\n"
  5663. "@example\n"
  5664. "(sqrt 9.0) @result{} 3.0\n"
  5665. "(sqrt -9.0) @result{} 0.0+3.0i\n"
  5666. "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
  5667. "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
  5668. "@end example")
  5669. #define FUNC_NAME s_scm_sqrt
  5670. {
  5671. if (SCM_COMPLEXP (x))
  5672. {
  5673. #if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT && defined (SCM_COMPLEX_VALUE)
  5674. return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));
  5675. #else
  5676. double re = SCM_COMPLEX_REAL (x);
  5677. double im = SCM_COMPLEX_IMAG (x);
  5678. return scm_c_make_polar (sqrt (hypot (re, im)),
  5679. 0.5 * atan2 (im, re));
  5680. #endif
  5681. }
  5682. else
  5683. {
  5684. double xx = scm_to_double (x);
  5685. if (xx < 0)
  5686. return scm_c_make_rectangular (0.0, sqrt (-xx));
  5687. else
  5688. return scm_from_double (sqrt (xx));
  5689. }
  5690. }
  5691. #undef FUNC_NAME
  5692. void
  5693. scm_init_numbers ()
  5694. {
  5695. int i;
  5696. mpz_init_set_si (z_negative_one, -1);
  5697. /* It may be possible to tune the performance of some algorithms by using
  5698. * the following constants to avoid the creation of bignums. Please, before
  5699. * using these values, remember the two rules of program optimization:
  5700. * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
  5701. scm_c_define ("most-positive-fixnum",
  5702. SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
  5703. scm_c_define ("most-negative-fixnum",
  5704. SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
  5705. scm_add_feature ("complex");
  5706. scm_add_feature ("inexact");
  5707. scm_flo0 = scm_from_double (0.0);
  5708. /* determine floating point precision */
  5709. for (i=2; i <= SCM_MAX_DBL_RADIX; ++i)
  5710. {
  5711. init_dblprec(&scm_dblprec[i-2],i);
  5712. init_fx_radix(fx_per_radix[i-2],i);
  5713. }
  5714. #ifdef DBL_DIG
  5715. /* hard code precision for base 10 if the preprocessor tells us to... */
  5716. scm_dblprec[10-2] = (DBL_DIG > 20) ? 20 : DBL_DIG;
  5717. #endif
  5718. exactly_one_half = scm_permanent_object (scm_divide (SCM_I_MAKINUM (1),
  5719. SCM_I_MAKINUM (2)));
  5720. #include "libguile/numbers.x"
  5721. }
  5722. /*
  5723. Local Variables:
  5724. c-file-style: "gnu"
  5725. End:
  5726. */