numbers.c 300 KB

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