numbers.c 297 KB

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