numbers.c 161 KB

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