numbers.c 299 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434
  1. /* Copyright 1995-2016,2018-2020
  2. Free Software Foundation, Inc.
  3. Portions Copyright 1990-1993 by AT&T Bell Laboratories and Bellcore.
  4. See scm_divide.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. /* General assumptions:
  18. * All objects satisfying SCM_BIGP() are too large to fit in a fixnum.
  19. * If an object satisfies integer?, it's either an inum, a bignum, or a real.
  20. * If floor (r) == r, r is an int, and mpz_set_d will DTRT.
  21. * XXX What about infinities? They are equal to their own floor! -mhw
  22. * All objects satisfying SCM_FRACTIONP are never an integer.
  23. */
  24. /* TODO:
  25. - see if special casing bignums and reals in integer-exponent when
  26. possible (to use mpz_pow and mpf_pow_ui) is faster.
  27. - look in to better short-circuiting of common cases in
  28. integer-expt and elsewhere.
  29. - see if direct mpz operations can help in ash and elsewhere.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <assert.h>
  35. #include <math.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <unicase.h>
  39. #include <unictype.h>
  40. #include <verify.h>
  41. #if HAVE_COMPLEX_H
  42. #include <complex.h>
  43. #endif
  44. #include "bdw-gc.h"
  45. #include "boolean.h"
  46. #include "deprecation.h"
  47. #include "eq.h"
  48. #include "feature.h"
  49. #include "finalizers.h"
  50. #include "goops.h"
  51. #include "gsubr.h"
  52. #include "modules.h"
  53. #include "pairs.h"
  54. #include "ports.h"
  55. #include "smob.h"
  56. #include "strings.h"
  57. #include "values.h"
  58. #include "numbers.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_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_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_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_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_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. #define MIN(A, B) ((A) <= (B) ? (A) : (B))
  4806. SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
  4807. (SCM n, SCM start, SCM end),
  4808. "Return the integer composed of the @var{start} (inclusive)\n"
  4809. "through @var{end} (exclusive) bits of @var{n}. The\n"
  4810. "@var{start}th bit becomes the 0-th bit in the result.\n"
  4811. "\n"
  4812. "@lisp\n"
  4813. "(number->string (bit-extract #b1101101010 0 4) 2)\n"
  4814. " @result{} \"1010\"\n"
  4815. "(number->string (bit-extract #b1101101010 4 9) 2)\n"
  4816. " @result{} \"10110\"\n"
  4817. "@end lisp")
  4818. #define FUNC_NAME s_scm_bit_extract
  4819. {
  4820. unsigned long int istart, iend, bits;
  4821. istart = scm_to_ulong (start);
  4822. iend = scm_to_ulong (end);
  4823. SCM_ASSERT_RANGE (3, end, (iend >= istart));
  4824. /* how many bits to keep */
  4825. bits = iend - istart;
  4826. if (SCM_I_INUMP (n))
  4827. {
  4828. scm_t_inum in = SCM_I_INUM (n);
  4829. /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
  4830. SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
  4831. in = SCM_SRS (in, MIN (istart, SCM_I_FIXNUM_BIT-1));
  4832. if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
  4833. {
  4834. /* Since we emulate two's complement encoded numbers, this
  4835. * special case requires us to produce a result that has
  4836. * more bits than can be stored in a fixnum.
  4837. */
  4838. SCM result = scm_i_inum2big (in);
  4839. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  4840. bits);
  4841. return result;
  4842. }
  4843. /* mask down to requisite bits */
  4844. bits = MIN (bits, SCM_I_FIXNUM_BIT);
  4845. return SCM_I_MAKINUM (in & ((1L << bits) - 1));
  4846. }
  4847. else if (SCM_BIGP (n))
  4848. {
  4849. SCM result;
  4850. if (bits == 1)
  4851. {
  4852. result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
  4853. }
  4854. else
  4855. {
  4856. /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
  4857. bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
  4858. such bits into a ulong. */
  4859. result = scm_i_mkbig ();
  4860. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
  4861. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
  4862. result = scm_i_normbig (result);
  4863. }
  4864. scm_remember_upto_here_1 (n);
  4865. return result;
  4866. }
  4867. else
  4868. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4869. }
  4870. #undef FUNC_NAME
  4871. static const char scm_logtab[] = {
  4872. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  4873. };
  4874. SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
  4875. (SCM n),
  4876. "Return the number of bits in integer @var{n}. If integer is\n"
  4877. "positive, the 1-bits in its binary representation are counted.\n"
  4878. "If negative, the 0-bits in its two's-complement binary\n"
  4879. "representation are counted. If 0, 0 is returned.\n"
  4880. "\n"
  4881. "@lisp\n"
  4882. "(logcount #b10101010)\n"
  4883. " @result{} 4\n"
  4884. "(logcount 0)\n"
  4885. " @result{} 0\n"
  4886. "(logcount -2)\n"
  4887. " @result{} 1\n"
  4888. "@end lisp")
  4889. #define FUNC_NAME s_scm_logcount
  4890. {
  4891. if (SCM_I_INUMP (n))
  4892. {
  4893. unsigned long c = 0;
  4894. scm_t_inum nn = SCM_I_INUM (n);
  4895. if (nn < 0)
  4896. nn = -1 - nn;
  4897. while (nn)
  4898. {
  4899. c += scm_logtab[15 & nn];
  4900. nn >>= 4;
  4901. }
  4902. return SCM_I_MAKINUM (c);
  4903. }
  4904. else if (SCM_BIGP (n))
  4905. {
  4906. unsigned long count;
  4907. if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
  4908. count = mpz_popcount (SCM_I_BIG_MPZ (n));
  4909. else
  4910. count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
  4911. scm_remember_upto_here_1 (n);
  4912. return SCM_I_MAKINUM (count);
  4913. }
  4914. else
  4915. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4916. }
  4917. #undef FUNC_NAME
  4918. static const char scm_ilentab[] = {
  4919. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
  4920. };
  4921. SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
  4922. (SCM n),
  4923. "Return the number of bits necessary to represent @var{n}.\n"
  4924. "\n"
  4925. "@lisp\n"
  4926. "(integer-length #b10101010)\n"
  4927. " @result{} 8\n"
  4928. "(integer-length 0)\n"
  4929. " @result{} 0\n"
  4930. "(integer-length #b1111)\n"
  4931. " @result{} 4\n"
  4932. "@end lisp")
  4933. #define FUNC_NAME s_scm_integer_length
  4934. {
  4935. if (SCM_I_INUMP (n))
  4936. {
  4937. unsigned long c = 0;
  4938. unsigned int l = 4;
  4939. scm_t_inum nn = SCM_I_INUM (n);
  4940. if (nn < 0)
  4941. nn = -1 - nn;
  4942. while (nn)
  4943. {
  4944. c += 4;
  4945. l = scm_ilentab [15 & nn];
  4946. nn >>= 4;
  4947. }
  4948. return SCM_I_MAKINUM (c - 4 + l);
  4949. }
  4950. else if (SCM_BIGP (n))
  4951. {
  4952. /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
  4953. want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
  4954. 1 too big, so check for that and adjust. */
  4955. size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
  4956. if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
  4957. && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
  4958. mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
  4959. size--;
  4960. scm_remember_upto_here_1 (n);
  4961. return SCM_I_MAKINUM (size);
  4962. }
  4963. else
  4964. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  4965. }
  4966. #undef FUNC_NAME
  4967. /*** NUMBERS -> STRINGS ***/
  4968. #define SCM_MAX_DBL_RADIX 36
  4969. /* use this array as a way to generate a single digit */
  4970. static const char number_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  4971. static mpz_t dbl_minimum_normal_mantissa;
  4972. static size_t
  4973. idbl2str (double dbl, char *a, int radix)
  4974. {
  4975. int ch = 0;
  4976. if (radix < 2 || radix > SCM_MAX_DBL_RADIX)
  4977. /* revert to existing behavior */
  4978. radix = 10;
  4979. if (isinf (dbl))
  4980. {
  4981. strcpy (a, (dbl > 0.0) ? "+inf.0" : "-inf.0");
  4982. return 6;
  4983. }
  4984. else if (dbl > 0.0)
  4985. ;
  4986. else if (dbl < 0.0)
  4987. {
  4988. dbl = -dbl;
  4989. a[ch++] = '-';
  4990. }
  4991. else if (dbl == 0.0)
  4992. {
  4993. if (copysign (1.0, dbl) < 0.0)
  4994. a[ch++] = '-';
  4995. strcpy (a + ch, "0.0");
  4996. return ch + 3;
  4997. }
  4998. else if (isnan (dbl))
  4999. {
  5000. strcpy (a, "+nan.0");
  5001. return 6;
  5002. }
  5003. /* Algorithm taken from "Printing Floating-Point Numbers Quickly and
  5004. Accurately" by Robert G. Burger and R. Kent Dybvig */
  5005. {
  5006. int e, k;
  5007. mpz_t f, r, s, mplus, mminus, hi, digit;
  5008. int f_is_even, f_is_odd;
  5009. int expon;
  5010. int show_exp = 0;
  5011. mpz_inits (f, r, s, mplus, mminus, hi, digit, NULL);
  5012. mpz_set_d (f, ldexp (frexp (dbl, &e), DBL_MANT_DIG));
  5013. if (e < DBL_MIN_EXP)
  5014. {
  5015. mpz_tdiv_q_2exp (f, f, DBL_MIN_EXP - e);
  5016. e = DBL_MIN_EXP;
  5017. }
  5018. e -= DBL_MANT_DIG;
  5019. f_is_even = !mpz_odd_p (f);
  5020. f_is_odd = !f_is_even;
  5021. /* Initialize r, s, mplus, and mminus according
  5022. to Table 1 from the paper. */
  5023. if (e < 0)
  5024. {
  5025. mpz_set_ui (mminus, 1);
  5026. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0
  5027. || e == DBL_MIN_EXP - DBL_MANT_DIG)
  5028. {
  5029. mpz_set_ui (mplus, 1);
  5030. mpz_mul_2exp (r, f, 1);
  5031. mpz_mul_2exp (s, mminus, 1 - e);
  5032. }
  5033. else
  5034. {
  5035. mpz_set_ui (mplus, 2);
  5036. mpz_mul_2exp (r, f, 2);
  5037. mpz_mul_2exp (s, mminus, 2 - e);
  5038. }
  5039. }
  5040. else
  5041. {
  5042. mpz_set_ui (mminus, 1);
  5043. mpz_mul_2exp (mminus, mminus, e);
  5044. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0)
  5045. {
  5046. mpz_set (mplus, mminus);
  5047. mpz_mul_2exp (r, f, 1 + e);
  5048. mpz_set_ui (s, 2);
  5049. }
  5050. else
  5051. {
  5052. mpz_mul_2exp (mplus, mminus, 1);
  5053. mpz_mul_2exp (r, f, 2 + e);
  5054. mpz_set_ui (s, 4);
  5055. }
  5056. }
  5057. /* Find the smallest k such that:
  5058. (r + mplus) / s < radix^k (if f is even)
  5059. (r + mplus) / s <= radix^k (if f is odd) */
  5060. {
  5061. /* IMPROVE-ME: Make an initial guess to speed this up */
  5062. mpz_add (hi, r, mplus);
  5063. k = 0;
  5064. while (mpz_cmp (hi, s) >= f_is_odd)
  5065. {
  5066. mpz_mul_ui (s, s, radix);
  5067. k++;
  5068. }
  5069. if (k == 0)
  5070. {
  5071. mpz_mul_ui (hi, hi, radix);
  5072. while (mpz_cmp (hi, s) < f_is_odd)
  5073. {
  5074. mpz_mul_ui (r, r, radix);
  5075. mpz_mul_ui (mplus, mplus, radix);
  5076. mpz_mul_ui (mminus, mminus, radix);
  5077. mpz_mul_ui (hi, hi, radix);
  5078. k--;
  5079. }
  5080. }
  5081. }
  5082. expon = k - 1;
  5083. if (k <= 0)
  5084. {
  5085. if (k <= -3)
  5086. {
  5087. /* Use scientific notation */
  5088. show_exp = 1;
  5089. k = 1;
  5090. }
  5091. else
  5092. {
  5093. int i;
  5094. /* Print leading zeroes */
  5095. a[ch++] = '0';
  5096. a[ch++] = '.';
  5097. for (i = 0; i > k; i--)
  5098. a[ch++] = '0';
  5099. }
  5100. }
  5101. for (;;)
  5102. {
  5103. int end_1_p, end_2_p;
  5104. int d;
  5105. mpz_mul_ui (mplus, mplus, radix);
  5106. mpz_mul_ui (mminus, mminus, radix);
  5107. mpz_mul_ui (r, r, radix);
  5108. mpz_fdiv_qr (digit, r, r, s);
  5109. d = mpz_get_ui (digit);
  5110. mpz_add (hi, r, mplus);
  5111. end_1_p = (mpz_cmp (r, mminus) < f_is_even);
  5112. end_2_p = (mpz_cmp (s, hi) < f_is_even);
  5113. if (end_1_p || end_2_p)
  5114. {
  5115. mpz_mul_2exp (r, r, 1);
  5116. if (!end_2_p)
  5117. ;
  5118. else if (!end_1_p)
  5119. d++;
  5120. else if (mpz_cmp (r, s) >= !(d & 1))
  5121. d++;
  5122. a[ch++] = number_chars[d];
  5123. if (--k == 0)
  5124. a[ch++] = '.';
  5125. break;
  5126. }
  5127. else
  5128. {
  5129. a[ch++] = number_chars[d];
  5130. if (--k == 0)
  5131. a[ch++] = '.';
  5132. }
  5133. }
  5134. if (k > 0)
  5135. {
  5136. if (expon >= 7 && k >= 4 && expon >= k)
  5137. {
  5138. /* Here we would have to print more than three zeroes
  5139. followed by a decimal point and another zero. It
  5140. makes more sense to use scientific notation. */
  5141. /* Adjust k to what it would have been if we had chosen
  5142. scientific notation from the beginning. */
  5143. k -= expon;
  5144. /* k will now be <= 0, with magnitude equal to the number of
  5145. digits that we printed which should now be put after the
  5146. decimal point. */
  5147. /* Insert a decimal point */
  5148. memmove (a + ch + k + 1, a + ch + k, -k);
  5149. a[ch + k] = '.';
  5150. ch++;
  5151. show_exp = 1;
  5152. }
  5153. else
  5154. {
  5155. for (; k > 0; k--)
  5156. a[ch++] = '0';
  5157. a[ch++] = '.';
  5158. }
  5159. }
  5160. if (k == 0)
  5161. a[ch++] = '0';
  5162. if (show_exp)
  5163. {
  5164. a[ch++] = 'e';
  5165. ch += scm_iint2str (expon, radix, a + ch);
  5166. }
  5167. mpz_clears (f, r, s, mplus, mminus, hi, digit, NULL);
  5168. }
  5169. return ch;
  5170. }
  5171. static size_t
  5172. icmplx2str (double real, double imag, char *str, int radix)
  5173. {
  5174. size_t i;
  5175. double sgn;
  5176. i = idbl2str (real, str, radix);
  5177. #ifdef HAVE_COPYSIGN
  5178. sgn = copysign (1.0, imag);
  5179. #else
  5180. sgn = imag;
  5181. #endif
  5182. /* Don't output a '+' for negative numbers or for Inf and
  5183. NaN. They will provide their own sign. */
  5184. if (sgn >= 0 && isfinite (imag))
  5185. str[i++] = '+';
  5186. i += idbl2str (imag, &str[i], radix);
  5187. str[i++] = 'i';
  5188. return i;
  5189. }
  5190. static size_t
  5191. iflo2str (SCM flt, char *str, int radix)
  5192. {
  5193. size_t i;
  5194. if (SCM_REALP (flt))
  5195. i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
  5196. else
  5197. i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
  5198. str, radix);
  5199. return i;
  5200. }
  5201. /* convert a intmax_t to a string (unterminated). returns the number of
  5202. characters in the result.
  5203. rad is output base
  5204. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  5205. size_t
  5206. scm_iint2str (intmax_t num, int rad, char *p)
  5207. {
  5208. if (num < 0)
  5209. {
  5210. *p++ = '-';
  5211. return scm_iuint2str (-num, rad, p) + 1;
  5212. }
  5213. else
  5214. return scm_iuint2str (num, rad, p);
  5215. }
  5216. /* convert a intmax_t to a string (unterminated). returns the number of
  5217. characters in the result.
  5218. rad is output base
  5219. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  5220. size_t
  5221. scm_iuint2str (uintmax_t num, int rad, char *p)
  5222. {
  5223. size_t j = 1;
  5224. size_t i;
  5225. uintmax_t n = num;
  5226. if (rad < 2 || rad > 36)
  5227. scm_out_of_range ("scm_iuint2str", scm_from_int (rad));
  5228. for (n /= rad; n > 0; n /= rad)
  5229. j++;
  5230. i = j;
  5231. n = num;
  5232. while (i--)
  5233. {
  5234. int d = n % rad;
  5235. n /= rad;
  5236. p[i] = number_chars[d];
  5237. }
  5238. return j;
  5239. }
  5240. SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
  5241. (SCM n, SCM radix),
  5242. "Return a string holding the external representation of the\n"
  5243. "number @var{n} in the given @var{radix}. If @var{n} is\n"
  5244. "inexact, a radix of 10 will be used.")
  5245. #define FUNC_NAME s_scm_number_to_string
  5246. {
  5247. int base;
  5248. if (SCM_UNBNDP (radix))
  5249. base = 10;
  5250. else
  5251. base = scm_to_signed_integer (radix, 2, 36);
  5252. if (SCM_I_INUMP (n))
  5253. {
  5254. char num_buf [SCM_INTBUFLEN];
  5255. size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
  5256. return scm_from_latin1_stringn (num_buf, length);
  5257. }
  5258. else if (SCM_BIGP (n))
  5259. {
  5260. char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
  5261. size_t len = strlen (str);
  5262. void (*freefunc) (void *, size_t);
  5263. SCM ret;
  5264. mp_get_memory_functions (NULL, NULL, &freefunc);
  5265. scm_remember_upto_here_1 (n);
  5266. ret = scm_from_latin1_stringn (str, len);
  5267. freefunc (str, len + 1);
  5268. return ret;
  5269. }
  5270. else if (SCM_FRACTIONP (n))
  5271. {
  5272. return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
  5273. scm_from_latin1_string ("/"),
  5274. scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
  5275. }
  5276. else if (SCM_INEXACTP (n))
  5277. {
  5278. char num_buf [FLOBUFLEN];
  5279. return scm_from_latin1_stringn (num_buf, iflo2str (n, num_buf, base));
  5280. }
  5281. else
  5282. SCM_WRONG_TYPE_ARG (1, n);
  5283. }
  5284. #undef FUNC_NAME
  5285. /* These print routines used to be stubbed here so that scm_repl.c
  5286. wouldn't need SCM_BIGDIG conditionals (pre GMP) */
  5287. int
  5288. scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5289. {
  5290. char num_buf[FLOBUFLEN];
  5291. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  5292. return !0;
  5293. }
  5294. void
  5295. scm_i_print_double (double val, SCM port)
  5296. {
  5297. char num_buf[FLOBUFLEN];
  5298. scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
  5299. }
  5300. int
  5301. scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5302. {
  5303. char num_buf[FLOBUFLEN];
  5304. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  5305. return !0;
  5306. }
  5307. void
  5308. scm_i_print_complex (double real, double imag, SCM port)
  5309. {
  5310. char num_buf[FLOBUFLEN];
  5311. scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
  5312. }
  5313. int
  5314. scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5315. {
  5316. SCM str;
  5317. str = scm_number_to_string (sexp, SCM_UNDEFINED);
  5318. scm_display (str, port);
  5319. scm_remember_upto_here_1 (str);
  5320. return !0;
  5321. }
  5322. int
  5323. scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  5324. {
  5325. char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
  5326. size_t len = strlen (str);
  5327. void (*freefunc) (void *, size_t);
  5328. mp_get_memory_functions (NULL, NULL, &freefunc);
  5329. scm_remember_upto_here_1 (exp);
  5330. scm_lfwrite (str, len, port);
  5331. freefunc (str, len + 1);
  5332. return !0;
  5333. }
  5334. /*** END nums->strs ***/
  5335. /*** STRINGS -> NUMBERS ***/
  5336. /* The following functions implement the conversion from strings to numbers.
  5337. * The implementation somehow follows the grammar for numbers as it is given
  5338. * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
  5339. * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
  5340. * points should be noted about the implementation:
  5341. *
  5342. * * Each function keeps a local index variable 'idx' that points at the
  5343. * current position within the parsed string. The global index is only
  5344. * updated if the function could parse the corresponding syntactic unit
  5345. * successfully.
  5346. *
  5347. * * Similarly, the functions keep track of indicators of inexactness ('#',
  5348. * '.' or exponents) using local variables ('hash_seen', 'x').
  5349. *
  5350. * * Sequences of digits are parsed into temporary variables holding fixnums.
  5351. * Only if these fixnums would overflow, the result variables are updated
  5352. * using the standard functions scm_add, scm_product, scm_divide etc. Then,
  5353. * the temporary variables holding the fixnums are cleared, and the process
  5354. * starts over again. If for example fixnums were able to store five decimal
  5355. * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
  5356. * and the result was computed as 12345 * 100000 + 67890. In other words,
  5357. * only every five digits two bignum operations were performed.
  5358. *
  5359. * Notes on the handling of exactness specifiers:
  5360. *
  5361. * When parsing non-real complex numbers, we apply exactness specifiers on
  5362. * per-component basis, as is done in PLT Scheme. For complex numbers
  5363. * written in rectangular form, exactness specifiers are applied to the
  5364. * real and imaginary parts before calling scm_make_rectangular. For
  5365. * complex numbers written in polar form, exactness specifiers are applied
  5366. * to the magnitude and angle before calling scm_make_polar.
  5367. *
  5368. * There are two kinds of exactness specifiers: forced and implicit. A
  5369. * forced exactness specifier is a "#e" or "#i" prefix at the beginning of
  5370. * the entire number, and applies to both components of a complex number.
  5371. * "#e" causes each component to be made exact, and "#i" causes each
  5372. * component to be made inexact. If no forced exactness specifier is
  5373. * present, then the exactness of each component is determined
  5374. * independently by the presence or absence of a decimal point or hash mark
  5375. * within that component. If a decimal point or hash mark is present, the
  5376. * component is made inexact, otherwise it is made exact.
  5377. *
  5378. * After the exactness specifiers have been applied to each component, they
  5379. * are passed to either scm_make_rectangular or scm_make_polar to produce
  5380. * the final result. Note that this will result in a real number if the
  5381. * imaginary part, magnitude, or angle is an exact 0.
  5382. *
  5383. * For example, (string->number "#i5.0+0i") does the equivalent of:
  5384. *
  5385. * (make-rectangular (exact->inexact 5) (exact->inexact 0))
  5386. */
  5387. enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
  5388. /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
  5389. /* Caller is responsible for checking that the return value is in range
  5390. for the given radix, which should be <= 36. */
  5391. static unsigned int
  5392. char_decimal_value (uint32_t c)
  5393. {
  5394. if (c >= (uint32_t) '0' && c <= (uint32_t) '9')
  5395. return c - (uint32_t) '0';
  5396. else
  5397. {
  5398. /* uc_decimal_value returns -1 on error. When cast to an unsigned int,
  5399. that's certainly above any valid decimal, so we take advantage of
  5400. that to elide some tests. */
  5401. unsigned int d = (unsigned int) uc_decimal_value (c);
  5402. /* If that failed, try extended hexadecimals, then. Only accept ascii
  5403. hexadecimals. */
  5404. if (d >= 10U)
  5405. {
  5406. c = uc_tolower (c);
  5407. if (c >= (uint32_t) 'a')
  5408. d = c - (uint32_t)'a' + 10U;
  5409. }
  5410. return d;
  5411. }
  5412. }
  5413. /* Parse the substring of MEM starting at *P_IDX for an unsigned integer
  5414. in base RADIX. Upon success, return the unsigned integer and update
  5415. *P_IDX and *P_EXACTNESS accordingly. Return #f on failure. */
  5416. static SCM
  5417. mem2uinteger (SCM mem, unsigned int *p_idx,
  5418. unsigned int radix, enum t_exactness *p_exactness)
  5419. {
  5420. unsigned int idx = *p_idx;
  5421. unsigned int hash_seen = 0;
  5422. scm_t_bits shift = 1;
  5423. scm_t_bits add = 0;
  5424. unsigned int digit_value;
  5425. SCM result;
  5426. char c;
  5427. size_t len = scm_i_string_length (mem);
  5428. if (idx == len)
  5429. return SCM_BOOL_F;
  5430. c = scm_i_string_ref (mem, idx);
  5431. digit_value = char_decimal_value (c);
  5432. if (digit_value >= radix)
  5433. return SCM_BOOL_F;
  5434. idx++;
  5435. result = SCM_I_MAKINUM (digit_value);
  5436. while (idx != len)
  5437. {
  5438. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5439. if (c == '#')
  5440. {
  5441. hash_seen = 1;
  5442. digit_value = 0;
  5443. }
  5444. else if (hash_seen)
  5445. break;
  5446. else
  5447. {
  5448. digit_value = char_decimal_value (c);
  5449. /* This check catches non-decimals in addition to out-of-range
  5450. decimals. */
  5451. if (digit_value >= radix)
  5452. break;
  5453. }
  5454. idx++;
  5455. if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
  5456. {
  5457. result = scm_product (result, SCM_I_MAKINUM (shift));
  5458. if (add > 0)
  5459. result = scm_sum (result, SCM_I_MAKINUM (add));
  5460. shift = radix;
  5461. add = digit_value;
  5462. }
  5463. else
  5464. {
  5465. shift = shift * radix;
  5466. add = add * radix + digit_value;
  5467. }
  5468. };
  5469. if (shift > 1)
  5470. result = scm_product (result, SCM_I_MAKINUM (shift));
  5471. if (add > 0)
  5472. result = scm_sum (result, SCM_I_MAKINUM (add));
  5473. *p_idx = idx;
  5474. if (hash_seen)
  5475. *p_exactness = INEXACT;
  5476. return result;
  5477. }
  5478. /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
  5479. * covers the parts of the rules that start at a potential point. The value
  5480. * of the digits up to the point have been parsed by the caller and are given
  5481. * in variable result. The content of *p_exactness indicates, whether a hash
  5482. * has already been seen in the digits before the point.
  5483. */
  5484. #define DIGIT2UINT(d) (uc_numeric_value(d).numerator)
  5485. static SCM
  5486. mem2decimal_from_point (SCM result, SCM mem,
  5487. unsigned int *p_idx, enum t_exactness *p_exactness)
  5488. {
  5489. unsigned int idx = *p_idx;
  5490. enum t_exactness x = *p_exactness;
  5491. size_t len = scm_i_string_length (mem);
  5492. if (idx == len)
  5493. return result;
  5494. if (scm_i_string_ref (mem, idx) == '.')
  5495. {
  5496. scm_t_bits shift = 1;
  5497. scm_t_bits add = 0;
  5498. unsigned int digit_value;
  5499. SCM big_shift = SCM_INUM1;
  5500. idx++;
  5501. while (idx != len)
  5502. {
  5503. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5504. if (uc_is_property_decimal_digit ((uint32_t) c))
  5505. {
  5506. if (x == INEXACT)
  5507. return SCM_BOOL_F;
  5508. else
  5509. digit_value = DIGIT2UINT (c);
  5510. }
  5511. else if (c == '#')
  5512. {
  5513. x = INEXACT;
  5514. digit_value = 0;
  5515. }
  5516. else
  5517. break;
  5518. idx++;
  5519. if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
  5520. {
  5521. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  5522. result = scm_product (result, SCM_I_MAKINUM (shift));
  5523. if (add > 0)
  5524. result = scm_sum (result, SCM_I_MAKINUM (add));
  5525. shift = 10;
  5526. add = digit_value;
  5527. }
  5528. else
  5529. {
  5530. shift = shift * 10;
  5531. add = add * 10 + digit_value;
  5532. }
  5533. };
  5534. if (add > 0)
  5535. {
  5536. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  5537. result = scm_product (result, SCM_I_MAKINUM (shift));
  5538. result = scm_sum (result, SCM_I_MAKINUM (add));
  5539. }
  5540. result = scm_divide (result, big_shift);
  5541. /* We've seen a decimal point, thus the value is implicitly inexact. */
  5542. x = INEXACT;
  5543. }
  5544. if (idx != len)
  5545. {
  5546. int sign = 1;
  5547. unsigned int start;
  5548. scm_t_wchar c;
  5549. int exponent;
  5550. SCM e;
  5551. /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
  5552. switch (scm_i_string_ref (mem, idx))
  5553. {
  5554. case 'd': case 'D':
  5555. case 'e': case 'E':
  5556. case 'f': case 'F':
  5557. case 'l': case 'L':
  5558. case 's': case 'S':
  5559. idx++;
  5560. if (idx == len)
  5561. return SCM_BOOL_F;
  5562. start = idx;
  5563. c = scm_i_string_ref (mem, idx);
  5564. if (c == '-')
  5565. {
  5566. idx++;
  5567. if (idx == len)
  5568. return SCM_BOOL_F;
  5569. sign = -1;
  5570. c = scm_i_string_ref (mem, idx);
  5571. }
  5572. else if (c == '+')
  5573. {
  5574. idx++;
  5575. if (idx == len)
  5576. return SCM_BOOL_F;
  5577. sign = 1;
  5578. c = scm_i_string_ref (mem, idx);
  5579. }
  5580. else
  5581. sign = 1;
  5582. if (!uc_is_property_decimal_digit ((uint32_t) c))
  5583. return SCM_BOOL_F;
  5584. idx++;
  5585. exponent = DIGIT2UINT (c);
  5586. while (idx != len)
  5587. {
  5588. scm_t_wchar c = scm_i_string_ref (mem, idx);
  5589. if (uc_is_property_decimal_digit ((uint32_t) c))
  5590. {
  5591. idx++;
  5592. if (exponent <= SCM_MAXEXP)
  5593. exponent = exponent * 10 + DIGIT2UINT (c);
  5594. }
  5595. else
  5596. break;
  5597. }
  5598. if (exponent > ((sign == 1) ? SCM_MAXEXP : SCM_MAXEXP + DBL_DIG + 1))
  5599. {
  5600. size_t exp_len = idx - start;
  5601. SCM exp_string = scm_i_substring_copy (mem, start, start + exp_len);
  5602. SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
  5603. scm_out_of_range ("string->number", exp_num);
  5604. }
  5605. e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
  5606. if (sign == 1)
  5607. result = scm_product (result, e);
  5608. else
  5609. result = scm_divide (result, e);
  5610. /* We've seen an exponent, thus the value is implicitly inexact. */
  5611. x = INEXACT;
  5612. break;
  5613. default:
  5614. break;
  5615. }
  5616. }
  5617. *p_idx = idx;
  5618. if (x == INEXACT)
  5619. *p_exactness = x;
  5620. return result;
  5621. }
  5622. /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
  5623. static SCM
  5624. mem2ureal (SCM mem, unsigned int *p_idx,
  5625. unsigned int radix, enum t_exactness forced_x,
  5626. int allow_inf_or_nan)
  5627. {
  5628. unsigned int idx = *p_idx;
  5629. SCM result;
  5630. size_t len = scm_i_string_length (mem);
  5631. /* Start off believing that the number will be exact. This changes
  5632. to INEXACT if we see a decimal point or a hash. */
  5633. enum t_exactness implicit_x = EXACT;
  5634. if (idx == len)
  5635. return SCM_BOOL_F;
  5636. if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len)
  5637. switch (scm_i_string_ref (mem, idx))
  5638. {
  5639. case 'i': case 'I':
  5640. switch (scm_i_string_ref (mem, idx + 1))
  5641. {
  5642. case 'n': case 'N':
  5643. switch (scm_i_string_ref (mem, idx + 2))
  5644. {
  5645. case 'f': case 'F':
  5646. if (scm_i_string_ref (mem, idx + 3) == '.'
  5647. && scm_i_string_ref (mem, idx + 4) == '0')
  5648. {
  5649. *p_idx = idx+5;
  5650. return scm_inf ();
  5651. }
  5652. }
  5653. }
  5654. case 'n': case 'N':
  5655. switch (scm_i_string_ref (mem, idx + 1))
  5656. {
  5657. case 'a': case 'A':
  5658. switch (scm_i_string_ref (mem, idx + 2))
  5659. {
  5660. case 'n': case 'N':
  5661. if (scm_i_string_ref (mem, idx + 3) == '.')
  5662. {
  5663. /* Cobble up the fractional part. We might want to
  5664. set the NaN's mantissa from it. */
  5665. idx += 4;
  5666. if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x),
  5667. SCM_INUM0))
  5668. return SCM_BOOL_F;
  5669. *p_idx = idx;
  5670. return scm_nan ();
  5671. }
  5672. }
  5673. }
  5674. }
  5675. if (scm_i_string_ref (mem, idx) == '.')
  5676. {
  5677. if (radix != 10)
  5678. return SCM_BOOL_F;
  5679. else if (idx + 1 == len)
  5680. return SCM_BOOL_F;
  5681. else if (!uc_is_property_decimal_digit ((uint32_t) scm_i_string_ref (mem, idx+1)))
  5682. return SCM_BOOL_F;
  5683. else
  5684. result = mem2decimal_from_point (SCM_INUM0, mem,
  5685. p_idx, &implicit_x);
  5686. }
  5687. else
  5688. {
  5689. SCM uinteger;
  5690. uinteger = mem2uinteger (mem, &idx, radix, &implicit_x);
  5691. if (scm_is_false (uinteger))
  5692. return SCM_BOOL_F;
  5693. if (idx == len)
  5694. result = uinteger;
  5695. else if (scm_i_string_ref (mem, idx) == '/')
  5696. {
  5697. SCM divisor;
  5698. idx++;
  5699. if (idx == len)
  5700. return SCM_BOOL_F;
  5701. divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
  5702. if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
  5703. return SCM_BOOL_F;
  5704. /* both are int/big here, I assume */
  5705. result = scm_i_make_ratio (uinteger, divisor);
  5706. }
  5707. else if (radix == 10)
  5708. {
  5709. result = mem2decimal_from_point (uinteger, mem, &idx, &implicit_x);
  5710. if (scm_is_false (result))
  5711. return SCM_BOOL_F;
  5712. }
  5713. else
  5714. result = uinteger;
  5715. *p_idx = idx;
  5716. }
  5717. switch (forced_x)
  5718. {
  5719. case EXACT:
  5720. if (SCM_INEXACTP (result))
  5721. return scm_inexact_to_exact (result);
  5722. else
  5723. return result;
  5724. case INEXACT:
  5725. if (SCM_INEXACTP (result))
  5726. return result;
  5727. else
  5728. return scm_exact_to_inexact (result);
  5729. case NO_EXACTNESS:
  5730. if (implicit_x == INEXACT)
  5731. {
  5732. if (SCM_INEXACTP (result))
  5733. return result;
  5734. else
  5735. return scm_exact_to_inexact (result);
  5736. }
  5737. else
  5738. return result;
  5739. }
  5740. /* We should never get here */
  5741. assert (0);
  5742. }
  5743. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  5744. static SCM
  5745. mem2complex (SCM mem, unsigned int idx,
  5746. unsigned int radix, enum t_exactness forced_x)
  5747. {
  5748. scm_t_wchar c;
  5749. int sign = 0;
  5750. SCM ureal;
  5751. size_t len = scm_i_string_length (mem);
  5752. if (idx == len)
  5753. return SCM_BOOL_F;
  5754. c = scm_i_string_ref (mem, idx);
  5755. if (c == '+')
  5756. {
  5757. idx++;
  5758. sign = 1;
  5759. }
  5760. else if (c == '-')
  5761. {
  5762. idx++;
  5763. sign = -1;
  5764. }
  5765. if (idx == len)
  5766. return SCM_BOOL_F;
  5767. ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5768. if (scm_is_false (ureal))
  5769. {
  5770. /* input must be either +i or -i */
  5771. if (sign == 0)
  5772. return SCM_BOOL_F;
  5773. if (scm_i_string_ref (mem, idx) == 'i'
  5774. || scm_i_string_ref (mem, idx) == 'I')
  5775. {
  5776. idx++;
  5777. if (idx != len)
  5778. return SCM_BOOL_F;
  5779. return scm_make_rectangular (SCM_INUM0, SCM_I_MAKINUM (sign));
  5780. }
  5781. else
  5782. return SCM_BOOL_F;
  5783. }
  5784. else
  5785. {
  5786. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  5787. ureal = scm_difference (ureal, SCM_UNDEFINED);
  5788. if (idx == len)
  5789. return ureal;
  5790. c = scm_i_string_ref (mem, idx);
  5791. switch (c)
  5792. {
  5793. case 'i': case 'I':
  5794. /* either +<ureal>i or -<ureal>i */
  5795. idx++;
  5796. if (sign == 0)
  5797. return SCM_BOOL_F;
  5798. if (idx != len)
  5799. return SCM_BOOL_F;
  5800. return scm_make_rectangular (SCM_INUM0, ureal);
  5801. case '@':
  5802. /* polar input: <real>@<real>. */
  5803. idx++;
  5804. if (idx == len)
  5805. return SCM_BOOL_F;
  5806. else
  5807. {
  5808. int sign;
  5809. SCM angle;
  5810. SCM result;
  5811. c = scm_i_string_ref (mem, idx);
  5812. if (c == '+')
  5813. {
  5814. idx++;
  5815. if (idx == len)
  5816. return SCM_BOOL_F;
  5817. sign = 1;
  5818. }
  5819. else if (c == '-')
  5820. {
  5821. idx++;
  5822. if (idx == len)
  5823. return SCM_BOOL_F;
  5824. sign = -1;
  5825. }
  5826. else
  5827. sign = 0;
  5828. angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5829. if (scm_is_false (angle))
  5830. return SCM_BOOL_F;
  5831. if (idx != len)
  5832. return SCM_BOOL_F;
  5833. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  5834. angle = scm_difference (angle, SCM_UNDEFINED);
  5835. result = scm_make_polar (ureal, angle);
  5836. return result;
  5837. }
  5838. case '+':
  5839. case '-':
  5840. /* expecting input matching <real>[+-]<ureal>?i */
  5841. idx++;
  5842. if (idx == len)
  5843. return SCM_BOOL_F;
  5844. else
  5845. {
  5846. int sign = (c == '+') ? 1 : -1;
  5847. SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  5848. if (scm_is_false (imag))
  5849. imag = SCM_I_MAKINUM (sign);
  5850. else if (sign == -1 && scm_is_false (scm_nan_p (imag)))
  5851. imag = scm_difference (imag, SCM_UNDEFINED);
  5852. if (idx == len)
  5853. return SCM_BOOL_F;
  5854. if (scm_i_string_ref (mem, idx) != 'i'
  5855. && scm_i_string_ref (mem, idx) != 'I')
  5856. return SCM_BOOL_F;
  5857. idx++;
  5858. if (idx != len)
  5859. return SCM_BOOL_F;
  5860. return scm_make_rectangular (ureal, imag);
  5861. }
  5862. default:
  5863. return SCM_BOOL_F;
  5864. }
  5865. }
  5866. }
  5867. /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
  5868. enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
  5869. SCM
  5870. scm_i_string_to_number (SCM mem, unsigned int default_radix)
  5871. {
  5872. unsigned int idx = 0;
  5873. unsigned int radix = NO_RADIX;
  5874. enum t_exactness forced_x = NO_EXACTNESS;
  5875. size_t len = scm_i_string_length (mem);
  5876. /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
  5877. while (idx + 2 < len && scm_i_string_ref (mem, idx) == '#')
  5878. {
  5879. switch (scm_i_string_ref (mem, idx + 1))
  5880. {
  5881. case 'b': case 'B':
  5882. if (radix != NO_RADIX)
  5883. return SCM_BOOL_F;
  5884. radix = DUAL;
  5885. break;
  5886. case 'd': case 'D':
  5887. if (radix != NO_RADIX)
  5888. return SCM_BOOL_F;
  5889. radix = DEC;
  5890. break;
  5891. case 'i': case 'I':
  5892. if (forced_x != NO_EXACTNESS)
  5893. return SCM_BOOL_F;
  5894. forced_x = INEXACT;
  5895. break;
  5896. case 'e': case 'E':
  5897. if (forced_x != NO_EXACTNESS)
  5898. return SCM_BOOL_F;
  5899. forced_x = EXACT;
  5900. break;
  5901. case 'o': case 'O':
  5902. if (radix != NO_RADIX)
  5903. return SCM_BOOL_F;
  5904. radix = OCT;
  5905. break;
  5906. case 'x': case 'X':
  5907. if (radix != NO_RADIX)
  5908. return SCM_BOOL_F;
  5909. radix = HEX;
  5910. break;
  5911. default:
  5912. return SCM_BOOL_F;
  5913. }
  5914. idx += 2;
  5915. }
  5916. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  5917. if (radix == NO_RADIX)
  5918. radix = default_radix;
  5919. return mem2complex (mem, idx, radix, forced_x);
  5920. }
  5921. SCM
  5922. scm_c_locale_stringn_to_number (const char* mem, size_t len,
  5923. unsigned int default_radix)
  5924. {
  5925. SCM str = scm_from_locale_stringn (mem, len);
  5926. return scm_i_string_to_number (str, default_radix);
  5927. }
  5928. SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
  5929. (SCM string, SCM radix),
  5930. "Return a number of the maximally precise representation\n"
  5931. "expressed by the given @var{string}. @var{radix} must be an\n"
  5932. "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
  5933. "is a default radix that may be overridden by an explicit radix\n"
  5934. "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
  5935. "supplied, then the default radix is 10. If string is not a\n"
  5936. "syntactically valid notation for a number, then\n"
  5937. "@code{string->number} returns @code{#f}.")
  5938. #define FUNC_NAME s_scm_string_to_number
  5939. {
  5940. SCM answer;
  5941. unsigned int base;
  5942. SCM_VALIDATE_STRING (1, string);
  5943. if (SCM_UNBNDP (radix))
  5944. base = 10;
  5945. else
  5946. base = scm_to_unsigned_integer (radix, 2, INT_MAX);
  5947. answer = scm_i_string_to_number (string, base);
  5948. scm_remember_upto_here_1 (string);
  5949. return answer;
  5950. }
  5951. #undef FUNC_NAME
  5952. /*** END strs->nums ***/
  5953. SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
  5954. (SCM x),
  5955. "Return @code{#t} if @var{x} is a number, @code{#f}\n"
  5956. "otherwise.")
  5957. #define FUNC_NAME s_scm_number_p
  5958. {
  5959. return scm_from_bool (SCM_NUMBERP (x));
  5960. }
  5961. #undef FUNC_NAME
  5962. SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
  5963. (SCM x),
  5964. "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
  5965. "otherwise. Note that the sets of real, rational and integer\n"
  5966. "values form subsets of the set of complex numbers, i. e. the\n"
  5967. "predicate will also be fulfilled if @var{x} is a real,\n"
  5968. "rational or integer number.")
  5969. #define FUNC_NAME s_scm_complex_p
  5970. {
  5971. /* all numbers are complex. */
  5972. return scm_number_p (x);
  5973. }
  5974. #undef FUNC_NAME
  5975. SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
  5976. (SCM x),
  5977. "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
  5978. "otherwise. Note that the set of integer values forms a subset of\n"
  5979. "the set of real numbers, i. e. the predicate will also be\n"
  5980. "fulfilled if @var{x} is an integer number.")
  5981. #define FUNC_NAME s_scm_real_p
  5982. {
  5983. return scm_from_bool
  5984. (SCM_I_INUMP (x) || SCM_REALP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x));
  5985. }
  5986. #undef FUNC_NAME
  5987. SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
  5988. (SCM x),
  5989. "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
  5990. "otherwise. Note that the set of integer values forms a subset of\n"
  5991. "the set of rational numbers, i. e. the predicate will also be\n"
  5992. "fulfilled if @var{x} is an integer number.")
  5993. #define FUNC_NAME s_scm_rational_p
  5994. {
  5995. if (SCM_I_INUMP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x))
  5996. return SCM_BOOL_T;
  5997. else if (SCM_REALP (x))
  5998. /* due to their limited precision, finite floating point numbers are
  5999. rational as well. (finite means neither infinity nor a NaN) */
  6000. return scm_from_bool (isfinite (SCM_REAL_VALUE (x)));
  6001. else
  6002. return SCM_BOOL_F;
  6003. }
  6004. #undef FUNC_NAME
  6005. SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
  6006. (SCM x),
  6007. "Return @code{#t} if @var{x} is an integer number,\n"
  6008. "else return @code{#f}.")
  6009. #define FUNC_NAME s_scm_integer_p
  6010. {
  6011. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  6012. return SCM_BOOL_T;
  6013. else if (SCM_REALP (x))
  6014. {
  6015. double val = SCM_REAL_VALUE (x);
  6016. return scm_from_bool (!isinf (val) && (val == floor (val)));
  6017. }
  6018. else
  6019. return SCM_BOOL_F;
  6020. }
  6021. #undef FUNC_NAME
  6022. SCM_DEFINE (scm_exact_integer_p, "exact-integer?", 1, 0, 0,
  6023. (SCM x),
  6024. "Return @code{#t} if @var{x} is an exact integer number,\n"
  6025. "else return @code{#f}.")
  6026. #define FUNC_NAME s_scm_exact_integer_p
  6027. {
  6028. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  6029. return SCM_BOOL_T;
  6030. else
  6031. return SCM_BOOL_F;
  6032. }
  6033. #undef FUNC_NAME
  6034. SCM scm_i_num_eq_p (SCM, SCM, SCM);
  6035. SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1,
  6036. (SCM x, SCM y, SCM rest),
  6037. "Return @code{#t} if all parameters are numerically equal.")
  6038. #define FUNC_NAME s_scm_i_num_eq_p
  6039. {
  6040. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6041. return SCM_BOOL_T;
  6042. while (!scm_is_null (rest))
  6043. {
  6044. if (scm_is_false (scm_num_eq_p (x, y)))
  6045. return SCM_BOOL_F;
  6046. x = y;
  6047. y = scm_car (rest);
  6048. rest = scm_cdr (rest);
  6049. }
  6050. return scm_num_eq_p (x, y);
  6051. }
  6052. #undef FUNC_NAME
  6053. SCM
  6054. scm_num_eq_p (SCM x, SCM y)
  6055. {
  6056. again:
  6057. if (SCM_I_INUMP (x))
  6058. {
  6059. scm_t_signed_bits xx = SCM_I_INUM (x);
  6060. if (SCM_I_INUMP (y))
  6061. {
  6062. scm_t_signed_bits yy = SCM_I_INUM (y);
  6063. return scm_from_bool (xx == yy);
  6064. }
  6065. else if (SCM_BIGP (y))
  6066. return SCM_BOOL_F;
  6067. else if (SCM_REALP (y))
  6068. {
  6069. /* On a 32-bit system an inum fits a double, we can cast the inum
  6070. to a double and compare.
  6071. But on a 64-bit system an inum is bigger than a double and
  6072. casting it to a double (call that dxx) will round.
  6073. Although dxx will not in general be equal to xx, dxx will
  6074. always be an integer and within a factor of 2 of xx, so if
  6075. dxx==yy, we know that yy is an integer and fits in
  6076. scm_t_signed_bits. So we cast yy to scm_t_signed_bits and
  6077. compare with plain xx.
  6078. An alternative (for any size system actually) would be to check
  6079. yy is an integer (with floor) and is in range of an inum
  6080. (compare against appropriate powers of 2) then test
  6081. xx==(scm_t_signed_bits)yy. It's just a matter of which
  6082. casts/comparisons might be fastest or easiest for the cpu. */
  6083. double yy = SCM_REAL_VALUE (y);
  6084. return scm_from_bool ((double) xx == yy
  6085. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6086. || xx == (scm_t_signed_bits) yy));
  6087. }
  6088. else if (SCM_COMPLEXP (y))
  6089. {
  6090. /* see comments with inum/real above */
  6091. double ry = SCM_COMPLEX_REAL (y);
  6092. return scm_from_bool ((double) xx == ry
  6093. && 0.0 == SCM_COMPLEX_IMAG (y)
  6094. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6095. || xx == (scm_t_signed_bits) ry));
  6096. }
  6097. else if (SCM_FRACTIONP (y))
  6098. return SCM_BOOL_F;
  6099. else
  6100. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6101. s_scm_i_num_eq_p);
  6102. }
  6103. else if (SCM_BIGP (x))
  6104. {
  6105. if (SCM_I_INUMP (y))
  6106. return SCM_BOOL_F;
  6107. else if (SCM_BIGP (y))
  6108. {
  6109. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6110. scm_remember_upto_here_2 (x, y);
  6111. return scm_from_bool (0 == cmp);
  6112. }
  6113. else if (SCM_REALP (y))
  6114. {
  6115. int cmp;
  6116. if (isnan (SCM_REAL_VALUE (y)))
  6117. return SCM_BOOL_F;
  6118. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  6119. scm_remember_upto_here_1 (x);
  6120. return scm_from_bool (0 == cmp);
  6121. }
  6122. else if (SCM_COMPLEXP (y))
  6123. {
  6124. int cmp;
  6125. if (0.0 != SCM_COMPLEX_IMAG (y))
  6126. return SCM_BOOL_F;
  6127. if (isnan (SCM_COMPLEX_REAL (y)))
  6128. return SCM_BOOL_F;
  6129. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
  6130. scm_remember_upto_here_1 (x);
  6131. return scm_from_bool (0 == cmp);
  6132. }
  6133. else if (SCM_FRACTIONP (y))
  6134. return SCM_BOOL_F;
  6135. else
  6136. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6137. s_scm_i_num_eq_p);
  6138. }
  6139. else if (SCM_REALP (x))
  6140. {
  6141. double xx = SCM_REAL_VALUE (x);
  6142. if (SCM_I_INUMP (y))
  6143. {
  6144. /* see comments with inum/real above */
  6145. scm_t_signed_bits yy = SCM_I_INUM (y);
  6146. return scm_from_bool (xx == (double) yy
  6147. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6148. || (scm_t_signed_bits) xx == yy));
  6149. }
  6150. else if (SCM_BIGP (y))
  6151. {
  6152. int cmp;
  6153. if (isnan (xx))
  6154. return SCM_BOOL_F;
  6155. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), xx);
  6156. scm_remember_upto_here_1 (y);
  6157. return scm_from_bool (0 == cmp);
  6158. }
  6159. else if (SCM_REALP (y))
  6160. return scm_from_bool (xx == SCM_REAL_VALUE (y));
  6161. else if (SCM_COMPLEXP (y))
  6162. return scm_from_bool ((xx == SCM_COMPLEX_REAL (y))
  6163. && (0.0 == SCM_COMPLEX_IMAG (y)));
  6164. else if (SCM_FRACTIONP (y))
  6165. {
  6166. if (isnan (xx) || isinf (xx))
  6167. return SCM_BOOL_F;
  6168. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6169. goto again;
  6170. }
  6171. else
  6172. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6173. s_scm_i_num_eq_p);
  6174. }
  6175. else if (SCM_COMPLEXP (x))
  6176. {
  6177. if (SCM_I_INUMP (y))
  6178. {
  6179. /* see comments with inum/real above */
  6180. double rx = SCM_COMPLEX_REAL (x);
  6181. scm_t_signed_bits yy = SCM_I_INUM (y);
  6182. return scm_from_bool (rx == (double) yy
  6183. && 0.0 == SCM_COMPLEX_IMAG (x)
  6184. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  6185. || (scm_t_signed_bits) rx == yy));
  6186. }
  6187. else if (SCM_BIGP (y))
  6188. {
  6189. int cmp;
  6190. if (0.0 != SCM_COMPLEX_IMAG (x))
  6191. return SCM_BOOL_F;
  6192. if (isnan (SCM_COMPLEX_REAL (x)))
  6193. return SCM_BOOL_F;
  6194. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
  6195. scm_remember_upto_here_1 (y);
  6196. return scm_from_bool (0 == cmp);
  6197. }
  6198. else if (SCM_REALP (y))
  6199. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
  6200. && (SCM_COMPLEX_IMAG (x) == 0.0));
  6201. else if (SCM_COMPLEXP (y))
  6202. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
  6203. && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
  6204. else if (SCM_FRACTIONP (y))
  6205. {
  6206. double xx;
  6207. if (SCM_COMPLEX_IMAG (x) != 0.0)
  6208. return SCM_BOOL_F;
  6209. xx = SCM_COMPLEX_REAL (x);
  6210. if (isnan (xx) || isinf (xx))
  6211. return SCM_BOOL_F;
  6212. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6213. goto again;
  6214. }
  6215. else
  6216. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6217. s_scm_i_num_eq_p);
  6218. }
  6219. else if (SCM_FRACTIONP (x))
  6220. {
  6221. if (SCM_I_INUMP (y))
  6222. return SCM_BOOL_F;
  6223. else if (SCM_BIGP (y))
  6224. return SCM_BOOL_F;
  6225. else if (SCM_REALP (y))
  6226. {
  6227. double yy = SCM_REAL_VALUE (y);
  6228. if (isnan (yy) || isinf (yy))
  6229. return SCM_BOOL_F;
  6230. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6231. goto again;
  6232. }
  6233. else if (SCM_COMPLEXP (y))
  6234. {
  6235. double yy;
  6236. if (SCM_COMPLEX_IMAG (y) != 0.0)
  6237. return SCM_BOOL_F;
  6238. yy = SCM_COMPLEX_REAL (y);
  6239. if (isnan (yy) || isinf(yy))
  6240. return SCM_BOOL_F;
  6241. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6242. goto again;
  6243. }
  6244. else if (SCM_FRACTIONP (y))
  6245. return scm_i_fraction_equalp (x, y);
  6246. else
  6247. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARGn,
  6248. s_scm_i_num_eq_p);
  6249. }
  6250. else
  6251. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARG1,
  6252. s_scm_i_num_eq_p);
  6253. }
  6254. /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
  6255. done are good for inums, but for bignums an answer can almost always be
  6256. had by just examining a few high bits of the operands, as done by GMP in
  6257. mpq_cmp. flonum/frac compares likewise, but with the slight complication
  6258. of the float exponent to take into account. */
  6259. SCM_INTERNAL SCM scm_i_num_less_p (SCM, SCM, SCM);
  6260. SCM_PRIMITIVE_GENERIC (scm_i_num_less_p, "<", 0, 2, 1,
  6261. (SCM x, SCM y, SCM rest),
  6262. "Return @code{#t} if the list of parameters is monotonically\n"
  6263. "increasing.")
  6264. #define FUNC_NAME s_scm_i_num_less_p
  6265. {
  6266. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6267. return SCM_BOOL_T;
  6268. while (!scm_is_null (rest))
  6269. {
  6270. if (scm_is_false (scm_less_p (x, y)))
  6271. return SCM_BOOL_F;
  6272. x = y;
  6273. y = scm_car (rest);
  6274. rest = scm_cdr (rest);
  6275. }
  6276. return scm_less_p (x, y);
  6277. }
  6278. #undef FUNC_NAME
  6279. SCM
  6280. scm_less_p (SCM x, SCM y)
  6281. {
  6282. again:
  6283. if (SCM_I_INUMP (x))
  6284. {
  6285. scm_t_inum xx = SCM_I_INUM (x);
  6286. if (SCM_I_INUMP (y))
  6287. {
  6288. scm_t_inum yy = SCM_I_INUM (y);
  6289. return scm_from_bool (xx < yy);
  6290. }
  6291. else if (SCM_BIGP (y))
  6292. {
  6293. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6294. scm_remember_upto_here_1 (y);
  6295. return scm_from_bool (sgn > 0);
  6296. }
  6297. else if (SCM_REALP (y))
  6298. {
  6299. /* We can safely take the ceiling of y without changing the
  6300. result of x<y, given that x is an integer. */
  6301. double yy = ceil (SCM_REAL_VALUE (y));
  6302. /* In the following comparisons, it's important that the right
  6303. hand side always be a power of 2, so that it can be
  6304. losslessly converted to a double even on 64-bit
  6305. machines. */
  6306. if (yy >= (double) (SCM_MOST_POSITIVE_FIXNUM+1))
  6307. return SCM_BOOL_T;
  6308. else if (!(yy > (double) SCM_MOST_NEGATIVE_FIXNUM))
  6309. /* The condition above is carefully written to include the
  6310. case where yy==NaN. */
  6311. return SCM_BOOL_F;
  6312. else
  6313. /* yy is a finite integer that fits in an inum. */
  6314. return scm_from_bool (xx < (scm_t_inum) yy);
  6315. }
  6316. else if (SCM_FRACTIONP (y))
  6317. {
  6318. /* "x < a/b" becomes "x*b < a" */
  6319. int_frac:
  6320. x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
  6321. y = SCM_FRACTION_NUMERATOR (y);
  6322. goto again;
  6323. }
  6324. else
  6325. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6326. s_scm_i_num_less_p);
  6327. }
  6328. else if (SCM_BIGP (x))
  6329. {
  6330. if (SCM_I_INUMP (y))
  6331. {
  6332. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6333. scm_remember_upto_here_1 (x);
  6334. return scm_from_bool (sgn < 0);
  6335. }
  6336. else if (SCM_BIGP (y))
  6337. {
  6338. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6339. scm_remember_upto_here_2 (x, y);
  6340. return scm_from_bool (cmp < 0);
  6341. }
  6342. else if (SCM_REALP (y))
  6343. {
  6344. int cmp;
  6345. if (isnan (SCM_REAL_VALUE (y)))
  6346. return SCM_BOOL_F;
  6347. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  6348. scm_remember_upto_here_1 (x);
  6349. return scm_from_bool (cmp < 0);
  6350. }
  6351. else if (SCM_FRACTIONP (y))
  6352. goto int_frac;
  6353. else
  6354. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6355. s_scm_i_num_less_p);
  6356. }
  6357. else if (SCM_REALP (x))
  6358. {
  6359. if (SCM_I_INUMP (y))
  6360. {
  6361. /* We can safely take the floor of x without changing the
  6362. result of x<y, given that y is an integer. */
  6363. double xx = floor (SCM_REAL_VALUE (x));
  6364. /* In the following comparisons, it's important that the right
  6365. hand side always be a power of 2, so that it can be
  6366. losslessly converted to a double even on 64-bit
  6367. machines. */
  6368. if (xx < (double) SCM_MOST_NEGATIVE_FIXNUM)
  6369. return SCM_BOOL_T;
  6370. else if (!(xx < (double) (SCM_MOST_POSITIVE_FIXNUM+1)))
  6371. /* The condition above is carefully written to include the
  6372. case where xx==NaN. */
  6373. return SCM_BOOL_F;
  6374. else
  6375. /* xx is a finite integer that fits in an inum. */
  6376. return scm_from_bool ((scm_t_inum) xx < SCM_I_INUM (y));
  6377. }
  6378. else if (SCM_BIGP (y))
  6379. {
  6380. int cmp;
  6381. if (isnan (SCM_REAL_VALUE (x)))
  6382. return SCM_BOOL_F;
  6383. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  6384. scm_remember_upto_here_1 (y);
  6385. return scm_from_bool (cmp > 0);
  6386. }
  6387. else if (SCM_REALP (y))
  6388. return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
  6389. else if (SCM_FRACTIONP (y))
  6390. {
  6391. double xx = SCM_REAL_VALUE (x);
  6392. if (isnan (xx))
  6393. return SCM_BOOL_F;
  6394. if (isinf (xx))
  6395. return scm_from_bool (xx < 0.0);
  6396. x = scm_inexact_to_exact (x); /* with x as frac or int */
  6397. goto again;
  6398. }
  6399. else
  6400. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6401. s_scm_i_num_less_p);
  6402. }
  6403. else if (SCM_FRACTIONP (x))
  6404. {
  6405. if (SCM_I_INUMP (y) || SCM_BIGP (y))
  6406. {
  6407. /* "a/b < y" becomes "a < y*b" */
  6408. y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
  6409. x = SCM_FRACTION_NUMERATOR (x);
  6410. goto again;
  6411. }
  6412. else if (SCM_REALP (y))
  6413. {
  6414. double yy = SCM_REAL_VALUE (y);
  6415. if (isnan (yy))
  6416. return SCM_BOOL_F;
  6417. if (isinf (yy))
  6418. return scm_from_bool (0.0 < yy);
  6419. y = scm_inexact_to_exact (y); /* with y as frac or int */
  6420. goto again;
  6421. }
  6422. else if (SCM_FRACTIONP (y))
  6423. {
  6424. /* "a/b < c/d" becomes "a*d < c*b" */
  6425. SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
  6426. SCM_FRACTION_DENOMINATOR (y));
  6427. SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
  6428. SCM_FRACTION_DENOMINATOR (x));
  6429. x = new_x;
  6430. y = new_y;
  6431. goto again;
  6432. }
  6433. else
  6434. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARGn,
  6435. s_scm_i_num_less_p);
  6436. }
  6437. else
  6438. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARG1,
  6439. s_scm_i_num_less_p);
  6440. }
  6441. SCM scm_i_num_gr_p (SCM, SCM, SCM);
  6442. SCM_PRIMITIVE_GENERIC (scm_i_num_gr_p, ">", 0, 2, 1,
  6443. (SCM x, SCM y, SCM rest),
  6444. "Return @code{#t} if the list of parameters is monotonically\n"
  6445. "decreasing.")
  6446. #define FUNC_NAME s_scm_i_num_gr_p
  6447. {
  6448. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6449. return SCM_BOOL_T;
  6450. while (!scm_is_null (rest))
  6451. {
  6452. if (scm_is_false (scm_gr_p (x, y)))
  6453. return SCM_BOOL_F;
  6454. x = y;
  6455. y = scm_car (rest);
  6456. rest = scm_cdr (rest);
  6457. }
  6458. return scm_gr_p (x, y);
  6459. }
  6460. #undef FUNC_NAME
  6461. #define FUNC_NAME s_scm_i_num_gr_p
  6462. SCM
  6463. scm_gr_p (SCM x, SCM y)
  6464. {
  6465. if (!SCM_NUMBERP (x))
  6466. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG1, FUNC_NAME);
  6467. else if (!SCM_NUMBERP (y))
  6468. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG2, FUNC_NAME);
  6469. else
  6470. return scm_less_p (y, x);
  6471. }
  6472. #undef FUNC_NAME
  6473. SCM scm_i_num_leq_p (SCM, SCM, SCM);
  6474. SCM_PRIMITIVE_GENERIC (scm_i_num_leq_p, "<=", 0, 2, 1,
  6475. (SCM x, SCM y, SCM rest),
  6476. "Return @code{#t} if the list of parameters is monotonically\n"
  6477. "non-decreasing.")
  6478. #define FUNC_NAME s_scm_i_num_leq_p
  6479. {
  6480. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6481. return SCM_BOOL_T;
  6482. while (!scm_is_null (rest))
  6483. {
  6484. if (scm_is_false (scm_leq_p (x, y)))
  6485. return SCM_BOOL_F;
  6486. x = y;
  6487. y = scm_car (rest);
  6488. rest = scm_cdr (rest);
  6489. }
  6490. return scm_leq_p (x, y);
  6491. }
  6492. #undef FUNC_NAME
  6493. #define FUNC_NAME s_scm_i_num_leq_p
  6494. SCM
  6495. scm_leq_p (SCM x, SCM y)
  6496. {
  6497. if (!SCM_NUMBERP (x))
  6498. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG1, FUNC_NAME);
  6499. else if (!SCM_NUMBERP (y))
  6500. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG2, FUNC_NAME);
  6501. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  6502. return SCM_BOOL_F;
  6503. else
  6504. return scm_not (scm_less_p (y, x));
  6505. }
  6506. #undef FUNC_NAME
  6507. SCM scm_i_num_geq_p (SCM, SCM, SCM);
  6508. SCM_PRIMITIVE_GENERIC (scm_i_num_geq_p, ">=", 0, 2, 1,
  6509. (SCM x, SCM y, SCM rest),
  6510. "Return @code{#t} if the list of parameters is monotonically\n"
  6511. "non-increasing.")
  6512. #define FUNC_NAME s_scm_i_num_geq_p
  6513. {
  6514. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  6515. return SCM_BOOL_T;
  6516. while (!scm_is_null (rest))
  6517. {
  6518. if (scm_is_false (scm_geq_p (x, y)))
  6519. return SCM_BOOL_F;
  6520. x = y;
  6521. y = scm_car (rest);
  6522. rest = scm_cdr (rest);
  6523. }
  6524. return scm_geq_p (x, y);
  6525. }
  6526. #undef FUNC_NAME
  6527. #define FUNC_NAME s_scm_i_num_geq_p
  6528. SCM
  6529. scm_geq_p (SCM x, SCM y)
  6530. {
  6531. if (!SCM_NUMBERP (x))
  6532. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG1, FUNC_NAME);
  6533. else if (!SCM_NUMBERP (y))
  6534. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG2, FUNC_NAME);
  6535. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  6536. return SCM_BOOL_F;
  6537. else
  6538. return scm_not (scm_less_p (x, y));
  6539. }
  6540. #undef FUNC_NAME
  6541. SCM_PRIMITIVE_GENERIC (scm_zero_p, "zero?", 1, 0, 0,
  6542. (SCM z),
  6543. "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
  6544. "zero.")
  6545. #define FUNC_NAME s_scm_zero_p
  6546. {
  6547. if (SCM_I_INUMP (z))
  6548. return scm_from_bool (scm_is_eq (z, SCM_INUM0));
  6549. else if (SCM_BIGP (z))
  6550. return SCM_BOOL_F;
  6551. else if (SCM_REALP (z))
  6552. return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
  6553. else if (SCM_COMPLEXP (z))
  6554. return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
  6555. && SCM_COMPLEX_IMAG (z) == 0.0);
  6556. else if (SCM_FRACTIONP (z))
  6557. return SCM_BOOL_F;
  6558. else
  6559. return scm_wta_dispatch_1 (g_scm_zero_p, z, SCM_ARG1, s_scm_zero_p);
  6560. }
  6561. #undef FUNC_NAME
  6562. SCM_PRIMITIVE_GENERIC (scm_positive_p, "positive?", 1, 0, 0,
  6563. (SCM x),
  6564. "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
  6565. "zero.")
  6566. #define FUNC_NAME s_scm_positive_p
  6567. {
  6568. if (SCM_I_INUMP (x))
  6569. return scm_from_bool (SCM_I_INUM (x) > 0);
  6570. else if (SCM_BIGP (x))
  6571. {
  6572. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6573. scm_remember_upto_here_1 (x);
  6574. return scm_from_bool (sgn > 0);
  6575. }
  6576. else if (SCM_REALP (x))
  6577. return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
  6578. else if (SCM_FRACTIONP (x))
  6579. return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
  6580. else
  6581. return scm_wta_dispatch_1 (g_scm_positive_p, x, SCM_ARG1, s_scm_positive_p);
  6582. }
  6583. #undef FUNC_NAME
  6584. SCM_PRIMITIVE_GENERIC (scm_negative_p, "negative?", 1, 0, 0,
  6585. (SCM x),
  6586. "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
  6587. "zero.")
  6588. #define FUNC_NAME s_scm_negative_p
  6589. {
  6590. if (SCM_I_INUMP (x))
  6591. return scm_from_bool (SCM_I_INUM (x) < 0);
  6592. else if (SCM_BIGP (x))
  6593. {
  6594. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6595. scm_remember_upto_here_1 (x);
  6596. return scm_from_bool (sgn < 0);
  6597. }
  6598. else if (SCM_REALP (x))
  6599. return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
  6600. else if (SCM_FRACTIONP (x))
  6601. return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
  6602. else
  6603. return scm_wta_dispatch_1 (g_scm_negative_p, x, SCM_ARG1, s_scm_negative_p);
  6604. }
  6605. #undef FUNC_NAME
  6606. /* scm_min and scm_max return an inexact when either argument is inexact, as
  6607. required by r5rs. On that basis, for exact/inexact combinations the
  6608. exact is converted to inexact to compare and possibly return. This is
  6609. unlike scm_less_p above which takes some trouble to preserve all bits in
  6610. its test, such trouble is not required for min and max. */
  6611. SCM_PRIMITIVE_GENERIC (scm_i_max, "max", 0, 2, 1,
  6612. (SCM x, SCM y, SCM rest),
  6613. "Return the maximum of all parameter values.")
  6614. #define FUNC_NAME s_scm_i_max
  6615. {
  6616. while (!scm_is_null (rest))
  6617. { x = scm_max (x, y);
  6618. y = scm_car (rest);
  6619. rest = scm_cdr (rest);
  6620. }
  6621. return scm_max (x, y);
  6622. }
  6623. #undef FUNC_NAME
  6624. #define s_max s_scm_i_max
  6625. #define g_max g_scm_i_max
  6626. SCM
  6627. scm_max (SCM x, SCM y)
  6628. {
  6629. if (SCM_UNBNDP (y))
  6630. {
  6631. if (SCM_UNBNDP (x))
  6632. return scm_wta_dispatch_0 (g_max, s_max);
  6633. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  6634. return x;
  6635. else
  6636. return scm_wta_dispatch_1 (g_max, x, SCM_ARG1, s_max);
  6637. }
  6638. if (SCM_I_INUMP (x))
  6639. {
  6640. scm_t_inum xx = SCM_I_INUM (x);
  6641. if (SCM_I_INUMP (y))
  6642. {
  6643. scm_t_inum yy = SCM_I_INUM (y);
  6644. return (xx < yy) ? y : x;
  6645. }
  6646. else if (SCM_BIGP (y))
  6647. {
  6648. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6649. scm_remember_upto_here_1 (y);
  6650. return (sgn < 0) ? x : y;
  6651. }
  6652. else if (SCM_REALP (y))
  6653. {
  6654. double xxd = xx;
  6655. double yyd = SCM_REAL_VALUE (y);
  6656. if (xxd > yyd)
  6657. return scm_i_from_double (xxd);
  6658. /* If y is a NaN, then "==" is false and we return the NaN */
  6659. else if (SCM_LIKELY (!(xxd == yyd)))
  6660. return y;
  6661. /* Handle signed zeroes properly */
  6662. else if (xx == 0)
  6663. return flo0;
  6664. else
  6665. return y;
  6666. }
  6667. else if (SCM_FRACTIONP (y))
  6668. {
  6669. use_less:
  6670. return (scm_is_false (scm_less_p (x, y)) ? x : y);
  6671. }
  6672. else
  6673. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6674. }
  6675. else if (SCM_BIGP (x))
  6676. {
  6677. if (SCM_I_INUMP (y))
  6678. {
  6679. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6680. scm_remember_upto_here_1 (x);
  6681. return (sgn < 0) ? y : x;
  6682. }
  6683. else if (SCM_BIGP (y))
  6684. {
  6685. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6686. scm_remember_upto_here_2 (x, y);
  6687. return (cmp > 0) ? x : y;
  6688. }
  6689. else if (SCM_REALP (y))
  6690. {
  6691. /* if y==NaN then xx>yy is false, so we return the NaN y */
  6692. double xx, yy;
  6693. big_real:
  6694. xx = scm_i_big2dbl (x);
  6695. yy = SCM_REAL_VALUE (y);
  6696. return (xx > yy ? scm_i_from_double (xx) : y);
  6697. }
  6698. else if (SCM_FRACTIONP (y))
  6699. {
  6700. goto use_less;
  6701. }
  6702. else
  6703. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6704. }
  6705. else if (SCM_REALP (x))
  6706. {
  6707. if (SCM_I_INUMP (y))
  6708. {
  6709. scm_t_inum yy = SCM_I_INUM (y);
  6710. double xxd = SCM_REAL_VALUE (x);
  6711. double yyd = yy;
  6712. if (yyd > xxd)
  6713. return scm_i_from_double (yyd);
  6714. /* If x is a NaN, then "==" is false and we return the NaN */
  6715. else if (SCM_LIKELY (!(xxd == yyd)))
  6716. return x;
  6717. /* Handle signed zeroes properly */
  6718. else if (yy == 0)
  6719. return flo0;
  6720. else
  6721. return x;
  6722. }
  6723. else if (SCM_BIGP (y))
  6724. {
  6725. SCM_SWAP (x, y);
  6726. goto big_real;
  6727. }
  6728. else if (SCM_REALP (y))
  6729. {
  6730. double xx = SCM_REAL_VALUE (x);
  6731. double yy = SCM_REAL_VALUE (y);
  6732. /* For purposes of max: nan > +inf.0 > everything else,
  6733. per the R6RS errata */
  6734. if (xx > yy)
  6735. return x;
  6736. else if (SCM_LIKELY (xx < yy))
  6737. return y;
  6738. /* If neither (xx > yy) nor (xx < yy), then
  6739. either they're equal or one is a NaN */
  6740. else if (SCM_UNLIKELY (xx != yy))
  6741. return (xx != xx) ? x : y; /* Return the NaN */
  6742. /* xx == yy, but handle signed zeroes properly */
  6743. else if (copysign (1.0, yy) < 0.0)
  6744. return x;
  6745. else
  6746. return y;
  6747. }
  6748. else if (SCM_FRACTIONP (y))
  6749. {
  6750. double yy = scm_i_fraction2double (y);
  6751. double xx = SCM_REAL_VALUE (x);
  6752. return (xx < yy) ? scm_i_from_double (yy) : x;
  6753. }
  6754. else
  6755. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6756. }
  6757. else if (SCM_FRACTIONP (x))
  6758. {
  6759. if (SCM_I_INUMP (y))
  6760. {
  6761. goto use_less;
  6762. }
  6763. else if (SCM_BIGP (y))
  6764. {
  6765. goto use_less;
  6766. }
  6767. else if (SCM_REALP (y))
  6768. {
  6769. double xx = scm_i_fraction2double (x);
  6770. /* if y==NaN then ">" is false, so we return the NaN y */
  6771. return (xx > SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y;
  6772. }
  6773. else if (SCM_FRACTIONP (y))
  6774. {
  6775. goto use_less;
  6776. }
  6777. else
  6778. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARGn, s_max);
  6779. }
  6780. else
  6781. return scm_wta_dispatch_2 (g_max, x, y, SCM_ARG1, s_max);
  6782. }
  6783. SCM_PRIMITIVE_GENERIC (scm_i_min, "min", 0, 2, 1,
  6784. (SCM x, SCM y, SCM rest),
  6785. "Return the minimum of all parameter values.")
  6786. #define FUNC_NAME s_scm_i_min
  6787. {
  6788. while (!scm_is_null (rest))
  6789. { x = scm_min (x, y);
  6790. y = scm_car (rest);
  6791. rest = scm_cdr (rest);
  6792. }
  6793. return scm_min (x, y);
  6794. }
  6795. #undef FUNC_NAME
  6796. #define s_min s_scm_i_min
  6797. #define g_min g_scm_i_min
  6798. SCM
  6799. scm_min (SCM x, SCM y)
  6800. {
  6801. if (SCM_UNBNDP (y))
  6802. {
  6803. if (SCM_UNBNDP (x))
  6804. return scm_wta_dispatch_0 (g_min, s_min);
  6805. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  6806. return x;
  6807. else
  6808. return scm_wta_dispatch_1 (g_min, x, SCM_ARG1, s_min);
  6809. }
  6810. if (SCM_I_INUMP (x))
  6811. {
  6812. scm_t_inum xx = SCM_I_INUM (x);
  6813. if (SCM_I_INUMP (y))
  6814. {
  6815. scm_t_inum yy = SCM_I_INUM (y);
  6816. return (xx < yy) ? x : y;
  6817. }
  6818. else if (SCM_BIGP (y))
  6819. {
  6820. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  6821. scm_remember_upto_here_1 (y);
  6822. return (sgn < 0) ? y : x;
  6823. }
  6824. else if (SCM_REALP (y))
  6825. {
  6826. double z = xx;
  6827. /* if y==NaN then "<" is false and we return NaN */
  6828. return (z < SCM_REAL_VALUE (y)) ? scm_i_from_double (z) : y;
  6829. }
  6830. else if (SCM_FRACTIONP (y))
  6831. {
  6832. use_less:
  6833. return (scm_is_false (scm_less_p (x, y)) ? y : x);
  6834. }
  6835. else
  6836. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6837. }
  6838. else if (SCM_BIGP (x))
  6839. {
  6840. if (SCM_I_INUMP (y))
  6841. {
  6842. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  6843. scm_remember_upto_here_1 (x);
  6844. return (sgn < 0) ? x : y;
  6845. }
  6846. else if (SCM_BIGP (y))
  6847. {
  6848. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  6849. scm_remember_upto_here_2 (x, y);
  6850. return (cmp > 0) ? y : x;
  6851. }
  6852. else if (SCM_REALP (y))
  6853. {
  6854. /* if y==NaN then xx<yy is false, so we return the NaN y */
  6855. double xx, yy;
  6856. big_real:
  6857. xx = scm_i_big2dbl (x);
  6858. yy = SCM_REAL_VALUE (y);
  6859. return (xx < yy ? scm_i_from_double (xx) : y);
  6860. }
  6861. else if (SCM_FRACTIONP (y))
  6862. {
  6863. goto use_less;
  6864. }
  6865. else
  6866. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6867. }
  6868. else if (SCM_REALP (x))
  6869. {
  6870. if (SCM_I_INUMP (y))
  6871. {
  6872. double z = SCM_I_INUM (y);
  6873. /* if x==NaN then "<" is false and we return NaN */
  6874. return (z < SCM_REAL_VALUE (x)) ? scm_i_from_double (z) : x;
  6875. }
  6876. else if (SCM_BIGP (y))
  6877. {
  6878. SCM_SWAP (x, y);
  6879. goto big_real;
  6880. }
  6881. else if (SCM_REALP (y))
  6882. {
  6883. double xx = SCM_REAL_VALUE (x);
  6884. double yy = SCM_REAL_VALUE (y);
  6885. /* For purposes of min: nan < -inf.0 < everything else,
  6886. per the R6RS errata */
  6887. if (xx < yy)
  6888. return x;
  6889. else if (SCM_LIKELY (xx > yy))
  6890. return y;
  6891. /* If neither (xx < yy) nor (xx > yy), then
  6892. either they're equal or one is a NaN */
  6893. else if (SCM_UNLIKELY (xx != yy))
  6894. return (xx != xx) ? x : y; /* Return the NaN */
  6895. /* xx == yy, but handle signed zeroes properly */
  6896. else if (copysign (1.0, xx) < 0.0)
  6897. return x;
  6898. else
  6899. return y;
  6900. }
  6901. else if (SCM_FRACTIONP (y))
  6902. {
  6903. double yy = scm_i_fraction2double (y);
  6904. double xx = SCM_REAL_VALUE (x);
  6905. return (yy < xx) ? scm_i_from_double (yy) : x;
  6906. }
  6907. else
  6908. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6909. }
  6910. else if (SCM_FRACTIONP (x))
  6911. {
  6912. if (SCM_I_INUMP (y))
  6913. {
  6914. goto use_less;
  6915. }
  6916. else if (SCM_BIGP (y))
  6917. {
  6918. goto use_less;
  6919. }
  6920. else if (SCM_REALP (y))
  6921. {
  6922. double xx = scm_i_fraction2double (x);
  6923. /* if y==NaN then "<" is false, so we return the NaN y */
  6924. return (xx < SCM_REAL_VALUE (y)) ? scm_i_from_double (xx) : y;
  6925. }
  6926. else if (SCM_FRACTIONP (y))
  6927. {
  6928. goto use_less;
  6929. }
  6930. else
  6931. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARGn, s_min);
  6932. }
  6933. else
  6934. return scm_wta_dispatch_2 (g_min, x, y, SCM_ARG1, s_min);
  6935. }
  6936. SCM_PRIMITIVE_GENERIC (scm_i_sum, "+", 0, 2, 1,
  6937. (SCM x, SCM y, SCM rest),
  6938. "Return the sum of all parameter values. Return 0 if called without\n"
  6939. "any parameters." )
  6940. #define FUNC_NAME s_scm_i_sum
  6941. {
  6942. while (!scm_is_null (rest))
  6943. { x = scm_sum (x, y);
  6944. y = scm_car (rest);
  6945. rest = scm_cdr (rest);
  6946. }
  6947. return scm_sum (x, y);
  6948. }
  6949. #undef FUNC_NAME
  6950. #define s_sum s_scm_i_sum
  6951. #define g_sum g_scm_i_sum
  6952. SCM
  6953. scm_sum (SCM x, SCM y)
  6954. {
  6955. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  6956. {
  6957. if (SCM_NUMBERP (x)) return x;
  6958. if (SCM_UNBNDP (x)) return SCM_INUM0;
  6959. return scm_wta_dispatch_1 (g_sum, x, SCM_ARG1, s_sum);
  6960. }
  6961. if (SCM_LIKELY (SCM_I_INUMP (x)))
  6962. {
  6963. if (SCM_LIKELY (SCM_I_INUMP (y)))
  6964. {
  6965. scm_t_inum xx = SCM_I_INUM (x);
  6966. scm_t_inum yy = SCM_I_INUM (y);
  6967. scm_t_inum z = xx + yy;
  6968. return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_inum2big (z);
  6969. }
  6970. else if (SCM_BIGP (y))
  6971. {
  6972. SCM_SWAP (x, y);
  6973. goto add_big_inum;
  6974. }
  6975. else if (SCM_REALP (y))
  6976. {
  6977. scm_t_inum xx = SCM_I_INUM (x);
  6978. return scm_i_from_double (xx + SCM_REAL_VALUE (y));
  6979. }
  6980. else if (SCM_COMPLEXP (y))
  6981. {
  6982. scm_t_inum xx = SCM_I_INUM (x);
  6983. return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
  6984. SCM_COMPLEX_IMAG (y));
  6985. }
  6986. else if (SCM_FRACTIONP (y))
  6987. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  6988. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  6989. SCM_FRACTION_DENOMINATOR (y));
  6990. else
  6991. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  6992. }
  6993. else if (SCM_BIGP (x))
  6994. {
  6995. if (SCM_I_INUMP (y))
  6996. {
  6997. scm_t_inum inum;
  6998. int bigsgn;
  6999. add_big_inum:
  7000. inum = SCM_I_INUM (y);
  7001. if (inum == 0)
  7002. return x;
  7003. bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  7004. if (inum < 0)
  7005. {
  7006. SCM result = scm_i_mkbig ();
  7007. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
  7008. scm_remember_upto_here_1 (x);
  7009. /* we know the result will have to be a bignum */
  7010. if (bigsgn == -1)
  7011. return result;
  7012. return scm_i_normbig (result);
  7013. }
  7014. else
  7015. {
  7016. SCM result = scm_i_mkbig ();
  7017. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
  7018. scm_remember_upto_here_1 (x);
  7019. /* we know the result will have to be a bignum */
  7020. if (bigsgn == 1)
  7021. return result;
  7022. return scm_i_normbig (result);
  7023. }
  7024. }
  7025. else if (SCM_BIGP (y))
  7026. {
  7027. SCM result = scm_i_mkbig ();
  7028. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7029. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7030. mpz_add (SCM_I_BIG_MPZ (result),
  7031. SCM_I_BIG_MPZ (x),
  7032. SCM_I_BIG_MPZ (y));
  7033. scm_remember_upto_here_2 (x, y);
  7034. /* we know the result will have to be a bignum */
  7035. if (sgn_x == sgn_y)
  7036. return result;
  7037. return scm_i_normbig (result);
  7038. }
  7039. else if (SCM_REALP (y))
  7040. {
  7041. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
  7042. scm_remember_upto_here_1 (x);
  7043. return scm_i_from_double (result);
  7044. }
  7045. else if (SCM_COMPLEXP (y))
  7046. {
  7047. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  7048. + SCM_COMPLEX_REAL (y));
  7049. scm_remember_upto_here_1 (x);
  7050. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  7051. }
  7052. else if (SCM_FRACTIONP (y))
  7053. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  7054. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  7055. SCM_FRACTION_DENOMINATOR (y));
  7056. else
  7057. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7058. }
  7059. else if (SCM_REALP (x))
  7060. {
  7061. if (SCM_I_INUMP (y))
  7062. return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
  7063. else if (SCM_BIGP (y))
  7064. {
  7065. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
  7066. scm_remember_upto_here_1 (y);
  7067. return scm_i_from_double (result);
  7068. }
  7069. else if (SCM_REALP (y))
  7070. return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
  7071. else if (SCM_COMPLEXP (y))
  7072. return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
  7073. SCM_COMPLEX_IMAG (y));
  7074. else if (SCM_FRACTIONP (y))
  7075. return scm_i_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
  7076. else
  7077. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7078. }
  7079. else if (SCM_COMPLEXP (x))
  7080. {
  7081. if (SCM_I_INUMP (y))
  7082. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
  7083. SCM_COMPLEX_IMAG (x));
  7084. else if (SCM_BIGP (y))
  7085. {
  7086. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
  7087. + SCM_COMPLEX_REAL (x));
  7088. scm_remember_upto_here_1 (y);
  7089. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
  7090. }
  7091. else if (SCM_REALP (y))
  7092. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
  7093. SCM_COMPLEX_IMAG (x));
  7094. else if (SCM_COMPLEXP (y))
  7095. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
  7096. SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
  7097. else if (SCM_FRACTIONP (y))
  7098. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
  7099. SCM_COMPLEX_IMAG (x));
  7100. else
  7101. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7102. }
  7103. else if (SCM_FRACTIONP (x))
  7104. {
  7105. if (SCM_I_INUMP (y))
  7106. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  7107. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  7108. SCM_FRACTION_DENOMINATOR (x));
  7109. else if (SCM_BIGP (y))
  7110. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  7111. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  7112. SCM_FRACTION_DENOMINATOR (x));
  7113. else if (SCM_REALP (y))
  7114. return scm_i_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
  7115. else if (SCM_COMPLEXP (y))
  7116. return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
  7117. SCM_COMPLEX_IMAG (y));
  7118. else if (SCM_FRACTIONP (y))
  7119. /* a/b + c/d = (ad + bc) / bd */
  7120. return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  7121. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  7122. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  7123. else
  7124. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARGn, s_sum);
  7125. }
  7126. else
  7127. return scm_wta_dispatch_2 (g_sum, x, y, SCM_ARG1, s_sum);
  7128. }
  7129. SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
  7130. (SCM x),
  7131. "Return @math{@var{x}+1}.")
  7132. #define FUNC_NAME s_scm_oneplus
  7133. {
  7134. return scm_sum (x, SCM_INUM1);
  7135. }
  7136. #undef FUNC_NAME
  7137. SCM_PRIMITIVE_GENERIC (scm_i_difference, "-", 0, 2, 1,
  7138. (SCM x, SCM y, SCM rest),
  7139. "If called with one argument @var{z1}, -@var{z1} returned. Otherwise\n"
  7140. "the sum of all but the first argument are subtracted from the first\n"
  7141. "argument.")
  7142. #define FUNC_NAME s_scm_i_difference
  7143. {
  7144. while (!scm_is_null (rest))
  7145. { x = scm_difference (x, y);
  7146. y = scm_car (rest);
  7147. rest = scm_cdr (rest);
  7148. }
  7149. return scm_difference (x, y);
  7150. }
  7151. #undef FUNC_NAME
  7152. #define s_difference s_scm_i_difference
  7153. #define g_difference g_scm_i_difference
  7154. SCM
  7155. scm_difference (SCM x, SCM y)
  7156. #define FUNC_NAME s_difference
  7157. {
  7158. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7159. {
  7160. if (SCM_UNBNDP (x))
  7161. return scm_wta_dispatch_0 (g_difference, s_difference);
  7162. else
  7163. if (SCM_I_INUMP (x))
  7164. {
  7165. scm_t_inum xx = -SCM_I_INUM (x);
  7166. if (SCM_FIXABLE (xx))
  7167. return SCM_I_MAKINUM (xx);
  7168. else
  7169. return scm_i_inum2big (xx);
  7170. }
  7171. else if (SCM_BIGP (x))
  7172. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  7173. bignum, but negating that gives a fixnum. */
  7174. return scm_i_normbig (scm_i_clonebig (x, 0));
  7175. else if (SCM_REALP (x))
  7176. return scm_i_from_double (-SCM_REAL_VALUE (x));
  7177. else if (SCM_COMPLEXP (x))
  7178. return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
  7179. -SCM_COMPLEX_IMAG (x));
  7180. else if (SCM_FRACTIONP (x))
  7181. return scm_i_make_ratio_already_reduced
  7182. (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  7183. SCM_FRACTION_DENOMINATOR (x));
  7184. else
  7185. return scm_wta_dispatch_1 (g_difference, x, SCM_ARG1, s_difference);
  7186. }
  7187. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7188. {
  7189. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7190. {
  7191. scm_t_inum xx = SCM_I_INUM (x);
  7192. scm_t_inum yy = SCM_I_INUM (y);
  7193. scm_t_inum z = xx - yy;
  7194. if (SCM_FIXABLE (z))
  7195. return SCM_I_MAKINUM (z);
  7196. else
  7197. return scm_i_inum2big (z);
  7198. }
  7199. else if (SCM_BIGP (y))
  7200. {
  7201. /* inum-x - big-y */
  7202. scm_t_inum xx = SCM_I_INUM (x);
  7203. if (xx == 0)
  7204. {
  7205. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  7206. bignum, but negating that gives a fixnum. */
  7207. return scm_i_normbig (scm_i_clonebig (y, 0));
  7208. }
  7209. else
  7210. {
  7211. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7212. SCM result = scm_i_mkbig ();
  7213. if (xx >= 0)
  7214. mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
  7215. else
  7216. {
  7217. /* x - y == -(y + -x) */
  7218. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
  7219. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  7220. }
  7221. scm_remember_upto_here_1 (y);
  7222. if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
  7223. /* we know the result will have to be a bignum */
  7224. return result;
  7225. else
  7226. return scm_i_normbig (result);
  7227. }
  7228. }
  7229. else if (SCM_REALP (y))
  7230. {
  7231. scm_t_inum xx = SCM_I_INUM (x);
  7232. /*
  7233. * We need to handle x == exact 0
  7234. * specially because R6RS states that:
  7235. * (- 0.0) ==> -0.0 and
  7236. * (- 0.0 0.0) ==> 0.0
  7237. * and the scheme compiler changes
  7238. * (- 0.0) into (- 0 0.0)
  7239. * So we need to treat (- 0 0.0) like (- 0.0).
  7240. * At the C level, (-x) is different than (0.0 - x).
  7241. * (0.0 - 0.0) ==> 0.0, but (- 0.0) ==> -0.0.
  7242. */
  7243. if (xx == 0)
  7244. return scm_i_from_double (- SCM_REAL_VALUE (y));
  7245. else
  7246. return scm_i_from_double (xx - SCM_REAL_VALUE (y));
  7247. }
  7248. else if (SCM_COMPLEXP (y))
  7249. {
  7250. scm_t_inum xx = SCM_I_INUM (x);
  7251. /* We need to handle x == exact 0 specially.
  7252. See the comment above (for SCM_REALP (y)) */
  7253. if (xx == 0)
  7254. return scm_c_make_rectangular (- SCM_COMPLEX_REAL (y),
  7255. - SCM_COMPLEX_IMAG (y));
  7256. else
  7257. return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
  7258. - SCM_COMPLEX_IMAG (y));
  7259. }
  7260. else if (SCM_FRACTIONP (y))
  7261. /* a - b/c = (ac - b) / c */
  7262. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7263. SCM_FRACTION_NUMERATOR (y)),
  7264. SCM_FRACTION_DENOMINATOR (y));
  7265. else
  7266. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7267. }
  7268. else if (SCM_BIGP (x))
  7269. {
  7270. if (SCM_I_INUMP (y))
  7271. {
  7272. /* big-x - inum-y */
  7273. scm_t_inum yy = SCM_I_INUM (y);
  7274. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7275. scm_remember_upto_here_1 (x);
  7276. if (sgn_x == 0)
  7277. return (SCM_FIXABLE (-yy) ?
  7278. SCM_I_MAKINUM (-yy) : scm_from_inum (-yy));
  7279. else
  7280. {
  7281. SCM result = scm_i_mkbig ();
  7282. if (yy >= 0)
  7283. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  7284. else
  7285. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
  7286. scm_remember_upto_here_1 (x);
  7287. if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
  7288. /* we know the result will have to be a bignum */
  7289. return result;
  7290. else
  7291. return scm_i_normbig (result);
  7292. }
  7293. }
  7294. else if (SCM_BIGP (y))
  7295. {
  7296. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  7297. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  7298. SCM result = scm_i_mkbig ();
  7299. mpz_sub (SCM_I_BIG_MPZ (result),
  7300. SCM_I_BIG_MPZ (x),
  7301. SCM_I_BIG_MPZ (y));
  7302. scm_remember_upto_here_2 (x, y);
  7303. /* we know the result will have to be a bignum */
  7304. if ((sgn_x == 1) && (sgn_y == -1))
  7305. return result;
  7306. if ((sgn_x == -1) && (sgn_y == 1))
  7307. return result;
  7308. return scm_i_normbig (result);
  7309. }
  7310. else if (SCM_REALP (y))
  7311. {
  7312. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
  7313. scm_remember_upto_here_1 (x);
  7314. return scm_i_from_double (result);
  7315. }
  7316. else if (SCM_COMPLEXP (y))
  7317. {
  7318. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  7319. - SCM_COMPLEX_REAL (y));
  7320. scm_remember_upto_here_1 (x);
  7321. return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
  7322. }
  7323. else if (SCM_FRACTIONP (y))
  7324. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7325. SCM_FRACTION_NUMERATOR (y)),
  7326. SCM_FRACTION_DENOMINATOR (y));
  7327. else
  7328. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7329. }
  7330. else if (SCM_REALP (x))
  7331. {
  7332. if (SCM_I_INUMP (y))
  7333. return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
  7334. else if (SCM_BIGP (y))
  7335. {
  7336. double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
  7337. scm_remember_upto_here_1 (x);
  7338. return scm_i_from_double (result);
  7339. }
  7340. else if (SCM_REALP (y))
  7341. return scm_i_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
  7342. else if (SCM_COMPLEXP (y))
  7343. return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
  7344. -SCM_COMPLEX_IMAG (y));
  7345. else if (SCM_FRACTIONP (y))
  7346. return scm_i_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
  7347. else
  7348. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7349. }
  7350. else if (SCM_COMPLEXP (x))
  7351. {
  7352. if (SCM_I_INUMP (y))
  7353. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
  7354. SCM_COMPLEX_IMAG (x));
  7355. else if (SCM_BIGP (y))
  7356. {
  7357. double real_part = (SCM_COMPLEX_REAL (x)
  7358. - mpz_get_d (SCM_I_BIG_MPZ (y)));
  7359. scm_remember_upto_here_1 (x);
  7360. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  7361. }
  7362. else if (SCM_REALP (y))
  7363. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
  7364. SCM_COMPLEX_IMAG (x));
  7365. else if (SCM_COMPLEXP (y))
  7366. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
  7367. SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
  7368. else if (SCM_FRACTIONP (y))
  7369. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
  7370. SCM_COMPLEX_IMAG (x));
  7371. else
  7372. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7373. }
  7374. else if (SCM_FRACTIONP (x))
  7375. {
  7376. if (SCM_I_INUMP (y))
  7377. /* a/b - c = (a - cb) / b */
  7378. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  7379. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  7380. SCM_FRACTION_DENOMINATOR (x));
  7381. else if (SCM_BIGP (y))
  7382. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  7383. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  7384. SCM_FRACTION_DENOMINATOR (x));
  7385. else if (SCM_REALP (y))
  7386. return scm_i_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
  7387. else if (SCM_COMPLEXP (y))
  7388. return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
  7389. -SCM_COMPLEX_IMAG (y));
  7390. else if (SCM_FRACTIONP (y))
  7391. /* a/b - c/d = (ad - bc) / bd */
  7392. return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  7393. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  7394. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  7395. else
  7396. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARGn, s_difference);
  7397. }
  7398. else
  7399. return scm_wta_dispatch_2 (g_difference, x, y, SCM_ARG1, s_difference);
  7400. }
  7401. #undef FUNC_NAME
  7402. SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
  7403. (SCM x),
  7404. "Return @math{@var{x}-1}.")
  7405. #define FUNC_NAME s_scm_oneminus
  7406. {
  7407. return scm_difference (x, SCM_INUM1);
  7408. }
  7409. #undef FUNC_NAME
  7410. SCM_PRIMITIVE_GENERIC (scm_i_product, "*", 0, 2, 1,
  7411. (SCM x, SCM y, SCM rest),
  7412. "Return the product of all arguments. If called without arguments,\n"
  7413. "1 is returned.")
  7414. #define FUNC_NAME s_scm_i_product
  7415. {
  7416. while (!scm_is_null (rest))
  7417. { x = scm_product (x, y);
  7418. y = scm_car (rest);
  7419. rest = scm_cdr (rest);
  7420. }
  7421. return scm_product (x, y);
  7422. }
  7423. #undef FUNC_NAME
  7424. #define s_product s_scm_i_product
  7425. #define g_product g_scm_i_product
  7426. SCM
  7427. scm_product (SCM x, SCM y)
  7428. {
  7429. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7430. {
  7431. if (SCM_UNBNDP (x))
  7432. return SCM_I_MAKINUM (1L);
  7433. else if (SCM_NUMBERP (x))
  7434. return x;
  7435. else
  7436. return scm_wta_dispatch_1 (g_product, x, SCM_ARG1, s_product);
  7437. }
  7438. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7439. {
  7440. scm_t_inum xx;
  7441. xinum:
  7442. xx = SCM_I_INUM (x);
  7443. switch (xx)
  7444. {
  7445. case 1:
  7446. /* exact1 is the universal multiplicative identity */
  7447. return y;
  7448. break;
  7449. case 0:
  7450. /* exact0 times a fixnum is exact0: optimize this case */
  7451. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7452. return SCM_INUM0;
  7453. /* if the other argument is inexact, the result is inexact,
  7454. and we must do the multiplication in order to handle
  7455. infinities and NaNs properly. */
  7456. else if (SCM_REALP (y))
  7457. return scm_i_from_double (0.0 * SCM_REAL_VALUE (y));
  7458. else if (SCM_COMPLEXP (y))
  7459. return scm_c_make_rectangular (0.0 * SCM_COMPLEX_REAL (y),
  7460. 0.0 * SCM_COMPLEX_IMAG (y));
  7461. /* we've already handled inexact numbers,
  7462. so y must be exact, and we return exact0 */
  7463. else if (SCM_NUMP (y))
  7464. return SCM_INUM0;
  7465. else
  7466. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7467. break;
  7468. }
  7469. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7470. {
  7471. scm_t_inum yy = SCM_I_INUM (y);
  7472. #if SCM_I_FIXNUM_BIT < 32 && SCM_HAVE_T_INT64
  7473. int64_t kk = xx * (int64_t) yy;
  7474. if (SCM_FIXABLE (kk))
  7475. return SCM_I_MAKINUM (kk);
  7476. #else
  7477. scm_t_inum axx = (xx > 0) ? xx : -xx;
  7478. scm_t_inum ayy = (yy > 0) ? yy : -yy;
  7479. if (SCM_MOST_POSITIVE_FIXNUM / axx >= ayy)
  7480. return SCM_I_MAKINUM (xx * yy);
  7481. #endif
  7482. else
  7483. {
  7484. SCM result = scm_i_inum2big (xx);
  7485. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
  7486. return scm_i_normbig (result);
  7487. }
  7488. }
  7489. else if (SCM_BIGP (y))
  7490. {
  7491. /* There is one bignum which, when multiplied by negative one,
  7492. becomes a non-zero fixnum: (1+ most-positive-fixum). Since
  7493. we know the type of X and Y are numbers, delegate this
  7494. special case to scm_difference. */
  7495. if (xx == -1)
  7496. return scm_difference (y, SCM_UNDEFINED);
  7497. else
  7498. {
  7499. SCM result = scm_i_mkbig ();
  7500. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
  7501. scm_remember_upto_here_1 (y);
  7502. return result;
  7503. }
  7504. }
  7505. else if (SCM_REALP (y))
  7506. return scm_i_from_double (xx * SCM_REAL_VALUE (y));
  7507. else if (SCM_COMPLEXP (y))
  7508. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  7509. xx * SCM_COMPLEX_IMAG (y));
  7510. else if (SCM_FRACTIONP (y))
  7511. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  7512. SCM_FRACTION_DENOMINATOR (y));
  7513. else
  7514. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7515. }
  7516. else if (SCM_BIGP (x))
  7517. {
  7518. if (SCM_I_INUMP (y))
  7519. {
  7520. SCM_SWAP (x, y);
  7521. goto xinum;
  7522. }
  7523. else if (SCM_BIGP (y))
  7524. {
  7525. SCM result = scm_i_mkbig ();
  7526. mpz_mul (SCM_I_BIG_MPZ (result),
  7527. SCM_I_BIG_MPZ (x),
  7528. SCM_I_BIG_MPZ (y));
  7529. scm_remember_upto_here_2 (x, y);
  7530. return result;
  7531. }
  7532. else if (SCM_REALP (y))
  7533. {
  7534. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
  7535. scm_remember_upto_here_1 (x);
  7536. return scm_i_from_double (result);
  7537. }
  7538. else if (SCM_COMPLEXP (y))
  7539. {
  7540. double z = mpz_get_d (SCM_I_BIG_MPZ (x));
  7541. scm_remember_upto_here_1 (x);
  7542. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
  7543. z * SCM_COMPLEX_IMAG (y));
  7544. }
  7545. else if (SCM_FRACTIONP (y))
  7546. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  7547. SCM_FRACTION_DENOMINATOR (y));
  7548. else
  7549. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7550. }
  7551. else if (SCM_REALP (x))
  7552. {
  7553. if (SCM_I_INUMP (y))
  7554. {
  7555. SCM_SWAP (x, y);
  7556. goto xinum;
  7557. }
  7558. else if (SCM_BIGP (y))
  7559. {
  7560. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
  7561. scm_remember_upto_here_1 (y);
  7562. return scm_i_from_double (result);
  7563. }
  7564. else if (SCM_REALP (y))
  7565. return scm_i_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
  7566. else if (SCM_COMPLEXP (y))
  7567. return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
  7568. SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
  7569. else if (SCM_FRACTIONP (y))
  7570. return scm_i_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
  7571. else
  7572. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7573. }
  7574. else if (SCM_COMPLEXP (x))
  7575. {
  7576. if (SCM_I_INUMP (y))
  7577. {
  7578. SCM_SWAP (x, y);
  7579. goto xinum;
  7580. }
  7581. else if (SCM_BIGP (y))
  7582. {
  7583. double z = mpz_get_d (SCM_I_BIG_MPZ (y));
  7584. scm_remember_upto_here_1 (y);
  7585. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
  7586. z * SCM_COMPLEX_IMAG (x));
  7587. }
  7588. else if (SCM_REALP (y))
  7589. return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
  7590. SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
  7591. else if (SCM_COMPLEXP (y))
  7592. {
  7593. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
  7594. - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
  7595. SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
  7596. + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
  7597. }
  7598. else if (SCM_FRACTIONP (y))
  7599. {
  7600. double yy = scm_i_fraction2double (y);
  7601. return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
  7602. yy * SCM_COMPLEX_IMAG (x));
  7603. }
  7604. else
  7605. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7606. }
  7607. else if (SCM_FRACTIONP (x))
  7608. {
  7609. if (SCM_I_INUMP (y))
  7610. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  7611. SCM_FRACTION_DENOMINATOR (x));
  7612. else if (SCM_BIGP (y))
  7613. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  7614. SCM_FRACTION_DENOMINATOR (x));
  7615. else if (SCM_REALP (y))
  7616. return scm_i_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
  7617. else if (SCM_COMPLEXP (y))
  7618. {
  7619. double xx = scm_i_fraction2double (x);
  7620. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  7621. xx * SCM_COMPLEX_IMAG (y));
  7622. }
  7623. else if (SCM_FRACTIONP (y))
  7624. /* a/b * c/d = ac / bd */
  7625. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  7626. SCM_FRACTION_NUMERATOR (y)),
  7627. scm_product (SCM_FRACTION_DENOMINATOR (x),
  7628. SCM_FRACTION_DENOMINATOR (y)));
  7629. else
  7630. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARGn, s_product);
  7631. }
  7632. else
  7633. return scm_wta_dispatch_2 (g_product, x, y, SCM_ARG1, s_product);
  7634. }
  7635. #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
  7636. || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
  7637. #define ALLOW_DIVIDE_BY_ZERO
  7638. /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
  7639. #endif
  7640. /* The code below for complex division is adapted from the GNU
  7641. libstdc++, which adapted it from f2c's libF77, and is subject to
  7642. this copyright: */
  7643. /****************************************************************
  7644. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  7645. Permission to use, copy, modify, and distribute this software
  7646. and its documentation for any purpose and without fee is hereby
  7647. granted, provided that the above copyright notice appear in all
  7648. copies and that both that the copyright notice and this
  7649. permission notice and warranty disclaimer appear in supporting
  7650. documentation, and that the names of AT&T Bell Laboratories or
  7651. Bellcore or any of their entities not be used in advertising or
  7652. publicity pertaining to distribution of the software without
  7653. specific, written prior permission.
  7654. AT&T and Bellcore disclaim all warranties with regard to this
  7655. software, including all implied warranties of merchantability
  7656. and fitness. In no event shall AT&T or Bellcore be liable for
  7657. any special, indirect or consequential damages or any damages
  7658. whatsoever resulting from loss of use, data or profits, whether
  7659. in an action of contract, negligence or other tortious action,
  7660. arising out of or in connection with the use or performance of
  7661. this software.
  7662. ****************************************************************/
  7663. SCM_PRIMITIVE_GENERIC (scm_i_divide, "/", 0, 2, 1,
  7664. (SCM x, SCM y, SCM rest),
  7665. "Divide the first argument by the product of the remaining\n"
  7666. "arguments. If called with one argument @var{z1}, 1/@var{z1} is\n"
  7667. "returned.")
  7668. #define FUNC_NAME s_scm_i_divide
  7669. {
  7670. while (!scm_is_null (rest))
  7671. { x = scm_divide (x, y);
  7672. y = scm_car (rest);
  7673. rest = scm_cdr (rest);
  7674. }
  7675. return scm_divide (x, y);
  7676. }
  7677. #undef FUNC_NAME
  7678. #define s_divide s_scm_i_divide
  7679. #define g_divide g_scm_i_divide
  7680. SCM
  7681. scm_divide (SCM x, SCM y)
  7682. #define FUNC_NAME s_divide
  7683. {
  7684. double a;
  7685. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  7686. {
  7687. if (SCM_UNBNDP (x))
  7688. return scm_wta_dispatch_0 (g_divide, s_divide);
  7689. else if (SCM_I_INUMP (x))
  7690. {
  7691. scm_t_inum xx = SCM_I_INUM (x);
  7692. if (xx == 1 || xx == -1)
  7693. return x;
  7694. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7695. else if (xx == 0)
  7696. scm_num_overflow (s_divide);
  7697. #endif
  7698. else
  7699. return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  7700. }
  7701. else if (SCM_BIGP (x))
  7702. return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  7703. else if (SCM_REALP (x))
  7704. {
  7705. double xx = SCM_REAL_VALUE (x);
  7706. #ifndef ALLOW_DIVIDE_BY_ZERO
  7707. if (xx == 0.0)
  7708. scm_num_overflow (s_divide);
  7709. else
  7710. #endif
  7711. return scm_i_from_double (1.0 / xx);
  7712. }
  7713. else if (SCM_COMPLEXP (x))
  7714. {
  7715. double r = SCM_COMPLEX_REAL (x);
  7716. double i = SCM_COMPLEX_IMAG (x);
  7717. if (fabs(r) <= fabs(i))
  7718. {
  7719. double t = r / i;
  7720. double d = i * (1.0 + t * t);
  7721. return scm_c_make_rectangular (t / d, -1.0 / d);
  7722. }
  7723. else
  7724. {
  7725. double t = i / r;
  7726. double d = r * (1.0 + t * t);
  7727. return scm_c_make_rectangular (1.0 / d, -t / d);
  7728. }
  7729. }
  7730. else if (SCM_FRACTIONP (x))
  7731. return scm_i_make_ratio_already_reduced (SCM_FRACTION_DENOMINATOR (x),
  7732. SCM_FRACTION_NUMERATOR (x));
  7733. else
  7734. return scm_wta_dispatch_1 (g_divide, x, SCM_ARG1, s_divide);
  7735. }
  7736. if (SCM_LIKELY (SCM_I_INUMP (x)))
  7737. {
  7738. scm_t_inum xx = SCM_I_INUM (x);
  7739. if (SCM_LIKELY (SCM_I_INUMP (y)))
  7740. {
  7741. scm_t_inum yy = SCM_I_INUM (y);
  7742. if (yy == 0)
  7743. {
  7744. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7745. scm_num_overflow (s_divide);
  7746. #else
  7747. return scm_i_from_double ((double) xx / (double) yy);
  7748. #endif
  7749. }
  7750. else if (xx % yy != 0)
  7751. return scm_i_make_ratio (x, y);
  7752. else
  7753. {
  7754. scm_t_inum z = xx / yy;
  7755. if (SCM_FIXABLE (z))
  7756. return SCM_I_MAKINUM (z);
  7757. else
  7758. return scm_i_inum2big (z);
  7759. }
  7760. }
  7761. else if (SCM_BIGP (y))
  7762. return scm_i_make_ratio (x, y);
  7763. else if (SCM_REALP (y))
  7764. {
  7765. double yy = SCM_REAL_VALUE (y);
  7766. #ifndef ALLOW_DIVIDE_BY_ZERO
  7767. if (yy == 0.0)
  7768. scm_num_overflow (s_divide);
  7769. else
  7770. #endif
  7771. /* FIXME: Precision may be lost here due to:
  7772. (1) The cast from 'scm_t_inum' to 'double'
  7773. (2) Double rounding */
  7774. return scm_i_from_double ((double) xx / yy);
  7775. }
  7776. else if (SCM_COMPLEXP (y))
  7777. {
  7778. a = xx;
  7779. complex_div: /* y _must_ be a complex number */
  7780. {
  7781. double r = SCM_COMPLEX_REAL (y);
  7782. double i = SCM_COMPLEX_IMAG (y);
  7783. if (fabs(r) <= fabs(i))
  7784. {
  7785. double t = r / i;
  7786. double d = i * (1.0 + t * t);
  7787. return scm_c_make_rectangular ((a * t) / d, -a / d);
  7788. }
  7789. else
  7790. {
  7791. double t = i / r;
  7792. double d = r * (1.0 + t * t);
  7793. return scm_c_make_rectangular (a / d, -(a * t) / d);
  7794. }
  7795. }
  7796. }
  7797. else if (SCM_FRACTIONP (y))
  7798. /* a / b/c = ac / b */
  7799. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7800. SCM_FRACTION_NUMERATOR (y));
  7801. else
  7802. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7803. }
  7804. else if (SCM_BIGP (x))
  7805. {
  7806. if (SCM_I_INUMP (y))
  7807. {
  7808. scm_t_inum yy = SCM_I_INUM (y);
  7809. if (yy == 0)
  7810. {
  7811. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7812. scm_num_overflow (s_divide);
  7813. #else
  7814. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  7815. scm_remember_upto_here_1 (x);
  7816. return (sgn == 0) ? scm_nan () : scm_inf ();
  7817. #endif
  7818. }
  7819. else if (yy == 1)
  7820. return x;
  7821. else
  7822. {
  7823. /* FIXME: HMM, what are the relative performance issues here?
  7824. We need to test. Is it faster on average to test
  7825. divisible_p, then perform whichever operation, or is it
  7826. faster to perform the integer div opportunistically and
  7827. switch to real if there's a remainder? For now we take the
  7828. middle ground: test, then if divisible, use the faster div
  7829. func. */
  7830. scm_t_inum abs_yy = yy < 0 ? -yy : yy;
  7831. int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
  7832. if (divisible_p)
  7833. {
  7834. SCM result = scm_i_mkbig ();
  7835. mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
  7836. scm_remember_upto_here_1 (x);
  7837. if (yy < 0)
  7838. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  7839. return scm_i_normbig (result);
  7840. }
  7841. else
  7842. return scm_i_make_ratio (x, y);
  7843. }
  7844. }
  7845. else if (SCM_BIGP (y))
  7846. {
  7847. int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
  7848. SCM_I_BIG_MPZ (y));
  7849. if (divisible_p)
  7850. {
  7851. SCM result = scm_i_mkbig ();
  7852. mpz_divexact (SCM_I_BIG_MPZ (result),
  7853. SCM_I_BIG_MPZ (x),
  7854. SCM_I_BIG_MPZ (y));
  7855. scm_remember_upto_here_2 (x, y);
  7856. return scm_i_normbig (result);
  7857. }
  7858. else
  7859. return scm_i_make_ratio (x, y);
  7860. }
  7861. else if (SCM_REALP (y))
  7862. {
  7863. double yy = SCM_REAL_VALUE (y);
  7864. #ifndef ALLOW_DIVIDE_BY_ZERO
  7865. if (yy == 0.0)
  7866. scm_num_overflow (s_divide);
  7867. else
  7868. #endif
  7869. /* FIXME: Precision may be lost here due to:
  7870. (1) scm_i_big2dbl (2) Double rounding */
  7871. return scm_i_from_double (scm_i_big2dbl (x) / yy);
  7872. }
  7873. else if (SCM_COMPLEXP (y))
  7874. {
  7875. a = scm_i_big2dbl (x);
  7876. goto complex_div;
  7877. }
  7878. else if (SCM_FRACTIONP (y))
  7879. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  7880. SCM_FRACTION_NUMERATOR (y));
  7881. else
  7882. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7883. }
  7884. else if (SCM_REALP (x))
  7885. {
  7886. double rx = SCM_REAL_VALUE (x);
  7887. if (SCM_I_INUMP (y))
  7888. {
  7889. scm_t_inum yy = SCM_I_INUM (y);
  7890. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7891. if (yy == 0)
  7892. scm_num_overflow (s_divide);
  7893. else
  7894. #endif
  7895. /* FIXME: Precision may be lost here due to:
  7896. (1) The cast from 'scm_t_inum' to 'double'
  7897. (2) Double rounding */
  7898. return scm_i_from_double (rx / (double) yy);
  7899. }
  7900. else if (SCM_BIGP (y))
  7901. {
  7902. /* FIXME: Precision may be lost here due to:
  7903. (1) The conversion from bignum to double
  7904. (2) Double rounding */
  7905. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  7906. scm_remember_upto_here_1 (y);
  7907. return scm_i_from_double (rx / dby);
  7908. }
  7909. else if (SCM_REALP (y))
  7910. {
  7911. double yy = SCM_REAL_VALUE (y);
  7912. #ifndef ALLOW_DIVIDE_BY_ZERO
  7913. if (yy == 0.0)
  7914. scm_num_overflow (s_divide);
  7915. else
  7916. #endif
  7917. return scm_i_from_double (rx / yy);
  7918. }
  7919. else if (SCM_COMPLEXP (y))
  7920. {
  7921. a = rx;
  7922. goto complex_div;
  7923. }
  7924. else if (SCM_FRACTIONP (y))
  7925. return scm_i_from_double (rx / scm_i_fraction2double (y));
  7926. else
  7927. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7928. }
  7929. else if (SCM_COMPLEXP (x))
  7930. {
  7931. double rx = SCM_COMPLEX_REAL (x);
  7932. double ix = SCM_COMPLEX_IMAG (x);
  7933. if (SCM_I_INUMP (y))
  7934. {
  7935. scm_t_inum yy = SCM_I_INUM (y);
  7936. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  7937. if (yy == 0)
  7938. scm_num_overflow (s_divide);
  7939. else
  7940. #endif
  7941. {
  7942. /* FIXME: Precision may be lost here due to:
  7943. (1) The conversion from 'scm_t_inum' to double
  7944. (2) Double rounding */
  7945. double d = yy;
  7946. return scm_c_make_rectangular (rx / d, ix / d);
  7947. }
  7948. }
  7949. else if (SCM_BIGP (y))
  7950. {
  7951. /* FIXME: Precision may be lost here due to:
  7952. (1) The conversion from bignum to double
  7953. (2) Double rounding */
  7954. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  7955. scm_remember_upto_here_1 (y);
  7956. return scm_c_make_rectangular (rx / dby, ix / dby);
  7957. }
  7958. else if (SCM_REALP (y))
  7959. {
  7960. double yy = SCM_REAL_VALUE (y);
  7961. #ifndef ALLOW_DIVIDE_BY_ZERO
  7962. if (yy == 0.0)
  7963. scm_num_overflow (s_divide);
  7964. else
  7965. #endif
  7966. return scm_c_make_rectangular (rx / yy, ix / yy);
  7967. }
  7968. else if (SCM_COMPLEXP (y))
  7969. {
  7970. double ry = SCM_COMPLEX_REAL (y);
  7971. double iy = SCM_COMPLEX_IMAG (y);
  7972. if (fabs(ry) <= fabs(iy))
  7973. {
  7974. double t = ry / iy;
  7975. double d = iy * (1.0 + t * t);
  7976. return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
  7977. }
  7978. else
  7979. {
  7980. double t = iy / ry;
  7981. double d = ry * (1.0 + t * t);
  7982. return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
  7983. }
  7984. }
  7985. else if (SCM_FRACTIONP (y))
  7986. {
  7987. /* FIXME: Precision may be lost here due to:
  7988. (1) The conversion from fraction to double
  7989. (2) Double rounding */
  7990. double yy = scm_i_fraction2double (y);
  7991. return scm_c_make_rectangular (rx / yy, ix / yy);
  7992. }
  7993. else
  7994. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  7995. }
  7996. else if (SCM_FRACTIONP (x))
  7997. {
  7998. if (SCM_I_INUMP (y))
  7999. {
  8000. scm_t_inum yy = SCM_I_INUM (y);
  8001. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  8002. if (yy == 0)
  8003. scm_num_overflow (s_divide);
  8004. else
  8005. #endif
  8006. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  8007. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  8008. }
  8009. else if (SCM_BIGP (y))
  8010. {
  8011. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  8012. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  8013. }
  8014. else if (SCM_REALP (y))
  8015. {
  8016. double yy = SCM_REAL_VALUE (y);
  8017. #ifndef ALLOW_DIVIDE_BY_ZERO
  8018. if (yy == 0.0)
  8019. scm_num_overflow (s_divide);
  8020. else
  8021. #endif
  8022. /* FIXME: Precision may be lost here due to:
  8023. (1) The conversion from fraction to double
  8024. (2) Double rounding */
  8025. return scm_i_from_double (scm_i_fraction2double (x) / yy);
  8026. }
  8027. else if (SCM_COMPLEXP (y))
  8028. {
  8029. /* FIXME: Precision may be lost here due to:
  8030. (1) The conversion from fraction to double
  8031. (2) Double rounding */
  8032. a = scm_i_fraction2double (x);
  8033. goto complex_div;
  8034. }
  8035. else if (SCM_FRACTIONP (y))
  8036. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  8037. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
  8038. else
  8039. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARGn, s_divide);
  8040. }
  8041. else
  8042. return scm_wta_dispatch_2 (g_divide, x, y, SCM_ARG1, s_divide);
  8043. }
  8044. #undef FUNC_NAME
  8045. double
  8046. scm_c_truncate (double x)
  8047. {
  8048. return trunc (x);
  8049. }
  8050. /* scm_c_round is done using floor(x+0.5) to round to nearest and with
  8051. half-way case (ie. when x is an integer plus 0.5) going upwards.
  8052. Then half-way cases are identified and adjusted down if the
  8053. round-upwards didn't give the desired even integer.
  8054. "plus_half == result" identifies a half-way case. If plus_half, which is
  8055. x + 0.5, is an integer then x must be an integer plus 0.5.
  8056. An odd "result" value is identified with result/2 != floor(result/2).
  8057. This is done with plus_half, since that value is ready for use sooner in
  8058. a pipelined cpu, and we're already requiring plus_half == result.
  8059. Note however that we need to be careful when x is big and already an
  8060. integer. In that case "x+0.5" may round to an adjacent integer, causing
  8061. us to return such a value, incorrectly. For instance if the hardware is
  8062. in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
  8063. (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
  8064. returned. Or if the hardware is in round-upwards mode, then other bigger
  8065. values like say x == 2^128 will see x+0.5 rounding up to the next higher
  8066. representable value, 2^128+2^76 (or whatever), again incorrect.
  8067. These bad roundings of x+0.5 are avoided by testing at the start whether
  8068. x is already an integer. If it is then clearly that's the desired result
  8069. already. And if it's not then the exponent must be small enough to allow
  8070. an 0.5 to be represented, and hence added without a bad rounding. */
  8071. double
  8072. scm_c_round (double x)
  8073. {
  8074. double plus_half, result;
  8075. if (x == floor (x))
  8076. return x;
  8077. plus_half = x + 0.5;
  8078. result = floor (plus_half);
  8079. /* Adjust so that the rounding is towards even. */
  8080. return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
  8081. ? result - 1
  8082. : result);
  8083. }
  8084. SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0,
  8085. (SCM x),
  8086. "Round the number @var{x} towards zero.")
  8087. #define FUNC_NAME s_scm_truncate_number
  8088. {
  8089. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8090. return x;
  8091. else if (SCM_REALP (x))
  8092. return scm_i_from_double (trunc (SCM_REAL_VALUE (x)));
  8093. else if (SCM_FRACTIONP (x))
  8094. return scm_truncate_quotient (SCM_FRACTION_NUMERATOR (x),
  8095. SCM_FRACTION_DENOMINATOR (x));
  8096. else
  8097. return scm_wta_dispatch_1 (g_scm_truncate_number, x, SCM_ARG1,
  8098. s_scm_truncate_number);
  8099. }
  8100. #undef FUNC_NAME
  8101. SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0,
  8102. (SCM x),
  8103. "Round the number @var{x} towards the nearest integer. "
  8104. "When it is exactly halfway between two integers, "
  8105. "round towards the even one.")
  8106. #define FUNC_NAME s_scm_round_number
  8107. {
  8108. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8109. return x;
  8110. else if (SCM_REALP (x))
  8111. return scm_i_from_double (scm_c_round (SCM_REAL_VALUE (x)));
  8112. else if (SCM_FRACTIONP (x))
  8113. return scm_round_quotient (SCM_FRACTION_NUMERATOR (x),
  8114. SCM_FRACTION_DENOMINATOR (x));
  8115. else
  8116. return scm_wta_dispatch_1 (g_scm_round_number, x, SCM_ARG1,
  8117. s_scm_round_number);
  8118. }
  8119. #undef FUNC_NAME
  8120. SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
  8121. (SCM x),
  8122. "Round the number @var{x} towards minus infinity.")
  8123. #define FUNC_NAME s_scm_floor
  8124. {
  8125. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8126. return x;
  8127. else if (SCM_REALP (x))
  8128. return scm_i_from_double (floor (SCM_REAL_VALUE (x)));
  8129. else if (SCM_FRACTIONP (x))
  8130. return scm_floor_quotient (SCM_FRACTION_NUMERATOR (x),
  8131. SCM_FRACTION_DENOMINATOR (x));
  8132. else
  8133. return scm_wta_dispatch_1 (g_scm_floor, x, 1, s_scm_floor);
  8134. }
  8135. #undef FUNC_NAME
  8136. SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
  8137. (SCM x),
  8138. "Round the number @var{x} towards infinity.")
  8139. #define FUNC_NAME s_scm_ceiling
  8140. {
  8141. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  8142. return x;
  8143. else if (SCM_REALP (x))
  8144. return scm_i_from_double (ceil (SCM_REAL_VALUE (x)));
  8145. else if (SCM_FRACTIONP (x))
  8146. return scm_ceiling_quotient (SCM_FRACTION_NUMERATOR (x),
  8147. SCM_FRACTION_DENOMINATOR (x));
  8148. else
  8149. return scm_wta_dispatch_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
  8150. }
  8151. #undef FUNC_NAME
  8152. SCM_PRIMITIVE_GENERIC (scm_expt, "expt", 2, 0, 0,
  8153. (SCM x, SCM y),
  8154. "Return @var{x} raised to the power of @var{y}.")
  8155. #define FUNC_NAME s_scm_expt
  8156. {
  8157. if (scm_is_integer (y))
  8158. {
  8159. if (scm_is_true (scm_exact_p (y)))
  8160. return scm_integer_expt (x, y);
  8161. else
  8162. {
  8163. /* Here we handle the case where the exponent is an inexact
  8164. integer. We make the exponent exact in order to use
  8165. scm_integer_expt, and thus avoid the spurious imaginary
  8166. parts that may result from round-off errors in the general
  8167. e^(y log x) method below (for example when squaring a large
  8168. negative number). In this case, we must return an inexact
  8169. result for correctness. We also make the base inexact so
  8170. that scm_integer_expt will use fast inexact arithmetic
  8171. internally. Note that making the base inexact is not
  8172. sufficient to guarantee an inexact result, because
  8173. scm_integer_expt will return an exact 1 when the exponent
  8174. is 0, even if the base is inexact. */
  8175. return scm_exact_to_inexact
  8176. (scm_integer_expt (scm_exact_to_inexact (x),
  8177. scm_inexact_to_exact (y)));
  8178. }
  8179. }
  8180. else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0)
  8181. {
  8182. return scm_i_from_double (pow (scm_to_double (x), scm_to_double (y)));
  8183. }
  8184. else if (scm_is_complex (x) && scm_is_complex (y))
  8185. return scm_exp (scm_product (scm_log (x), y));
  8186. else if (scm_is_complex (x))
  8187. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG2, s_scm_expt);
  8188. else
  8189. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG1, s_scm_expt);
  8190. }
  8191. #undef FUNC_NAME
  8192. /* sin/cos/tan/asin/acos/atan
  8193. sinh/cosh/tanh/asinh/acosh/atanh
  8194. Derived from "Transcen.scm", Complex trancendental functions for SCM.
  8195. Written by Jerry D. Hedden, (C) FSF.
  8196. See the file `COPYING' for terms applying to this program. */
  8197. SCM_PRIMITIVE_GENERIC (scm_sin, "sin", 1, 0, 0,
  8198. (SCM z),
  8199. "Compute the sine of @var{z}.")
  8200. #define FUNC_NAME s_scm_sin
  8201. {
  8202. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8203. return z; /* sin(exact0) = exact0 */
  8204. else if (scm_is_real (z))
  8205. return scm_i_from_double (sin (scm_to_double (z)));
  8206. else if (SCM_COMPLEXP (z))
  8207. { double x, y;
  8208. x = SCM_COMPLEX_REAL (z);
  8209. y = SCM_COMPLEX_IMAG (z);
  8210. return scm_c_make_rectangular (sin (x) * cosh (y),
  8211. cos (x) * sinh (y));
  8212. }
  8213. else
  8214. return scm_wta_dispatch_1 (g_scm_sin, z, 1, s_scm_sin);
  8215. }
  8216. #undef FUNC_NAME
  8217. SCM_PRIMITIVE_GENERIC (scm_cos, "cos", 1, 0, 0,
  8218. (SCM z),
  8219. "Compute the cosine of @var{z}.")
  8220. #define FUNC_NAME s_scm_cos
  8221. {
  8222. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8223. return SCM_INUM1; /* cos(exact0) = exact1 */
  8224. else if (scm_is_real (z))
  8225. return scm_i_from_double (cos (scm_to_double (z)));
  8226. else if (SCM_COMPLEXP (z))
  8227. { double x, y;
  8228. x = SCM_COMPLEX_REAL (z);
  8229. y = SCM_COMPLEX_IMAG (z);
  8230. return scm_c_make_rectangular (cos (x) * cosh (y),
  8231. -sin (x) * sinh (y));
  8232. }
  8233. else
  8234. return scm_wta_dispatch_1 (g_scm_cos, z, 1, s_scm_cos);
  8235. }
  8236. #undef FUNC_NAME
  8237. SCM_PRIMITIVE_GENERIC (scm_tan, "tan", 1, 0, 0,
  8238. (SCM z),
  8239. "Compute the tangent of @var{z}.")
  8240. #define FUNC_NAME s_scm_tan
  8241. {
  8242. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8243. return z; /* tan(exact0) = exact0 */
  8244. else if (scm_is_real (z))
  8245. return scm_i_from_double (tan (scm_to_double (z)));
  8246. else if (SCM_COMPLEXP (z))
  8247. { double x, y, w;
  8248. x = 2.0 * SCM_COMPLEX_REAL (z);
  8249. y = 2.0 * SCM_COMPLEX_IMAG (z);
  8250. w = cos (x) + cosh (y);
  8251. #ifndef ALLOW_DIVIDE_BY_ZERO
  8252. if (w == 0.0)
  8253. scm_num_overflow (s_scm_tan);
  8254. #endif
  8255. return scm_c_make_rectangular (sin (x) / w, sinh (y) / w);
  8256. }
  8257. else
  8258. return scm_wta_dispatch_1 (g_scm_tan, z, 1, s_scm_tan);
  8259. }
  8260. #undef FUNC_NAME
  8261. SCM_PRIMITIVE_GENERIC (scm_sinh, "sinh", 1, 0, 0,
  8262. (SCM z),
  8263. "Compute the hyperbolic sine of @var{z}.")
  8264. #define FUNC_NAME s_scm_sinh
  8265. {
  8266. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8267. return z; /* sinh(exact0) = exact0 */
  8268. else if (scm_is_real (z))
  8269. return scm_i_from_double (sinh (scm_to_double (z)));
  8270. else if (SCM_COMPLEXP (z))
  8271. { double x, y;
  8272. x = SCM_COMPLEX_REAL (z);
  8273. y = SCM_COMPLEX_IMAG (z);
  8274. return scm_c_make_rectangular (sinh (x) * cos (y),
  8275. cosh (x) * sin (y));
  8276. }
  8277. else
  8278. return scm_wta_dispatch_1 (g_scm_sinh, z, 1, s_scm_sinh);
  8279. }
  8280. #undef FUNC_NAME
  8281. SCM_PRIMITIVE_GENERIC (scm_cosh, "cosh", 1, 0, 0,
  8282. (SCM z),
  8283. "Compute the hyperbolic cosine of @var{z}.")
  8284. #define FUNC_NAME s_scm_cosh
  8285. {
  8286. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8287. return SCM_INUM1; /* cosh(exact0) = exact1 */
  8288. else if (scm_is_real (z))
  8289. return scm_i_from_double (cosh (scm_to_double (z)));
  8290. else if (SCM_COMPLEXP (z))
  8291. { double x, y;
  8292. x = SCM_COMPLEX_REAL (z);
  8293. y = SCM_COMPLEX_IMAG (z);
  8294. return scm_c_make_rectangular (cosh (x) * cos (y),
  8295. sinh (x) * sin (y));
  8296. }
  8297. else
  8298. return scm_wta_dispatch_1 (g_scm_cosh, z, 1, s_scm_cosh);
  8299. }
  8300. #undef FUNC_NAME
  8301. SCM_PRIMITIVE_GENERIC (scm_tanh, "tanh", 1, 0, 0,
  8302. (SCM z),
  8303. "Compute the hyperbolic tangent of @var{z}.")
  8304. #define FUNC_NAME s_scm_tanh
  8305. {
  8306. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8307. return z; /* tanh(exact0) = exact0 */
  8308. else if (scm_is_real (z))
  8309. return scm_i_from_double (tanh (scm_to_double (z)));
  8310. else if (SCM_COMPLEXP (z))
  8311. { double x, y, w;
  8312. x = 2.0 * SCM_COMPLEX_REAL (z);
  8313. y = 2.0 * SCM_COMPLEX_IMAG (z);
  8314. w = cosh (x) + cos (y);
  8315. #ifndef ALLOW_DIVIDE_BY_ZERO
  8316. if (w == 0.0)
  8317. scm_num_overflow (s_scm_tanh);
  8318. #endif
  8319. return scm_c_make_rectangular (sinh (x) / w, sin (y) / w);
  8320. }
  8321. else
  8322. return scm_wta_dispatch_1 (g_scm_tanh, z, 1, s_scm_tanh);
  8323. }
  8324. #undef FUNC_NAME
  8325. SCM_PRIMITIVE_GENERIC (scm_asin, "asin", 1, 0, 0,
  8326. (SCM z),
  8327. "Compute the arc sine of @var{z}.")
  8328. #define FUNC_NAME s_scm_asin
  8329. {
  8330. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8331. return z; /* asin(exact0) = exact0 */
  8332. else if (scm_is_real (z))
  8333. {
  8334. double w = scm_to_double (z);
  8335. if (w >= -1.0 && w <= 1.0)
  8336. return scm_i_from_double (asin (w));
  8337. else
  8338. return scm_product (scm_c_make_rectangular (0, -1),
  8339. scm_sys_asinh (scm_c_make_rectangular (0, w)));
  8340. }
  8341. else if (SCM_COMPLEXP (z))
  8342. { double x, y;
  8343. x = SCM_COMPLEX_REAL (z);
  8344. y = SCM_COMPLEX_IMAG (z);
  8345. return scm_product (scm_c_make_rectangular (0, -1),
  8346. scm_sys_asinh (scm_c_make_rectangular (-y, x)));
  8347. }
  8348. else
  8349. return scm_wta_dispatch_1 (g_scm_asin, z, 1, s_scm_asin);
  8350. }
  8351. #undef FUNC_NAME
  8352. SCM_PRIMITIVE_GENERIC (scm_acos, "acos", 1, 0, 0,
  8353. (SCM z),
  8354. "Compute the arc cosine of @var{z}.")
  8355. #define FUNC_NAME s_scm_acos
  8356. {
  8357. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  8358. return SCM_INUM0; /* acos(exact1) = exact0 */
  8359. else if (scm_is_real (z))
  8360. {
  8361. double w = scm_to_double (z);
  8362. if (w >= -1.0 && w <= 1.0)
  8363. return scm_i_from_double (acos (w));
  8364. else
  8365. return scm_sum (scm_i_from_double (acos (0.0)),
  8366. scm_product (scm_c_make_rectangular (0, 1),
  8367. scm_sys_asinh (scm_c_make_rectangular (0, w))));
  8368. }
  8369. else if (SCM_COMPLEXP (z))
  8370. { double x, y;
  8371. x = SCM_COMPLEX_REAL (z);
  8372. y = SCM_COMPLEX_IMAG (z);
  8373. return scm_sum (scm_i_from_double (acos (0.0)),
  8374. scm_product (scm_c_make_rectangular (0, 1),
  8375. scm_sys_asinh (scm_c_make_rectangular (-y, x))));
  8376. }
  8377. else
  8378. return scm_wta_dispatch_1 (g_scm_acos, z, 1, s_scm_acos);
  8379. }
  8380. #undef FUNC_NAME
  8381. SCM_PRIMITIVE_GENERIC (scm_atan, "atan", 1, 1, 0,
  8382. (SCM z, SCM y),
  8383. "With one argument, compute the arc tangent of @var{z}.\n"
  8384. "If @var{y} is present, compute the arc tangent of @var{z}/@var{y},\n"
  8385. "using the sign of @var{z} and @var{y} to determine the quadrant.")
  8386. #define FUNC_NAME s_scm_atan
  8387. {
  8388. if (SCM_UNBNDP (y))
  8389. {
  8390. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8391. return z; /* atan(exact0) = exact0 */
  8392. else if (scm_is_real (z))
  8393. return scm_i_from_double (atan (scm_to_double (z)));
  8394. else if (SCM_COMPLEXP (z))
  8395. {
  8396. double v, w;
  8397. v = SCM_COMPLEX_REAL (z);
  8398. w = SCM_COMPLEX_IMAG (z);
  8399. return scm_divide (scm_log (scm_divide (scm_c_make_rectangular (-v, 1.0 - w),
  8400. scm_c_make_rectangular ( v, 1.0 + w))),
  8401. scm_c_make_rectangular (0, 2));
  8402. }
  8403. else
  8404. return scm_wta_dispatch_1 (g_scm_atan, z, SCM_ARG1, s_scm_atan);
  8405. }
  8406. else if (scm_is_real (z))
  8407. {
  8408. if (scm_is_real (y))
  8409. return scm_i_from_double (atan2 (scm_to_double (z), scm_to_double (y)));
  8410. else
  8411. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG2, s_scm_atan);
  8412. }
  8413. else
  8414. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG1, s_scm_atan);
  8415. }
  8416. #undef FUNC_NAME
  8417. SCM_PRIMITIVE_GENERIC (scm_sys_asinh, "asinh", 1, 0, 0,
  8418. (SCM z),
  8419. "Compute the inverse hyperbolic sine of @var{z}.")
  8420. #define FUNC_NAME s_scm_sys_asinh
  8421. {
  8422. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8423. return z; /* asinh(exact0) = exact0 */
  8424. else if (scm_is_real (z))
  8425. return scm_i_from_double (asinh (scm_to_double (z)));
  8426. else if (scm_is_number (z))
  8427. return scm_log (scm_sum (z,
  8428. scm_sqrt (scm_sum (scm_product (z, z),
  8429. SCM_INUM1))));
  8430. else
  8431. return scm_wta_dispatch_1 (g_scm_sys_asinh, z, 1, s_scm_sys_asinh);
  8432. }
  8433. #undef FUNC_NAME
  8434. SCM_PRIMITIVE_GENERIC (scm_sys_acosh, "acosh", 1, 0, 0,
  8435. (SCM z),
  8436. "Compute the inverse hyperbolic cosine of @var{z}.")
  8437. #define FUNC_NAME s_scm_sys_acosh
  8438. {
  8439. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  8440. return SCM_INUM0; /* acosh(exact1) = exact0 */
  8441. else if (scm_is_real (z) && scm_to_double (z) >= 1.0)
  8442. return scm_i_from_double (acosh (scm_to_double (z)));
  8443. else if (scm_is_number (z))
  8444. return scm_log (scm_sum (z,
  8445. scm_sqrt (scm_difference (scm_product (z, z),
  8446. SCM_INUM1))));
  8447. else
  8448. return scm_wta_dispatch_1 (g_scm_sys_acosh, z, 1, s_scm_sys_acosh);
  8449. }
  8450. #undef FUNC_NAME
  8451. SCM_PRIMITIVE_GENERIC (scm_sys_atanh, "atanh", 1, 0, 0,
  8452. (SCM z),
  8453. "Compute the inverse hyperbolic tangent of @var{z}.")
  8454. #define FUNC_NAME s_scm_sys_atanh
  8455. {
  8456. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  8457. return z; /* atanh(exact0) = exact0 */
  8458. else if (scm_is_real (z) && scm_to_double (z) >= -1.0 && scm_to_double (z) <= 1.0)
  8459. return scm_i_from_double (atanh (scm_to_double (z)));
  8460. else if (scm_is_number (z))
  8461. return scm_divide (scm_log (scm_divide (scm_sum (SCM_INUM1, z),
  8462. scm_difference (SCM_INUM1, z))),
  8463. SCM_I_MAKINUM (2));
  8464. else
  8465. return scm_wta_dispatch_1 (g_scm_sys_atanh, z, 1, s_scm_sys_atanh);
  8466. }
  8467. #undef FUNC_NAME
  8468. SCM
  8469. scm_c_make_rectangular (double re, double im)
  8470. {
  8471. SCM z;
  8472. z = SCM_PACK_POINTER (scm_gc_malloc_pointerless (sizeof (scm_t_complex),
  8473. "complex"));
  8474. SCM_SET_CELL_TYPE (z, scm_tc16_complex);
  8475. SCM_COMPLEX_REAL (z) = re;
  8476. SCM_COMPLEX_IMAG (z) = im;
  8477. return z;
  8478. }
  8479. SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
  8480. (SCM real_part, SCM imaginary_part),
  8481. "Return a complex number constructed of the given @var{real_part} "
  8482. "and @var{imaginary_part} parts.")
  8483. #define FUNC_NAME s_scm_make_rectangular
  8484. {
  8485. SCM_ASSERT_TYPE (scm_is_real (real_part), real_part,
  8486. SCM_ARG1, FUNC_NAME, "real");
  8487. SCM_ASSERT_TYPE (scm_is_real (imaginary_part), imaginary_part,
  8488. SCM_ARG2, FUNC_NAME, "real");
  8489. /* Return a real if and only if the imaginary_part is an _exact_ 0 */
  8490. if (scm_is_eq (imaginary_part, SCM_INUM0))
  8491. return real_part;
  8492. else
  8493. return scm_c_make_rectangular (scm_to_double (real_part),
  8494. scm_to_double (imaginary_part));
  8495. }
  8496. #undef FUNC_NAME
  8497. SCM
  8498. scm_c_make_polar (double mag, double ang)
  8499. {
  8500. double s, c;
  8501. /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
  8502. use it on Glibc-based systems that have it (it's a GNU extension). See
  8503. http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
  8504. details. */
  8505. #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
  8506. sincos (ang, &s, &c);
  8507. #elif (defined HAVE___SINCOS)
  8508. __sincos (ang, &s, &c);
  8509. #else
  8510. s = sin (ang);
  8511. c = cos (ang);
  8512. #endif
  8513. /* If s and c are NaNs, this indicates that the angle is a NaN,
  8514. infinite, or perhaps simply too large to determine its value
  8515. mod 2*pi. However, we know something that the floating-point
  8516. implementation doesn't know: We know that s and c are finite.
  8517. Therefore, if the magnitude is zero, return a complex zero.
  8518. The reason we check for the NaNs instead of using this case
  8519. whenever mag == 0.0 is because when the angle is known, we'd
  8520. like to return the correct kind of non-real complex zero:
  8521. +0.0+0.0i, -0.0+0.0i, -0.0-0.0i, or +0.0-0.0i, depending
  8522. on which quadrant the angle is in.
  8523. */
  8524. if (SCM_UNLIKELY (isnan(s)) && isnan(c) && (mag == 0.0))
  8525. return scm_c_make_rectangular (0.0, 0.0);
  8526. else
  8527. return scm_c_make_rectangular (mag * c, mag * s);
  8528. }
  8529. SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
  8530. (SCM mag, SCM ang),
  8531. "Return the complex number @var{mag} * e^(i * @var{ang}).")
  8532. #define FUNC_NAME s_scm_make_polar
  8533. {
  8534. SCM_ASSERT_TYPE (scm_is_real (mag), mag, SCM_ARG1, FUNC_NAME, "real");
  8535. SCM_ASSERT_TYPE (scm_is_real (ang), ang, SCM_ARG2, FUNC_NAME, "real");
  8536. /* If mag is exact0, return exact0 */
  8537. if (scm_is_eq (mag, SCM_INUM0))
  8538. return SCM_INUM0;
  8539. /* Return a real if ang is exact0 */
  8540. else if (scm_is_eq (ang, SCM_INUM0))
  8541. return mag;
  8542. else
  8543. return scm_c_make_polar (scm_to_double (mag), scm_to_double (ang));
  8544. }
  8545. #undef FUNC_NAME
  8546. SCM_PRIMITIVE_GENERIC (scm_real_part, "real-part", 1, 0, 0,
  8547. (SCM z),
  8548. "Return the real part of the number @var{z}.")
  8549. #define FUNC_NAME s_scm_real_part
  8550. {
  8551. if (SCM_COMPLEXP (z))
  8552. return scm_i_from_double (SCM_COMPLEX_REAL (z));
  8553. else if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_REALP (z) || SCM_FRACTIONP (z))
  8554. return z;
  8555. else
  8556. return scm_wta_dispatch_1 (g_scm_real_part, z, SCM_ARG1, s_scm_real_part);
  8557. }
  8558. #undef FUNC_NAME
  8559. SCM_PRIMITIVE_GENERIC (scm_imag_part, "imag-part", 1, 0, 0,
  8560. (SCM z),
  8561. "Return the imaginary part of the number @var{z}.")
  8562. #define FUNC_NAME s_scm_imag_part
  8563. {
  8564. if (SCM_COMPLEXP (z))
  8565. return scm_i_from_double (SCM_COMPLEX_IMAG (z));
  8566. else if (SCM_I_INUMP (z) || SCM_REALP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  8567. return SCM_INUM0;
  8568. else
  8569. return scm_wta_dispatch_1 (g_scm_imag_part, z, SCM_ARG1, s_scm_imag_part);
  8570. }
  8571. #undef FUNC_NAME
  8572. SCM_PRIMITIVE_GENERIC (scm_numerator, "numerator", 1, 0, 0,
  8573. (SCM z),
  8574. "Return the numerator of the number @var{z}.")
  8575. #define FUNC_NAME s_scm_numerator
  8576. {
  8577. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  8578. return z;
  8579. else if (SCM_FRACTIONP (z))
  8580. return SCM_FRACTION_NUMERATOR (z);
  8581. else if (SCM_REALP (z))
  8582. {
  8583. double zz = SCM_REAL_VALUE (z);
  8584. if (zz == floor (zz))
  8585. /* Handle -0.0 and infinities in accordance with R6RS
  8586. flnumerator, and optimize handling of integers. */
  8587. return z;
  8588. else
  8589. return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
  8590. }
  8591. else
  8592. return scm_wta_dispatch_1 (g_scm_numerator, z, SCM_ARG1, s_scm_numerator);
  8593. }
  8594. #undef FUNC_NAME
  8595. SCM_PRIMITIVE_GENERIC (scm_denominator, "denominator", 1, 0, 0,
  8596. (SCM z),
  8597. "Return the denominator of the number @var{z}.")
  8598. #define FUNC_NAME s_scm_denominator
  8599. {
  8600. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  8601. return SCM_INUM1;
  8602. else if (SCM_FRACTIONP (z))
  8603. return SCM_FRACTION_DENOMINATOR (z);
  8604. else if (SCM_REALP (z))
  8605. {
  8606. double zz = SCM_REAL_VALUE (z);
  8607. if (zz == floor (zz))
  8608. /* Handle infinities in accordance with R6RS fldenominator, and
  8609. optimize handling of integers. */
  8610. return scm_i_from_double (1.0);
  8611. else
  8612. return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
  8613. }
  8614. else
  8615. return scm_wta_dispatch_1 (g_scm_denominator, z, SCM_ARG1,
  8616. s_scm_denominator);
  8617. }
  8618. #undef FUNC_NAME
  8619. SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1, 0, 0,
  8620. (SCM z),
  8621. "Return the magnitude of the number @var{z}. This is the same as\n"
  8622. "@code{abs} for real arguments, but also allows complex numbers.")
  8623. #define FUNC_NAME s_scm_magnitude
  8624. {
  8625. if (SCM_I_INUMP (z))
  8626. {
  8627. scm_t_inum zz = SCM_I_INUM (z);
  8628. if (zz >= 0)
  8629. return z;
  8630. else if (SCM_POSFIXABLE (-zz))
  8631. return SCM_I_MAKINUM (-zz);
  8632. else
  8633. return scm_i_inum2big (-zz);
  8634. }
  8635. else if (SCM_BIGP (z))
  8636. {
  8637. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  8638. scm_remember_upto_here_1 (z);
  8639. if (sgn < 0)
  8640. return scm_i_clonebig (z, 0);
  8641. else
  8642. return z;
  8643. }
  8644. else if (SCM_REALP (z))
  8645. return scm_i_from_double (fabs (SCM_REAL_VALUE (z)));
  8646. else if (SCM_COMPLEXP (z))
  8647. return scm_i_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
  8648. else if (SCM_FRACTIONP (z))
  8649. {
  8650. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  8651. return z;
  8652. return scm_i_make_ratio_already_reduced
  8653. (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
  8654. SCM_FRACTION_DENOMINATOR (z));
  8655. }
  8656. else
  8657. return scm_wta_dispatch_1 (g_scm_magnitude, z, SCM_ARG1,
  8658. s_scm_magnitude);
  8659. }
  8660. #undef FUNC_NAME
  8661. SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0,
  8662. (SCM z),
  8663. "Return the angle of the complex number @var{z}.")
  8664. #define FUNC_NAME s_scm_angle
  8665. {
  8666. /* atan(0,-1) is pi and it'd be possible to have that as a constant like
  8667. flo0 to save allocating a new flonum with scm_i_from_double each time.
  8668. But if atan2 follows the floating point rounding mode, then the value
  8669. is not a constant. Maybe it'd be close enough though. */
  8670. if (SCM_I_INUMP (z))
  8671. {
  8672. if (SCM_I_INUM (z) >= 0)
  8673. return flo0;
  8674. else
  8675. return scm_i_from_double (atan2 (0.0, -1.0));
  8676. }
  8677. else if (SCM_BIGP (z))
  8678. {
  8679. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  8680. scm_remember_upto_here_1 (z);
  8681. if (sgn < 0)
  8682. return scm_i_from_double (atan2 (0.0, -1.0));
  8683. else
  8684. return flo0;
  8685. }
  8686. else if (SCM_REALP (z))
  8687. {
  8688. double x = SCM_REAL_VALUE (z);
  8689. if (copysign (1.0, x) > 0.0)
  8690. return flo0;
  8691. else
  8692. return scm_i_from_double (atan2 (0.0, -1.0));
  8693. }
  8694. else if (SCM_COMPLEXP (z))
  8695. return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
  8696. else if (SCM_FRACTIONP (z))
  8697. {
  8698. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  8699. return flo0;
  8700. else return scm_i_from_double (atan2 (0.0, -1.0));
  8701. }
  8702. else
  8703. return scm_wta_dispatch_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle);
  8704. }
  8705. #undef FUNC_NAME
  8706. SCM_PRIMITIVE_GENERIC (scm_exact_to_inexact, "exact->inexact", 1, 0, 0,
  8707. (SCM z),
  8708. "Convert the number @var{z} to its inexact representation.\n")
  8709. #define FUNC_NAME s_scm_exact_to_inexact
  8710. {
  8711. if (SCM_I_INUMP (z))
  8712. return scm_i_from_double ((double) SCM_I_INUM (z));
  8713. else if (SCM_BIGP (z))
  8714. return scm_i_from_double (scm_i_big2dbl (z));
  8715. else if (SCM_FRACTIONP (z))
  8716. return scm_i_from_double (scm_i_fraction2double (z));
  8717. else if (SCM_INEXACTP (z))
  8718. return z;
  8719. else
  8720. return scm_wta_dispatch_1 (g_scm_exact_to_inexact, z, 1,
  8721. s_scm_exact_to_inexact);
  8722. }
  8723. #undef FUNC_NAME
  8724. SCM_PRIMITIVE_GENERIC (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
  8725. (SCM z),
  8726. "Return an exact number that is numerically closest to @var{z}.")
  8727. #define FUNC_NAME s_scm_inexact_to_exact
  8728. {
  8729. if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  8730. return z;
  8731. else
  8732. {
  8733. double val;
  8734. if (SCM_REALP (z))
  8735. val = SCM_REAL_VALUE (z);
  8736. else if (SCM_COMPLEXP (z) && SCM_COMPLEX_IMAG (z) == 0.0)
  8737. val = SCM_COMPLEX_REAL (z);
  8738. else
  8739. return scm_wta_dispatch_1 (g_scm_inexact_to_exact, z, 1,
  8740. s_scm_inexact_to_exact);
  8741. if (!SCM_LIKELY (isfinite (val)))
  8742. SCM_OUT_OF_RANGE (1, z);
  8743. else if (val == 0.0)
  8744. return SCM_INUM0;
  8745. else
  8746. {
  8747. int expon;
  8748. SCM numerator;
  8749. numerator = scm_i_dbl2big (ldexp (frexp (val, &expon),
  8750. DBL_MANT_DIG));
  8751. expon -= DBL_MANT_DIG;
  8752. if (expon < 0)
  8753. {
  8754. int shift = mpz_scan1 (SCM_I_BIG_MPZ (numerator), 0);
  8755. if (shift > -expon)
  8756. shift = -expon;
  8757. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (numerator),
  8758. SCM_I_BIG_MPZ (numerator),
  8759. shift);
  8760. expon += shift;
  8761. }
  8762. numerator = scm_i_normbig (numerator);
  8763. if (expon < 0)
  8764. return scm_i_make_ratio_already_reduced
  8765. (numerator, left_shift_exact_integer (SCM_INUM1, -expon));
  8766. else if (expon > 0)
  8767. return left_shift_exact_integer (numerator, expon);
  8768. else
  8769. return numerator;
  8770. }
  8771. }
  8772. }
  8773. #undef FUNC_NAME
  8774. SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
  8775. (SCM x, SCM eps),
  8776. "Returns the @emph{simplest} rational number differing\n"
  8777. "from @var{x} by no more than @var{eps}.\n"
  8778. "\n"
  8779. "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
  8780. "exact result when both its arguments are exact. Thus, you might need\n"
  8781. "to use @code{inexact->exact} on the arguments.\n"
  8782. "\n"
  8783. "@lisp\n"
  8784. "(rationalize (inexact->exact 1.2) 1/100)\n"
  8785. "@result{} 6/5\n"
  8786. "@end lisp")
  8787. #define FUNC_NAME s_scm_rationalize
  8788. {
  8789. SCM_ASSERT_TYPE (scm_is_real (x), x, SCM_ARG1, FUNC_NAME, "real");
  8790. SCM_ASSERT_TYPE (scm_is_real (eps), eps, SCM_ARG2, FUNC_NAME, "real");
  8791. if (SCM_UNLIKELY (!scm_is_exact (eps) || !scm_is_exact (x)))
  8792. {
  8793. if (SCM_UNLIKELY (scm_is_false (scm_finite_p (eps))))
  8794. {
  8795. if (scm_is_false (scm_nan_p (eps)) && scm_is_true (scm_finite_p (x)))
  8796. return flo0;
  8797. else
  8798. return scm_nan ();
  8799. }
  8800. else if (SCM_UNLIKELY (scm_is_false (scm_finite_p (x))))
  8801. return x;
  8802. else
  8803. return scm_exact_to_inexact
  8804. (scm_rationalize (scm_inexact_to_exact (x),
  8805. scm_inexact_to_exact (eps)));
  8806. }
  8807. else
  8808. {
  8809. /* X and EPS are exact rationals.
  8810. The code that follows is equivalent to the following Scheme code:
  8811. (define (exact-rationalize x eps)
  8812. (let ((n1 (if (negative? x) -1 1))
  8813. (x (abs x))
  8814. (eps (abs eps)))
  8815. (let ((lo (- x eps))
  8816. (hi (+ x eps)))
  8817. (if (<= lo 0)
  8818. 0
  8819. (let loop ((nlo (numerator lo)) (dlo (denominator lo))
  8820. (nhi (numerator hi)) (dhi (denominator hi))
  8821. (n1 n1) (d1 0) (n2 0) (d2 1))
  8822. (let-values (((qlo rlo) (floor/ nlo dlo))
  8823. ((qhi rhi) (floor/ nhi dhi)))
  8824. (let ((n0 (+ n2 (* n1 qlo)))
  8825. (d0 (+ d2 (* d1 qlo))))
  8826. (cond ((zero? rlo) (/ n0 d0))
  8827. ((< qlo qhi) (/ (+ n0 n1) (+ d0 d1)))
  8828. (else (loop dhi rhi dlo rlo n0 d0 n1 d1))))))))))
  8829. */
  8830. int n1_init = 1;
  8831. SCM lo, hi;
  8832. eps = scm_abs (eps);
  8833. if (scm_is_true (scm_negative_p (x)))
  8834. {
  8835. n1_init = -1;
  8836. x = scm_difference (x, SCM_UNDEFINED);
  8837. }
  8838. /* X and EPS are non-negative exact rationals. */
  8839. lo = scm_difference (x, eps);
  8840. hi = scm_sum (x, eps);
  8841. if (scm_is_false (scm_positive_p (lo)))
  8842. /* If zero is included in the interval, return it.
  8843. It is the simplest rational of all. */
  8844. return SCM_INUM0;
  8845. else
  8846. {
  8847. SCM result;
  8848. mpz_t n0, d0, n1, d1, n2, d2;
  8849. mpz_t nlo, dlo, nhi, dhi;
  8850. mpz_t qlo, rlo, qhi, rhi;
  8851. /* LO and HI are positive exact rationals. */
  8852. /* Our approach here follows the method described by Alan
  8853. Bawden in a message entitled "(rationalize x y)" on the
  8854. rrrs-authors mailing list, dated 16 Feb 1988 14:08:28 EST:
  8855. http://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1988/msg00063.html
  8856. In brief, we compute the continued fractions of the two
  8857. endpoints of the interval (LO and HI). The continued
  8858. fraction of the result consists of the common prefix of the
  8859. continued fractions of LO and HI, plus one final term. The
  8860. final term of the result is the smallest integer contained
  8861. in the interval between the remainders of LO and HI after
  8862. the common prefix has been removed.
  8863. The following code lazily computes the continued fraction
  8864. representations of LO and HI, and simultaneously converts
  8865. the continued fraction of the result into a rational
  8866. number. We use MPZ functions directly to avoid type
  8867. dispatch and GC allocation during the loop. */
  8868. mpz_inits (n0, d0, n1, d1, n2, d2,
  8869. nlo, dlo, nhi, dhi,
  8870. qlo, rlo, qhi, rhi,
  8871. NULL);
  8872. /* The variables N1, D1, N2 and D2 are used to compute the
  8873. resulting rational from its continued fraction. At each
  8874. step, N2/D2 and N1/D1 are the last two convergents. They
  8875. are normally initialized to 0/1 and 1/0, respectively.
  8876. However, if we negated X then we must negate the result as
  8877. well, and we do that by initializing N1/D1 to -1/0. */
  8878. mpz_set_si (n1, n1_init);
  8879. mpz_set_ui (d1, 0);
  8880. mpz_set_ui (n2, 0);
  8881. mpz_set_ui (d2, 1);
  8882. /* The variables NLO, DLO, NHI, and DHI are used to lazily
  8883. compute the continued fraction representations of LO and HI
  8884. using Euclid's algorithm. Initially, NLO/DLO == LO and
  8885. NHI/DHI == HI. */
  8886. scm_to_mpz (scm_numerator (lo), nlo);
  8887. scm_to_mpz (scm_denominator (lo), dlo);
  8888. scm_to_mpz (scm_numerator (hi), nhi);
  8889. scm_to_mpz (scm_denominator (hi), dhi);
  8890. /* As long as we're using exact arithmetic, the following loop
  8891. is guaranteed to terminate. */
  8892. for (;;)
  8893. {
  8894. /* Compute the next terms (QLO and QHI) of the continued
  8895. fractions of LO and HI. */
  8896. mpz_fdiv_qr (qlo, rlo, nlo, dlo); /* QLO <-- floor (NLO/DLO), RLO <-- NLO - QLO * DLO */
  8897. mpz_fdiv_qr (qhi, rhi, nhi, dhi); /* QHI <-- floor (NHI/DHI), RHI <-- NHI - QHI * DHI */
  8898. /* The next term of the result will be either QLO or
  8899. QLO+1. Here we compute the next convergent of the
  8900. result based on the assumption that QLO is the next
  8901. term. If that turns out to be wrong, we'll adjust
  8902. these later by adding N1 to N0 and D1 to D0. */
  8903. mpz_set (n0, n2); mpz_addmul (n0, n1, qlo); /* N0 <-- N2 + (QLO * N1) */
  8904. mpz_set (d0, d2); mpz_addmul (d0, d1, qlo); /* D0 <-- D2 + (QLO * D1) */
  8905. /* We stop iterating when an integer is contained in the
  8906. interval between the remainders NLO/DLO and NHI/DHI.
  8907. There are two cases to consider: either NLO/DLO == QLO
  8908. is an integer (indicated by RLO == 0), or QLO < QHI. */
  8909. if (mpz_sgn (rlo) == 0 || mpz_cmp (qlo, qhi) != 0)
  8910. break;
  8911. /* Efficiently shuffle variables around for the next
  8912. iteration. First we shift the recent convergents. */
  8913. mpz_swap (n2, n1); mpz_swap (n1, n0); /* N2 <-- N1 <-- N0 */
  8914. mpz_swap (d2, d1); mpz_swap (d1, d0); /* D2 <-- D1 <-- D0 */
  8915. /* The following shuffling is a bit confusing, so some
  8916. explanation is in order. Conceptually, we're doing a
  8917. couple of things here. After substracting the floor of
  8918. NLO/DLO, the remainder is RLO/DLO. The rest of the
  8919. continued fraction will represent the remainder's
  8920. reciprocal DLO/RLO. Similarly for the HI endpoint.
  8921. So in the next iteration, the new endpoints will be
  8922. DLO/RLO and DHI/RHI. However, when we take the
  8923. reciprocals of these endpoints, their order is
  8924. switched. So in summary, we want NLO/DLO <-- DHI/RHI
  8925. and NHI/DHI <-- DLO/RLO. */
  8926. mpz_swap (nlo, dhi); mpz_swap (dhi, rlo); /* NLO <-- DHI <-- RLO */
  8927. mpz_swap (nhi, dlo); mpz_swap (dlo, rhi); /* NHI <-- DLO <-- RHI */
  8928. }
  8929. /* There is now an integer in the interval [NLO/DLO NHI/DHI].
  8930. The last term of the result will be the smallest integer in
  8931. that interval, which is ceiling(NLO/DLO). We have already
  8932. computed floor(NLO/DLO) in QLO, so now we adjust QLO to be
  8933. equal to the ceiling. */
  8934. if (mpz_sgn (rlo) != 0)
  8935. {
  8936. /* If RLO is non-zero, then NLO/DLO is not an integer and
  8937. the next term will be QLO+1. QLO was used in the
  8938. computation of N0 and D0 above. Here we adjust N0 and
  8939. D0 to be based on QLO+1 instead of QLO. */
  8940. mpz_add (n0, n0, n1); /* N0 <-- N0 + N1 */
  8941. mpz_add (d0, d0, d1); /* D0 <-- D0 + D1 */
  8942. }
  8943. /* The simplest rational in the interval is N0/D0 */
  8944. result = scm_i_make_ratio_already_reduced (scm_from_mpz (n0),
  8945. scm_from_mpz (d0));
  8946. mpz_clears (n0, d0, n1, d1, n2, d2,
  8947. nlo, dlo, nhi, dhi,
  8948. qlo, rlo, qhi, rhi,
  8949. NULL);
  8950. return result;
  8951. }
  8952. }
  8953. }
  8954. #undef FUNC_NAME
  8955. /* conversion functions */
  8956. int
  8957. scm_is_integer (SCM val)
  8958. {
  8959. return scm_is_true (scm_integer_p (val));
  8960. }
  8961. int
  8962. scm_is_exact_integer (SCM val)
  8963. {
  8964. return scm_is_true (scm_exact_integer_p (val));
  8965. }
  8966. int
  8967. scm_is_signed_integer (SCM val, intmax_t min, intmax_t max)
  8968. {
  8969. if (SCM_I_INUMP (val))
  8970. {
  8971. scm_t_signed_bits n = SCM_I_INUM (val);
  8972. return n >= min && n <= max;
  8973. }
  8974. else if (SCM_BIGP (val))
  8975. {
  8976. if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
  8977. return 0;
  8978. else if (min >= LONG_MIN && max <= LONG_MAX)
  8979. {
  8980. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
  8981. {
  8982. long n = mpz_get_si (SCM_I_BIG_MPZ (val));
  8983. return n >= min && n <= max;
  8984. }
  8985. else
  8986. return 0;
  8987. }
  8988. else
  8989. {
  8990. uintmax_t abs_n;
  8991. intmax_t n;
  8992. size_t count;
  8993. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  8994. > CHAR_BIT*sizeof (uintmax_t))
  8995. return 0;
  8996. mpz_export (&abs_n, &count, 1, sizeof (uintmax_t), 0, 0,
  8997. SCM_I_BIG_MPZ (val));
  8998. if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
  8999. {
  9000. if (abs_n <= max)
  9001. n = abs_n;
  9002. else
  9003. return 0;
  9004. }
  9005. else
  9006. {
  9007. /* Carefully avoid signed integer overflow. */
  9008. if (min < 0 && abs_n - 1 <= -(min + 1))
  9009. n = -1 - (intmax_t)(abs_n - 1);
  9010. else
  9011. return 0;
  9012. }
  9013. return n >= min && n <= max;
  9014. }
  9015. }
  9016. else
  9017. return 0;
  9018. }
  9019. int
  9020. scm_is_unsigned_integer (SCM val, uintmax_t min, uintmax_t max)
  9021. {
  9022. if (SCM_I_INUMP (val))
  9023. {
  9024. scm_t_signed_bits n = SCM_I_INUM (val);
  9025. return n >= 0 && ((uintmax_t)n) >= min && ((uintmax_t)n) <= max;
  9026. }
  9027. else if (SCM_BIGP (val))
  9028. {
  9029. if (max <= SCM_MOST_POSITIVE_FIXNUM)
  9030. return 0;
  9031. else if (max <= ULONG_MAX)
  9032. {
  9033. if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
  9034. {
  9035. unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
  9036. return n >= min && n <= max;
  9037. }
  9038. else
  9039. return 0;
  9040. }
  9041. else
  9042. {
  9043. uintmax_t n;
  9044. size_t count;
  9045. if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
  9046. return 0;
  9047. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  9048. > CHAR_BIT*sizeof (uintmax_t))
  9049. return 0;
  9050. mpz_export (&n, &count, 1, sizeof (uintmax_t), 0, 0,
  9051. SCM_I_BIG_MPZ (val));
  9052. return n >= min && n <= max;
  9053. }
  9054. }
  9055. else
  9056. return 0;
  9057. }
  9058. static void
  9059. scm_i_range_error (SCM bad_val, SCM min, SCM max)
  9060. {
  9061. scm_error (scm_out_of_range_key,
  9062. NULL,
  9063. "Value out of range ~S to ~S: ~S",
  9064. scm_list_3 (min, max, bad_val),
  9065. scm_list_1 (bad_val));
  9066. }
  9067. #define TYPE intmax_t
  9068. #define TYPE_MIN min
  9069. #define TYPE_MAX max
  9070. #define SIZEOF_TYPE 0
  9071. #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, intmax_t min, intmax_t max)
  9072. #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
  9073. #include "conv-integer.i.c"
  9074. #define TYPE uintmax_t
  9075. #define TYPE_MIN min
  9076. #define TYPE_MAX max
  9077. #define SIZEOF_TYPE 0
  9078. #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, uintmax_t min, uintmax_t max)
  9079. #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
  9080. #include "conv-uinteger.i.c"
  9081. #define TYPE int8_t
  9082. #define TYPE_MIN INT8_MIN
  9083. #define TYPE_MAX INT8_MAX
  9084. #define SIZEOF_TYPE 1
  9085. #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
  9086. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
  9087. #include "conv-integer.i.c"
  9088. #define TYPE uint8_t
  9089. #define TYPE_MIN 0
  9090. #define TYPE_MAX UINT8_MAX
  9091. #define SIZEOF_TYPE 1
  9092. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
  9093. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
  9094. #include "conv-uinteger.i.c"
  9095. #define TYPE int16_t
  9096. #define TYPE_MIN INT16_MIN
  9097. #define TYPE_MAX INT16_MAX
  9098. #define SIZEOF_TYPE 2
  9099. #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
  9100. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
  9101. #include "conv-integer.i.c"
  9102. #define TYPE uint16_t
  9103. #define TYPE_MIN 0
  9104. #define TYPE_MAX UINT16_MAX
  9105. #define SIZEOF_TYPE 2
  9106. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
  9107. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
  9108. #include "conv-uinteger.i.c"
  9109. #define TYPE int32_t
  9110. #define TYPE_MIN INT32_MIN
  9111. #define TYPE_MAX INT32_MAX
  9112. #define SIZEOF_TYPE 4
  9113. #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
  9114. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
  9115. #include "conv-integer.i.c"
  9116. #define TYPE uint32_t
  9117. #define TYPE_MIN 0
  9118. #define TYPE_MAX UINT32_MAX
  9119. #define SIZEOF_TYPE 4
  9120. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
  9121. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
  9122. #include "conv-uinteger.i.c"
  9123. #define TYPE scm_t_wchar
  9124. #define TYPE_MIN (int32_t)-1
  9125. #define TYPE_MAX (int32_t)0x10ffff
  9126. #define SIZEOF_TYPE 4
  9127. #define SCM_TO_TYPE_PROTO(arg) scm_to_wchar (arg)
  9128. #define SCM_FROM_TYPE_PROTO(arg) scm_from_wchar (arg)
  9129. #include "conv-integer.i.c"
  9130. #define TYPE int64_t
  9131. #define TYPE_MIN INT64_MIN
  9132. #define TYPE_MAX INT64_MAX
  9133. #define SIZEOF_TYPE 8
  9134. #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
  9135. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
  9136. #include "conv-integer.i.c"
  9137. #define TYPE uint64_t
  9138. #define TYPE_MIN 0
  9139. #define TYPE_MAX UINT64_MAX
  9140. #define SIZEOF_TYPE 8
  9141. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
  9142. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
  9143. #include "conv-uinteger.i.c"
  9144. void
  9145. scm_to_mpz (SCM val, mpz_t rop)
  9146. {
  9147. if (SCM_I_INUMP (val))
  9148. mpz_set_si (rop, SCM_I_INUM (val));
  9149. else if (SCM_BIGP (val))
  9150. mpz_set (rop, SCM_I_BIG_MPZ (val));
  9151. else
  9152. scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
  9153. }
  9154. SCM
  9155. scm_from_mpz (mpz_t val)
  9156. {
  9157. return scm_i_mpz2num (val);
  9158. }
  9159. int
  9160. scm_is_real (SCM val)
  9161. {
  9162. return scm_is_true (scm_real_p (val));
  9163. }
  9164. int
  9165. scm_is_rational (SCM val)
  9166. {
  9167. return scm_is_true (scm_rational_p (val));
  9168. }
  9169. double
  9170. scm_to_double (SCM val)
  9171. {
  9172. if (SCM_I_INUMP (val))
  9173. return SCM_I_INUM (val);
  9174. else if (SCM_BIGP (val))
  9175. return scm_i_big2dbl (val);
  9176. else if (SCM_FRACTIONP (val))
  9177. return scm_i_fraction2double (val);
  9178. else if (SCM_REALP (val))
  9179. return SCM_REAL_VALUE (val);
  9180. else
  9181. scm_wrong_type_arg_msg (NULL, 0, val, "real number");
  9182. }
  9183. SCM
  9184. scm_from_double (double val)
  9185. {
  9186. return scm_i_from_double (val);
  9187. }
  9188. int
  9189. scm_is_complex (SCM val)
  9190. {
  9191. return scm_is_true (scm_complex_p (val));
  9192. }
  9193. double
  9194. scm_c_real_part (SCM z)
  9195. {
  9196. if (SCM_COMPLEXP (z))
  9197. return SCM_COMPLEX_REAL (z);
  9198. else
  9199. {
  9200. /* Use the scm_real_part to get proper error checking and
  9201. dispatching.
  9202. */
  9203. return scm_to_double (scm_real_part (z));
  9204. }
  9205. }
  9206. double
  9207. scm_c_imag_part (SCM z)
  9208. {
  9209. if (SCM_COMPLEXP (z))
  9210. return SCM_COMPLEX_IMAG (z);
  9211. else
  9212. {
  9213. /* Use the scm_imag_part to get proper error checking and
  9214. dispatching. The result will almost always be 0.0, but not
  9215. always.
  9216. */
  9217. return scm_to_double (scm_imag_part (z));
  9218. }
  9219. }
  9220. double
  9221. scm_c_magnitude (SCM z)
  9222. {
  9223. return scm_to_double (scm_magnitude (z));
  9224. }
  9225. double
  9226. scm_c_angle (SCM z)
  9227. {
  9228. return scm_to_double (scm_angle (z));
  9229. }
  9230. int
  9231. scm_is_number (SCM z)
  9232. {
  9233. return scm_is_true (scm_number_p (z));
  9234. }
  9235. /* Returns log(x * 2^shift) */
  9236. static SCM
  9237. log_of_shifted_double (double x, long shift)
  9238. {
  9239. double ans = log (fabs (x)) + shift * M_LN2;
  9240. if (copysign (1.0, x) > 0.0)
  9241. return scm_i_from_double (ans);
  9242. else
  9243. return scm_c_make_rectangular (ans, M_PI);
  9244. }
  9245. /* Returns log(n), for exact integer n */
  9246. static SCM
  9247. log_of_exact_integer (SCM n)
  9248. {
  9249. if (SCM_I_INUMP (n))
  9250. return log_of_shifted_double (SCM_I_INUM (n), 0);
  9251. else if (SCM_BIGP (n))
  9252. {
  9253. long expon;
  9254. double signif = scm_i_big2dbl_2exp (n, &expon);
  9255. return log_of_shifted_double (signif, expon);
  9256. }
  9257. else
  9258. scm_wrong_type_arg ("log_of_exact_integer", SCM_ARG1, n);
  9259. }
  9260. /* Returns log(n/d), for exact non-zero integers n and d */
  9261. static SCM
  9262. log_of_fraction (SCM n, SCM d)
  9263. {
  9264. long n_size = scm_to_long (scm_integer_length (n));
  9265. long d_size = scm_to_long (scm_integer_length (d));
  9266. if (labs (n_size - d_size) > 1)
  9267. return (scm_difference (log_of_exact_integer (n),
  9268. log_of_exact_integer (d)));
  9269. else if (scm_is_false (scm_negative_p (n)))
  9270. return scm_i_from_double
  9271. (log1p (scm_i_divide2double (scm_difference (n, d), d)));
  9272. else
  9273. return scm_c_make_rectangular
  9274. (log1p (scm_i_divide2double (scm_difference (scm_abs (n), d),
  9275. d)),
  9276. M_PI);
  9277. }
  9278. /* In the following functions we dispatch to the real-arg funcs like log()
  9279. when we know the arg is real, instead of just handing everything to
  9280. clog() for instance. This is in case clog() doesn't optimize for a
  9281. real-only case, and because we have to test SCM_COMPLEXP anyway so may as
  9282. well use it to go straight to the applicable C func. */
  9283. SCM_PRIMITIVE_GENERIC (scm_log, "log", 1, 0, 0,
  9284. (SCM z),
  9285. "Return the natural logarithm of @var{z}.")
  9286. #define FUNC_NAME s_scm_log
  9287. {
  9288. if (SCM_COMPLEXP (z))
  9289. {
  9290. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG \
  9291. && defined (SCM_COMPLEX_VALUE)
  9292. return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
  9293. #else
  9294. double re = SCM_COMPLEX_REAL (z);
  9295. double im = SCM_COMPLEX_IMAG (z);
  9296. return scm_c_make_rectangular (log (hypot (re, im)),
  9297. atan2 (im, re));
  9298. #endif
  9299. }
  9300. else if (SCM_REALP (z))
  9301. return log_of_shifted_double (SCM_REAL_VALUE (z), 0);
  9302. else if (SCM_I_INUMP (z))
  9303. {
  9304. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  9305. if (scm_is_eq (z, SCM_INUM0))
  9306. scm_num_overflow (s_scm_log);
  9307. #endif
  9308. return log_of_shifted_double (SCM_I_INUM (z), 0);
  9309. }
  9310. else if (SCM_BIGP (z))
  9311. return log_of_exact_integer (z);
  9312. else if (SCM_FRACTIONP (z))
  9313. return log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  9314. SCM_FRACTION_DENOMINATOR (z));
  9315. else
  9316. return scm_wta_dispatch_1 (g_scm_log, z, 1, s_scm_log);
  9317. }
  9318. #undef FUNC_NAME
  9319. SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0,
  9320. (SCM z),
  9321. "Return the base 10 logarithm of @var{z}.")
  9322. #define FUNC_NAME s_scm_log10
  9323. {
  9324. if (SCM_COMPLEXP (z))
  9325. {
  9326. /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
  9327. clog() and a multiply by M_LOG10E, rather than the fallback
  9328. log10+hypot+atan2.) */
  9329. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG10 \
  9330. && defined SCM_COMPLEX_VALUE
  9331. return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
  9332. #else
  9333. double re = SCM_COMPLEX_REAL (z);
  9334. double im = SCM_COMPLEX_IMAG (z);
  9335. return scm_c_make_rectangular (log10 (hypot (re, im)),
  9336. M_LOG10E * atan2 (im, re));
  9337. #endif
  9338. }
  9339. else if (SCM_REALP (z) || SCM_I_INUMP (z))
  9340. {
  9341. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  9342. if (scm_is_eq (z, SCM_INUM0))
  9343. scm_num_overflow (s_scm_log10);
  9344. #endif
  9345. {
  9346. double re = scm_to_double (z);
  9347. double l = log10 (fabs (re));
  9348. if (copysign (1.0, re) > 0.0)
  9349. return scm_i_from_double (l);
  9350. else
  9351. return scm_c_make_rectangular (l, M_LOG10E * M_PI);
  9352. }
  9353. }
  9354. else if (SCM_BIGP (z))
  9355. return scm_product (flo_log10e, log_of_exact_integer (z));
  9356. else if (SCM_FRACTIONP (z))
  9357. return scm_product (flo_log10e,
  9358. log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  9359. SCM_FRACTION_DENOMINATOR (z)));
  9360. else
  9361. return scm_wta_dispatch_1 (g_scm_log10, z, 1, s_scm_log10);
  9362. }
  9363. #undef FUNC_NAME
  9364. SCM_PRIMITIVE_GENERIC (scm_exp, "exp", 1, 0, 0,
  9365. (SCM z),
  9366. "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
  9367. "base of natural logarithms (2.71828@dots{}).")
  9368. #define FUNC_NAME s_scm_exp
  9369. {
  9370. if (SCM_COMPLEXP (z))
  9371. {
  9372. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CEXP \
  9373. && defined (SCM_COMPLEX_VALUE)
  9374. return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
  9375. #else
  9376. return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
  9377. SCM_COMPLEX_IMAG (z));
  9378. #endif
  9379. }
  9380. else if (SCM_NUMBERP (z))
  9381. {
  9382. /* When z is a negative bignum the conversion to double overflows,
  9383. giving -infinity, but that's ok, the exp is still 0.0. */
  9384. return scm_i_from_double (exp (scm_to_double (z)));
  9385. }
  9386. else
  9387. return scm_wta_dispatch_1 (g_scm_exp, z, 1, s_scm_exp);
  9388. }
  9389. #undef FUNC_NAME
  9390. SCM_DEFINE (scm_i_exact_integer_sqrt, "exact-integer-sqrt", 1, 0, 0,
  9391. (SCM k),
  9392. "Return two exact non-negative integers @var{s} and @var{r}\n"
  9393. "such that @math{@var{k} = @var{s}^2 + @var{r}} and\n"
  9394. "@math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.\n"
  9395. "An error is raised if @var{k} is not an exact non-negative integer.\n"
  9396. "\n"
  9397. "@lisp\n"
  9398. "(exact-integer-sqrt 10) @result{} 3 and 1\n"
  9399. "@end lisp")
  9400. #define FUNC_NAME s_scm_i_exact_integer_sqrt
  9401. {
  9402. SCM s, r;
  9403. scm_exact_integer_sqrt (k, &s, &r);
  9404. return scm_values_2 (s, r);
  9405. }
  9406. #undef FUNC_NAME
  9407. void
  9408. scm_exact_integer_sqrt (SCM k, SCM *sp, SCM *rp)
  9409. {
  9410. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9411. {
  9412. if (SCM_I_INUM (k) > 0)
  9413. {
  9414. mp_limb_t kk, ss, rr;
  9415. kk = SCM_I_INUM (k);
  9416. if (mpn_sqrtrem (&ss, &rr, &kk, 1) == 0)
  9417. rr = 0;
  9418. *sp = SCM_I_MAKINUM (ss);
  9419. *rp = SCM_I_MAKINUM (rr);
  9420. }
  9421. else if (SCM_I_INUM (k) == 0)
  9422. *sp = *rp = SCM_INUM0;
  9423. else
  9424. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9425. "exact non-negative integer");
  9426. }
  9427. else if (SCM_LIKELY (SCM_BIGP (k)))
  9428. {
  9429. SCM s, r;
  9430. if (mpz_sgn (SCM_I_BIG_MPZ (k)) < 0)
  9431. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9432. "exact non-negative integer");
  9433. s = scm_i_mkbig ();
  9434. r = scm_i_mkbig ();
  9435. mpz_sqrtrem (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (r), SCM_I_BIG_MPZ (k));
  9436. scm_remember_upto_here_1 (k);
  9437. *sp = scm_i_normbig (s);
  9438. *rp = scm_i_normbig (r);
  9439. }
  9440. else
  9441. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  9442. "exact non-negative integer");
  9443. }
  9444. /* Return true iff K is a perfect square.
  9445. K must be an exact integer. */
  9446. static int
  9447. exact_integer_is_perfect_square (SCM k)
  9448. {
  9449. int result;
  9450. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9451. {
  9452. if (SCM_I_INUM (k) > 0)
  9453. {
  9454. mp_limb_t kk = SCM_I_INUM (k);
  9455. result = mpn_perfect_square_p (&kk, 1);
  9456. }
  9457. else
  9458. result = (SCM_I_INUM (k) == 0);
  9459. }
  9460. else
  9461. {
  9462. result = mpz_perfect_square_p (SCM_I_BIG_MPZ (k));
  9463. scm_remember_upto_here_1 (k);
  9464. }
  9465. return result;
  9466. }
  9467. /* Return the floor of the square root of K.
  9468. K must be an exact non-negative integer. */
  9469. static SCM
  9470. exact_integer_floor_square_root (SCM k)
  9471. {
  9472. if (SCM_LIKELY (SCM_I_INUMP (k)))
  9473. {
  9474. if (SCM_I_INUM (k) > 0)
  9475. {
  9476. mp_limb_t kk, ss, rr;
  9477. kk = SCM_I_INUM (k);
  9478. mpn_sqrtrem (&ss, &rr, &kk, 1);
  9479. return SCM_I_MAKINUM (ss);
  9480. }
  9481. else
  9482. return SCM_INUM0;
  9483. }
  9484. else
  9485. {
  9486. SCM s;
  9487. s = scm_i_mkbig ();
  9488. mpz_sqrt (SCM_I_BIG_MPZ (s), SCM_I_BIG_MPZ (k));
  9489. scm_remember_upto_here_1 (k);
  9490. return scm_i_normbig (s);
  9491. }
  9492. }
  9493. SCM_PRIMITIVE_GENERIC (scm_sqrt, "sqrt", 1, 0, 0,
  9494. (SCM z),
  9495. "Return the square root of @var{z}. Of the two possible roots\n"
  9496. "(positive and negative), the one with positive real part\n"
  9497. "is returned, or if that's zero then a positive imaginary part.\n"
  9498. "Thus,\n"
  9499. "\n"
  9500. "@example\n"
  9501. "(sqrt 9.0) @result{} 3.0\n"
  9502. "(sqrt -9.0) @result{} 0.0+3.0i\n"
  9503. "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
  9504. "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
  9505. "@end example")
  9506. #define FUNC_NAME s_scm_sqrt
  9507. {
  9508. if (SCM_COMPLEXP (z))
  9509. {
  9510. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_USABLE_CSQRT \
  9511. && defined SCM_COMPLEX_VALUE
  9512. return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (z)));
  9513. #else
  9514. double re = SCM_COMPLEX_REAL (z);
  9515. double im = SCM_COMPLEX_IMAG (z);
  9516. return scm_c_make_polar (sqrt (hypot (re, im)),
  9517. 0.5 * atan2 (im, re));
  9518. #endif
  9519. }
  9520. else if (SCM_NUMBERP (z))
  9521. {
  9522. if (SCM_I_INUMP (z))
  9523. {
  9524. scm_t_inum x = SCM_I_INUM (z);
  9525. if (SCM_LIKELY (x >= 0))
  9526. {
  9527. if (SCM_LIKELY (SCM_I_FIXNUM_BIT < DBL_MANT_DIG
  9528. || x < (1L << (DBL_MANT_DIG - 1))))
  9529. {
  9530. double root = sqrt (x);
  9531. /* If 0 <= x < 2^(DBL_MANT_DIG-1) and sqrt(x) is an
  9532. integer, then the result is exact. */
  9533. if (root == floor (root))
  9534. return SCM_I_MAKINUM ((scm_t_inum) root);
  9535. else
  9536. return scm_i_from_double (root);
  9537. }
  9538. else
  9539. {
  9540. mp_limb_t xx, root, rem;
  9541. assert (x != 0);
  9542. xx = x;
  9543. if (mpn_perfect_square_p (&xx, 1))
  9544. {
  9545. mpn_sqrtrem (&root, &rem, &xx, 1);
  9546. return SCM_I_MAKINUM (root);
  9547. }
  9548. }
  9549. }
  9550. }
  9551. else if (SCM_BIGP (z))
  9552. {
  9553. if (mpz_perfect_square_p (SCM_I_BIG_MPZ (z)))
  9554. {
  9555. SCM root = scm_i_mkbig ();
  9556. mpz_sqrt (SCM_I_BIG_MPZ (root), SCM_I_BIG_MPZ (z));
  9557. scm_remember_upto_here_1 (z);
  9558. return scm_i_normbig (root);
  9559. }
  9560. else
  9561. {
  9562. long expon;
  9563. double signif = scm_i_big2dbl_2exp (z, &expon);
  9564. if (expon & 1)
  9565. {
  9566. signif *= 2;
  9567. expon--;
  9568. }
  9569. if (signif < 0)
  9570. return scm_c_make_rectangular
  9571. (0.0, ldexp (sqrt (-signif), expon / 2));
  9572. else
  9573. return scm_i_from_double (ldexp (sqrt (signif), expon / 2));
  9574. }
  9575. }
  9576. else if (SCM_FRACTIONP (z))
  9577. {
  9578. SCM n = SCM_FRACTION_NUMERATOR (z);
  9579. SCM d = SCM_FRACTION_DENOMINATOR (z);
  9580. if (exact_integer_is_perfect_square (n)
  9581. && exact_integer_is_perfect_square (d))
  9582. return scm_i_make_ratio_already_reduced
  9583. (exact_integer_floor_square_root (n),
  9584. exact_integer_floor_square_root (d));
  9585. else
  9586. {
  9587. double xx = scm_i_divide2double (n, d);
  9588. double abs_xx = fabs (xx);
  9589. long shift = 0;
  9590. if (SCM_UNLIKELY (abs_xx > DBL_MAX || abs_xx < DBL_MIN))
  9591. {
  9592. shift = (scm_to_long (scm_integer_length (n))
  9593. - scm_to_long (scm_integer_length (d))) / 2;
  9594. if (shift > 0)
  9595. d = left_shift_exact_integer (d, 2 * shift);
  9596. else
  9597. n = left_shift_exact_integer (n, -2 * shift);
  9598. xx = scm_i_divide2double (n, d);
  9599. }
  9600. if (xx < 0)
  9601. return scm_c_make_rectangular (0.0, ldexp (sqrt (-xx), shift));
  9602. else
  9603. return scm_i_from_double (ldexp (sqrt (xx), shift));
  9604. }
  9605. }
  9606. /* Fallback method, when the cases above do not apply. */
  9607. {
  9608. double xx = scm_to_double (z);
  9609. if (xx < 0)
  9610. return scm_c_make_rectangular (0.0, sqrt (-xx));
  9611. else
  9612. return scm_i_from_double (sqrt (xx));
  9613. }
  9614. }
  9615. else
  9616. return scm_wta_dispatch_1 (g_scm_sqrt, z, 1, s_scm_sqrt);
  9617. }
  9618. #undef FUNC_NAME
  9619. void
  9620. scm_init_numbers ()
  9621. {
  9622. if (scm_install_gmp_memory_functions)
  9623. mp_set_memory_functions (custom_gmp_malloc,
  9624. custom_gmp_realloc,
  9625. custom_gmp_free);
  9626. mpz_init_set_si (z_negative_one, -1);
  9627. /* It may be possible to tune the performance of some algorithms by using
  9628. * the following constants to avoid the creation of bignums. Please, before
  9629. * using these values, remember the two rules of program optimization:
  9630. * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
  9631. scm_c_define ("most-positive-fixnum",
  9632. SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
  9633. scm_c_define ("most-negative-fixnum",
  9634. SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
  9635. scm_add_feature ("complex");
  9636. scm_add_feature ("inexact");
  9637. flo0 = scm_i_from_double (0.0);
  9638. flo_log10e = scm_i_from_double (M_LOG10E);
  9639. exactly_one_half = scm_divide (SCM_INUM1, SCM_I_MAKINUM (2));
  9640. {
  9641. /* Set scm_i_divide2double_lo2b to (2 b^p - 1) */
  9642. mpz_init_set_ui (scm_i_divide2double_lo2b, 1);
  9643. mpz_mul_2exp (scm_i_divide2double_lo2b,
  9644. scm_i_divide2double_lo2b,
  9645. DBL_MANT_DIG + 1); /* 2 b^p */
  9646. mpz_sub_ui (scm_i_divide2double_lo2b, scm_i_divide2double_lo2b, 1);
  9647. }
  9648. {
  9649. /* Set dbl_minimum_normal_mantissa to b^{p-1} */
  9650. mpz_init_set_ui (dbl_minimum_normal_mantissa, 1);
  9651. mpz_mul_2exp (dbl_minimum_normal_mantissa,
  9652. dbl_minimum_normal_mantissa,
  9653. DBL_MANT_DIG - 1);
  9654. }
  9655. #include "numbers.x"
  9656. }