numbers.c 297 KB

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