numbers.c 296 KB

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