numbers.c 223 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298
  1. /* Copyright 1995-2016,2018-2022
  2. Free Software Foundation, Inc.
  3. Portions Copyright 1990-1993 by AT&T Bell Laboratories and Bellcore.
  4. See scm_divide.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. /* General assumptions:
  18. * All objects satisfying SCM_BIGP() are too large to fit in a fixnum.
  19. * If an object satisfies integer?, it's either an inum, a bignum, or a real.
  20. * If floor (r) == r, r is an int, and mpz_set_d will DTRT.
  21. * XXX What about infinities? They are equal to their own floor! -mhw
  22. * All objects satisfying SCM_FRACTIONP are never an integer.
  23. */
  24. /* TODO:
  25. - see if special casing bignums and reals in integer-exponent when
  26. possible (to use mpz_pow and mpf_pow_ui) is faster.
  27. - look in to better short-circuiting of common cases in
  28. integer-expt and elsewhere.
  29. - see if direct mpz operations can help in ash and elsewhere.
  30. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <assert.h>
  35. #include <math.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <unicase.h>
  39. #include <unictype.h>
  40. #include <verify.h>
  41. #if HAVE_COMPLEX_H
  42. #include <complex.h>
  43. #endif
  44. #include "bdw-gc.h"
  45. #include "boolean.h"
  46. #include "deprecation.h"
  47. #include "dynwind.h"
  48. #include "eq.h"
  49. #include "feature.h"
  50. #include "finalizers.h"
  51. #include "goops.h"
  52. #include "gsubr.h"
  53. #include "integers.h"
  54. #include "modules.h"
  55. #include "pairs.h"
  56. #include "ports.h"
  57. #include "simpos.h"
  58. #include "smob.h"
  59. #include "strings.h"
  60. #include "values.h"
  61. #include "numbers.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. /* Make sure that scm_t_inum fits within a SCM value. */
  75. verify (sizeof (scm_t_inum) <= sizeof (scm_t_bits));
  76. /* Several functions below assume that fixnums fit within a long, and
  77. furthermore that there is some headroom to spare for other operations
  78. without overflowing. */
  79. verify (SCM_I_FIXNUM_BIT <= SCM_LONG_BIT - 2);
  80. /* Some functions that use GMP's mpn functions assume that a
  81. non-negative fixnum will always fit in a 'mp_limb_t'. */
  82. verify (SCM_MOST_POSITIVE_FIXNUM <= (mp_limb_t) -1);
  83. #define scm_from_inum(x) (scm_from_signed_integer (x))
  84. /* Test an inum to see if it can be converted to a double without loss
  85. of precision. Note that this will sometimes return 0 even when 1
  86. could have been returned, e.g. for large powers of 2. It is designed
  87. to be a fast check to optimize common cases. */
  88. #define INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE(n) \
  89. (SCM_I_FIXNUM_BIT-1 <= DBL_MANT_DIG \
  90. || ((n) ^ ((n) >> (SCM_I_FIXNUM_BIT-1))) < (1L << DBL_MANT_DIG))
  91. #if (! HAVE_DECL_MPZ_INITS) || SCM_ENABLE_MINI_GMP
  92. /* GMP < 5.0.0 and mini-gmp lack `mpz_inits' and `mpz_clears'. Provide
  93. them. */
  94. #define VARARG_MPZ_ITERATOR(func) \
  95. static void \
  96. func ## s (mpz_t x, ...) \
  97. { \
  98. va_list ap; \
  99. \
  100. va_start (ap, x); \
  101. while (x != NULL) \
  102. { \
  103. func (x); \
  104. x = va_arg (ap, mpz_ptr); \
  105. } \
  106. va_end (ap); \
  107. }
  108. VARARG_MPZ_ITERATOR (mpz_init)
  109. VARARG_MPZ_ITERATOR (mpz_clear)
  110. #endif
  111. /*
  112. Wonder if this might be faster for some of our code? A switch on
  113. the numtag would jump directly to the right case, and the
  114. SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests...
  115. #define SCM_I_NUMTAG_NOTNUM 0
  116. #define SCM_I_NUMTAG_INUM 1
  117. #define SCM_I_NUMTAG_BIG scm_tc16_big
  118. #define SCM_I_NUMTAG_REAL scm_tc16_real
  119. #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex
  120. #define SCM_I_NUMTAG(x) \
  121. (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \
  122. : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \
  123. : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \
  124. : SCM_I_NUMTAG_NOTNUM)))
  125. */
  126. /* the macro above will not work as is with fractions */
  127. static SCM flo0;
  128. static SCM exactly_one_half;
  129. static SCM flo_log10e;
  130. #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0)
  131. /* FLOBUFLEN is the maximum number of characters necessary for the
  132. * printed or scm_string representation of an inexact number.
  133. */
  134. #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  135. #if !defined (HAVE_ASINH)
  136. static double asinh (double x) { return log (x + sqrt (x * x + 1)); }
  137. #endif
  138. #if !defined (HAVE_ACOSH)
  139. static double acosh (double x) { return log (x + sqrt (x * x - 1)); }
  140. #endif
  141. #if !defined (HAVE_ATANH)
  142. static double atanh (double x) { return 0.5 * log ((1 + x) / (1 - x)); }
  143. #endif
  144. /* mpz_cmp_d in GMP before 4.2 didn't recognise infinities, so
  145. xmpz_cmp_d uses an explicit check. Starting with GMP 4.2 (released
  146. in March 2006), mpz_cmp_d now handles infinities properly. */
  147. #if 1
  148. #define xmpz_cmp_d(z, d) \
  149. (isinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d))
  150. #else
  151. #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d)
  152. #endif
  153. #if defined (GUILE_I)
  154. #if defined HAVE_COMPLEX_DOUBLE
  155. /* For an SCM object Z which is a complex number (ie. satisfies
  156. SCM_COMPLEXP), return its value as a C level "complex double". */
  157. #define SCM_COMPLEX_VALUE(z) \
  158. (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
  159. static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
  160. /* Convert a C "complex double" to an SCM value. */
  161. static inline SCM
  162. scm_from_complex_double (complex double z)
  163. {
  164. return scm_c_make_rectangular (creal (z), cimag (z));
  165. }
  166. #endif /* HAVE_COMPLEX_DOUBLE */
  167. #endif /* GUILE_I */
  168. /* Make the ratio NUMERATOR/DENOMINATOR, where:
  169. 1. NUMERATOR and DENOMINATOR are exact integers
  170. 2. NUMERATOR and DENOMINATOR are reduced to lowest terms: gcd(n,d) == 1 */
  171. static SCM
  172. scm_i_make_ratio_already_reduced (SCM numerator, SCM denominator)
  173. {
  174. /* Flip signs so that the denominator is positive. */
  175. if (scm_is_false (scm_positive_p (denominator)))
  176. {
  177. if (SCM_UNLIKELY (scm_is_eq (denominator, SCM_INUM0)))
  178. scm_num_overflow ("make-ratio");
  179. else
  180. {
  181. numerator = scm_difference (numerator, SCM_UNDEFINED);
  182. denominator = scm_difference (denominator, SCM_UNDEFINED);
  183. }
  184. }
  185. /* Check for the integer case */
  186. if (scm_is_eq (denominator, SCM_INUM1))
  187. return numerator;
  188. return scm_double_cell (scm_tc16_fraction,
  189. SCM_UNPACK (numerator),
  190. SCM_UNPACK (denominator), 0);
  191. }
  192. static SCM scm_exact_integer_quotient (SCM x, SCM y);
  193. /* Make the ratio NUMERATOR/DENOMINATOR */
  194. static SCM
  195. scm_i_make_ratio (SCM numerator, SCM denominator)
  196. #define FUNC_NAME "make-ratio"
  197. {
  198. if (!scm_is_exact_integer (numerator))
  199. abort();
  200. if (!scm_is_exact_integer (denominator))
  201. abort();
  202. SCM the_gcd = scm_gcd (numerator, denominator);
  203. if (!(scm_is_eq (the_gcd, SCM_INUM1)))
  204. {
  205. /* Reduce to lowest terms */
  206. numerator = scm_exact_integer_quotient (numerator, the_gcd);
  207. denominator = scm_exact_integer_quotient (denominator, the_gcd);
  208. }
  209. return scm_i_make_ratio_already_reduced (numerator, denominator);
  210. }
  211. #undef FUNC_NAME
  212. static mpz_t scm_i_divide2double_lo2b;
  213. /* Return the double that is closest to the exact rational N/D, with
  214. ties rounded toward even mantissas. N and D must be exact
  215. integers. */
  216. static double
  217. scm_i_divide2double (SCM n, SCM d)
  218. {
  219. int neg;
  220. mpz_t nn, dd, lo, hi, x;
  221. ssize_t e;
  222. if (SCM_I_INUMP (d))
  223. {
  224. if (SCM_I_INUMP (n)
  225. && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (n))
  226. && INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE (SCM_I_INUM (d)))
  227. /* If both N and D can be losslessly converted to doubles, then
  228. we can rely on IEEE floating point to do proper rounding much
  229. faster than we can. */
  230. return ((double) SCM_I_INUM (n)) / ((double) SCM_I_INUM (d));
  231. if (scm_is_eq (d, SCM_INUM0))
  232. {
  233. if (scm_is_true (scm_positive_p (n)))
  234. return 1.0 / 0.0;
  235. else if (scm_is_true (scm_negative_p (n)))
  236. return -1.0 / 0.0;
  237. else
  238. return 0.0 / 0.0;
  239. }
  240. mpz_init_set_si (dd, SCM_I_INUM (d));
  241. }
  242. else
  243. scm_integer_init_set_mpz_z (scm_bignum (d), dd);
  244. if (SCM_I_INUMP (n))
  245. mpz_init_set_si (nn, SCM_I_INUM (n));
  246. else
  247. scm_integer_init_set_mpz_z (scm_bignum (n), nn);
  248. neg = (mpz_sgn (nn) < 0) ^ (mpz_sgn (dd) < 0);
  249. mpz_abs (nn, nn);
  250. mpz_abs (dd, dd);
  251. /* Now we need to find the value of e such that:
  252. For e <= 0:
  253. b^{p-1} - 1/2b <= b^-e n / d < b^p - 1/2 [1A]
  254. (2 b^p - 1) <= 2 b b^-e n / d < (2 b^p - 1) b [2A]
  255. (2 b^p - 1) d <= 2 b b^-e n < (2 b^p - 1) d b [3A]
  256. For e >= 0:
  257. b^{p-1} - 1/2b <= n / b^e d < b^p - 1/2 [1B]
  258. (2 b^p - 1) <= 2 b n / b^e d < (2 b^p - 1) b [2B]
  259. (2 b^p - 1) d b^e <= 2 b n < (2 b^p - 1) d b b^e [3B]
  260. where: p = DBL_MANT_DIG
  261. b = FLT_RADIX (here assumed to be 2)
  262. After rounding, the mantissa must be an integer between b^{p-1} and
  263. (b^p - 1), except for subnormal numbers. In the inequations [1A]
  264. and [1B], the middle expression represents the mantissa *before*
  265. rounding, and therefore is bounded by the range of values that will
  266. round to a floating-point number with the exponent e. The upper
  267. bound is (b^p - 1 + 1/2) = (b^p - 1/2), and is exclusive because
  268. ties will round up to the next power of b. The lower bound is
  269. (b^{p-1} - 1/2b), and is inclusive because ties will round toward
  270. this power of b. Here we subtract 1/2b instead of 1/2 because it
  271. is in the range of the next smaller exponent, where the
  272. representable numbers are closer together by a factor of b.
  273. Inequations [2A] and [2B] are derived from [1A] and [1B] by
  274. multiplying by 2b, and in [3A] and [3B] we multiply by the
  275. denominator of the middle value to obtain integer expressions.
  276. In the code below, we refer to the three expressions in [3A] or
  277. [3B] as lo, x, and hi. If the number is normalizable, we will
  278. achieve the goal: lo <= x < hi */
  279. /* Make an initial guess for e */
  280. e = mpz_sizeinbase (nn, 2) - mpz_sizeinbase (dd, 2) - (DBL_MANT_DIG-1);
  281. if (e < DBL_MIN_EXP - DBL_MANT_DIG)
  282. e = DBL_MIN_EXP - DBL_MANT_DIG;
  283. /* Compute the initial values of lo, x, and hi
  284. based on the initial guess of e */
  285. mpz_inits (lo, hi, x, NULL);
  286. mpz_mul_2exp (x, nn, 2 + ((e < 0) ? -e : 0));
  287. mpz_mul (lo, dd, scm_i_divide2double_lo2b);
  288. if (e > 0)
  289. mpz_mul_2exp (lo, lo, e);
  290. mpz_mul_2exp (hi, lo, 1);
  291. /* Adjust e as needed to satisfy the inequality lo <= x < hi,
  292. (but without making e less than the minimum exponent) */
  293. while (mpz_cmp (x, lo) < 0 && e > DBL_MIN_EXP - DBL_MANT_DIG)
  294. {
  295. mpz_mul_2exp (x, x, 1);
  296. e--;
  297. }
  298. while (mpz_cmp (x, hi) >= 0)
  299. {
  300. /* If we ever used lo's value again,
  301. we would need to double lo here. */
  302. mpz_mul_2exp (hi, hi, 1);
  303. e++;
  304. }
  305. /* Now compute the rounded mantissa:
  306. n / b^e d (if e >= 0)
  307. n b^-e / d (if e <= 0) */
  308. {
  309. int cmp;
  310. double result;
  311. if (e < 0)
  312. mpz_mul_2exp (nn, nn, -e);
  313. else
  314. mpz_mul_2exp (dd, dd, e);
  315. /* mpz does not directly support rounded right
  316. shifts, so we have to do it the hard way.
  317. For efficiency, we reuse lo and hi.
  318. hi == quotient, lo == remainder */
  319. mpz_fdiv_qr (hi, lo, nn, dd);
  320. /* The fractional part of the unrounded mantissa would be
  321. remainder/dividend, i.e. lo/dd. So we have a tie if
  322. lo/dd = 1/2. Multiplying both sides by 2*dd yields the
  323. integer expression 2*lo = dd. Here we do that comparison
  324. to decide whether to round up or down. */
  325. mpz_mul_2exp (lo, lo, 1);
  326. cmp = mpz_cmp (lo, dd);
  327. if (cmp > 0 || (cmp == 0 && mpz_odd_p (hi)))
  328. mpz_add_ui (hi, hi, 1);
  329. result = ldexp (mpz_get_d (hi), e);
  330. if (neg)
  331. result = -result;
  332. mpz_clears (nn, dd, lo, hi, x, NULL);
  333. return result;
  334. }
  335. }
  336. double
  337. scm_i_fraction2double (SCM z)
  338. {
  339. return scm_i_divide2double (SCM_FRACTION_NUMERATOR (z),
  340. SCM_FRACTION_DENOMINATOR (z));
  341. }
  342. static SCM
  343. scm_i_from_double (double val)
  344. {
  345. SCM z;
  346. z = SCM_PACK_POINTER (scm_gc_malloc_pointerless (sizeof (scm_t_double), "real"));
  347. SCM_SET_CELL_TYPE (z, scm_tc16_real);
  348. SCM_REAL_VALUE (z) = val;
  349. return z;
  350. }
  351. SCM_PRIMITIVE_GENERIC (scm_exact_p, "exact?", 1, 0, 0,
  352. (SCM x),
  353. "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
  354. "otherwise.")
  355. #define FUNC_NAME s_scm_exact_p
  356. {
  357. if (SCM_INEXACTP (x))
  358. return SCM_BOOL_F;
  359. else if (SCM_NUMBERP (x))
  360. return SCM_BOOL_T;
  361. else
  362. return scm_wta_dispatch_1 (g_scm_exact_p, x, 1, s_scm_exact_p);
  363. }
  364. #undef FUNC_NAME
  365. int
  366. scm_is_exact (SCM val)
  367. {
  368. return scm_is_true (scm_exact_p (val));
  369. }
  370. SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0,
  371. (SCM x),
  372. "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
  373. "else.")
  374. #define FUNC_NAME s_scm_inexact_p
  375. {
  376. if (SCM_INEXACTP (x))
  377. return SCM_BOOL_T;
  378. else if (SCM_NUMBERP (x))
  379. return SCM_BOOL_F;
  380. else
  381. return scm_wta_dispatch_1 (g_scm_inexact_p, x, 1, s_scm_inexact_p);
  382. }
  383. #undef FUNC_NAME
  384. int
  385. scm_is_inexact (SCM val)
  386. {
  387. return scm_is_true (scm_inexact_p (val));
  388. }
  389. SCM_PRIMITIVE_GENERIC (scm_odd_p, "odd?", 1, 0, 0,
  390. (SCM n),
  391. "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
  392. "otherwise.")
  393. #define FUNC_NAME s_scm_odd_p
  394. {
  395. if (SCM_I_INUMP (n))
  396. return scm_from_bool (scm_is_integer_odd_i (SCM_I_INUM (n)));
  397. else if (SCM_BIGP (n))
  398. return scm_from_bool (scm_is_integer_odd_z (scm_bignum (n)));
  399. else if (SCM_REALP (n))
  400. {
  401. double val = SCM_REAL_VALUE (n);
  402. if (isfinite (val))
  403. {
  404. double rem = fabs (fmod (val, 2.0));
  405. if (rem == 1.0)
  406. return SCM_BOOL_T;
  407. else if (rem == 0.0)
  408. return SCM_BOOL_F;
  409. }
  410. }
  411. return scm_wta_dispatch_1 (g_scm_odd_p, n, 1, s_scm_odd_p);
  412. }
  413. #undef FUNC_NAME
  414. SCM_PRIMITIVE_GENERIC (scm_even_p, "even?", 1, 0, 0,
  415. (SCM n),
  416. "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
  417. "otherwise.")
  418. #define FUNC_NAME s_scm_even_p
  419. {
  420. if (SCM_I_INUMP (n))
  421. return scm_from_bool (!scm_is_integer_odd_i (SCM_I_INUM (n)));
  422. else if (SCM_BIGP (n))
  423. return scm_from_bool (!scm_is_integer_odd_z (scm_bignum (n)));
  424. else if (SCM_REALP (n))
  425. {
  426. double val = SCM_REAL_VALUE (n);
  427. if (isfinite (val))
  428. {
  429. double rem = fabs (fmod (val, 2.0));
  430. if (rem == 1.0)
  431. return SCM_BOOL_F;
  432. else if (rem == 0.0)
  433. return SCM_BOOL_T;
  434. }
  435. }
  436. return scm_wta_dispatch_1 (g_scm_even_p, n, 1, s_scm_even_p);
  437. }
  438. #undef FUNC_NAME
  439. SCM_PRIMITIVE_GENERIC (scm_finite_p, "finite?", 1, 0, 0,
  440. (SCM x),
  441. "Return @code{#t} if the real number @var{x} is neither\n"
  442. "infinite nor a NaN, @code{#f} otherwise.")
  443. #define FUNC_NAME s_scm_finite_p
  444. {
  445. if (SCM_REALP (x))
  446. return scm_from_bool (isfinite (SCM_REAL_VALUE (x)));
  447. else if (scm_is_real (x))
  448. return SCM_BOOL_T;
  449. else
  450. return scm_wta_dispatch_1 (g_scm_finite_p, x, 1, s_scm_finite_p);
  451. }
  452. #undef FUNC_NAME
  453. SCM_PRIMITIVE_GENERIC (scm_inf_p, "inf?", 1, 0, 0,
  454. (SCM x),
  455. "Return @code{#t} if the real number @var{x} is @samp{+inf.0} or\n"
  456. "@samp{-inf.0}. Otherwise return @code{#f}.")
  457. #define FUNC_NAME s_scm_inf_p
  458. {
  459. if (SCM_REALP (x))
  460. return scm_from_bool (isinf (SCM_REAL_VALUE (x)));
  461. else if (scm_is_real (x))
  462. return SCM_BOOL_F;
  463. else
  464. return scm_wta_dispatch_1 (g_scm_inf_p, x, 1, s_scm_inf_p);
  465. }
  466. #undef FUNC_NAME
  467. SCM_PRIMITIVE_GENERIC (scm_nan_p, "nan?", 1, 0, 0,
  468. (SCM x),
  469. "Return @code{#t} if the real number @var{x} is a NaN,\n"
  470. "or @code{#f} otherwise.")
  471. #define FUNC_NAME s_scm_nan_p
  472. {
  473. if (SCM_REALP (x))
  474. return scm_from_bool (isnan (SCM_REAL_VALUE (x)));
  475. else if (scm_is_real (x))
  476. return SCM_BOOL_F;
  477. else
  478. return scm_wta_dispatch_1 (g_scm_nan_p, x, 1, s_scm_nan_p);
  479. }
  480. #undef FUNC_NAME
  481. /* Guile's idea of infinity. */
  482. static double guile_Inf;
  483. /* Guile's idea of not a number. */
  484. static double guile_NaN;
  485. static void
  486. guile_ieee_init (void)
  487. {
  488. /* Some version of gcc on some old version of Linux used to crash when
  489. trying to make Inf and NaN. */
  490. #ifdef INFINITY
  491. /* C99 INFINITY, when available.
  492. FIXME: The standard allows for INFINITY to be something that overflows
  493. at compile time. We ought to have a configure test to check for that
  494. before trying to use it. (But in practice we believe this is not a
  495. problem on any system guile is likely to target.) */
  496. guile_Inf = INFINITY;
  497. #elif defined HAVE_DINFINITY
  498. /* OSF */
  499. extern unsigned int DINFINITY[2];
  500. guile_Inf = (*((double *) (DINFINITY)));
  501. #else
  502. double tmp = 1e+10;
  503. guile_Inf = tmp;
  504. for (;;)
  505. {
  506. guile_Inf *= 1e+10;
  507. if (guile_Inf == tmp)
  508. break;
  509. tmp = guile_Inf;
  510. }
  511. #endif
  512. #ifdef NAN
  513. /* C99 NAN, when available */
  514. guile_NaN = NAN;
  515. #elif defined HAVE_DQNAN
  516. {
  517. /* OSF */
  518. extern unsigned int DQNAN[2];
  519. guile_NaN = (*((double *)(DQNAN)));
  520. }
  521. #else
  522. guile_NaN = guile_Inf / guile_Inf;
  523. #endif
  524. }
  525. SCM_DEFINE (scm_inf, "inf", 0, 0, 0,
  526. (void),
  527. "Return Inf.")
  528. #define FUNC_NAME s_scm_inf
  529. {
  530. static int initialized = 0;
  531. if (! initialized)
  532. {
  533. guile_ieee_init ();
  534. initialized = 1;
  535. }
  536. return scm_i_from_double (guile_Inf);
  537. }
  538. #undef FUNC_NAME
  539. SCM_DEFINE (scm_nan, "nan", 0, 0, 0,
  540. (void),
  541. "Return NaN.")
  542. #define FUNC_NAME s_scm_nan
  543. {
  544. static int initialized = 0;
  545. if (!initialized)
  546. {
  547. guile_ieee_init ();
  548. initialized = 1;
  549. }
  550. return scm_i_from_double (guile_NaN);
  551. }
  552. #undef FUNC_NAME
  553. SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
  554. (SCM x),
  555. "Return the absolute value of @var{x}.")
  556. #define FUNC_NAME s_scm_abs
  557. {
  558. if (SCM_I_INUMP (x))
  559. return scm_integer_abs_i (SCM_I_INUM (x));
  560. else if (SCM_LIKELY (SCM_REALP (x)))
  561. return scm_i_from_double (copysign (SCM_REAL_VALUE (x), 1.0));
  562. else if (SCM_BIGP (x))
  563. return scm_integer_abs_z (scm_bignum (x));
  564. else if (SCM_FRACTIONP (x))
  565. {
  566. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
  567. return x;
  568. return scm_i_make_ratio_already_reduced
  569. (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  570. SCM_FRACTION_DENOMINATOR (x));
  571. }
  572. else
  573. return scm_wta_dispatch_1 (g_scm_abs, x, 1, s_scm_abs);
  574. }
  575. #undef FUNC_NAME
  576. SCM_PRIMITIVE_GENERIC (scm_quotient, "quotient", 2, 0, 0,
  577. (SCM x, SCM y),
  578. "Return the quotient of the numbers @var{x} and @var{y}.")
  579. #define FUNC_NAME s_scm_quotient
  580. {
  581. if (SCM_LIKELY (scm_is_integer (x)))
  582. {
  583. if (SCM_LIKELY (scm_is_integer (y)))
  584. return scm_truncate_quotient (x, y);
  585. else
  586. return scm_wta_dispatch_2 (g_scm_quotient, x, y, SCM_ARG2, s_scm_quotient);
  587. }
  588. else
  589. return scm_wta_dispatch_2 (g_scm_quotient, x, y, SCM_ARG1, s_scm_quotient);
  590. }
  591. #undef FUNC_NAME
  592. SCM_PRIMITIVE_GENERIC (scm_remainder, "remainder", 2, 0, 0,
  593. (SCM x, SCM y),
  594. "Return the remainder of the numbers @var{x} and @var{y}.\n"
  595. "@lisp\n"
  596. "(remainder 13 4) @result{} 1\n"
  597. "(remainder -13 4) @result{} -1\n"
  598. "@end lisp")
  599. #define FUNC_NAME s_scm_remainder
  600. {
  601. if (SCM_LIKELY (scm_is_integer (x)))
  602. {
  603. if (SCM_LIKELY (scm_is_integer (y)))
  604. return scm_truncate_remainder (x, y);
  605. else
  606. return scm_wta_dispatch_2 (g_scm_remainder, x, y, SCM_ARG2, s_scm_remainder);
  607. }
  608. else
  609. return scm_wta_dispatch_2 (g_scm_remainder, x, y, SCM_ARG1, s_scm_remainder);
  610. }
  611. #undef FUNC_NAME
  612. SCM_PRIMITIVE_GENERIC (scm_modulo, "modulo", 2, 0, 0,
  613. (SCM x, SCM y),
  614. "Return the modulo of the numbers @var{x} and @var{y}.\n"
  615. "@lisp\n"
  616. "(modulo 13 4) @result{} 1\n"
  617. "(modulo -13 4) @result{} 3\n"
  618. "@end lisp")
  619. #define FUNC_NAME s_scm_modulo
  620. {
  621. if (SCM_LIKELY (scm_is_integer (x)))
  622. {
  623. if (SCM_LIKELY (scm_is_integer (y)))
  624. return scm_floor_remainder (x, y);
  625. else
  626. return scm_wta_dispatch_2 (g_scm_modulo, x, y, SCM_ARG2, s_scm_modulo);
  627. }
  628. else
  629. return scm_wta_dispatch_2 (g_scm_modulo, x, y, SCM_ARG1, s_scm_modulo);
  630. }
  631. #undef FUNC_NAME
  632. /* Return the exact integer q such that n = q*d, for exact integers n
  633. and d, where d is known in advance to divide n evenly (with zero
  634. remainder). For large integers, this can be computed more
  635. efficiently than when the remainder is unknown. */
  636. static SCM
  637. scm_exact_integer_quotient (SCM n, SCM d)
  638. #define FUNC_NAME "exact-integer-quotient"
  639. {
  640. if (SCM_I_INUMP (n))
  641. {
  642. if (scm_is_eq (n, d))
  643. return SCM_INUM1;
  644. if (SCM_I_INUMP (d))
  645. return scm_integer_exact_quotient_ii (SCM_I_INUM (n), SCM_I_INUM (d));
  646. else if (SCM_BIGP (d))
  647. return scm_integer_exact_quotient_iz (SCM_I_INUM (n), scm_bignum (d));
  648. else
  649. abort (); // Unreachable.
  650. }
  651. else if (SCM_BIGP (n))
  652. {
  653. if (scm_is_eq (n, d))
  654. return SCM_INUM1;
  655. if (SCM_I_INUMP (d))
  656. return scm_integer_exact_quotient_zi (scm_bignum (n), SCM_I_INUM (d));
  657. else if (SCM_BIGP (d))
  658. return scm_integer_exact_quotient_zz (scm_bignum (n), scm_bignum (d));
  659. else
  660. abort (); // Unreachable.
  661. }
  662. else
  663. abort (); // Unreachable.
  664. }
  665. #undef FUNC_NAME
  666. /* two_valued_wta_dispatch_2 is a version of SCM_WTA_DISPATCH_2 for
  667. two-valued functions. It is called from primitive generics that take
  668. two arguments and return two values, when the core procedure is
  669. unable to handle the given argument types. If there are GOOPS
  670. methods for this primitive generic, it dispatches to GOOPS and, if
  671. successful, expects two values to be returned, which are placed in
  672. *rp1 and *rp2. If there are no GOOPS methods, it throws a
  673. wrong-type-arg exception.
  674. FIXME: This obviously belongs somewhere else, but until we decide on
  675. the right API, it is here as a static function, because it is needed
  676. by the *_divide functions below.
  677. */
  678. static void
  679. two_valued_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos,
  680. const char *subr, SCM *rp1, SCM *rp2)
  681. {
  682. SCM vals = scm_wta_dispatch_2 (gf, a1, a2, pos, subr);
  683. scm_i_extract_values_2 (vals, rp1, rp2);
  684. }
  685. SCM_DEFINE (scm_euclidean_quotient, "euclidean-quotient", 2, 0, 0,
  686. (SCM x, SCM y),
  687. "Return the integer @var{q} such that\n"
  688. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  689. "where @math{0 <= @var{r} < abs(@var{y})}.\n"
  690. "@lisp\n"
  691. "(euclidean-quotient 123 10) @result{} 12\n"
  692. "(euclidean-quotient 123 -10) @result{} -12\n"
  693. "(euclidean-quotient -123 10) @result{} -13\n"
  694. "(euclidean-quotient -123 -10) @result{} 13\n"
  695. "(euclidean-quotient -123.2 -63.5) @result{} 2.0\n"
  696. "(euclidean-quotient 16/3 -10/7) @result{} -3\n"
  697. "@end lisp")
  698. #define FUNC_NAME s_scm_euclidean_quotient
  699. {
  700. if (scm_is_false (scm_negative_p (y)))
  701. return scm_floor_quotient (x, y);
  702. else
  703. return scm_ceiling_quotient (x, y);
  704. }
  705. #undef FUNC_NAME
  706. SCM_DEFINE (scm_euclidean_remainder, "euclidean-remainder", 2, 0, 0,
  707. (SCM x, SCM y),
  708. "Return the real number @var{r} such that\n"
  709. "@math{0 <= @var{r} < abs(@var{y})} and\n"
  710. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  711. "for some integer @var{q}.\n"
  712. "@lisp\n"
  713. "(euclidean-remainder 123 10) @result{} 3\n"
  714. "(euclidean-remainder 123 -10) @result{} 3\n"
  715. "(euclidean-remainder -123 10) @result{} 7\n"
  716. "(euclidean-remainder -123 -10) @result{} 7\n"
  717. "(euclidean-remainder -123.2 -63.5) @result{} 3.8\n"
  718. "(euclidean-remainder 16/3 -10/7) @result{} 22/21\n"
  719. "@end lisp")
  720. #define FUNC_NAME s_scm_euclidean_remainder
  721. {
  722. if (scm_is_false (scm_negative_p (y)))
  723. return scm_floor_remainder (x, y);
  724. else
  725. return scm_ceiling_remainder (x, y);
  726. }
  727. #undef FUNC_NAME
  728. SCM_DEFINE (scm_i_euclidean_divide, "euclidean/", 2, 0, 0,
  729. (SCM x, SCM y),
  730. "Return the integer @var{q} and the real number @var{r}\n"
  731. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  732. "and @math{0 <= @var{r} < abs(@var{y})}.\n"
  733. "@lisp\n"
  734. "(euclidean/ 123 10) @result{} 12 and 3\n"
  735. "(euclidean/ 123 -10) @result{} -12 and 3\n"
  736. "(euclidean/ -123 10) @result{} -13 and 7\n"
  737. "(euclidean/ -123 -10) @result{} 13 and 7\n"
  738. "(euclidean/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  739. "(euclidean/ 16/3 -10/7) @result{} -3 and 22/21\n"
  740. "@end lisp")
  741. #define FUNC_NAME s_scm_i_euclidean_divide
  742. {
  743. if (scm_is_false (scm_negative_p (y)))
  744. return scm_i_floor_divide (x, y);
  745. else
  746. return scm_i_ceiling_divide (x, y);
  747. }
  748. #undef FUNC_NAME
  749. void
  750. scm_euclidean_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  751. {
  752. if (scm_is_false (scm_negative_p (y)))
  753. scm_floor_divide (x, y, qp, rp);
  754. else
  755. scm_ceiling_divide (x, y, qp, rp);
  756. }
  757. static SCM scm_i_inexact_floor_quotient (double x, double y);
  758. static SCM scm_i_exact_rational_floor_quotient (SCM x, SCM y);
  759. SCM_PRIMITIVE_GENERIC (scm_floor_quotient, "floor-quotient", 2, 0, 0,
  760. (SCM x, SCM y),
  761. "Return the floor of @math{@var{x} / @var{y}}.\n"
  762. "@lisp\n"
  763. "(floor-quotient 123 10) @result{} 12\n"
  764. "(floor-quotient 123 -10) @result{} -13\n"
  765. "(floor-quotient -123 10) @result{} -13\n"
  766. "(floor-quotient -123 -10) @result{} 12\n"
  767. "(floor-quotient -123.2 -63.5) @result{} 1.0\n"
  768. "(floor-quotient 16/3 -10/7) @result{} -4\n"
  769. "@end lisp")
  770. #define FUNC_NAME s_scm_floor_quotient
  771. {
  772. if (SCM_I_INUMP (x))
  773. {
  774. if (SCM_I_INUMP (y))
  775. return scm_integer_floor_quotient_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  776. else if (SCM_BIGP (y))
  777. return scm_integer_floor_quotient_iz (SCM_I_INUM (x), scm_bignum (y));
  778. else if (SCM_REALP (y))
  779. return scm_i_inexact_floor_quotient (SCM_I_INUM (x), SCM_REAL_VALUE (y));
  780. else if (SCM_FRACTIONP (y))
  781. return scm_i_exact_rational_floor_quotient (x, y);
  782. else
  783. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  784. s_scm_floor_quotient);
  785. }
  786. else if (SCM_BIGP (x))
  787. {
  788. if (SCM_I_INUMP (y))
  789. return scm_integer_floor_quotient_zi (scm_bignum (x), SCM_I_INUM (y));
  790. else if (SCM_BIGP (y))
  791. return scm_integer_floor_quotient_zz (scm_bignum (x), scm_bignum (y));
  792. else if (SCM_REALP (y))
  793. return scm_i_inexact_floor_quotient
  794. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  795. else if (SCM_FRACTIONP (y))
  796. return scm_i_exact_rational_floor_quotient (x, y);
  797. else
  798. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  799. s_scm_floor_quotient);
  800. }
  801. else if (SCM_REALP (x))
  802. {
  803. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  804. SCM_BIGP (y) || SCM_FRACTIONP (y))
  805. return scm_i_inexact_floor_quotient
  806. (SCM_REAL_VALUE (x), scm_to_double (y));
  807. else
  808. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  809. s_scm_floor_quotient);
  810. }
  811. else if (SCM_FRACTIONP (x))
  812. {
  813. if (SCM_REALP (y))
  814. return scm_i_inexact_floor_quotient
  815. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  816. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  817. return scm_i_exact_rational_floor_quotient (x, y);
  818. else
  819. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG2,
  820. s_scm_floor_quotient);
  821. }
  822. else
  823. return scm_wta_dispatch_2 (g_scm_floor_quotient, x, y, SCM_ARG1,
  824. s_scm_floor_quotient);
  825. }
  826. #undef FUNC_NAME
  827. static SCM
  828. scm_i_inexact_floor_quotient (double x, double y)
  829. {
  830. if (SCM_UNLIKELY (y == 0))
  831. scm_num_overflow (s_scm_floor_quotient); /* or return a NaN? */
  832. else
  833. return scm_i_from_double (floor (x / y));
  834. }
  835. static SCM
  836. scm_i_exact_rational_floor_quotient (SCM x, SCM y)
  837. {
  838. return scm_floor_quotient
  839. (scm_product (scm_numerator (x), scm_denominator (y)),
  840. scm_product (scm_numerator (y), scm_denominator (x)));
  841. }
  842. static SCM scm_i_inexact_floor_remainder (double x, double y);
  843. static SCM scm_i_exact_rational_floor_remainder (SCM x, SCM y);
  844. SCM_PRIMITIVE_GENERIC (scm_floor_remainder, "floor-remainder", 2, 0, 0,
  845. (SCM x, SCM y),
  846. "Return the real number @var{r} such that\n"
  847. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  848. "where @math{@var{q} = floor(@var{x} / @var{y})}.\n"
  849. "@lisp\n"
  850. "(floor-remainder 123 10) @result{} 3\n"
  851. "(floor-remainder 123 -10) @result{} -7\n"
  852. "(floor-remainder -123 10) @result{} 7\n"
  853. "(floor-remainder -123 -10) @result{} -3\n"
  854. "(floor-remainder -123.2 -63.5) @result{} -59.7\n"
  855. "(floor-remainder 16/3 -10/7) @result{} -8/21\n"
  856. "@end lisp")
  857. #define FUNC_NAME s_scm_floor_remainder
  858. {
  859. if (SCM_LIKELY (SCM_I_INUMP (x)))
  860. {
  861. if (SCM_I_INUMP (y))
  862. return scm_integer_floor_remainder_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  863. else if (SCM_BIGP (y))
  864. return scm_integer_floor_remainder_iz (SCM_I_INUM (x), scm_bignum (y));
  865. else if (SCM_REALP (y))
  866. return scm_i_inexact_floor_remainder (SCM_I_INUM (x),
  867. SCM_REAL_VALUE (y));
  868. else if (SCM_FRACTIONP (y))
  869. return scm_i_exact_rational_floor_remainder (x, y);
  870. else
  871. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  872. s_scm_floor_remainder);
  873. }
  874. else if (SCM_BIGP (x))
  875. {
  876. if (SCM_I_INUMP (y))
  877. return scm_integer_floor_remainder_zi (scm_bignum (x), SCM_I_INUM (y));
  878. else if (SCM_BIGP (y))
  879. return scm_integer_floor_remainder_zz (scm_bignum (x), scm_bignum (y));
  880. else if (SCM_REALP (y))
  881. return scm_i_inexact_floor_remainder
  882. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  883. else if (SCM_FRACTIONP (y))
  884. return scm_i_exact_rational_floor_remainder (x, y);
  885. else
  886. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  887. s_scm_floor_remainder);
  888. }
  889. else if (SCM_REALP (x))
  890. {
  891. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  892. SCM_BIGP (y) || SCM_FRACTIONP (y))
  893. return scm_i_inexact_floor_remainder
  894. (SCM_REAL_VALUE (x), scm_to_double (y));
  895. else
  896. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  897. s_scm_floor_remainder);
  898. }
  899. else if (SCM_FRACTIONP (x))
  900. {
  901. if (SCM_REALP (y))
  902. return scm_i_inexact_floor_remainder
  903. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  904. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  905. return scm_i_exact_rational_floor_remainder (x, y);
  906. else
  907. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG2,
  908. s_scm_floor_remainder);
  909. }
  910. else
  911. return scm_wta_dispatch_2 (g_scm_floor_remainder, x, y, SCM_ARG1,
  912. s_scm_floor_remainder);
  913. }
  914. #undef FUNC_NAME
  915. static SCM
  916. scm_i_inexact_floor_remainder (double x, double y)
  917. {
  918. /* Although it would be more efficient to use fmod here, we can't
  919. because it would in some cases produce results inconsistent with
  920. scm_i_inexact_floor_quotient, such that x != q * y + r (not even
  921. close). In particular, when x is very close to a multiple of y,
  922. then r might be either 0.0 or y, but those two cases must
  923. correspond to different choices of q. If r = 0.0 then q must be
  924. x/y, and if r = y then q must be x/y-1. If quotient chooses one
  925. and remainder chooses the other, it would be bad. */
  926. if (SCM_UNLIKELY (y == 0))
  927. scm_num_overflow (s_scm_floor_remainder); /* or return a NaN? */
  928. else
  929. return scm_i_from_double (x - y * floor (x / y));
  930. }
  931. static SCM
  932. scm_i_exact_rational_floor_remainder (SCM x, SCM y)
  933. {
  934. SCM xd = scm_denominator (x);
  935. SCM yd = scm_denominator (y);
  936. SCM r1 = scm_floor_remainder (scm_product (scm_numerator (x), yd),
  937. scm_product (scm_numerator (y), xd));
  938. return scm_divide (r1, scm_product (xd, yd));
  939. }
  940. static void scm_i_inexact_floor_divide (double x, double y,
  941. SCM *qp, SCM *rp);
  942. static void scm_i_exact_rational_floor_divide (SCM x, SCM y,
  943. SCM *qp, SCM *rp);
  944. SCM_PRIMITIVE_GENERIC (scm_i_floor_divide, "floor/", 2, 0, 0,
  945. (SCM x, SCM y),
  946. "Return the integer @var{q} and the real number @var{r}\n"
  947. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  948. "and @math{@var{q} = floor(@var{x} / @var{y})}.\n"
  949. "@lisp\n"
  950. "(floor/ 123 10) @result{} 12 and 3\n"
  951. "(floor/ 123 -10) @result{} -13 and -7\n"
  952. "(floor/ -123 10) @result{} -13 and 7\n"
  953. "(floor/ -123 -10) @result{} 12 and -3\n"
  954. "(floor/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
  955. "(floor/ 16/3 -10/7) @result{} -4 and -8/21\n"
  956. "@end lisp")
  957. #define FUNC_NAME s_scm_i_floor_divide
  958. {
  959. SCM q, r;
  960. scm_floor_divide(x, y, &q, &r);
  961. return scm_values_2 (q, r);
  962. }
  963. #undef FUNC_NAME
  964. #define s_scm_floor_divide s_scm_i_floor_divide
  965. #define g_scm_floor_divide g_scm_i_floor_divide
  966. void
  967. scm_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  968. {
  969. if (SCM_I_INUMP (x))
  970. {
  971. if (SCM_I_INUMP (y))
  972. scm_integer_floor_divide_ii (SCM_I_INUM (x), SCM_I_INUM (y), qp, rp);
  973. else if (SCM_BIGP (y))
  974. scm_integer_floor_divide_iz (SCM_I_INUM (x), scm_bignum (y), qp, rp);
  975. else if (SCM_REALP (y))
  976. scm_i_inexact_floor_divide (SCM_I_INUM (x), SCM_REAL_VALUE (y), qp, rp);
  977. else if (SCM_FRACTIONP (y))
  978. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  979. else
  980. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  981. s_scm_floor_divide, qp, rp);
  982. }
  983. else if (SCM_BIGP (x))
  984. {
  985. if (SCM_I_INUMP (y))
  986. scm_integer_floor_divide_zi (scm_bignum (x), SCM_I_INUM (y), qp, rp);
  987. else if (SCM_BIGP (y))
  988. scm_integer_floor_divide_zz (scm_bignum (x), scm_bignum (y), qp, rp);
  989. else if (SCM_REALP (y))
  990. scm_i_inexact_floor_divide (scm_integer_to_double_z (scm_bignum (x)),
  991. SCM_REAL_VALUE (y),
  992. qp, rp);
  993. else if (SCM_FRACTIONP (y))
  994. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  995. else
  996. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  997. s_scm_floor_divide, qp, rp);
  998. }
  999. else if (SCM_REALP (x))
  1000. {
  1001. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1002. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1003. scm_i_inexact_floor_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  1004. qp, rp);
  1005. else
  1006. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1007. s_scm_floor_divide, qp, rp);
  1008. }
  1009. else if (SCM_FRACTIONP (x))
  1010. {
  1011. if (SCM_REALP (y))
  1012. scm_i_inexact_floor_divide
  1013. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  1014. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1015. scm_i_exact_rational_floor_divide (x, y, qp, rp);
  1016. else
  1017. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG2,
  1018. s_scm_floor_divide, qp, rp);
  1019. }
  1020. else
  1021. two_valued_wta_dispatch_2 (g_scm_floor_divide, x, y, SCM_ARG1,
  1022. s_scm_floor_divide, qp, rp);
  1023. }
  1024. static void
  1025. scm_i_inexact_floor_divide (double x, double y, SCM *qp, SCM *rp)
  1026. {
  1027. if (SCM_UNLIKELY (y == 0))
  1028. scm_num_overflow (s_scm_floor_divide); /* or return a NaN? */
  1029. else
  1030. {
  1031. double q = floor (x / y);
  1032. double r = x - q * y;
  1033. *qp = scm_i_from_double (q);
  1034. *rp = scm_i_from_double (r);
  1035. }
  1036. }
  1037. static void
  1038. scm_i_exact_rational_floor_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1039. {
  1040. SCM r1;
  1041. SCM xd = scm_denominator (x);
  1042. SCM yd = scm_denominator (y);
  1043. scm_floor_divide (scm_product (scm_numerator (x), yd),
  1044. scm_product (scm_numerator (y), xd),
  1045. qp, &r1);
  1046. *rp = scm_divide (r1, scm_product (xd, yd));
  1047. }
  1048. static SCM scm_i_inexact_ceiling_quotient (double x, double y);
  1049. static SCM scm_i_exact_rational_ceiling_quotient (SCM x, SCM y);
  1050. SCM_PRIMITIVE_GENERIC (scm_ceiling_quotient, "ceiling-quotient", 2, 0, 0,
  1051. (SCM x, SCM y),
  1052. "Return the ceiling of @math{@var{x} / @var{y}}.\n"
  1053. "@lisp\n"
  1054. "(ceiling-quotient 123 10) @result{} 13\n"
  1055. "(ceiling-quotient 123 -10) @result{} -12\n"
  1056. "(ceiling-quotient -123 10) @result{} -12\n"
  1057. "(ceiling-quotient -123 -10) @result{} 13\n"
  1058. "(ceiling-quotient -123.2 -63.5) @result{} 2.0\n"
  1059. "(ceiling-quotient 16/3 -10/7) @result{} -3\n"
  1060. "@end lisp")
  1061. #define FUNC_NAME s_scm_ceiling_quotient
  1062. {
  1063. if (SCM_I_INUMP (x))
  1064. {
  1065. if (SCM_I_INUMP (y))
  1066. return scm_integer_ceiling_quotient_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  1067. else if (SCM_BIGP (y))
  1068. return scm_integer_ceiling_quotient_iz (SCM_I_INUM (x), scm_bignum (y));
  1069. else if (SCM_REALP (y))
  1070. return scm_i_inexact_ceiling_quotient (SCM_I_INUM (x),
  1071. SCM_REAL_VALUE (y));
  1072. else if (SCM_FRACTIONP (y))
  1073. return scm_i_exact_rational_ceiling_quotient (x, y);
  1074. else
  1075. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1076. s_scm_ceiling_quotient);
  1077. }
  1078. else if (SCM_BIGP (x))
  1079. {
  1080. if (SCM_I_INUMP (y))
  1081. return scm_integer_ceiling_quotient_zi (scm_bignum (x), SCM_I_INUM (y));
  1082. else if (SCM_BIGP (y))
  1083. return scm_integer_ceiling_quotient_zz (scm_bignum (x), scm_bignum (y));
  1084. else if (SCM_REALP (y))
  1085. return scm_i_inexact_ceiling_quotient
  1086. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1087. else if (SCM_FRACTIONP (y))
  1088. return scm_i_exact_rational_ceiling_quotient (x, y);
  1089. else
  1090. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1091. s_scm_ceiling_quotient);
  1092. }
  1093. else if (SCM_REALP (x))
  1094. {
  1095. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1096. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1097. return scm_i_inexact_ceiling_quotient
  1098. (SCM_REAL_VALUE (x), scm_to_double (y));
  1099. else
  1100. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1101. s_scm_ceiling_quotient);
  1102. }
  1103. else if (SCM_FRACTIONP (x))
  1104. {
  1105. if (SCM_REALP (y))
  1106. return scm_i_inexact_ceiling_quotient
  1107. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1108. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1109. return scm_i_exact_rational_ceiling_quotient (x, y);
  1110. else
  1111. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG2,
  1112. s_scm_ceiling_quotient);
  1113. }
  1114. else
  1115. return scm_wta_dispatch_2 (g_scm_ceiling_quotient, x, y, SCM_ARG1,
  1116. s_scm_ceiling_quotient);
  1117. }
  1118. #undef FUNC_NAME
  1119. static SCM
  1120. scm_i_inexact_ceiling_quotient (double x, double y)
  1121. {
  1122. if (SCM_UNLIKELY (y == 0))
  1123. scm_num_overflow (s_scm_ceiling_quotient); /* or return a NaN? */
  1124. else
  1125. return scm_i_from_double (ceil (x / y));
  1126. }
  1127. static SCM
  1128. scm_i_exact_rational_ceiling_quotient (SCM x, SCM y)
  1129. {
  1130. return scm_ceiling_quotient
  1131. (scm_product (scm_numerator (x), scm_denominator (y)),
  1132. scm_product (scm_numerator (y), scm_denominator (x)));
  1133. }
  1134. static SCM scm_i_inexact_ceiling_remainder (double x, double y);
  1135. static SCM scm_i_exact_rational_ceiling_remainder (SCM x, SCM y);
  1136. SCM_PRIMITIVE_GENERIC (scm_ceiling_remainder, "ceiling-remainder", 2, 0, 0,
  1137. (SCM x, SCM y),
  1138. "Return the real number @var{r} such that\n"
  1139. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1140. "where @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
  1141. "@lisp\n"
  1142. "(ceiling-remainder 123 10) @result{} -7\n"
  1143. "(ceiling-remainder 123 -10) @result{} 3\n"
  1144. "(ceiling-remainder -123 10) @result{} -3\n"
  1145. "(ceiling-remainder -123 -10) @result{} 7\n"
  1146. "(ceiling-remainder -123.2 -63.5) @result{} 3.8\n"
  1147. "(ceiling-remainder 16/3 -10/7) @result{} 22/21\n"
  1148. "@end lisp")
  1149. #define FUNC_NAME s_scm_ceiling_remainder
  1150. {
  1151. if (SCM_I_INUMP (x))
  1152. {
  1153. if (SCM_I_INUMP (y))
  1154. return scm_integer_ceiling_remainder_ii (SCM_I_INUM (x),
  1155. SCM_I_INUM (y));
  1156. else if (SCM_BIGP (y))
  1157. return scm_integer_ceiling_remainder_iz (SCM_I_INUM (x),
  1158. scm_bignum (y));
  1159. else if (SCM_REALP (y))
  1160. return scm_i_inexact_ceiling_remainder (SCM_I_INUM (x),
  1161. SCM_REAL_VALUE (y));
  1162. else if (SCM_FRACTIONP (y))
  1163. return scm_i_exact_rational_ceiling_remainder (x, y);
  1164. else
  1165. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1166. s_scm_ceiling_remainder);
  1167. }
  1168. else if (SCM_BIGP (x))
  1169. {
  1170. if (SCM_I_INUMP (y))
  1171. return scm_integer_ceiling_remainder_zi (scm_bignum (x),
  1172. SCM_I_INUM (y));
  1173. else if (SCM_BIGP (y))
  1174. return scm_integer_ceiling_remainder_zz (scm_bignum (x),
  1175. scm_bignum (y));
  1176. else if (SCM_REALP (y))
  1177. return scm_i_inexact_ceiling_remainder
  1178. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1179. else if (SCM_FRACTIONP (y))
  1180. return scm_i_exact_rational_ceiling_remainder (x, y);
  1181. else
  1182. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1183. s_scm_ceiling_remainder);
  1184. }
  1185. else if (SCM_REALP (x))
  1186. {
  1187. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1188. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1189. return scm_i_inexact_ceiling_remainder
  1190. (SCM_REAL_VALUE (x), scm_to_double (y));
  1191. else
  1192. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1193. s_scm_ceiling_remainder);
  1194. }
  1195. else if (SCM_FRACTIONP (x))
  1196. {
  1197. if (SCM_REALP (y))
  1198. return scm_i_inexact_ceiling_remainder
  1199. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1200. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1201. return scm_i_exact_rational_ceiling_remainder (x, y);
  1202. else
  1203. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG2,
  1204. s_scm_ceiling_remainder);
  1205. }
  1206. else
  1207. return scm_wta_dispatch_2 (g_scm_ceiling_remainder, x, y, SCM_ARG1,
  1208. s_scm_ceiling_remainder);
  1209. }
  1210. #undef FUNC_NAME
  1211. static SCM
  1212. scm_i_inexact_ceiling_remainder (double x, double y)
  1213. {
  1214. /* Although it would be more efficient to use fmod here, we can't
  1215. because it would in some cases produce results inconsistent with
  1216. scm_i_inexact_ceiling_quotient, such that x != q * y + r (not even
  1217. close). In particular, when x is very close to a multiple of y,
  1218. then r might be either 0.0 or -y, but those two cases must
  1219. correspond to different choices of q. If r = 0.0 then q must be
  1220. x/y, and if r = -y then q must be x/y+1. If quotient chooses one
  1221. and remainder chooses the other, it would be bad. */
  1222. if (SCM_UNLIKELY (y == 0))
  1223. scm_num_overflow (s_scm_ceiling_remainder); /* or return a NaN? */
  1224. else
  1225. return scm_i_from_double (x - y * ceil (x / y));
  1226. }
  1227. static SCM
  1228. scm_i_exact_rational_ceiling_remainder (SCM x, SCM y)
  1229. {
  1230. SCM xd = scm_denominator (x);
  1231. SCM yd = scm_denominator (y);
  1232. SCM r1 = scm_ceiling_remainder (scm_product (scm_numerator (x), yd),
  1233. scm_product (scm_numerator (y), xd));
  1234. return scm_divide (r1, scm_product (xd, yd));
  1235. }
  1236. static void scm_i_inexact_ceiling_divide (double x, double y,
  1237. SCM *qp, SCM *rp);
  1238. static void scm_i_exact_rational_ceiling_divide (SCM x, SCM y,
  1239. SCM *qp, SCM *rp);
  1240. SCM_PRIMITIVE_GENERIC (scm_i_ceiling_divide, "ceiling/", 2, 0, 0,
  1241. (SCM x, SCM y),
  1242. "Return the integer @var{q} and the real number @var{r}\n"
  1243. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1244. "and @math{@var{q} = ceiling(@var{x} / @var{y})}.\n"
  1245. "@lisp\n"
  1246. "(ceiling/ 123 10) @result{} 13 and -7\n"
  1247. "(ceiling/ 123 -10) @result{} -12 and 3\n"
  1248. "(ceiling/ -123 10) @result{} -12 and -3\n"
  1249. "(ceiling/ -123 -10) @result{} 13 and 7\n"
  1250. "(ceiling/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  1251. "(ceiling/ 16/3 -10/7) @result{} -3 and 22/21\n"
  1252. "@end lisp")
  1253. #define FUNC_NAME s_scm_i_ceiling_divide
  1254. {
  1255. SCM q, r;
  1256. scm_ceiling_divide(x, y, &q, &r);
  1257. return scm_values_2 (q, r);
  1258. }
  1259. #undef FUNC_NAME
  1260. #define s_scm_ceiling_divide s_scm_i_ceiling_divide
  1261. #define g_scm_ceiling_divide g_scm_i_ceiling_divide
  1262. void
  1263. scm_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1264. {
  1265. if (SCM_I_INUMP (x))
  1266. {
  1267. if (SCM_I_INUMP (y))
  1268. scm_integer_ceiling_divide_ii (SCM_I_INUM (x), SCM_I_INUM (y), qp, rp);
  1269. else if (SCM_BIGP (y))
  1270. scm_integer_ceiling_divide_iz (SCM_I_INUM (x), scm_bignum (y), qp, rp);
  1271. else if (SCM_REALP (y))
  1272. scm_i_inexact_ceiling_divide (SCM_I_INUM (x), SCM_REAL_VALUE (y), qp, rp);
  1273. else if (SCM_FRACTIONP (y))
  1274. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  1275. else
  1276. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  1277. s_scm_ceiling_divide, qp, rp);
  1278. }
  1279. else if (SCM_BIGP (x))
  1280. {
  1281. if (SCM_I_INUMP (y))
  1282. scm_integer_ceiling_divide_zi (scm_bignum (x), SCM_I_INUM (y), qp, rp);
  1283. else if (SCM_BIGP (y))
  1284. scm_integer_ceiling_divide_zz (scm_bignum (x), scm_bignum (y), qp, rp);
  1285. else if (SCM_REALP (y))
  1286. scm_i_inexact_ceiling_divide (scm_integer_to_double_z (scm_bignum (x)),
  1287. SCM_REAL_VALUE (y), qp, rp);
  1288. else if (SCM_FRACTIONP (y))
  1289. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  1290. else
  1291. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  1292. s_scm_ceiling_divide, qp, rp);
  1293. }
  1294. else if (SCM_REALP (x))
  1295. {
  1296. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1297. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1298. scm_i_inexact_ceiling_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  1299. qp, rp);
  1300. else
  1301. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  1302. s_scm_ceiling_divide, qp, rp);
  1303. }
  1304. else if (SCM_FRACTIONP (x))
  1305. {
  1306. if (SCM_REALP (y))
  1307. scm_i_inexact_ceiling_divide
  1308. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  1309. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1310. scm_i_exact_rational_ceiling_divide (x, y, qp, rp);
  1311. else
  1312. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG2,
  1313. s_scm_ceiling_divide, qp, rp);
  1314. }
  1315. else
  1316. two_valued_wta_dispatch_2 (g_scm_ceiling_divide, x, y, SCM_ARG1,
  1317. s_scm_ceiling_divide, qp, rp);
  1318. }
  1319. static void
  1320. scm_i_inexact_ceiling_divide (double x, double y, SCM *qp, SCM *rp)
  1321. {
  1322. if (SCM_UNLIKELY (y == 0))
  1323. scm_num_overflow (s_scm_ceiling_divide); /* or return a NaN? */
  1324. else
  1325. {
  1326. double q = ceil (x / y);
  1327. double r = x - q * y;
  1328. *qp = scm_i_from_double (q);
  1329. *rp = scm_i_from_double (r);
  1330. }
  1331. }
  1332. static void
  1333. scm_i_exact_rational_ceiling_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1334. {
  1335. SCM r1;
  1336. SCM xd = scm_denominator (x);
  1337. SCM yd = scm_denominator (y);
  1338. scm_ceiling_divide (scm_product (scm_numerator (x), yd),
  1339. scm_product (scm_numerator (y), xd),
  1340. qp, &r1);
  1341. *rp = scm_divide (r1, scm_product (xd, yd));
  1342. }
  1343. static SCM scm_i_inexact_truncate_quotient (double x, double y);
  1344. static SCM scm_i_exact_rational_truncate_quotient (SCM x, SCM y);
  1345. SCM_PRIMITIVE_GENERIC (scm_truncate_quotient, "truncate-quotient", 2, 0, 0,
  1346. (SCM x, SCM y),
  1347. "Return @math{@var{x} / @var{y}} rounded toward zero.\n"
  1348. "@lisp\n"
  1349. "(truncate-quotient 123 10) @result{} 12\n"
  1350. "(truncate-quotient 123 -10) @result{} -12\n"
  1351. "(truncate-quotient -123 10) @result{} -12\n"
  1352. "(truncate-quotient -123 -10) @result{} 12\n"
  1353. "(truncate-quotient -123.2 -63.5) @result{} 1.0\n"
  1354. "(truncate-quotient 16/3 -10/7) @result{} -3\n"
  1355. "@end lisp")
  1356. #define FUNC_NAME s_scm_truncate_quotient
  1357. {
  1358. if (SCM_I_INUMP (x))
  1359. {
  1360. if (SCM_I_INUMP (y))
  1361. return scm_integer_truncate_quotient_ii (SCM_I_INUM (x),
  1362. SCM_I_INUM (y));
  1363. else if (SCM_BIGP (y))
  1364. return scm_integer_truncate_quotient_iz (SCM_I_INUM (x),
  1365. scm_bignum (y));
  1366. else if (SCM_REALP (y))
  1367. return scm_i_inexact_truncate_quotient (SCM_I_INUM (x),
  1368. SCM_REAL_VALUE (y));
  1369. else if (SCM_FRACTIONP (y))
  1370. return scm_i_exact_rational_truncate_quotient (x, y);
  1371. else
  1372. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  1373. s_scm_truncate_quotient);
  1374. }
  1375. else if (SCM_BIGP (x))
  1376. {
  1377. if (SCM_I_INUMP (y))
  1378. return scm_integer_truncate_quotient_zi (scm_bignum (x),
  1379. SCM_I_INUM (y));
  1380. else if (SCM_BIGP (y))
  1381. return scm_integer_truncate_quotient_zz (scm_bignum (x),
  1382. scm_bignum (y));
  1383. else if (SCM_REALP (y))
  1384. return scm_i_inexact_truncate_quotient
  1385. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1386. else if (SCM_FRACTIONP (y))
  1387. return scm_i_exact_rational_truncate_quotient (x, y);
  1388. else
  1389. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  1390. s_scm_truncate_quotient);
  1391. }
  1392. else if (SCM_REALP (x))
  1393. {
  1394. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1395. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1396. return scm_i_inexact_truncate_quotient
  1397. (SCM_REAL_VALUE (x), scm_to_double (y));
  1398. else
  1399. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  1400. s_scm_truncate_quotient);
  1401. }
  1402. else if (SCM_FRACTIONP (x))
  1403. {
  1404. if (SCM_REALP (y))
  1405. return scm_i_inexact_truncate_quotient
  1406. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1407. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1408. return scm_i_exact_rational_truncate_quotient (x, y);
  1409. else
  1410. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG2,
  1411. s_scm_truncate_quotient);
  1412. }
  1413. else
  1414. return scm_wta_dispatch_2 (g_scm_truncate_quotient, x, y, SCM_ARG1,
  1415. s_scm_truncate_quotient);
  1416. }
  1417. #undef FUNC_NAME
  1418. static SCM
  1419. scm_i_inexact_truncate_quotient (double x, double y)
  1420. {
  1421. if (SCM_UNLIKELY (y == 0))
  1422. scm_num_overflow (s_scm_truncate_quotient); /* or return a NaN? */
  1423. else
  1424. return scm_i_from_double (trunc (x / y));
  1425. }
  1426. static SCM
  1427. scm_i_exact_rational_truncate_quotient (SCM x, SCM y)
  1428. {
  1429. return scm_truncate_quotient
  1430. (scm_product (scm_numerator (x), scm_denominator (y)),
  1431. scm_product (scm_numerator (y), scm_denominator (x)));
  1432. }
  1433. static SCM scm_i_inexact_truncate_remainder (double x, double y);
  1434. static SCM scm_i_exact_rational_truncate_remainder (SCM x, SCM y);
  1435. SCM_PRIMITIVE_GENERIC (scm_truncate_remainder, "truncate-remainder", 2, 0, 0,
  1436. (SCM x, SCM y),
  1437. "Return the real number @var{r} such that\n"
  1438. "@math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1439. "where @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
  1440. "@lisp\n"
  1441. "(truncate-remainder 123 10) @result{} 3\n"
  1442. "(truncate-remainder 123 -10) @result{} 3\n"
  1443. "(truncate-remainder -123 10) @result{} -3\n"
  1444. "(truncate-remainder -123 -10) @result{} -3\n"
  1445. "(truncate-remainder -123.2 -63.5) @result{} -59.7\n"
  1446. "(truncate-remainder 16/3 -10/7) @result{} 22/21\n"
  1447. "@end lisp")
  1448. #define FUNC_NAME s_scm_truncate_remainder
  1449. {
  1450. if (SCM_I_INUMP (x))
  1451. {
  1452. if (SCM_I_INUMP (y))
  1453. return scm_integer_truncate_remainder_ii (SCM_I_INUM (x),
  1454. SCM_I_INUM (y));
  1455. else if (SCM_BIGP (y))
  1456. return scm_integer_truncate_remainder_iz (SCM_I_INUM (x),
  1457. scm_bignum (y));
  1458. else if (SCM_REALP (y))
  1459. return scm_i_inexact_truncate_remainder (SCM_I_INUM (x),
  1460. SCM_REAL_VALUE (y));
  1461. else if (SCM_FRACTIONP (y))
  1462. return scm_i_exact_rational_truncate_remainder (x, y);
  1463. else
  1464. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  1465. s_scm_truncate_remainder);
  1466. }
  1467. else if (SCM_BIGP (x))
  1468. {
  1469. if (SCM_I_INUMP (y))
  1470. return scm_integer_truncate_remainder_zi (scm_bignum (x),
  1471. SCM_I_INUM (y));
  1472. else if (SCM_BIGP (y))
  1473. return scm_integer_truncate_remainder_zz (scm_bignum (x),
  1474. scm_bignum (y));
  1475. else if (SCM_REALP (y))
  1476. return scm_i_inexact_truncate_remainder
  1477. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1478. else if (SCM_FRACTIONP (y))
  1479. return scm_i_exact_rational_truncate_remainder (x, y);
  1480. else
  1481. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  1482. s_scm_truncate_remainder);
  1483. }
  1484. else if (SCM_REALP (x))
  1485. {
  1486. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1487. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1488. return scm_i_inexact_truncate_remainder
  1489. (SCM_REAL_VALUE (x), scm_to_double (y));
  1490. else
  1491. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  1492. s_scm_truncate_remainder);
  1493. }
  1494. else if (SCM_FRACTIONP (x))
  1495. {
  1496. if (SCM_REALP (y))
  1497. return scm_i_inexact_truncate_remainder
  1498. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1499. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1500. return scm_i_exact_rational_truncate_remainder (x, y);
  1501. else
  1502. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG2,
  1503. s_scm_truncate_remainder);
  1504. }
  1505. else
  1506. return scm_wta_dispatch_2 (g_scm_truncate_remainder, x, y, SCM_ARG1,
  1507. s_scm_truncate_remainder);
  1508. }
  1509. #undef FUNC_NAME
  1510. static SCM
  1511. scm_i_inexact_truncate_remainder (double x, double y)
  1512. {
  1513. /* Although it would be more efficient to use fmod here, we can't
  1514. because it would in some cases produce results inconsistent with
  1515. scm_i_inexact_truncate_quotient, such that x != q * y + r (not even
  1516. close). In particular, when x is very close to a multiple of y,
  1517. then r might be either 0.0 or sgn(x)*|y|, but those two cases must
  1518. correspond to different choices of q. If quotient chooses one and
  1519. remainder chooses the other, it would be bad. */
  1520. if (SCM_UNLIKELY (y == 0))
  1521. scm_num_overflow (s_scm_truncate_remainder); /* or return a NaN? */
  1522. else
  1523. return scm_i_from_double (x - y * trunc (x / y));
  1524. }
  1525. static SCM
  1526. scm_i_exact_rational_truncate_remainder (SCM x, SCM y)
  1527. {
  1528. SCM xd = scm_denominator (x);
  1529. SCM yd = scm_denominator (y);
  1530. SCM r1 = scm_truncate_remainder (scm_product (scm_numerator (x), yd),
  1531. scm_product (scm_numerator (y), xd));
  1532. return scm_divide (r1, scm_product (xd, yd));
  1533. }
  1534. static void scm_i_inexact_truncate_divide (double x, double y,
  1535. SCM *qp, SCM *rp);
  1536. static void scm_i_exact_rational_truncate_divide (SCM x, SCM y,
  1537. SCM *qp, SCM *rp);
  1538. SCM_PRIMITIVE_GENERIC (scm_i_truncate_divide, "truncate/", 2, 0, 0,
  1539. (SCM x, SCM y),
  1540. "Return the integer @var{q} and the real number @var{r}\n"
  1541. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1542. "and @math{@var{q} = truncate(@var{x} / @var{y})}.\n"
  1543. "@lisp\n"
  1544. "(truncate/ 123 10) @result{} 12 and 3\n"
  1545. "(truncate/ 123 -10) @result{} -12 and 3\n"
  1546. "(truncate/ -123 10) @result{} -12 and -3\n"
  1547. "(truncate/ -123 -10) @result{} 12 and -3\n"
  1548. "(truncate/ -123.2 -63.5) @result{} 1.0 and -59.7\n"
  1549. "(truncate/ 16/3 -10/7) @result{} -3 and 22/21\n"
  1550. "@end lisp")
  1551. #define FUNC_NAME s_scm_i_truncate_divide
  1552. {
  1553. SCM q, r;
  1554. scm_truncate_divide(x, y, &q, &r);
  1555. return scm_values_2 (q, r);
  1556. }
  1557. #undef FUNC_NAME
  1558. #define s_scm_truncate_divide s_scm_i_truncate_divide
  1559. #define g_scm_truncate_divide g_scm_i_truncate_divide
  1560. void
  1561. scm_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1562. {
  1563. if (SCM_I_INUMP (x))
  1564. {
  1565. if (SCM_I_INUMP (y))
  1566. scm_integer_truncate_divide_ii (SCM_I_INUM (x), SCM_I_INUM (y),
  1567. qp, rp);
  1568. else if (SCM_BIGP (y))
  1569. scm_integer_truncate_divide_iz (SCM_I_INUM (x), scm_bignum (y),
  1570. qp, rp);
  1571. else if (SCM_REALP (y))
  1572. scm_i_inexact_truncate_divide (SCM_I_INUM (x), SCM_REAL_VALUE (y),
  1573. qp, rp);
  1574. else if (SCM_FRACTIONP (y))
  1575. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  1576. else
  1577. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  1578. s_scm_truncate_divide, qp, rp);
  1579. }
  1580. else if (SCM_BIGP (x))
  1581. {
  1582. if (SCM_I_INUMP (y))
  1583. scm_integer_truncate_divide_zi (scm_bignum (x), SCM_I_INUM (y), qp, rp);
  1584. else if (SCM_BIGP (y))
  1585. scm_integer_truncate_divide_zz (scm_bignum (x), scm_bignum (y), qp, rp);
  1586. else if (SCM_REALP (y))
  1587. scm_i_inexact_truncate_divide (scm_integer_to_double_z (scm_bignum (x)),
  1588. SCM_REAL_VALUE (y), qp, rp);
  1589. else if (SCM_FRACTIONP (y))
  1590. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  1591. else
  1592. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  1593. s_scm_truncate_divide, qp, rp);
  1594. }
  1595. else if (SCM_REALP (x))
  1596. {
  1597. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1598. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1599. scm_i_inexact_truncate_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  1600. qp, rp);
  1601. else
  1602. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  1603. s_scm_truncate_divide, qp, rp);
  1604. }
  1605. else if (SCM_FRACTIONP (x))
  1606. {
  1607. if (SCM_REALP (y))
  1608. scm_i_inexact_truncate_divide
  1609. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  1610. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1611. scm_i_exact_rational_truncate_divide (x, y, qp, rp);
  1612. else
  1613. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG2,
  1614. s_scm_truncate_divide, qp, rp);
  1615. }
  1616. else
  1617. two_valued_wta_dispatch_2 (g_scm_truncate_divide, x, y, SCM_ARG1,
  1618. s_scm_truncate_divide, qp, rp);
  1619. }
  1620. static void
  1621. scm_i_inexact_truncate_divide (double x, double y, SCM *qp, SCM *rp)
  1622. {
  1623. if (SCM_UNLIKELY (y == 0))
  1624. scm_num_overflow (s_scm_truncate_divide); /* or return a NaN? */
  1625. else
  1626. {
  1627. double q = trunc (x / y);
  1628. double r = x - q * y;
  1629. *qp = scm_i_from_double (q);
  1630. *rp = scm_i_from_double (r);
  1631. }
  1632. }
  1633. static void
  1634. scm_i_exact_rational_truncate_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1635. {
  1636. SCM r1;
  1637. SCM xd = scm_denominator (x);
  1638. SCM yd = scm_denominator (y);
  1639. scm_truncate_divide (scm_product (scm_numerator (x), yd),
  1640. scm_product (scm_numerator (y), xd),
  1641. qp, &r1);
  1642. *rp = scm_divide (r1, scm_product (xd, yd));
  1643. }
  1644. static SCM scm_i_inexact_centered_quotient (double x, double y);
  1645. static SCM scm_i_exact_rational_centered_quotient (SCM x, SCM y);
  1646. SCM_PRIMITIVE_GENERIC (scm_centered_quotient, "centered-quotient", 2, 0, 0,
  1647. (SCM x, SCM y),
  1648. "Return the integer @var{q} such that\n"
  1649. "@math{@var{x} = @var{q}*@var{y} + @var{r}} where\n"
  1650. "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
  1651. "@lisp\n"
  1652. "(centered-quotient 123 10) @result{} 12\n"
  1653. "(centered-quotient 123 -10) @result{} -12\n"
  1654. "(centered-quotient -123 10) @result{} -12\n"
  1655. "(centered-quotient -123 -10) @result{} 12\n"
  1656. "(centered-quotient -123.2 -63.5) @result{} 2.0\n"
  1657. "(centered-quotient 16/3 -10/7) @result{} -4\n"
  1658. "@end lisp")
  1659. #define FUNC_NAME s_scm_centered_quotient
  1660. {
  1661. if (SCM_I_INUMP (x))
  1662. {
  1663. if (SCM_I_INUMP (y))
  1664. return scm_integer_centered_quotient_ii (SCM_I_INUM (x),
  1665. SCM_I_INUM (y));
  1666. else if (SCM_BIGP (y))
  1667. return scm_integer_centered_quotient_iz (SCM_I_INUM (x), scm_bignum (y));
  1668. else if (SCM_REALP (y))
  1669. return scm_i_inexact_centered_quotient (SCM_I_INUM (x),
  1670. SCM_REAL_VALUE (y));
  1671. else if (SCM_FRACTIONP (y))
  1672. return scm_i_exact_rational_centered_quotient (x, y);
  1673. else
  1674. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  1675. s_scm_centered_quotient);
  1676. }
  1677. else if (SCM_BIGP (x))
  1678. {
  1679. if (SCM_I_INUMP (y))
  1680. return scm_integer_centered_quotient_zi (scm_bignum (x),
  1681. SCM_I_INUM (y));
  1682. else if (SCM_BIGP (y))
  1683. return scm_integer_centered_quotient_zz (scm_bignum (x),
  1684. scm_bignum (y));
  1685. else if (SCM_REALP (y))
  1686. return scm_i_inexact_centered_quotient
  1687. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1688. else if (SCM_FRACTIONP (y))
  1689. return scm_i_exact_rational_centered_quotient (x, y);
  1690. else
  1691. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  1692. s_scm_centered_quotient);
  1693. }
  1694. else if (SCM_REALP (x))
  1695. {
  1696. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1697. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1698. return scm_i_inexact_centered_quotient
  1699. (SCM_REAL_VALUE (x), scm_to_double (y));
  1700. else
  1701. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  1702. s_scm_centered_quotient);
  1703. }
  1704. else if (SCM_FRACTIONP (x))
  1705. {
  1706. if (SCM_REALP (y))
  1707. return scm_i_inexact_centered_quotient
  1708. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1709. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1710. return scm_i_exact_rational_centered_quotient (x, y);
  1711. else
  1712. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG2,
  1713. s_scm_centered_quotient);
  1714. }
  1715. else
  1716. return scm_wta_dispatch_2 (g_scm_centered_quotient, x, y, SCM_ARG1,
  1717. s_scm_centered_quotient);
  1718. }
  1719. #undef FUNC_NAME
  1720. static SCM
  1721. scm_i_inexact_centered_quotient (double x, double y)
  1722. {
  1723. if (SCM_LIKELY (y > 0))
  1724. return scm_i_from_double (floor (x/y + 0.5));
  1725. else if (SCM_LIKELY (y < 0))
  1726. return scm_i_from_double (ceil (x/y - 0.5));
  1727. else if (y == 0)
  1728. scm_num_overflow (s_scm_centered_quotient); /* or return a NaN? */
  1729. else
  1730. return scm_nan ();
  1731. }
  1732. static SCM
  1733. scm_i_exact_rational_centered_quotient (SCM x, SCM y)
  1734. {
  1735. return scm_centered_quotient
  1736. (scm_product (scm_numerator (x), scm_denominator (y)),
  1737. scm_product (scm_numerator (y), scm_denominator (x)));
  1738. }
  1739. static SCM scm_i_inexact_centered_remainder (double x, double y);
  1740. static SCM scm_i_exact_rational_centered_remainder (SCM x, SCM y);
  1741. SCM_PRIMITIVE_GENERIC (scm_centered_remainder, "centered-remainder", 2, 0, 0,
  1742. (SCM x, SCM y),
  1743. "Return the real number @var{r} such that\n"
  1744. "@math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}\n"
  1745. "and @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1746. "for some integer @var{q}.\n"
  1747. "@lisp\n"
  1748. "(centered-remainder 123 10) @result{} 3\n"
  1749. "(centered-remainder 123 -10) @result{} 3\n"
  1750. "(centered-remainder -123 10) @result{} -3\n"
  1751. "(centered-remainder -123 -10) @result{} -3\n"
  1752. "(centered-remainder -123.2 -63.5) @result{} 3.8\n"
  1753. "(centered-remainder 16/3 -10/7) @result{} -8/21\n"
  1754. "@end lisp")
  1755. #define FUNC_NAME s_scm_centered_remainder
  1756. {
  1757. if (SCM_I_INUMP (x))
  1758. {
  1759. if (SCM_I_INUMP (y))
  1760. return scm_integer_centered_remainder_ii (SCM_I_INUM (x),
  1761. SCM_I_INUM (y));
  1762. else if (SCM_BIGP (y))
  1763. return scm_integer_centered_remainder_iz (SCM_I_INUM (x),
  1764. scm_bignum (y));
  1765. else if (SCM_REALP (y))
  1766. return scm_i_inexact_centered_remainder (SCM_I_INUM (x),
  1767. SCM_REAL_VALUE (y));
  1768. else if (SCM_FRACTIONP (y))
  1769. return scm_i_exact_rational_centered_remainder (x, y);
  1770. else
  1771. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  1772. s_scm_centered_remainder);
  1773. }
  1774. else if (SCM_BIGP (x))
  1775. {
  1776. if (SCM_I_INUMP (y))
  1777. return scm_integer_centered_remainder_zi (scm_bignum (x),
  1778. SCM_I_INUM (y));
  1779. else if (SCM_BIGP (y))
  1780. return scm_integer_centered_remainder_zz (scm_bignum (x),
  1781. scm_bignum (y));
  1782. else if (SCM_REALP (y))
  1783. return scm_i_inexact_centered_remainder
  1784. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  1785. else if (SCM_FRACTIONP (y))
  1786. return scm_i_exact_rational_centered_remainder (x, y);
  1787. else
  1788. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  1789. s_scm_centered_remainder);
  1790. }
  1791. else if (SCM_REALP (x))
  1792. {
  1793. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1794. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1795. return scm_i_inexact_centered_remainder
  1796. (SCM_REAL_VALUE (x), scm_to_double (y));
  1797. else
  1798. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  1799. s_scm_centered_remainder);
  1800. }
  1801. else if (SCM_FRACTIONP (x))
  1802. {
  1803. if (SCM_REALP (y))
  1804. return scm_i_inexact_centered_remainder
  1805. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  1806. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1807. return scm_i_exact_rational_centered_remainder (x, y);
  1808. else
  1809. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG2,
  1810. s_scm_centered_remainder);
  1811. }
  1812. else
  1813. return scm_wta_dispatch_2 (g_scm_centered_remainder, x, y, SCM_ARG1,
  1814. s_scm_centered_remainder);
  1815. }
  1816. #undef FUNC_NAME
  1817. static SCM
  1818. scm_i_inexact_centered_remainder (double x, double y)
  1819. {
  1820. double q;
  1821. /* Although it would be more efficient to use fmod here, we can't
  1822. because it would in some cases produce results inconsistent with
  1823. scm_i_inexact_centered_quotient, such that x != r + q * y (not even
  1824. close). In particular, when x-y/2 is very close to a multiple of
  1825. y, then r might be either -abs(y/2) or abs(y/2)-epsilon, but those
  1826. two cases must correspond to different choices of q. If quotient
  1827. chooses one and remainder chooses the other, it would be bad. */
  1828. if (SCM_LIKELY (y > 0))
  1829. q = floor (x/y + 0.5);
  1830. else if (SCM_LIKELY (y < 0))
  1831. q = ceil (x/y - 0.5);
  1832. else if (y == 0)
  1833. scm_num_overflow (s_scm_centered_remainder); /* or return a NaN? */
  1834. else
  1835. return scm_nan ();
  1836. return scm_i_from_double (x - q * y);
  1837. }
  1838. static SCM
  1839. scm_i_exact_rational_centered_remainder (SCM x, SCM y)
  1840. {
  1841. SCM xd = scm_denominator (x);
  1842. SCM yd = scm_denominator (y);
  1843. SCM r1 = scm_centered_remainder (scm_product (scm_numerator (x), yd),
  1844. scm_product (scm_numerator (y), xd));
  1845. return scm_divide (r1, scm_product (xd, yd));
  1846. }
  1847. static void scm_i_inexact_centered_divide (double x, double y,
  1848. SCM *qp, SCM *rp);
  1849. static void scm_i_exact_rational_centered_divide (SCM x, SCM y,
  1850. SCM *qp, SCM *rp);
  1851. SCM_PRIMITIVE_GENERIC (scm_i_centered_divide, "centered/", 2, 0, 0,
  1852. (SCM x, SCM y),
  1853. "Return the integer @var{q} and the real number @var{r}\n"
  1854. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  1855. "and @math{-abs(@var{y}/2) <= @var{r} < abs(@var{y}/2)}.\n"
  1856. "@lisp\n"
  1857. "(centered/ 123 10) @result{} 12 and 3\n"
  1858. "(centered/ 123 -10) @result{} -12 and 3\n"
  1859. "(centered/ -123 10) @result{} -12 and -3\n"
  1860. "(centered/ -123 -10) @result{} 12 and -3\n"
  1861. "(centered/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  1862. "(centered/ 16/3 -10/7) @result{} -4 and -8/21\n"
  1863. "@end lisp")
  1864. #define FUNC_NAME s_scm_i_centered_divide
  1865. {
  1866. SCM q, r;
  1867. scm_centered_divide(x, y, &q, &r);
  1868. return scm_values_2 (q, r);
  1869. }
  1870. #undef FUNC_NAME
  1871. #define s_scm_centered_divide s_scm_i_centered_divide
  1872. #define g_scm_centered_divide g_scm_i_centered_divide
  1873. void
  1874. scm_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1875. {
  1876. if (SCM_I_INUMP (x))
  1877. {
  1878. if (SCM_I_INUMP (y))
  1879. scm_integer_centered_divide_ii (SCM_I_INUM (x), SCM_I_INUM (y), qp, rp);
  1880. else if (SCM_BIGP (y))
  1881. scm_integer_centered_divide_iz (SCM_I_INUM (x), scm_bignum (y), qp, rp);
  1882. else if (SCM_REALP (y))
  1883. scm_i_inexact_centered_divide (SCM_I_INUM (x), SCM_REAL_VALUE (y),
  1884. qp, rp);
  1885. else if (SCM_FRACTIONP (y))
  1886. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  1887. else
  1888. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  1889. s_scm_centered_divide, qp, rp);
  1890. }
  1891. else if (SCM_BIGP (x))
  1892. {
  1893. if (SCM_I_INUMP (y))
  1894. scm_integer_centered_divide_zi (scm_bignum (x), SCM_I_INUM (y), qp, rp);
  1895. else if (SCM_BIGP (y))
  1896. scm_integer_centered_divide_zz (scm_bignum (x), scm_bignum (y), qp, rp);
  1897. else if (SCM_REALP (y))
  1898. scm_i_inexact_centered_divide (scm_integer_to_double_z (scm_bignum (x)),
  1899. SCM_REAL_VALUE (y), qp, rp);
  1900. else if (SCM_FRACTIONP (y))
  1901. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  1902. else
  1903. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  1904. s_scm_centered_divide, qp, rp);
  1905. }
  1906. else if (SCM_REALP (x))
  1907. {
  1908. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  1909. SCM_BIGP (y) || SCM_FRACTIONP (y))
  1910. scm_i_inexact_centered_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  1911. qp, rp);
  1912. else
  1913. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  1914. s_scm_centered_divide, qp, rp);
  1915. }
  1916. else if (SCM_FRACTIONP (x))
  1917. {
  1918. if (SCM_REALP (y))
  1919. scm_i_inexact_centered_divide
  1920. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  1921. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  1922. scm_i_exact_rational_centered_divide (x, y, qp, rp);
  1923. else
  1924. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG2,
  1925. s_scm_centered_divide, qp, rp);
  1926. }
  1927. else
  1928. two_valued_wta_dispatch_2 (g_scm_centered_divide, x, y, SCM_ARG1,
  1929. s_scm_centered_divide, qp, rp);
  1930. }
  1931. static void
  1932. scm_i_inexact_centered_divide (double x, double y, SCM *qp, SCM *rp)
  1933. {
  1934. double q, r;
  1935. if (SCM_LIKELY (y > 0))
  1936. q = floor (x/y + 0.5);
  1937. else if (SCM_LIKELY (y < 0))
  1938. q = ceil (x/y - 0.5);
  1939. else if (y == 0)
  1940. scm_num_overflow (s_scm_centered_divide); /* or return a NaN? */
  1941. else
  1942. q = guile_NaN;
  1943. r = x - q * y;
  1944. *qp = scm_i_from_double (q);
  1945. *rp = scm_i_from_double (r);
  1946. }
  1947. static void
  1948. scm_i_exact_rational_centered_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  1949. {
  1950. SCM r1;
  1951. SCM xd = scm_denominator (x);
  1952. SCM yd = scm_denominator (y);
  1953. scm_centered_divide (scm_product (scm_numerator (x), yd),
  1954. scm_product (scm_numerator (y), xd),
  1955. qp, &r1);
  1956. *rp = scm_divide (r1, scm_product (xd, yd));
  1957. }
  1958. static SCM scm_i_inexact_round_quotient (double x, double y);
  1959. static SCM scm_i_exact_rational_round_quotient (SCM x, SCM y);
  1960. SCM_PRIMITIVE_GENERIC (scm_round_quotient, "round-quotient", 2, 0, 0,
  1961. (SCM x, SCM y),
  1962. "Return @math{@var{x} / @var{y}} to the nearest integer,\n"
  1963. "with ties going to the nearest even integer.\n"
  1964. "@lisp\n"
  1965. "(round-quotient 123 10) @result{} 12\n"
  1966. "(round-quotient 123 -10) @result{} -12\n"
  1967. "(round-quotient -123 10) @result{} -12\n"
  1968. "(round-quotient -123 -10) @result{} 12\n"
  1969. "(round-quotient 125 10) @result{} 12\n"
  1970. "(round-quotient 127 10) @result{} 13\n"
  1971. "(round-quotient 135 10) @result{} 14\n"
  1972. "(round-quotient -123.2 -63.5) @result{} 2.0\n"
  1973. "(round-quotient 16/3 -10/7) @result{} -4\n"
  1974. "@end lisp")
  1975. #define FUNC_NAME s_scm_round_quotient
  1976. {
  1977. if (SCM_I_INUMP (x))
  1978. {
  1979. if (SCM_I_INUMP (y))
  1980. return scm_integer_round_quotient_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  1981. else if (SCM_BIGP (y))
  1982. return scm_integer_round_quotient_iz (SCM_I_INUM (x), scm_bignum (y));
  1983. else if (SCM_REALP (y))
  1984. return scm_i_inexact_round_quotient (SCM_I_INUM (x),
  1985. SCM_REAL_VALUE (y));
  1986. else if (SCM_FRACTIONP (y))
  1987. return scm_i_exact_rational_round_quotient (x, y);
  1988. else
  1989. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  1990. s_scm_round_quotient);
  1991. }
  1992. else if (SCM_BIGP (x))
  1993. {
  1994. if (SCM_I_INUMP (y))
  1995. return scm_integer_round_quotient_zi (scm_bignum (x), SCM_I_INUM (y));
  1996. else if (SCM_BIGP (y))
  1997. return scm_integer_round_quotient_zz (scm_bignum (x), scm_bignum (y));
  1998. else if (SCM_REALP (y))
  1999. return scm_i_inexact_round_quotient
  2000. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  2001. else if (SCM_FRACTIONP (y))
  2002. return scm_i_exact_rational_round_quotient (x, y);
  2003. else
  2004. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  2005. s_scm_round_quotient);
  2006. }
  2007. else if (SCM_REALP (x))
  2008. {
  2009. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2010. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2011. return scm_i_inexact_round_quotient
  2012. (SCM_REAL_VALUE (x), scm_to_double (y));
  2013. else
  2014. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  2015. s_scm_round_quotient);
  2016. }
  2017. else if (SCM_FRACTIONP (x))
  2018. {
  2019. if (SCM_REALP (y))
  2020. return scm_i_inexact_round_quotient
  2021. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2022. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2023. return scm_i_exact_rational_round_quotient (x, y);
  2024. else
  2025. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG2,
  2026. s_scm_round_quotient);
  2027. }
  2028. else
  2029. return scm_wta_dispatch_2 (g_scm_round_quotient, x, y, SCM_ARG1,
  2030. s_scm_round_quotient);
  2031. }
  2032. #undef FUNC_NAME
  2033. static SCM
  2034. scm_i_inexact_round_quotient (double x, double y)
  2035. {
  2036. if (SCM_UNLIKELY (y == 0))
  2037. scm_num_overflow (s_scm_round_quotient); /* or return a NaN? */
  2038. else
  2039. return scm_i_from_double (scm_c_round (x / y));
  2040. }
  2041. static SCM
  2042. scm_i_exact_rational_round_quotient (SCM x, SCM y)
  2043. {
  2044. return scm_round_quotient
  2045. (scm_product (scm_numerator (x), scm_denominator (y)),
  2046. scm_product (scm_numerator (y), scm_denominator (x)));
  2047. }
  2048. static SCM scm_i_inexact_round_remainder (double x, double y);
  2049. static SCM scm_i_exact_rational_round_remainder (SCM x, SCM y);
  2050. SCM_PRIMITIVE_GENERIC (scm_round_remainder, "round-remainder", 2, 0, 0,
  2051. (SCM x, SCM y),
  2052. "Return the real number @var{r} such that\n"
  2053. "@math{@var{x} = @var{q}*@var{y} + @var{r}}, where\n"
  2054. "@var{q} is @math{@var{x} / @var{y}} rounded to the\n"
  2055. "nearest integer, with ties going to the nearest\n"
  2056. "even integer.\n"
  2057. "@lisp\n"
  2058. "(round-remainder 123 10) @result{} 3\n"
  2059. "(round-remainder 123 -10) @result{} 3\n"
  2060. "(round-remainder -123 10) @result{} -3\n"
  2061. "(round-remainder -123 -10) @result{} -3\n"
  2062. "(round-remainder 125 10) @result{} 5\n"
  2063. "(round-remainder 127 10) @result{} -3\n"
  2064. "(round-remainder 135 10) @result{} -5\n"
  2065. "(round-remainder -123.2 -63.5) @result{} 3.8\n"
  2066. "(round-remainder 16/3 -10/7) @result{} -8/21\n"
  2067. "@end lisp")
  2068. #define FUNC_NAME s_scm_round_remainder
  2069. {
  2070. if (SCM_I_INUMP (x))
  2071. {
  2072. if (SCM_I_INUMP (y))
  2073. return scm_integer_round_remainder_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  2074. else if (SCM_BIGP (y))
  2075. return scm_integer_round_remainder_iz (SCM_I_INUM (x), scm_bignum (y));
  2076. else if (SCM_REALP (y))
  2077. return scm_i_inexact_round_remainder (SCM_I_INUM (x),
  2078. SCM_REAL_VALUE (y));
  2079. else if (SCM_FRACTIONP (y))
  2080. return scm_i_exact_rational_round_remainder (x, y);
  2081. else
  2082. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  2083. s_scm_round_remainder);
  2084. }
  2085. else if (SCM_BIGP (x))
  2086. {
  2087. if (SCM_I_INUMP (y))
  2088. return scm_integer_round_remainder_zi (scm_bignum (x), SCM_I_INUM (y));
  2089. else if (SCM_BIGP (y))
  2090. return scm_integer_round_remainder_zz (scm_bignum (x), scm_bignum (y));
  2091. else if (SCM_REALP (y))
  2092. return scm_i_inexact_round_remainder
  2093. (scm_integer_to_double_z (scm_bignum (x)), SCM_REAL_VALUE (y));
  2094. else if (SCM_FRACTIONP (y))
  2095. return scm_i_exact_rational_round_remainder (x, y);
  2096. else
  2097. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  2098. s_scm_round_remainder);
  2099. }
  2100. else if (SCM_REALP (x))
  2101. {
  2102. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2103. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2104. return scm_i_inexact_round_remainder
  2105. (SCM_REAL_VALUE (x), scm_to_double (y));
  2106. else
  2107. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  2108. s_scm_round_remainder);
  2109. }
  2110. else if (SCM_FRACTIONP (x))
  2111. {
  2112. if (SCM_REALP (y))
  2113. return scm_i_inexact_round_remainder
  2114. (scm_i_fraction2double (x), SCM_REAL_VALUE (y));
  2115. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2116. return scm_i_exact_rational_round_remainder (x, y);
  2117. else
  2118. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG2,
  2119. s_scm_round_remainder);
  2120. }
  2121. else
  2122. return scm_wta_dispatch_2 (g_scm_round_remainder, x, y, SCM_ARG1,
  2123. s_scm_round_remainder);
  2124. }
  2125. #undef FUNC_NAME
  2126. static SCM
  2127. scm_i_inexact_round_remainder (double x, double y)
  2128. {
  2129. /* Although it would be more efficient to use fmod here, we can't
  2130. because it would in some cases produce results inconsistent with
  2131. scm_i_inexact_round_quotient, such that x != r + q * y (not even
  2132. close). In particular, when x-y/2 is very close to a multiple of
  2133. y, then r might be either -abs(y/2) or abs(y/2), but those two
  2134. cases must correspond to different choices of q. If quotient
  2135. chooses one and remainder chooses the other, it would be bad. */
  2136. if (SCM_UNLIKELY (y == 0))
  2137. scm_num_overflow (s_scm_round_remainder); /* or return a NaN? */
  2138. else
  2139. {
  2140. double q = scm_c_round (x / y);
  2141. return scm_i_from_double (x - q * y);
  2142. }
  2143. }
  2144. static SCM
  2145. scm_i_exact_rational_round_remainder (SCM x, SCM y)
  2146. {
  2147. SCM xd = scm_denominator (x);
  2148. SCM yd = scm_denominator (y);
  2149. SCM r1 = scm_round_remainder (scm_product (scm_numerator (x), yd),
  2150. scm_product (scm_numerator (y), xd));
  2151. return scm_divide (r1, scm_product (xd, yd));
  2152. }
  2153. static void scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp);
  2154. static void scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp);
  2155. SCM_PRIMITIVE_GENERIC (scm_i_round_divide, "round/", 2, 0, 0,
  2156. (SCM x, SCM y),
  2157. "Return the integer @var{q} and the real number @var{r}\n"
  2158. "such that @math{@var{x} = @var{q}*@var{y} + @var{r}}\n"
  2159. "and @var{q} is @math{@var{x} / @var{y}} rounded to the\n"
  2160. "nearest integer, with ties going to the nearest even integer.\n"
  2161. "@lisp\n"
  2162. "(round/ 123 10) @result{} 12 and 3\n"
  2163. "(round/ 123 -10) @result{} -12 and 3\n"
  2164. "(round/ -123 10) @result{} -12 and -3\n"
  2165. "(round/ -123 -10) @result{} 12 and -3\n"
  2166. "(round/ 125 10) @result{} 12 and 5\n"
  2167. "(round/ 127 10) @result{} 13 and -3\n"
  2168. "(round/ 135 10) @result{} 14 and -5\n"
  2169. "(round/ -123.2 -63.5) @result{} 2.0 and 3.8\n"
  2170. "(round/ 16/3 -10/7) @result{} -4 and -8/21\n"
  2171. "@end lisp")
  2172. #define FUNC_NAME s_scm_i_round_divide
  2173. {
  2174. SCM q, r;
  2175. scm_round_divide(x, y, &q, &r);
  2176. return scm_values_2 (q, r);
  2177. }
  2178. #undef FUNC_NAME
  2179. #define s_scm_round_divide s_scm_i_round_divide
  2180. #define g_scm_round_divide g_scm_i_round_divide
  2181. void
  2182. scm_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2183. {
  2184. if (SCM_I_INUMP (x))
  2185. {
  2186. if (SCM_I_INUMP (y))
  2187. scm_integer_round_divide_ii (SCM_I_INUM (x), SCM_I_INUM (y), qp, rp);
  2188. else if (SCM_BIGP (y))
  2189. scm_integer_round_divide_iz (SCM_I_INUM (x), scm_bignum (y), qp, rp);
  2190. else if (SCM_REALP (y))
  2191. scm_i_inexact_round_divide (SCM_I_INUM (x), SCM_REAL_VALUE (y), qp, rp);
  2192. else if (SCM_FRACTIONP (y))
  2193. scm_i_exact_rational_round_divide (x, y, qp, rp);
  2194. else
  2195. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  2196. s_scm_round_divide, qp, rp);
  2197. }
  2198. else if (SCM_BIGP (x))
  2199. {
  2200. if (SCM_I_INUMP (y))
  2201. scm_integer_round_divide_zi (scm_bignum (x), SCM_I_INUM (y), qp, rp);
  2202. else if (SCM_BIGP (y))
  2203. scm_integer_round_divide_zz (scm_bignum (x), scm_bignum (y), qp, rp);
  2204. else if (SCM_REALP (y))
  2205. scm_i_inexact_round_divide (scm_integer_to_double_z (scm_bignum (x)),
  2206. SCM_REAL_VALUE (y), qp, rp);
  2207. else if (SCM_FRACTIONP (y))
  2208. scm_i_exact_rational_round_divide (x, y, qp, rp);
  2209. else
  2210. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  2211. s_scm_round_divide, qp, rp);
  2212. }
  2213. else if (SCM_REALP (x))
  2214. {
  2215. if (SCM_REALP (y) || SCM_I_INUMP (y) ||
  2216. SCM_BIGP (y) || SCM_FRACTIONP (y))
  2217. scm_i_inexact_round_divide (SCM_REAL_VALUE (x), scm_to_double (y),
  2218. qp, rp);
  2219. else
  2220. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  2221. s_scm_round_divide, qp, rp);
  2222. }
  2223. else if (SCM_FRACTIONP (x))
  2224. {
  2225. if (SCM_REALP (y))
  2226. scm_i_inexact_round_divide
  2227. (scm_i_fraction2double (x), SCM_REAL_VALUE (y), qp, rp);
  2228. else if (SCM_I_INUMP (y) || SCM_BIGP (y) || SCM_FRACTIONP (y))
  2229. scm_i_exact_rational_round_divide (x, y, qp, rp);
  2230. else
  2231. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG2,
  2232. s_scm_round_divide, qp, rp);
  2233. }
  2234. else
  2235. two_valued_wta_dispatch_2 (g_scm_round_divide, x, y, SCM_ARG1,
  2236. s_scm_round_divide, qp, rp);
  2237. }
  2238. static void
  2239. scm_i_inexact_round_divide (double x, double y, SCM *qp, SCM *rp)
  2240. {
  2241. if (SCM_UNLIKELY (y == 0))
  2242. scm_num_overflow (s_scm_round_divide); /* or return a NaN? */
  2243. else
  2244. {
  2245. double q = scm_c_round (x / y);
  2246. double r = x - q * y;
  2247. *qp = scm_i_from_double (q);
  2248. *rp = scm_i_from_double (r);
  2249. }
  2250. }
  2251. static void
  2252. scm_i_exact_rational_round_divide (SCM x, SCM y, SCM *qp, SCM *rp)
  2253. {
  2254. SCM r1;
  2255. SCM xd = scm_denominator (x);
  2256. SCM yd = scm_denominator (y);
  2257. scm_round_divide (scm_product (scm_numerator (x), yd),
  2258. scm_product (scm_numerator (y), xd),
  2259. qp, &r1);
  2260. *rp = scm_divide (r1, scm_product (xd, yd));
  2261. }
  2262. SCM_PRIMITIVE_GENERIC (scm_i_gcd, "gcd", 0, 2, 1,
  2263. (SCM x, SCM y, SCM rest),
  2264. "Return the greatest common divisor of all parameter values.\n"
  2265. "If called without arguments, 0 is returned.")
  2266. #define FUNC_NAME s_scm_i_gcd
  2267. {
  2268. while (!scm_is_null (rest))
  2269. { x = scm_gcd (x, y);
  2270. y = scm_car (rest);
  2271. rest = scm_cdr (rest);
  2272. }
  2273. return scm_gcd (x, y);
  2274. }
  2275. #undef FUNC_NAME
  2276. #define s_gcd s_scm_i_gcd
  2277. #define g_gcd g_scm_i_gcd
  2278. SCM
  2279. scm_gcd (SCM x, SCM y)
  2280. {
  2281. if (SCM_UNBNDP (y))
  2282. return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
  2283. if (SCM_I_INUMP (x))
  2284. {
  2285. if (SCM_I_INUMP (y))
  2286. return scm_integer_gcd_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  2287. else if (SCM_BIGP (y))
  2288. return scm_integer_gcd_zi (scm_bignum (y), SCM_I_INUM (x));
  2289. else if (SCM_REALP (y) && scm_is_integer (y))
  2290. goto handle_inexacts;
  2291. else
  2292. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  2293. }
  2294. else if (SCM_BIGP (x))
  2295. {
  2296. if (SCM_I_INUMP (y))
  2297. return scm_integer_gcd_zi (scm_bignum (x), SCM_I_INUM (y));
  2298. else if (SCM_BIGP (y))
  2299. return scm_integer_gcd_zz (scm_bignum (x), scm_bignum (y));
  2300. else if (SCM_REALP (y) && scm_is_integer (y))
  2301. goto handle_inexacts;
  2302. else
  2303. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  2304. }
  2305. else if (SCM_REALP (x) && scm_is_integer (x))
  2306. {
  2307. if (SCM_I_INUMP (y) || SCM_BIGP (y)
  2308. || (SCM_REALP (y) && scm_is_integer (y)))
  2309. {
  2310. handle_inexacts:
  2311. return scm_exact_to_inexact (scm_gcd (scm_inexact_to_exact (x),
  2312. scm_inexact_to_exact (y)));
  2313. }
  2314. else
  2315. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  2316. }
  2317. else
  2318. return scm_wta_dispatch_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
  2319. }
  2320. SCM_PRIMITIVE_GENERIC (scm_i_lcm, "lcm", 0, 2, 1,
  2321. (SCM x, SCM y, SCM rest),
  2322. "Return the least common multiple of the arguments.\n"
  2323. "If called without arguments, 1 is returned.")
  2324. #define FUNC_NAME s_scm_i_lcm
  2325. {
  2326. while (!scm_is_null (rest))
  2327. { x = scm_lcm (x, y);
  2328. y = scm_car (rest);
  2329. rest = scm_cdr (rest);
  2330. }
  2331. return scm_lcm (x, y);
  2332. }
  2333. #undef FUNC_NAME
  2334. #define s_lcm s_scm_i_lcm
  2335. #define g_lcm g_scm_i_lcm
  2336. SCM
  2337. scm_lcm (SCM n1, SCM n2)
  2338. {
  2339. if (SCM_UNBNDP (n2))
  2340. return SCM_UNBNDP (n1) ? SCM_INUM1 : scm_abs (n1);
  2341. if (SCM_I_INUMP (n1))
  2342. {
  2343. if (SCM_I_INUMP (n2))
  2344. return scm_integer_lcm_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
  2345. else if (SCM_BIGP (n2))
  2346. return scm_integer_lcm_zi (scm_bignum (n2), SCM_I_INUM (n1));
  2347. else if (SCM_REALP (n2) && scm_is_integer (n2))
  2348. goto handle_inexacts;
  2349. else
  2350. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  2351. }
  2352. else if (SCM_LIKELY (SCM_BIGP (n1)))
  2353. {
  2354. if (SCM_I_INUMP (n2))
  2355. return scm_integer_lcm_zi (scm_bignum (n1), SCM_I_INUM (n2));
  2356. else if (SCM_BIGP (n2))
  2357. return scm_integer_lcm_zz (scm_bignum (n1), scm_bignum (n2));
  2358. else if (SCM_REALP (n2) && scm_is_integer (n2))
  2359. goto handle_inexacts;
  2360. else
  2361. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  2362. }
  2363. else if (SCM_REALP (n1) && scm_is_integer (n1))
  2364. {
  2365. if (SCM_I_INUMP (n2) || SCM_BIGP (n2)
  2366. || (SCM_REALP (n2) && scm_is_integer (n2)))
  2367. {
  2368. handle_inexacts:
  2369. return scm_exact_to_inexact (scm_lcm (scm_inexact_to_exact (n1),
  2370. scm_inexact_to_exact (n2)));
  2371. }
  2372. else
  2373. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG2, s_lcm);
  2374. }
  2375. else
  2376. return scm_wta_dispatch_2 (g_lcm, n1, n2, SCM_ARG1, s_lcm);
  2377. }
  2378. /* Emulating 2's complement bignums with sign magnitude arithmetic:
  2379. Logand:
  2380. X Y Result Method:
  2381. (len)
  2382. + + + x (map digit:logand X Y)
  2383. + - + x (map digit:logand X (lognot (+ -1 Y)))
  2384. - + + y (map digit:logand (lognot (+ -1 X)) Y)
  2385. - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
  2386. Logior:
  2387. X Y Result Method:
  2388. + + + (map digit:logior X Y)
  2389. + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
  2390. - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
  2391. - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
  2392. Logxor:
  2393. X Y Result Method:
  2394. + + + (map digit:logxor X Y)
  2395. + - - (+ 1 (map digit:logxor X (+ -1 Y)))
  2396. - + - (+ 1 (map digit:logxor (+ -1 X) Y))
  2397. - - + (map digit:logxor (+ -1 X) (+ -1 Y))
  2398. Logtest:
  2399. X Y Result
  2400. + + (any digit:logand X Y)
  2401. + - (any digit:logand X (lognot (+ -1 Y)))
  2402. - + (any digit:logand (lognot (+ -1 X)) Y)
  2403. - - #t
  2404. */
  2405. SCM_DEFINE (scm_i_logand, "logand", 0, 2, 1,
  2406. (SCM x, SCM y, SCM rest),
  2407. "Return the bitwise AND of the integer arguments.\n\n"
  2408. "@lisp\n"
  2409. "(logand) @result{} -1\n"
  2410. "(logand 7) @result{} 7\n"
  2411. "(logand #b111 #b011 #b001) @result{} 1\n"
  2412. "@end lisp")
  2413. #define FUNC_NAME s_scm_i_logand
  2414. {
  2415. while (!scm_is_null (rest))
  2416. { x = scm_logand (x, y);
  2417. y = scm_car (rest);
  2418. rest = scm_cdr (rest);
  2419. }
  2420. return scm_logand (x, y);
  2421. }
  2422. #undef FUNC_NAME
  2423. #define s_scm_logand s_scm_i_logand
  2424. SCM scm_logand (SCM n1, SCM n2)
  2425. #define FUNC_NAME s_scm_logand
  2426. {
  2427. if (SCM_UNBNDP (n2))
  2428. {
  2429. if (SCM_UNBNDP (n1))
  2430. return SCM_I_MAKINUM (-1);
  2431. else if (!SCM_NUMBERP (n1))
  2432. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2433. else if (SCM_NUMBERP (n1))
  2434. return n1;
  2435. else
  2436. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2437. }
  2438. if (SCM_I_INUMP (n1))
  2439. {
  2440. if (SCM_I_INUMP (n2))
  2441. return scm_integer_logand_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
  2442. else if (SCM_BIGP (n2))
  2443. return scm_integer_logand_zi (scm_bignum (n2), SCM_I_INUM (n1));
  2444. else
  2445. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2446. }
  2447. else if (SCM_BIGP (n1))
  2448. {
  2449. if (SCM_I_INUMP (n2))
  2450. return scm_integer_logand_zi (scm_bignum (n1), SCM_I_INUM (n2));
  2451. else if (SCM_BIGP (n2))
  2452. return scm_integer_logand_zz (scm_bignum (n1), scm_bignum (n2));
  2453. else
  2454. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2455. }
  2456. else
  2457. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2458. }
  2459. #undef FUNC_NAME
  2460. SCM_DEFINE (scm_i_logior, "logior", 0, 2, 1,
  2461. (SCM x, SCM y, SCM rest),
  2462. "Return the bitwise OR of the integer arguments.\n\n"
  2463. "@lisp\n"
  2464. "(logior) @result{} 0\n"
  2465. "(logior 7) @result{} 7\n"
  2466. "(logior #b000 #b001 #b011) @result{} 3\n"
  2467. "@end lisp")
  2468. #define FUNC_NAME s_scm_i_logior
  2469. {
  2470. while (!scm_is_null (rest))
  2471. { x = scm_logior (x, y);
  2472. y = scm_car (rest);
  2473. rest = scm_cdr (rest);
  2474. }
  2475. return scm_logior (x, y);
  2476. }
  2477. #undef FUNC_NAME
  2478. #define s_scm_logior s_scm_i_logior
  2479. SCM scm_logior (SCM n1, SCM n2)
  2480. #define FUNC_NAME s_scm_logior
  2481. {
  2482. if (SCM_UNBNDP (n2))
  2483. {
  2484. if (SCM_UNBNDP (n1))
  2485. return SCM_INUM0;
  2486. else if (SCM_NUMBERP (n1))
  2487. return n1;
  2488. else
  2489. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2490. }
  2491. if (SCM_I_INUMP (n1))
  2492. {
  2493. if (SCM_I_INUMP (n2))
  2494. return scm_integer_logior_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
  2495. else if (SCM_BIGP (n2))
  2496. return scm_integer_logior_zi (scm_bignum (n2), SCM_I_INUM (n1));
  2497. else
  2498. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2499. }
  2500. else if (SCM_BIGP (n1))
  2501. {
  2502. if (SCM_I_INUMP (n2))
  2503. return scm_integer_logior_zi (scm_bignum (n1), SCM_I_INUM (n2));
  2504. else if (SCM_BIGP (n2))
  2505. return scm_integer_logior_zz (scm_bignum (n1), scm_bignum (n2));
  2506. else
  2507. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2508. }
  2509. else
  2510. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2511. }
  2512. #undef FUNC_NAME
  2513. SCM_DEFINE (scm_i_logxor, "logxor", 0, 2, 1,
  2514. (SCM x, SCM y, SCM rest),
  2515. "Return the bitwise XOR of the integer arguments. A bit is\n"
  2516. "set in the result if it is set in an odd number of arguments.\n"
  2517. "@lisp\n"
  2518. "(logxor) @result{} 0\n"
  2519. "(logxor 7) @result{} 7\n"
  2520. "(logxor #b000 #b001 #b011) @result{} 2\n"
  2521. "(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
  2522. "@end lisp")
  2523. #define FUNC_NAME s_scm_i_logxor
  2524. {
  2525. while (!scm_is_null (rest))
  2526. { x = scm_logxor (x, y);
  2527. y = scm_car (rest);
  2528. rest = scm_cdr (rest);
  2529. }
  2530. return scm_logxor (x, y);
  2531. }
  2532. #undef FUNC_NAME
  2533. #define s_scm_logxor s_scm_i_logxor
  2534. SCM scm_logxor (SCM n1, SCM n2)
  2535. #define FUNC_NAME s_scm_logxor
  2536. {
  2537. if (SCM_UNBNDP (n2))
  2538. {
  2539. if (SCM_UNBNDP (n1))
  2540. return SCM_INUM0;
  2541. else if (SCM_NUMBERP (n1))
  2542. return n1;
  2543. else
  2544. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2545. }
  2546. if (SCM_I_INUMP (n1))
  2547. {
  2548. if (SCM_I_INUMP (n2))
  2549. return scm_integer_logxor_ii (SCM_I_INUM (n1), SCM_I_INUM (n2));
  2550. else if (SCM_BIGP (n2))
  2551. return scm_integer_logxor_zi (scm_bignum (n2), SCM_I_INUM (n1));
  2552. else
  2553. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2554. }
  2555. else if (SCM_BIGP (n1))
  2556. {
  2557. if (SCM_I_INUMP (n2))
  2558. return scm_integer_logxor_zi (scm_bignum (n1), SCM_I_INUM (n2));
  2559. else if (SCM_BIGP (n2))
  2560. return scm_integer_logxor_zz (scm_bignum (n1), scm_bignum (n2));
  2561. else
  2562. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  2563. }
  2564. else
  2565. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  2566. }
  2567. #undef FUNC_NAME
  2568. SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
  2569. (SCM j, SCM k),
  2570. "Test whether @var{j} and @var{k} have any 1 bits in common.\n"
  2571. "This is equivalent to @code{(not (zero? (logand j k)))}, but\n"
  2572. "without actually calculating the @code{logand}, just testing\n"
  2573. "for non-zero.\n"
  2574. "\n"
  2575. "@lisp\n"
  2576. "(logtest #b0100 #b1011) @result{} #f\n"
  2577. "(logtest #b0100 #b0111) @result{} #t\n"
  2578. "@end lisp")
  2579. #define FUNC_NAME s_scm_logtest
  2580. {
  2581. if (SCM_I_INUMP (j))
  2582. {
  2583. if (SCM_I_INUMP (k))
  2584. return scm_from_bool (scm_integer_logtest_ii (SCM_I_INUM (j),
  2585. SCM_I_INUM (k)));
  2586. else if (SCM_BIGP (k))
  2587. return scm_from_bool (scm_integer_logtest_zi (scm_bignum (k),
  2588. SCM_I_INUM (j)));
  2589. else
  2590. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  2591. }
  2592. else if (SCM_BIGP (j))
  2593. {
  2594. if (SCM_I_INUMP (k))
  2595. return scm_from_bool (scm_integer_logtest_zi (scm_bignum (j),
  2596. SCM_I_INUM (k)));
  2597. else if (SCM_BIGP (k))
  2598. return scm_from_bool (scm_integer_logtest_zz (scm_bignum (j),
  2599. scm_bignum (k)));
  2600. else
  2601. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  2602. }
  2603. else
  2604. SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
  2605. }
  2606. #undef FUNC_NAME
  2607. SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
  2608. (SCM index, SCM j),
  2609. "Test whether bit number @var{index} in @var{j} is set.\n"
  2610. "@var{index} starts from 0 for the least significant bit.\n"
  2611. "\n"
  2612. "@lisp\n"
  2613. "(logbit? 0 #b1101) @result{} #t\n"
  2614. "(logbit? 1 #b1101) @result{} #f\n"
  2615. "(logbit? 2 #b1101) @result{} #t\n"
  2616. "(logbit? 3 #b1101) @result{} #t\n"
  2617. "(logbit? 4 #b1101) @result{} #f\n"
  2618. "@end lisp")
  2619. #define FUNC_NAME s_scm_logbit_p
  2620. {
  2621. unsigned long int iindex;
  2622. iindex = scm_to_ulong (index);
  2623. if (SCM_I_INUMP (j))
  2624. return scm_from_bool (scm_integer_logbit_ui (iindex, SCM_I_INUM (j)));
  2625. else if (SCM_BIGP (j))
  2626. return scm_from_bool (scm_integer_logbit_uz (iindex, scm_bignum (j)));
  2627. else
  2628. SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
  2629. }
  2630. #undef FUNC_NAME
  2631. SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
  2632. (SCM n),
  2633. "Return the integer which is the ones-complement of the integer\n"
  2634. "argument.\n"
  2635. "\n"
  2636. "@lisp\n"
  2637. "(number->string (lognot #b10000000) 2)\n"
  2638. " @result{} \"-10000001\"\n"
  2639. "(number->string (lognot #b0) 2)\n"
  2640. " @result{} \"-1\"\n"
  2641. "@end lisp")
  2642. #define FUNC_NAME s_scm_lognot
  2643. {
  2644. if (SCM_I_INUMP (n))
  2645. return scm_integer_lognot_i (SCM_I_INUM (n));
  2646. else if (SCM_BIGP (n))
  2647. return scm_integer_lognot_z (scm_bignum (n));
  2648. else
  2649. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2650. }
  2651. #undef FUNC_NAME
  2652. SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
  2653. (SCM n, SCM k, SCM m),
  2654. "Return @var{n} raised to the integer exponent\n"
  2655. "@var{k}, modulo @var{m}.\n"
  2656. "\n"
  2657. "@lisp\n"
  2658. "(modulo-expt 2 3 5)\n"
  2659. " @result{} 3\n"
  2660. "@end lisp")
  2661. #define FUNC_NAME s_scm_modulo_expt
  2662. {
  2663. if (!scm_is_exact_integer (n))
  2664. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2665. if (!scm_is_exact_integer (k))
  2666. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  2667. if (!scm_is_exact_integer (m))
  2668. SCM_WRONG_TYPE_ARG (SCM_ARG3, m);
  2669. return scm_integer_modulo_expt_nnn (n, k, m);
  2670. }
  2671. #undef FUNC_NAME
  2672. static void
  2673. mpz_clear_on_unwind (void *mpz)
  2674. {
  2675. mpz_clear (mpz);
  2676. }
  2677. SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
  2678. (SCM n, SCM k),
  2679. "Return @var{n} raised to the power @var{k}. @var{k} must be an\n"
  2680. "exact integer, @var{n} can be any number.\n"
  2681. "\n"
  2682. "Negative @var{k} is supported, and results in\n"
  2683. "@math{1/@var{n}^abs(@var{k})} in the usual way.\n"
  2684. "@math{@var{n}^0} is 1, as usual, and that\n"
  2685. "includes @math{0^0} is 1.\n"
  2686. "\n"
  2687. "@lisp\n"
  2688. "(integer-expt 2 5) @result{} 32\n"
  2689. "(integer-expt -3 3) @result{} -27\n"
  2690. "(integer-expt 5 -3) @result{} 1/125\n"
  2691. "(integer-expt 0 0) @result{} 1\n"
  2692. "@end lisp")
  2693. #define FUNC_NAME s_scm_integer_expt
  2694. {
  2695. // Fast cases first.
  2696. if (SCM_I_INUMP (k))
  2697. {
  2698. if (SCM_I_INUM (k) < 0)
  2699. {
  2700. if (SCM_NUMBERP (n) && scm_is_true (scm_zero_p (n)))
  2701. return scm_nan ();
  2702. k = scm_integer_negate_i (SCM_I_INUM (k));
  2703. n = scm_divide (n, SCM_UNDEFINED);
  2704. }
  2705. if (SCM_I_INUMP (n))
  2706. return scm_integer_expt_ii (SCM_I_INUM (n), SCM_I_INUM (k));
  2707. else if (SCM_BIGP (n))
  2708. return scm_integer_expt_zi (scm_bignum (n), SCM_I_INUM (k));
  2709. }
  2710. else if (SCM_BIGP (k))
  2711. {
  2712. if (scm_is_integer_negative_z (scm_bignum (k)))
  2713. {
  2714. if (SCM_NUMBERP (n) && scm_is_true (scm_zero_p (n)))
  2715. return scm_nan ();
  2716. k = scm_integer_negate_z (scm_bignum (k));
  2717. n = scm_divide (n, SCM_UNDEFINED);
  2718. }
  2719. if (scm_is_eq (n, SCM_INUM0) || scm_is_eq (n, SCM_INUM1))
  2720. return n;
  2721. else if (scm_is_eq (n, SCM_I_MAKINUM (-1)))
  2722. return scm_is_integer_odd_z (scm_bignum (k)) ? n : SCM_INUM1;
  2723. else if (scm_is_exact_integer (n))
  2724. scm_num_overflow ("integer-expt");
  2725. }
  2726. else
  2727. SCM_WRONG_TYPE_ARG (2, k);
  2728. // The general case.
  2729. if (scm_is_eq (k, SCM_INUM0))
  2730. return SCM_INUM1; /* n^(exact0) is exact 1, regardless of n */
  2731. if (SCM_FRACTIONP (n))
  2732. {
  2733. /* Optimize the fraction case by (a/b)^k ==> (a^k)/(b^k), to avoid
  2734. needless reduction of intermediate products to lowest terms.
  2735. If a and b have no common factors, then a^k and b^k have no
  2736. common factors. Use 'scm_i_make_ratio_already_reduced' to
  2737. construct the final result, so that no gcd computations are
  2738. needed to exponentiate a fraction. */
  2739. if (scm_is_true (scm_positive_p (k)))
  2740. return scm_i_make_ratio_already_reduced
  2741. (scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k),
  2742. scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k));
  2743. else
  2744. {
  2745. k = scm_difference (k, SCM_UNDEFINED);
  2746. return scm_i_make_ratio_already_reduced
  2747. (scm_integer_expt (SCM_FRACTION_DENOMINATOR (n), k),
  2748. scm_integer_expt (SCM_FRACTION_NUMERATOR (n), k));
  2749. }
  2750. }
  2751. mpz_t zk;
  2752. mpz_init (zk);
  2753. scm_to_mpz (k, zk);
  2754. scm_dynwind_begin (0);
  2755. scm_dynwind_unwind_handler (mpz_clear_on_unwind, zk, SCM_F_WIND_EXPLICITLY);
  2756. if (mpz_sgn (zk) == -1)
  2757. {
  2758. mpz_neg (zk, zk);
  2759. n = scm_divide (n, SCM_UNDEFINED);
  2760. }
  2761. SCM acc = SCM_INUM1;
  2762. while (1)
  2763. {
  2764. if (mpz_sgn (zk) == 0)
  2765. break;
  2766. if (mpz_cmp_ui(zk, 1) == 0)
  2767. {
  2768. acc = scm_product (acc, n);
  2769. break;
  2770. }
  2771. if (mpz_tstbit(zk, 0))
  2772. acc = scm_product (acc, n);
  2773. n = scm_product (n, n);
  2774. mpz_fdiv_q_2exp (zk, zk, 1);
  2775. }
  2776. scm_dynwind_end ();
  2777. return acc;
  2778. }
  2779. #undef FUNC_NAME
  2780. static SCM
  2781. lsh (SCM n, SCM count, const char *fn)
  2782. {
  2783. if (scm_is_eq (n, SCM_INUM0))
  2784. return n;
  2785. if (!scm_is_unsigned_integer (count, 0, ULONG_MAX))
  2786. scm_num_overflow (fn);
  2787. unsigned long ucount = scm_to_ulong (count);
  2788. if (ucount == 0)
  2789. return n;
  2790. if (ucount / (sizeof (int) * 8) >= (unsigned long) INT_MAX)
  2791. scm_num_overflow (fn);
  2792. if (SCM_I_INUMP (n))
  2793. return scm_integer_lsh_iu (SCM_I_INUM (n), ucount);
  2794. return scm_integer_lsh_zu (scm_bignum (n), ucount);
  2795. }
  2796. static SCM
  2797. floor_rsh (SCM n, SCM count)
  2798. {
  2799. if (!scm_is_unsigned_integer (count, 0, ULONG_MAX))
  2800. return scm_is_false (scm_negative_p (n)) ? SCM_INUM0 : SCM_I_MAKINUM (-1);
  2801. unsigned long ucount = scm_to_ulong (count);
  2802. if (ucount == 0)
  2803. return n;
  2804. if (SCM_I_INUMP (n))
  2805. return scm_integer_floor_rsh_iu (SCM_I_INUM (n), ucount);
  2806. return scm_integer_floor_rsh_zu (scm_bignum (n), ucount);
  2807. }
  2808. static SCM
  2809. round_rsh (SCM n, SCM count)
  2810. {
  2811. if (!scm_is_unsigned_integer (count, 0, ULONG_MAX))
  2812. return SCM_INUM0;
  2813. unsigned long ucount = scm_to_ulong (count);
  2814. if (ucount == 0)
  2815. return n;
  2816. if (SCM_I_INUMP (n))
  2817. return scm_integer_round_rsh_iu (SCM_I_INUM (n), ucount);
  2818. return scm_integer_round_rsh_zu (scm_bignum (n), ucount);
  2819. }
  2820. SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
  2821. (SCM n, SCM count),
  2822. "Return @math{floor(@var{n} * 2^@var{count})}.\n"
  2823. "@var{n} and @var{count} must be exact integers.\n"
  2824. "\n"
  2825. "With @var{n} viewed as an infinite-precision twos-complement\n"
  2826. "integer, @code{ash} means a left shift introducing zero bits\n"
  2827. "when @var{count} is positive, or a right shift dropping bits\n"
  2828. "when @var{count} is negative. This is an ``arithmetic'' shift.\n"
  2829. "\n"
  2830. "@lisp\n"
  2831. "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
  2832. "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
  2833. "\n"
  2834. ";; -23 is bits ...11101001, -6 is bits ...111010\n"
  2835. "(ash -23 -2) @result{} -6\n"
  2836. "@end lisp")
  2837. #define FUNC_NAME s_scm_ash
  2838. {
  2839. if (!scm_is_exact_integer (n))
  2840. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2841. if (!scm_is_exact_integer (count))
  2842. SCM_WRONG_TYPE_ARG (SCM_ARG2, count);
  2843. if (scm_is_false (scm_negative_p (count)))
  2844. return lsh (n, count, "ash");
  2845. return floor_rsh (n, scm_difference (count, SCM_UNDEFINED));
  2846. }
  2847. #undef FUNC_NAME
  2848. SCM_DEFINE (scm_round_ash, "round-ash", 2, 0, 0,
  2849. (SCM n, SCM count),
  2850. "Return @math{round(@var{n} * 2^@var{count})}.\n"
  2851. "@var{n} and @var{count} must be exact integers.\n"
  2852. "\n"
  2853. "With @var{n} viewed as an infinite-precision twos-complement\n"
  2854. "integer, @code{round-ash} means a left shift introducing zero\n"
  2855. "bits when @var{count} is positive, or a right shift rounding\n"
  2856. "to the nearest integer (with ties going to the nearest even\n"
  2857. "integer) when @var{count} is negative. This is a rounded\n"
  2858. "``arithmetic'' shift.\n"
  2859. "\n"
  2860. "@lisp\n"
  2861. "(number->string (round-ash #b1 3) 2) @result{} \"1000\"\n"
  2862. "(number->string (round-ash #b1010 -1) 2) @result{} \"101\"\n"
  2863. "(number->string (round-ash #b1010 -2) 2) @result{} \"10\"\n"
  2864. "(number->string (round-ash #b1011 -2) 2) @result{} \"11\"\n"
  2865. "(number->string (round-ash #b1101 -2) 2) @result{} \"11\"\n"
  2866. "(number->string (round-ash #b1110 -2) 2) @result{} \"100\"\n"
  2867. "@end lisp")
  2868. #define FUNC_NAME s_scm_round_ash
  2869. {
  2870. if (!scm_is_exact_integer (n))
  2871. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2872. if (!scm_is_exact_integer (count))
  2873. SCM_WRONG_TYPE_ARG (SCM_ARG2, count);
  2874. if (scm_is_false (scm_negative_p (count)))
  2875. return lsh (n, count, "round-ash");
  2876. return round_rsh (n, scm_difference (count, SCM_UNDEFINED));
  2877. }
  2878. #undef FUNC_NAME
  2879. SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
  2880. (SCM n, SCM start, SCM end),
  2881. "Return the integer composed of the @var{start} (inclusive)\n"
  2882. "through @var{end} (exclusive) bits of @var{n}. The\n"
  2883. "@var{start}th bit becomes the 0-th bit in the result.\n"
  2884. "\n"
  2885. "@lisp\n"
  2886. "(number->string (bit-extract #b1101101010 0 4) 2)\n"
  2887. " @result{} \"1010\"\n"
  2888. "(number->string (bit-extract #b1101101010 4 9) 2)\n"
  2889. " @result{} \"10110\"\n"
  2890. "@end lisp")
  2891. #define FUNC_NAME s_scm_bit_extract
  2892. {
  2893. if (!scm_is_exact_integer (n))
  2894. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2895. unsigned long istart = scm_to_ulong (start);
  2896. unsigned long iend = scm_to_ulong (end);
  2897. SCM_ASSERT_RANGE (3, end, (iend >= istart));
  2898. unsigned long bits = iend - istart;
  2899. if (SCM_I_INUMP (n))
  2900. return scm_integer_bit_extract_i (SCM_I_INUM (n), istart, bits);
  2901. else
  2902. return scm_integer_bit_extract_z (scm_bignum (n), istart, bits);
  2903. }
  2904. #undef FUNC_NAME
  2905. SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
  2906. (SCM n),
  2907. "Return the number of bits in integer @var{n}. If integer is\n"
  2908. "positive, the 1-bits in its binary representation are counted.\n"
  2909. "If negative, the 0-bits in its two's-complement binary\n"
  2910. "representation are counted. If 0, 0 is returned.\n"
  2911. "\n"
  2912. "@lisp\n"
  2913. "(logcount #b10101010)\n"
  2914. " @result{} 4\n"
  2915. "(logcount 0)\n"
  2916. " @result{} 0\n"
  2917. "(logcount -2)\n"
  2918. " @result{} 1\n"
  2919. "@end lisp")
  2920. #define FUNC_NAME s_scm_logcount
  2921. {
  2922. if (SCM_I_INUMP (n))
  2923. return scm_integer_logcount_i (SCM_I_INUM (n));
  2924. else if (SCM_BIGP (n))
  2925. return scm_integer_logcount_z (scm_bignum (n));
  2926. else
  2927. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2928. }
  2929. #undef FUNC_NAME
  2930. SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
  2931. (SCM n),
  2932. "Return the number of bits necessary to represent @var{n}.\n"
  2933. "\n"
  2934. "@lisp\n"
  2935. "(integer-length #b10101010)\n"
  2936. " @result{} 8\n"
  2937. "(integer-length 0)\n"
  2938. " @result{} 0\n"
  2939. "(integer-length #b1111)\n"
  2940. " @result{} 4\n"
  2941. "@end lisp")
  2942. #define FUNC_NAME s_scm_integer_length
  2943. {
  2944. if (SCM_I_INUMP (n))
  2945. return scm_integer_length_i (SCM_I_INUM (n));
  2946. else if (SCM_BIGP (n))
  2947. return scm_integer_length_z (scm_bignum (n));
  2948. else
  2949. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  2950. }
  2951. #undef FUNC_NAME
  2952. /*** NUMBERS -> STRINGS ***/
  2953. #define SCM_MAX_DBL_RADIX 36
  2954. /* use this array as a way to generate a single digit */
  2955. static const char number_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  2956. static mpz_t dbl_minimum_normal_mantissa;
  2957. static size_t
  2958. idbl2str (double dbl, char *a, int radix)
  2959. {
  2960. int ch = 0;
  2961. if (radix < 2 || radix > SCM_MAX_DBL_RADIX)
  2962. /* revert to existing behavior */
  2963. radix = 10;
  2964. if (isinf (dbl))
  2965. {
  2966. strcpy (a, (dbl > 0.0) ? "+inf.0" : "-inf.0");
  2967. return 6;
  2968. }
  2969. else if (dbl > 0.0)
  2970. ;
  2971. else if (dbl < 0.0)
  2972. {
  2973. dbl = -dbl;
  2974. a[ch++] = '-';
  2975. }
  2976. else if (dbl == 0.0)
  2977. {
  2978. if (copysign (1.0, dbl) < 0.0)
  2979. a[ch++] = '-';
  2980. strcpy (a + ch, "0.0");
  2981. return ch + 3;
  2982. }
  2983. else if (isnan (dbl))
  2984. {
  2985. strcpy (a, "+nan.0");
  2986. return 6;
  2987. }
  2988. /* Algorithm taken from "Printing Floating-Point Numbers Quickly and
  2989. Accurately" by Robert G. Burger and R. Kent Dybvig */
  2990. {
  2991. int e, k;
  2992. mpz_t f, r, s, mplus, mminus, hi, digit;
  2993. int f_is_even, f_is_odd;
  2994. int expon;
  2995. int show_exp = 0;
  2996. mpz_inits (f, r, s, mplus, mminus, hi, digit, NULL);
  2997. mpz_set_d (f, ldexp (frexp (dbl, &e), DBL_MANT_DIG));
  2998. if (e < DBL_MIN_EXP)
  2999. {
  3000. mpz_tdiv_q_2exp (f, f, DBL_MIN_EXP - e);
  3001. e = DBL_MIN_EXP;
  3002. }
  3003. e -= DBL_MANT_DIG;
  3004. f_is_even = !mpz_odd_p (f);
  3005. f_is_odd = !f_is_even;
  3006. /* Initialize r, s, mplus, and mminus according
  3007. to Table 1 from the paper. */
  3008. if (e < 0)
  3009. {
  3010. mpz_set_ui (mminus, 1);
  3011. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0
  3012. || e == DBL_MIN_EXP - DBL_MANT_DIG)
  3013. {
  3014. mpz_set_ui (mplus, 1);
  3015. mpz_mul_2exp (r, f, 1);
  3016. mpz_mul_2exp (s, mminus, 1 - e);
  3017. }
  3018. else
  3019. {
  3020. mpz_set_ui (mplus, 2);
  3021. mpz_mul_2exp (r, f, 2);
  3022. mpz_mul_2exp (s, mminus, 2 - e);
  3023. }
  3024. }
  3025. else
  3026. {
  3027. mpz_set_ui (mminus, 1);
  3028. mpz_mul_2exp (mminus, mminus, e);
  3029. if (mpz_cmp (f, dbl_minimum_normal_mantissa) != 0)
  3030. {
  3031. mpz_set (mplus, mminus);
  3032. mpz_mul_2exp (r, f, 1 + e);
  3033. mpz_set_ui (s, 2);
  3034. }
  3035. else
  3036. {
  3037. mpz_mul_2exp (mplus, mminus, 1);
  3038. mpz_mul_2exp (r, f, 2 + e);
  3039. mpz_set_ui (s, 4);
  3040. }
  3041. }
  3042. /* Find the smallest k such that:
  3043. (r + mplus) / s < radix^k (if f is even)
  3044. (r + mplus) / s <= radix^k (if f is odd) */
  3045. {
  3046. /* IMPROVE-ME: Make an initial guess to speed this up */
  3047. mpz_add (hi, r, mplus);
  3048. k = 0;
  3049. while (mpz_cmp (hi, s) >= f_is_odd)
  3050. {
  3051. mpz_mul_ui (s, s, radix);
  3052. k++;
  3053. }
  3054. if (k == 0)
  3055. {
  3056. mpz_mul_ui (hi, hi, radix);
  3057. while (mpz_cmp (hi, s) < f_is_odd)
  3058. {
  3059. mpz_mul_ui (r, r, radix);
  3060. mpz_mul_ui (mplus, mplus, radix);
  3061. mpz_mul_ui (mminus, mminus, radix);
  3062. mpz_mul_ui (hi, hi, radix);
  3063. k--;
  3064. }
  3065. }
  3066. }
  3067. expon = k - 1;
  3068. if (k <= 0)
  3069. {
  3070. if (k <= -3)
  3071. {
  3072. /* Use scientific notation */
  3073. show_exp = 1;
  3074. k = 1;
  3075. }
  3076. else
  3077. {
  3078. int i;
  3079. /* Print leading zeroes */
  3080. a[ch++] = '0';
  3081. a[ch++] = '.';
  3082. for (i = 0; i > k; i--)
  3083. a[ch++] = '0';
  3084. }
  3085. }
  3086. for (;;)
  3087. {
  3088. int end_1_p, end_2_p;
  3089. int d;
  3090. mpz_mul_ui (mplus, mplus, radix);
  3091. mpz_mul_ui (mminus, mminus, radix);
  3092. mpz_mul_ui (r, r, radix);
  3093. mpz_fdiv_qr (digit, r, r, s);
  3094. d = mpz_get_ui (digit);
  3095. mpz_add (hi, r, mplus);
  3096. end_1_p = (mpz_cmp (r, mminus) < f_is_even);
  3097. end_2_p = (mpz_cmp (s, hi) < f_is_even);
  3098. if (end_1_p || end_2_p)
  3099. {
  3100. mpz_mul_2exp (r, r, 1);
  3101. if (!end_2_p)
  3102. ;
  3103. else if (!end_1_p)
  3104. d++;
  3105. else if (mpz_cmp (r, s) >= !(d & 1))
  3106. d++;
  3107. a[ch++] = number_chars[d];
  3108. if (--k == 0)
  3109. a[ch++] = '.';
  3110. break;
  3111. }
  3112. else
  3113. {
  3114. a[ch++] = number_chars[d];
  3115. if (--k == 0)
  3116. a[ch++] = '.';
  3117. }
  3118. }
  3119. if (k > 0)
  3120. {
  3121. if (expon >= 7 && k >= 4 && expon >= k)
  3122. {
  3123. /* Here we would have to print more than three zeroes
  3124. followed by a decimal point and another zero. It
  3125. makes more sense to use scientific notation. */
  3126. /* Adjust k to what it would have been if we had chosen
  3127. scientific notation from the beginning. */
  3128. k -= expon;
  3129. /* k will now be <= 0, with magnitude equal to the number of
  3130. digits that we printed which should now be put after the
  3131. decimal point. */
  3132. /* Insert a decimal point */
  3133. memmove (a + ch + k + 1, a + ch + k, -k);
  3134. a[ch + k] = '.';
  3135. ch++;
  3136. show_exp = 1;
  3137. }
  3138. else
  3139. {
  3140. for (; k > 0; k--)
  3141. a[ch++] = '0';
  3142. a[ch++] = '.';
  3143. }
  3144. }
  3145. if (k == 0)
  3146. a[ch++] = '0';
  3147. if (show_exp)
  3148. {
  3149. a[ch++] = 'e';
  3150. ch += scm_iint2str (expon, radix, a + ch);
  3151. }
  3152. mpz_clears (f, r, s, mplus, mminus, hi, digit, NULL);
  3153. }
  3154. return ch;
  3155. }
  3156. static size_t
  3157. icmplx2str (double real, double imag, char *str, int radix)
  3158. {
  3159. size_t i;
  3160. double sgn;
  3161. i = idbl2str (real, str, radix);
  3162. sgn = copysign (1.0, imag);
  3163. /* Don't output a '+' for negative numbers or for Inf and
  3164. NaN. They will provide their own sign. */
  3165. if (sgn >= 0 && isfinite (imag))
  3166. str[i++] = '+';
  3167. i += idbl2str (imag, &str[i], radix);
  3168. str[i++] = 'i';
  3169. return i;
  3170. }
  3171. static size_t
  3172. iflo2str (SCM flt, char *str, int radix)
  3173. {
  3174. size_t i;
  3175. if (SCM_REALP (flt))
  3176. i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
  3177. else
  3178. i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
  3179. str, radix);
  3180. return i;
  3181. }
  3182. /* convert a intmax_t to a string (unterminated). returns the number of
  3183. characters in the result.
  3184. rad is output base
  3185. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  3186. size_t
  3187. scm_iint2str (intmax_t num, int rad, char *p)
  3188. {
  3189. if (num < 0)
  3190. {
  3191. *p++ = '-';
  3192. return scm_iuint2str (-num, rad, p) + 1;
  3193. }
  3194. else
  3195. return scm_iuint2str (num, rad, p);
  3196. }
  3197. /* convert a intmax_t to a string (unterminated). returns the number of
  3198. characters in the result.
  3199. rad is output base
  3200. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  3201. size_t
  3202. scm_iuint2str (uintmax_t num, int rad, char *p)
  3203. {
  3204. size_t j = 1;
  3205. size_t i;
  3206. uintmax_t n = num;
  3207. if (rad < 2 || rad > 36)
  3208. scm_out_of_range ("scm_iuint2str", scm_from_int (rad));
  3209. for (n /= rad; n > 0; n /= rad)
  3210. j++;
  3211. i = j;
  3212. n = num;
  3213. while (i--)
  3214. {
  3215. int d = n % rad;
  3216. n /= rad;
  3217. p[i] = number_chars[d];
  3218. }
  3219. return j;
  3220. }
  3221. SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
  3222. (SCM n, SCM radix),
  3223. "Return a string holding the external representation of the\n"
  3224. "number @var{n} in the given @var{radix}. If @var{n} is\n"
  3225. "inexact, a radix of 10 will be used.")
  3226. #define FUNC_NAME s_scm_number_to_string
  3227. {
  3228. int base;
  3229. if (SCM_UNBNDP (radix))
  3230. base = 10;
  3231. else
  3232. base = scm_to_signed_integer (radix, 2, 36);
  3233. if (SCM_I_INUMP (n))
  3234. return scm_integer_to_string_i (SCM_I_INUM (n), base);
  3235. else if (SCM_BIGP (n))
  3236. return scm_integer_to_string_z (scm_bignum (n), base);
  3237. else if (SCM_FRACTIONP (n))
  3238. return scm_string_append
  3239. (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
  3240. scm_from_latin1_string ("/"),
  3241. scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
  3242. else if (SCM_INEXACTP (n))
  3243. {
  3244. char num_buf [FLOBUFLEN];
  3245. return scm_from_latin1_stringn (num_buf, iflo2str (n, num_buf, base));
  3246. }
  3247. else
  3248. SCM_WRONG_TYPE_ARG (1, n);
  3249. }
  3250. #undef FUNC_NAME
  3251. /* These print routines used to be stubbed here so that scm_repl.c
  3252. wouldn't need SCM_BIGDIG conditionals (pre GMP) */
  3253. int
  3254. scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  3255. {
  3256. char num_buf[FLOBUFLEN];
  3257. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  3258. return !0;
  3259. }
  3260. void
  3261. scm_i_print_double (double val, SCM port)
  3262. {
  3263. char num_buf[FLOBUFLEN];
  3264. scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
  3265. }
  3266. int
  3267. scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  3268. {
  3269. char num_buf[FLOBUFLEN];
  3270. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  3271. return !0;
  3272. }
  3273. void
  3274. scm_i_print_complex (double real, double imag, SCM port)
  3275. {
  3276. char num_buf[FLOBUFLEN];
  3277. scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
  3278. }
  3279. int
  3280. scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  3281. {
  3282. SCM str;
  3283. str = scm_number_to_string (sexp, SCM_UNDEFINED);
  3284. scm_display (str, port);
  3285. scm_remember_upto_here_1 (str);
  3286. return !0;
  3287. }
  3288. int
  3289. scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  3290. {
  3291. SCM str = scm_integer_to_string_z (scm_bignum (exp), 10);
  3292. scm_c_put_string (port, str, 0, scm_c_string_length (str));
  3293. return !0;
  3294. }
  3295. /*** END nums->strs ***/
  3296. /*** STRINGS -> NUMBERS ***/
  3297. /* The following functions implement the conversion from strings to numbers.
  3298. * The implementation somehow follows the grammar for numbers as it is given
  3299. * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
  3300. * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
  3301. * points should be noted about the implementation:
  3302. *
  3303. * * Each function keeps a local index variable 'idx' that points at the
  3304. * current position within the parsed string. The global index is only
  3305. * updated if the function could parse the corresponding syntactic unit
  3306. * successfully.
  3307. *
  3308. * * Similarly, the functions keep track of indicators of inexactness ('#',
  3309. * '.' or exponents) using local variables ('hash_seen', 'x').
  3310. *
  3311. * * Sequences of digits are parsed into temporary variables holding fixnums.
  3312. * Only if these fixnums would overflow, the result variables are updated
  3313. * using the standard functions scm_add, scm_product, scm_divide etc. Then,
  3314. * the temporary variables holding the fixnums are cleared, and the process
  3315. * starts over again. If for example fixnums were able to store five decimal
  3316. * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
  3317. * and the result was computed as 12345 * 100000 + 67890. In other words,
  3318. * only every five digits two bignum operations were performed.
  3319. *
  3320. * Notes on the handling of exactness specifiers:
  3321. *
  3322. * When parsing non-real complex numbers, we apply exactness specifiers on
  3323. * per-component basis, as is done in PLT Scheme. For complex numbers
  3324. * written in rectangular form, exactness specifiers are applied to the
  3325. * real and imaginary parts before calling scm_make_rectangular. For
  3326. * complex numbers written in polar form, exactness specifiers are applied
  3327. * to the magnitude and angle before calling scm_make_polar.
  3328. *
  3329. * There are two kinds of exactness specifiers: forced and implicit. A
  3330. * forced exactness specifier is a "#e" or "#i" prefix at the beginning of
  3331. * the entire number, and applies to both components of a complex number.
  3332. * "#e" causes each component to be made exact, and "#i" causes each
  3333. * component to be made inexact. If no forced exactness specifier is
  3334. * present, then the exactness of each component is determined
  3335. * independently by the presence or absence of a decimal point or hash mark
  3336. * within that component. If a decimal point or hash mark is present, the
  3337. * component is made inexact, otherwise it is made exact.
  3338. *
  3339. * After the exactness specifiers have been applied to each component, they
  3340. * are passed to either scm_make_rectangular or scm_make_polar to produce
  3341. * the final result. Note that this will result in a real number if the
  3342. * imaginary part, magnitude, or angle is an exact 0.
  3343. *
  3344. * For example, (string->number "#i5.0+0i") does the equivalent of:
  3345. *
  3346. * (make-rectangular (exact->inexact 5) (exact->inexact 0))
  3347. */
  3348. enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
  3349. /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
  3350. /* Caller is responsible for checking that the return value is in range
  3351. for the given radix, which should be <= 36. */
  3352. static unsigned int
  3353. char_decimal_value (uint32_t c)
  3354. {
  3355. if (c >= (uint32_t) '0' && c <= (uint32_t) '9')
  3356. return c - (uint32_t) '0';
  3357. else
  3358. {
  3359. /* uc_decimal_value returns -1 on error. When cast to an unsigned int,
  3360. that's certainly above any valid decimal, so we take advantage of
  3361. that to elide some tests. */
  3362. unsigned int d = (unsigned int) uc_decimal_value (c);
  3363. /* If that failed, try extended hexadecimals, then. Only accept ascii
  3364. hexadecimals. */
  3365. if (d >= 10U)
  3366. {
  3367. c = uc_tolower (c);
  3368. if (c >= (uint32_t) 'a')
  3369. d = c - (uint32_t)'a' + 10U;
  3370. }
  3371. return d;
  3372. }
  3373. }
  3374. /* Parse the substring of MEM starting at *P_IDX for an unsigned integer
  3375. in base RADIX. Upon success, return the unsigned integer and update
  3376. *P_IDX and *P_EXACTNESS accordingly. Return #f on failure. */
  3377. static SCM
  3378. mem2uinteger (SCM mem, unsigned int *p_idx,
  3379. unsigned int radix, enum t_exactness *p_exactness)
  3380. {
  3381. unsigned int idx = *p_idx;
  3382. unsigned int hash_seen = 0;
  3383. scm_t_bits shift = 1;
  3384. scm_t_bits add = 0;
  3385. unsigned int digit_value;
  3386. SCM result;
  3387. char c;
  3388. size_t len = scm_i_string_length (mem);
  3389. if (idx == len)
  3390. return SCM_BOOL_F;
  3391. c = scm_i_string_ref (mem, idx);
  3392. digit_value = char_decimal_value (c);
  3393. if (digit_value >= radix)
  3394. return SCM_BOOL_F;
  3395. idx++;
  3396. result = SCM_I_MAKINUM (digit_value);
  3397. while (idx != len)
  3398. {
  3399. scm_t_wchar c = scm_i_string_ref (mem, idx);
  3400. if (c == '#')
  3401. {
  3402. hash_seen = 1;
  3403. digit_value = 0;
  3404. }
  3405. else if (hash_seen)
  3406. break;
  3407. else
  3408. {
  3409. digit_value = char_decimal_value (c);
  3410. /* This check catches non-decimals in addition to out-of-range
  3411. decimals. */
  3412. if (digit_value >= radix)
  3413. break;
  3414. }
  3415. idx++;
  3416. if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
  3417. {
  3418. result = scm_product (result, SCM_I_MAKINUM (shift));
  3419. if (add > 0)
  3420. result = scm_sum (result, SCM_I_MAKINUM (add));
  3421. shift = radix;
  3422. add = digit_value;
  3423. }
  3424. else
  3425. {
  3426. shift = shift * radix;
  3427. add = add * radix + digit_value;
  3428. }
  3429. };
  3430. if (shift > 1)
  3431. result = scm_product (result, SCM_I_MAKINUM (shift));
  3432. if (add > 0)
  3433. result = scm_sum (result, SCM_I_MAKINUM (add));
  3434. *p_idx = idx;
  3435. if (hash_seen)
  3436. *p_exactness = INEXACT;
  3437. return result;
  3438. }
  3439. /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
  3440. * covers the parts of the rules that start at a potential point. The value
  3441. * of the digits up to the point have been parsed by the caller and are given
  3442. * in variable result. The content of *p_exactness indicates, whether a hash
  3443. * has already been seen in the digits before the point.
  3444. */
  3445. #define DIGIT2UINT(d) (uc_numeric_value(d).numerator)
  3446. static SCM
  3447. mem2decimal_from_point (SCM result, SCM mem,
  3448. unsigned int *p_idx, enum t_exactness *p_exactness)
  3449. {
  3450. unsigned int idx = *p_idx;
  3451. enum t_exactness x = *p_exactness;
  3452. size_t len = scm_i_string_length (mem);
  3453. if (idx == len)
  3454. return result;
  3455. if (scm_i_string_ref (mem, idx) == '.')
  3456. {
  3457. scm_t_bits shift = 1;
  3458. scm_t_bits add = 0;
  3459. unsigned int digit_value;
  3460. SCM big_shift = SCM_INUM1;
  3461. idx++;
  3462. while (idx != len)
  3463. {
  3464. scm_t_wchar c = scm_i_string_ref (mem, idx);
  3465. if (uc_is_property_decimal_digit ((uint32_t) c))
  3466. {
  3467. if (x == INEXACT)
  3468. return SCM_BOOL_F;
  3469. else
  3470. digit_value = DIGIT2UINT (c);
  3471. }
  3472. else if (c == '#')
  3473. {
  3474. x = INEXACT;
  3475. digit_value = 0;
  3476. }
  3477. else
  3478. break;
  3479. idx++;
  3480. if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
  3481. {
  3482. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  3483. result = scm_product (result, SCM_I_MAKINUM (shift));
  3484. if (add > 0)
  3485. result = scm_sum (result, SCM_I_MAKINUM (add));
  3486. shift = 10;
  3487. add = digit_value;
  3488. }
  3489. else
  3490. {
  3491. shift = shift * 10;
  3492. add = add * 10 + digit_value;
  3493. }
  3494. };
  3495. if (add > 0)
  3496. {
  3497. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  3498. result = scm_product (result, SCM_I_MAKINUM (shift));
  3499. result = scm_sum (result, SCM_I_MAKINUM (add));
  3500. }
  3501. result = scm_divide (result, big_shift);
  3502. /* We've seen a decimal point, thus the value is implicitly inexact. */
  3503. x = INEXACT;
  3504. }
  3505. if (idx != len)
  3506. {
  3507. int sign = 1;
  3508. unsigned int start;
  3509. scm_t_wchar c;
  3510. int exponent;
  3511. SCM e;
  3512. /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
  3513. switch (scm_i_string_ref (mem, idx))
  3514. {
  3515. case 'd': case 'D':
  3516. case 'e': case 'E':
  3517. case 'f': case 'F':
  3518. case 'l': case 'L':
  3519. case 's': case 'S':
  3520. idx++;
  3521. if (idx == len)
  3522. return SCM_BOOL_F;
  3523. start = idx;
  3524. c = scm_i_string_ref (mem, idx);
  3525. if (c == '-')
  3526. {
  3527. idx++;
  3528. if (idx == len)
  3529. return SCM_BOOL_F;
  3530. sign = -1;
  3531. c = scm_i_string_ref (mem, idx);
  3532. }
  3533. else if (c == '+')
  3534. {
  3535. idx++;
  3536. if (idx == len)
  3537. return SCM_BOOL_F;
  3538. sign = 1;
  3539. c = scm_i_string_ref (mem, idx);
  3540. }
  3541. else
  3542. sign = 1;
  3543. if (!uc_is_property_decimal_digit ((uint32_t) c))
  3544. return SCM_BOOL_F;
  3545. idx++;
  3546. exponent = DIGIT2UINT (c);
  3547. while (idx != len)
  3548. {
  3549. scm_t_wchar c = scm_i_string_ref (mem, idx);
  3550. if (uc_is_property_decimal_digit ((uint32_t) c))
  3551. {
  3552. idx++;
  3553. if (exponent <= SCM_MAXEXP)
  3554. exponent = exponent * 10 + DIGIT2UINT (c);
  3555. }
  3556. else
  3557. break;
  3558. }
  3559. if (exponent > ((sign == 1) ? SCM_MAXEXP : SCM_MAXEXP + DBL_DIG + 1))
  3560. {
  3561. size_t exp_len = idx - start;
  3562. SCM exp_string = scm_i_substring_copy (mem, start, start + exp_len);
  3563. SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
  3564. scm_out_of_range ("string->number", exp_num);
  3565. }
  3566. e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
  3567. if (sign == 1)
  3568. result = scm_product (result, e);
  3569. else
  3570. result = scm_divide (result, e);
  3571. /* We've seen an exponent, thus the value is implicitly inexact. */
  3572. x = INEXACT;
  3573. break;
  3574. default:
  3575. break;
  3576. }
  3577. }
  3578. *p_idx = idx;
  3579. if (x == INEXACT)
  3580. *p_exactness = x;
  3581. return result;
  3582. }
  3583. /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
  3584. static SCM
  3585. mem2ureal (SCM mem, unsigned int *p_idx,
  3586. unsigned int radix, enum t_exactness forced_x,
  3587. int allow_inf_or_nan)
  3588. {
  3589. unsigned int idx = *p_idx;
  3590. SCM result;
  3591. size_t len = scm_i_string_length (mem);
  3592. /* Start off believing that the number will be exact. This changes
  3593. to INEXACT if we see a decimal point or a hash. */
  3594. enum t_exactness implicit_x = EXACT;
  3595. if (idx == len)
  3596. return SCM_BOOL_F;
  3597. if (allow_inf_or_nan && forced_x != EXACT && idx+5 <= len)
  3598. switch (scm_i_string_ref (mem, idx))
  3599. {
  3600. case 'i': case 'I':
  3601. switch (scm_i_string_ref (mem, idx + 1))
  3602. {
  3603. case 'n': case 'N':
  3604. switch (scm_i_string_ref (mem, idx + 2))
  3605. {
  3606. case 'f': case 'F':
  3607. if (scm_i_string_ref (mem, idx + 3) == '.'
  3608. && scm_i_string_ref (mem, idx + 4) == '0')
  3609. {
  3610. *p_idx = idx+5;
  3611. return scm_inf ();
  3612. }
  3613. }
  3614. }
  3615. case 'n': case 'N':
  3616. switch (scm_i_string_ref (mem, idx + 1))
  3617. {
  3618. case 'a': case 'A':
  3619. switch (scm_i_string_ref (mem, idx + 2))
  3620. {
  3621. case 'n': case 'N':
  3622. if (scm_i_string_ref (mem, idx + 3) == '.')
  3623. {
  3624. /* Cobble up the fractional part. We might want to
  3625. set the NaN's mantissa from it. */
  3626. idx += 4;
  3627. if (!scm_is_eq (mem2uinteger (mem, &idx, 10, &implicit_x),
  3628. SCM_INUM0))
  3629. return SCM_BOOL_F;
  3630. *p_idx = idx;
  3631. return scm_nan ();
  3632. }
  3633. }
  3634. }
  3635. }
  3636. if (scm_i_string_ref (mem, idx) == '.')
  3637. {
  3638. if (radix != 10)
  3639. return SCM_BOOL_F;
  3640. else if (idx + 1 == len)
  3641. return SCM_BOOL_F;
  3642. else if (!uc_is_property_decimal_digit ((uint32_t) scm_i_string_ref (mem, idx+1)))
  3643. return SCM_BOOL_F;
  3644. else
  3645. result = mem2decimal_from_point (SCM_INUM0, mem,
  3646. p_idx, &implicit_x);
  3647. }
  3648. else
  3649. {
  3650. SCM uinteger;
  3651. uinteger = mem2uinteger (mem, &idx, radix, &implicit_x);
  3652. if (scm_is_false (uinteger))
  3653. return SCM_BOOL_F;
  3654. if (idx == len)
  3655. result = uinteger;
  3656. else if (scm_i_string_ref (mem, idx) == '/')
  3657. {
  3658. SCM divisor;
  3659. idx++;
  3660. if (idx == len)
  3661. return SCM_BOOL_F;
  3662. divisor = mem2uinteger (mem, &idx, radix, &implicit_x);
  3663. if (scm_is_false (divisor) || scm_is_eq (divisor, SCM_INUM0))
  3664. return SCM_BOOL_F;
  3665. /* both are int/big here, I assume */
  3666. result = scm_i_make_ratio (uinteger, divisor);
  3667. }
  3668. else if (radix == 10)
  3669. {
  3670. result = mem2decimal_from_point (uinteger, mem, &idx, &implicit_x);
  3671. if (scm_is_false (result))
  3672. return SCM_BOOL_F;
  3673. }
  3674. else
  3675. result = uinteger;
  3676. *p_idx = idx;
  3677. }
  3678. switch (forced_x)
  3679. {
  3680. case EXACT:
  3681. if (SCM_INEXACTP (result))
  3682. return scm_inexact_to_exact (result);
  3683. else
  3684. return result;
  3685. case INEXACT:
  3686. if (SCM_INEXACTP (result))
  3687. return result;
  3688. else
  3689. return scm_exact_to_inexact (result);
  3690. case NO_EXACTNESS:
  3691. if (implicit_x == INEXACT)
  3692. {
  3693. if (SCM_INEXACTP (result))
  3694. return result;
  3695. else
  3696. return scm_exact_to_inexact (result);
  3697. }
  3698. else
  3699. return result;
  3700. }
  3701. /* We should never get here */
  3702. assert (0);
  3703. }
  3704. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  3705. static SCM
  3706. mem2complex (SCM mem, unsigned int idx,
  3707. unsigned int radix, enum t_exactness forced_x)
  3708. {
  3709. scm_t_wchar c;
  3710. int sign = 0;
  3711. SCM ureal;
  3712. size_t len = scm_i_string_length (mem);
  3713. if (idx == len)
  3714. return SCM_BOOL_F;
  3715. c = scm_i_string_ref (mem, idx);
  3716. if (c == '+')
  3717. {
  3718. idx++;
  3719. sign = 1;
  3720. }
  3721. else if (c == '-')
  3722. {
  3723. idx++;
  3724. sign = -1;
  3725. }
  3726. if (idx == len)
  3727. return SCM_BOOL_F;
  3728. ureal = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  3729. if (scm_is_false (ureal))
  3730. {
  3731. /* input must be either +i or -i */
  3732. if (sign == 0)
  3733. return SCM_BOOL_F;
  3734. if (scm_i_string_ref (mem, idx) == 'i'
  3735. || scm_i_string_ref (mem, idx) == 'I')
  3736. {
  3737. idx++;
  3738. if (idx != len)
  3739. return SCM_BOOL_F;
  3740. return scm_make_rectangular (SCM_INUM0, SCM_I_MAKINUM (sign));
  3741. }
  3742. else
  3743. return SCM_BOOL_F;
  3744. }
  3745. else
  3746. {
  3747. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  3748. ureal = scm_difference (ureal, SCM_UNDEFINED);
  3749. if (idx == len)
  3750. return ureal;
  3751. c = scm_i_string_ref (mem, idx);
  3752. switch (c)
  3753. {
  3754. case 'i': case 'I':
  3755. /* either +<ureal>i or -<ureal>i */
  3756. idx++;
  3757. if (sign == 0)
  3758. return SCM_BOOL_F;
  3759. if (idx != len)
  3760. return SCM_BOOL_F;
  3761. return scm_make_rectangular (SCM_INUM0, ureal);
  3762. case '@':
  3763. /* polar input: <real>@<real>. */
  3764. idx++;
  3765. if (idx == len)
  3766. return SCM_BOOL_F;
  3767. else
  3768. {
  3769. int sign;
  3770. SCM angle;
  3771. SCM result;
  3772. c = scm_i_string_ref (mem, idx);
  3773. if (c == '+')
  3774. {
  3775. idx++;
  3776. if (idx == len)
  3777. return SCM_BOOL_F;
  3778. sign = 1;
  3779. }
  3780. else if (c == '-')
  3781. {
  3782. idx++;
  3783. if (idx == len)
  3784. return SCM_BOOL_F;
  3785. sign = -1;
  3786. }
  3787. else
  3788. sign = 0;
  3789. angle = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  3790. if (scm_is_false (angle))
  3791. return SCM_BOOL_F;
  3792. if (idx != len)
  3793. return SCM_BOOL_F;
  3794. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  3795. angle = scm_difference (angle, SCM_UNDEFINED);
  3796. result = scm_make_polar (ureal, angle);
  3797. return result;
  3798. }
  3799. case '+':
  3800. case '-':
  3801. /* expecting input matching <real>[+-]<ureal>?i */
  3802. idx++;
  3803. if (idx == len)
  3804. return SCM_BOOL_F;
  3805. else
  3806. {
  3807. int sign = (c == '+') ? 1 : -1;
  3808. SCM imag = mem2ureal (mem, &idx, radix, forced_x, sign != 0);
  3809. if (scm_is_false (imag))
  3810. imag = SCM_I_MAKINUM (sign);
  3811. else if (sign == -1 && scm_is_false (scm_nan_p (imag)))
  3812. imag = scm_difference (imag, SCM_UNDEFINED);
  3813. if (idx == len)
  3814. return SCM_BOOL_F;
  3815. if (scm_i_string_ref (mem, idx) != 'i'
  3816. && scm_i_string_ref (mem, idx) != 'I')
  3817. return SCM_BOOL_F;
  3818. idx++;
  3819. if (idx != len)
  3820. return SCM_BOOL_F;
  3821. return scm_make_rectangular (ureal, imag);
  3822. }
  3823. default:
  3824. return SCM_BOOL_F;
  3825. }
  3826. }
  3827. }
  3828. /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
  3829. enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
  3830. SCM
  3831. scm_i_string_to_number (SCM mem, unsigned int default_radix)
  3832. {
  3833. unsigned int idx = 0;
  3834. unsigned int radix = NO_RADIX;
  3835. enum t_exactness forced_x = NO_EXACTNESS;
  3836. size_t len = scm_i_string_length (mem);
  3837. /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
  3838. while (idx + 2 < len && scm_i_string_ref (mem, idx) == '#')
  3839. {
  3840. switch (scm_i_string_ref (mem, idx + 1))
  3841. {
  3842. case 'b': case 'B':
  3843. if (radix != NO_RADIX)
  3844. return SCM_BOOL_F;
  3845. radix = DUAL;
  3846. break;
  3847. case 'd': case 'D':
  3848. if (radix != NO_RADIX)
  3849. return SCM_BOOL_F;
  3850. radix = DEC;
  3851. break;
  3852. case 'i': case 'I':
  3853. if (forced_x != NO_EXACTNESS)
  3854. return SCM_BOOL_F;
  3855. forced_x = INEXACT;
  3856. break;
  3857. case 'e': case 'E':
  3858. if (forced_x != NO_EXACTNESS)
  3859. return SCM_BOOL_F;
  3860. forced_x = EXACT;
  3861. break;
  3862. case 'o': case 'O':
  3863. if (radix != NO_RADIX)
  3864. return SCM_BOOL_F;
  3865. radix = OCT;
  3866. break;
  3867. case 'x': case 'X':
  3868. if (radix != NO_RADIX)
  3869. return SCM_BOOL_F;
  3870. radix = HEX;
  3871. break;
  3872. default:
  3873. return SCM_BOOL_F;
  3874. }
  3875. idx += 2;
  3876. }
  3877. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  3878. if (radix == NO_RADIX)
  3879. radix = default_radix;
  3880. return mem2complex (mem, idx, radix, forced_x);
  3881. }
  3882. SCM
  3883. scm_c_locale_stringn_to_number (const char* mem, size_t len,
  3884. unsigned int default_radix)
  3885. {
  3886. SCM str = scm_from_locale_stringn (mem, len);
  3887. return scm_i_string_to_number (str, default_radix);
  3888. }
  3889. SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
  3890. (SCM string, SCM radix),
  3891. "Return a number of the maximally precise representation\n"
  3892. "expressed by the given @var{string}. @var{radix} must be an\n"
  3893. "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
  3894. "is a default radix that may be overridden by an explicit radix\n"
  3895. "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
  3896. "supplied, then the default radix is 10. If string is not a\n"
  3897. "syntactically valid notation for a number, then\n"
  3898. "@code{string->number} returns @code{#f}.")
  3899. #define FUNC_NAME s_scm_string_to_number
  3900. {
  3901. SCM answer;
  3902. unsigned int base;
  3903. SCM_VALIDATE_STRING (1, string);
  3904. if (SCM_UNBNDP (radix))
  3905. base = 10;
  3906. else
  3907. base = scm_to_unsigned_integer (radix, 2, INT_MAX);
  3908. answer = scm_i_string_to_number (string, base);
  3909. scm_remember_upto_here_1 (string);
  3910. return answer;
  3911. }
  3912. #undef FUNC_NAME
  3913. /*** END strs->nums ***/
  3914. SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
  3915. (SCM x),
  3916. "Return @code{#t} if @var{x} is a number, @code{#f}\n"
  3917. "otherwise.")
  3918. #define FUNC_NAME s_scm_number_p
  3919. {
  3920. return scm_from_bool (SCM_NUMBERP (x));
  3921. }
  3922. #undef FUNC_NAME
  3923. SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
  3924. (SCM x),
  3925. "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
  3926. "otherwise. Note that the sets of real, rational and integer\n"
  3927. "values form subsets of the set of complex numbers, i. e. the\n"
  3928. "predicate will also be fulfilled if @var{x} is a real,\n"
  3929. "rational or integer number.")
  3930. #define FUNC_NAME s_scm_complex_p
  3931. {
  3932. /* all numbers are complex. */
  3933. return scm_number_p (x);
  3934. }
  3935. #undef FUNC_NAME
  3936. SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
  3937. (SCM x),
  3938. "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
  3939. "otherwise. Note that the set of integer values forms a subset of\n"
  3940. "the set of real numbers, i. e. the predicate will also be\n"
  3941. "fulfilled if @var{x} is an integer number.")
  3942. #define FUNC_NAME s_scm_real_p
  3943. {
  3944. return scm_from_bool
  3945. (SCM_I_INUMP (x) || SCM_REALP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x));
  3946. }
  3947. #undef FUNC_NAME
  3948. SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
  3949. (SCM x),
  3950. "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
  3951. "otherwise. Note that the set of integer values forms a subset of\n"
  3952. "the set of rational numbers, i. e. the predicate will also be\n"
  3953. "fulfilled if @var{x} is an integer number.")
  3954. #define FUNC_NAME s_scm_rational_p
  3955. {
  3956. if (SCM_I_INUMP (x) || SCM_BIGP (x) || SCM_FRACTIONP (x))
  3957. return SCM_BOOL_T;
  3958. else if (SCM_REALP (x))
  3959. /* due to their limited precision, finite floating point numbers are
  3960. rational as well. (finite means neither infinity nor a NaN) */
  3961. return scm_from_bool (isfinite (SCM_REAL_VALUE (x)));
  3962. else
  3963. return SCM_BOOL_F;
  3964. }
  3965. #undef FUNC_NAME
  3966. SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
  3967. (SCM x),
  3968. "Return @code{#t} if @var{x} is an integer number,\n"
  3969. "else return @code{#f}.")
  3970. #define FUNC_NAME s_scm_integer_p
  3971. {
  3972. return scm_from_bool (scm_is_integer (x));
  3973. }
  3974. #undef FUNC_NAME
  3975. SCM_DEFINE (scm_exact_integer_p, "exact-integer?", 1, 0, 0,
  3976. (SCM x),
  3977. "Return @code{#t} if @var{x} is an exact integer number,\n"
  3978. "else return @code{#f}.")
  3979. #define FUNC_NAME s_scm_exact_integer_p
  3980. {
  3981. return scm_from_bool (scm_is_exact_integer (x));
  3982. }
  3983. #undef FUNC_NAME
  3984. SCM
  3985. scm_bigequal (SCM x, SCM y)
  3986. {
  3987. return scm_from_bool
  3988. (scm_is_integer_equal_zz (scm_bignum (x), scm_bignum (y)));
  3989. }
  3990. SCM scm_i_num_eq_p (SCM, SCM, SCM);
  3991. SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1,
  3992. (SCM x, SCM y, SCM rest),
  3993. "Return @code{#t} if all parameters are numerically equal.")
  3994. #define FUNC_NAME s_scm_i_num_eq_p
  3995. {
  3996. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  3997. return SCM_BOOL_T;
  3998. while (!scm_is_null (rest))
  3999. {
  4000. if (scm_is_false (scm_num_eq_p (x, y)))
  4001. return SCM_BOOL_F;
  4002. x = y;
  4003. y = scm_car (rest);
  4004. rest = scm_cdr (rest);
  4005. }
  4006. return scm_num_eq_p (x, y);
  4007. }
  4008. #undef FUNC_NAME
  4009. SCM
  4010. scm_num_eq_p (SCM x, SCM y)
  4011. {
  4012. if (SCM_I_INUMP (x))
  4013. {
  4014. if (SCM_I_INUMP (y))
  4015. return scm_eq_p (x, y);
  4016. else if (SCM_BIGP (y))
  4017. return SCM_BOOL_F;
  4018. else if (SCM_REALP (y))
  4019. return scm_from_bool
  4020. (scm_is_integer_equal_ir (SCM_I_INUM (x), SCM_REAL_VALUE (y)));
  4021. else if (SCM_COMPLEXP (y))
  4022. return scm_from_bool
  4023. (scm_is_integer_equal_ic (SCM_I_INUM (x), SCM_COMPLEX_REAL (y),
  4024. SCM_COMPLEX_IMAG (y)));
  4025. else if (SCM_FRACTIONP (y))
  4026. return SCM_BOOL_F;
  4027. else
  4028. return scm_num_eq_p (y, x);
  4029. }
  4030. else if (SCM_BIGP (x))
  4031. {
  4032. if (SCM_BIGP (y))
  4033. return scm_from_bool
  4034. (scm_is_integer_equal_zz (scm_bignum (x), scm_bignum (y)));
  4035. else if (SCM_REALP (y))
  4036. return scm_from_bool
  4037. (scm_is_integer_equal_zr (scm_bignum (x), SCM_REAL_VALUE (y)));
  4038. else if (SCM_COMPLEXP (y))
  4039. return scm_from_bool
  4040. (scm_is_integer_equal_zc (scm_bignum (x), SCM_COMPLEX_REAL (y),
  4041. SCM_COMPLEX_IMAG (y)));
  4042. else if (SCM_FRACTIONP (y))
  4043. return SCM_BOOL_F;
  4044. else
  4045. return scm_num_eq_p (y, x);
  4046. }
  4047. else if (SCM_REALP (x))
  4048. {
  4049. if (SCM_REALP (y))
  4050. return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  4051. else if (SCM_COMPLEXP (y))
  4052. return scm_from_bool (SCM_COMPLEX_IMAG (y) == 0.0
  4053. && SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y));
  4054. else if (SCM_FRACTIONP (y))
  4055. {
  4056. if (isnan (SCM_REAL_VALUE (x)) || isinf (SCM_REAL_VALUE (x)))
  4057. return SCM_BOOL_F;
  4058. return scm_num_eq_p (scm_inexact_to_exact (x), y);
  4059. }
  4060. else
  4061. return scm_num_eq_p (y, x);
  4062. }
  4063. else if (SCM_COMPLEXP (x))
  4064. {
  4065. if (SCM_COMPLEXP (y))
  4066. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
  4067. && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
  4068. else if (SCM_FRACTIONP (y))
  4069. {
  4070. if (SCM_COMPLEX_IMAG (x) != 0.0
  4071. || isnan (SCM_COMPLEX_REAL (x))
  4072. || isinf (SCM_COMPLEX_REAL (x)))
  4073. return SCM_BOOL_F;
  4074. return scm_num_eq_p (scm_inexact_to_exact (x), y);
  4075. }
  4076. else
  4077. return scm_num_eq_p (y, x);
  4078. }
  4079. else if (SCM_FRACTIONP (x))
  4080. {
  4081. if (SCM_FRACTIONP (y))
  4082. return scm_i_fraction_equalp (x, y);
  4083. else
  4084. return scm_num_eq_p (y, x);
  4085. }
  4086. else
  4087. return scm_wta_dispatch_2 (g_scm_i_num_eq_p, x, y, SCM_ARG1,
  4088. s_scm_i_num_eq_p);
  4089. }
  4090. /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
  4091. done are good for inums, but for bignums an answer can almost always be
  4092. had by just examining a few high bits of the operands, as done by GMP in
  4093. mpq_cmp. flonum/frac compares likewise, but with the slight complication
  4094. of the float exponent to take into account. */
  4095. static int scm_is_less_than (SCM x, SCM y);
  4096. static int scm_is_greater_than (SCM x, SCM y);
  4097. static int scm_is_less_than_or_equal (SCM x, SCM y);
  4098. static int scm_is_greater_than_or_equal (SCM x, SCM y);
  4099. static int
  4100. scm_is_less_than (SCM x, SCM y)
  4101. {
  4102. if (SCM_I_INUMP (x))
  4103. {
  4104. if (SCM_I_INUMP (y))
  4105. return SCM_I_INUM (x) < SCM_I_INUM (y);
  4106. else if (SCM_BIGP (y))
  4107. return scm_is_integer_positive_z (scm_bignum (y));
  4108. else if (SCM_REALP (y))
  4109. return scm_is_integer_less_than_ir (SCM_I_INUM (x), SCM_REAL_VALUE (y));
  4110. if (!SCM_FRACTIONP (y))
  4111. abort ();
  4112. /* "x < a/b" becomes "x*b < a" */
  4113. return scm_is_less_than (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4114. SCM_FRACTION_NUMERATOR (y));
  4115. }
  4116. else if (SCM_BIGP (x))
  4117. {
  4118. if (SCM_I_INUMP (y))
  4119. return scm_is_integer_negative_z (scm_bignum (x));
  4120. else if (SCM_BIGP (y))
  4121. return scm_is_integer_less_than_zz (scm_bignum (x), scm_bignum (y));
  4122. else if (SCM_REALP (y))
  4123. return scm_is_integer_less_than_zr (scm_bignum (x), SCM_REAL_VALUE (y));
  4124. if (!SCM_FRACTIONP (y))
  4125. abort ();
  4126. /* "x < a/b" becomes "x*b < a" */
  4127. return scm_is_less_than (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4128. SCM_FRACTION_NUMERATOR (y));
  4129. }
  4130. else if (SCM_REALP (x))
  4131. {
  4132. if (SCM_I_INUMP (y))
  4133. return scm_is_integer_less_than_ri (SCM_REAL_VALUE (x), SCM_I_INUM (y));
  4134. else if (SCM_BIGP (y))
  4135. return scm_is_integer_less_than_rz (SCM_REAL_VALUE (x), scm_bignum (y));
  4136. else if (SCM_REALP (y))
  4137. return SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y);
  4138. if (!SCM_FRACTIONP (y))
  4139. abort ();
  4140. if (isnan (SCM_REAL_VALUE (x)))
  4141. return 0;
  4142. if (isinf (SCM_REAL_VALUE (x)))
  4143. return SCM_REAL_VALUE (x) < 0.0;
  4144. return scm_is_less_than (scm_inexact_to_exact (x), y);
  4145. }
  4146. if (!SCM_FRACTIONP (x))
  4147. abort ();
  4148. /* "a/b < " becomes "a < y*b" */
  4149. return scm_is_less_than (SCM_FRACTION_NUMERATOR (x),
  4150. scm_product (y, SCM_FRACTION_DENOMINATOR (x)));
  4151. }
  4152. static int
  4153. scm_is_greater_than (SCM x, SCM y)
  4154. {
  4155. return scm_is_less_than (y, x);
  4156. }
  4157. static int
  4158. scm_is_less_than_or_equal (SCM x, SCM y)
  4159. {
  4160. if ((SCM_REALP (x) && isnan (SCM_REAL_VALUE (x)))
  4161. || (SCM_REALP (y) && isnan (SCM_REAL_VALUE (y))))
  4162. return 0;
  4163. return !scm_is_less_than (y, x);
  4164. }
  4165. static int
  4166. scm_is_greater_than_or_equal (SCM x, SCM y)
  4167. {
  4168. return scm_is_less_than_or_equal (y, x);
  4169. }
  4170. SCM_INTERNAL SCM scm_i_num_less_p (SCM, SCM, SCM);
  4171. SCM_PRIMITIVE_GENERIC (scm_i_num_less_p, "<", 0, 2, 1,
  4172. (SCM x, SCM y, SCM rest),
  4173. "Return @code{#t} if the list of parameters is monotonically\n"
  4174. "increasing.")
  4175. #define FUNC_NAME s_scm_i_num_less_p
  4176. {
  4177. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  4178. return SCM_BOOL_T;
  4179. while (!scm_is_null (rest))
  4180. {
  4181. if (scm_is_false (scm_less_p (x, y)))
  4182. return SCM_BOOL_F;
  4183. x = y;
  4184. y = scm_car (rest);
  4185. rest = scm_cdr (rest);
  4186. }
  4187. return scm_less_p (x, y);
  4188. }
  4189. #undef FUNC_NAME
  4190. #define FUNC_NAME s_scm_i_num_less_p
  4191. SCM
  4192. scm_less_p (SCM x, SCM y)
  4193. {
  4194. if (!scm_is_real (x))
  4195. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARG1, FUNC_NAME);
  4196. if (!scm_is_real (y))
  4197. return scm_wta_dispatch_2 (g_scm_i_num_less_p, x, y, SCM_ARG2, FUNC_NAME);
  4198. return scm_from_bool (scm_is_less_than (x, y));
  4199. }
  4200. #undef FUNC_NAME
  4201. SCM scm_i_num_gr_p (SCM, SCM, SCM);
  4202. SCM_PRIMITIVE_GENERIC (scm_i_num_gr_p, ">", 0, 2, 1,
  4203. (SCM x, SCM y, SCM rest),
  4204. "Return @code{#t} if the list of parameters is monotonically\n"
  4205. "decreasing.")
  4206. #define FUNC_NAME s_scm_i_num_gr_p
  4207. {
  4208. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  4209. return SCM_BOOL_T;
  4210. while (!scm_is_null (rest))
  4211. {
  4212. if (scm_is_false (scm_gr_p (x, y)))
  4213. return SCM_BOOL_F;
  4214. x = y;
  4215. y = scm_car (rest);
  4216. rest = scm_cdr (rest);
  4217. }
  4218. return scm_gr_p (x, y);
  4219. }
  4220. #undef FUNC_NAME
  4221. #define FUNC_NAME s_scm_i_num_gr_p
  4222. SCM
  4223. scm_gr_p (SCM x, SCM y)
  4224. {
  4225. if (!scm_is_real (x))
  4226. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG1, FUNC_NAME);
  4227. if (!scm_is_real (y))
  4228. return scm_wta_dispatch_2 (g_scm_i_num_gr_p, x, y, SCM_ARG2, FUNC_NAME);
  4229. return scm_from_bool (scm_is_greater_than (x, y));
  4230. }
  4231. #undef FUNC_NAME
  4232. SCM scm_i_num_leq_p (SCM, SCM, SCM);
  4233. SCM_PRIMITIVE_GENERIC (scm_i_num_leq_p, "<=", 0, 2, 1,
  4234. (SCM x, SCM y, SCM rest),
  4235. "Return @code{#t} if the list of parameters is monotonically\n"
  4236. "non-decreasing.")
  4237. #define FUNC_NAME s_scm_i_num_leq_p
  4238. {
  4239. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  4240. return SCM_BOOL_T;
  4241. while (!scm_is_null (rest))
  4242. {
  4243. if (scm_is_false (scm_leq_p (x, y)))
  4244. return SCM_BOOL_F;
  4245. x = y;
  4246. y = scm_car (rest);
  4247. rest = scm_cdr (rest);
  4248. }
  4249. return scm_leq_p (x, y);
  4250. }
  4251. #undef FUNC_NAME
  4252. #define FUNC_NAME s_scm_i_num_leq_p
  4253. SCM
  4254. scm_leq_p (SCM x, SCM y)
  4255. {
  4256. if (!scm_is_real (x))
  4257. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG1, FUNC_NAME);
  4258. if (!scm_is_real (y))
  4259. return scm_wta_dispatch_2 (g_scm_i_num_leq_p, x, y, SCM_ARG2, FUNC_NAME);
  4260. return scm_from_bool (scm_is_less_than_or_equal (x, y));
  4261. }
  4262. #undef FUNC_NAME
  4263. SCM scm_i_num_geq_p (SCM, SCM, SCM);
  4264. SCM_PRIMITIVE_GENERIC (scm_i_num_geq_p, ">=", 0, 2, 1,
  4265. (SCM x, SCM y, SCM rest),
  4266. "Return @code{#t} if the list of parameters is monotonically\n"
  4267. "non-increasing.")
  4268. #define FUNC_NAME s_scm_i_num_geq_p
  4269. {
  4270. if (SCM_UNBNDP (x) || SCM_UNBNDP (y))
  4271. return SCM_BOOL_T;
  4272. while (!scm_is_null (rest))
  4273. {
  4274. if (scm_is_false (scm_geq_p (x, y)))
  4275. return SCM_BOOL_F;
  4276. x = y;
  4277. y = scm_car (rest);
  4278. rest = scm_cdr (rest);
  4279. }
  4280. return scm_geq_p (x, y);
  4281. }
  4282. #undef FUNC_NAME
  4283. #define FUNC_NAME s_scm_i_num_geq_p
  4284. SCM
  4285. scm_geq_p (SCM x, SCM y)
  4286. {
  4287. if (!scm_is_real (x))
  4288. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG1, FUNC_NAME);
  4289. if (!scm_is_real (y))
  4290. return scm_wta_dispatch_2 (g_scm_i_num_geq_p, x, y, SCM_ARG2, FUNC_NAME);
  4291. return scm_from_bool (scm_is_greater_than_or_equal (x, y));
  4292. }
  4293. #undef FUNC_NAME
  4294. SCM_PRIMITIVE_GENERIC (scm_zero_p, "zero?", 1, 0, 0,
  4295. (SCM z),
  4296. "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
  4297. "zero.")
  4298. #define FUNC_NAME s_scm_zero_p
  4299. {
  4300. if (SCM_I_INUMP (z))
  4301. return scm_from_bool (scm_is_eq (z, SCM_INUM0));
  4302. else if (SCM_BIGP (z))
  4303. return SCM_BOOL_F;
  4304. else if (SCM_REALP (z))
  4305. return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
  4306. else if (SCM_COMPLEXP (z))
  4307. return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
  4308. && SCM_COMPLEX_IMAG (z) == 0.0);
  4309. else if (SCM_FRACTIONP (z))
  4310. return SCM_BOOL_F;
  4311. else
  4312. return scm_wta_dispatch_1 (g_scm_zero_p, z, SCM_ARG1, s_scm_zero_p);
  4313. }
  4314. #undef FUNC_NAME
  4315. SCM_PRIMITIVE_GENERIC (scm_positive_p, "positive?", 1, 0, 0,
  4316. (SCM x),
  4317. "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
  4318. "zero.")
  4319. #define FUNC_NAME s_scm_positive_p
  4320. {
  4321. if (SCM_I_INUMP (x))
  4322. return scm_from_bool (SCM_I_INUM (x) > 0);
  4323. else if (SCM_BIGP (x))
  4324. return scm_from_bool (scm_is_integer_positive_z (scm_bignum (x)));
  4325. else if (SCM_REALP (x))
  4326. return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
  4327. else if (SCM_FRACTIONP (x))
  4328. return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
  4329. else
  4330. return scm_wta_dispatch_1 (g_scm_positive_p, x, SCM_ARG1, s_scm_positive_p);
  4331. }
  4332. #undef FUNC_NAME
  4333. SCM_PRIMITIVE_GENERIC (scm_negative_p, "negative?", 1, 0, 0,
  4334. (SCM x),
  4335. "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
  4336. "zero.")
  4337. #define FUNC_NAME s_scm_negative_p
  4338. {
  4339. if (SCM_I_INUMP (x))
  4340. return scm_from_bool (SCM_I_INUM (x) < 0);
  4341. else if (SCM_BIGP (x))
  4342. return scm_from_bool (scm_is_integer_negative_z (scm_bignum (x)));
  4343. else if (SCM_REALP (x))
  4344. return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
  4345. else if (SCM_FRACTIONP (x))
  4346. return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
  4347. else
  4348. return scm_wta_dispatch_1 (g_scm_negative_p, x, SCM_ARG1, s_scm_negative_p);
  4349. }
  4350. #undef FUNC_NAME
  4351. /* scm_min and scm_max return an inexact when either argument is inexact, as
  4352. required by r5rs. On that basis, for exact/inexact combinations the
  4353. exact is converted to inexact to compare and possibly return. This is
  4354. unlike scm_less_p above which takes some trouble to preserve all bits in
  4355. its test, such trouble is not required for min and max. */
  4356. SCM_PRIMITIVE_GENERIC (scm_i_max, "max", 0, 2, 1,
  4357. (SCM x, SCM y, SCM rest),
  4358. "Return the maximum of all parameter values.")
  4359. #define FUNC_NAME s_scm_i_max
  4360. {
  4361. while (!scm_is_null (rest))
  4362. { x = scm_max (x, y);
  4363. y = scm_car (rest);
  4364. rest = scm_cdr (rest);
  4365. }
  4366. return scm_max (x, y);
  4367. }
  4368. #undef FUNC_NAME
  4369. SCM
  4370. scm_max (SCM x, SCM y)
  4371. {
  4372. if (SCM_UNBNDP (y))
  4373. {
  4374. if (SCM_UNBNDP (x))
  4375. return scm_wta_dispatch_0 (g_scm_i_max, s_scm_i_max);
  4376. else if (scm_is_real (x))
  4377. return x;
  4378. else
  4379. return scm_wta_dispatch_1 (g_scm_i_max, x, SCM_ARG1, s_scm_i_max);
  4380. }
  4381. if (!scm_is_real (x))
  4382. return scm_wta_dispatch_2 (g_scm_i_max, x, y, SCM_ARG1, s_scm_i_max);
  4383. if (!scm_is_real (y))
  4384. return scm_wta_dispatch_2 (g_scm_i_max, x, y, SCM_ARG2, s_scm_i_max);
  4385. if (scm_is_exact (x) && scm_is_exact (y))
  4386. return scm_is_less_than (x, y) ? y : x;
  4387. x = SCM_REALP (x) ? x : scm_exact_to_inexact (x);
  4388. y = SCM_REALP (y) ? y : scm_exact_to_inexact (y);
  4389. double xx = SCM_REAL_VALUE (x);
  4390. double yy = SCM_REAL_VALUE (y);
  4391. if (isnan (xx))
  4392. return x;
  4393. if (isnan (yy))
  4394. return y;
  4395. if (xx < yy)
  4396. return y;
  4397. if (xx > yy)
  4398. return x;
  4399. // Distinguish -0.0 from 0.0.
  4400. return (copysign (1.0, xx) < 0) ? y : x;
  4401. }
  4402. SCM_PRIMITIVE_GENERIC (scm_i_min, "min", 0, 2, 1,
  4403. (SCM x, SCM y, SCM rest),
  4404. "Return the minimum of all parameter values.")
  4405. #define FUNC_NAME s_scm_i_min
  4406. {
  4407. while (!scm_is_null (rest))
  4408. { x = scm_min (x, y);
  4409. y = scm_car (rest);
  4410. rest = scm_cdr (rest);
  4411. }
  4412. return scm_min (x, y);
  4413. }
  4414. #undef FUNC_NAME
  4415. SCM
  4416. scm_min (SCM x, SCM y)
  4417. {
  4418. if (SCM_UNBNDP (y))
  4419. {
  4420. if (SCM_UNBNDP (x))
  4421. return scm_wta_dispatch_0 (g_scm_i_min, s_scm_i_min);
  4422. else if (scm_is_real (x))
  4423. return x;
  4424. else
  4425. return scm_wta_dispatch_1 (g_scm_i_min, x, SCM_ARG1, s_scm_i_min);
  4426. }
  4427. if (!scm_is_real (x))
  4428. return scm_wta_dispatch_2 (g_scm_i_min, x, y, SCM_ARG1, s_scm_i_min);
  4429. if (!scm_is_real (y))
  4430. return scm_wta_dispatch_2 (g_scm_i_min, x, y, SCM_ARG2, s_scm_i_min);
  4431. if (scm_is_exact (x) && scm_is_exact (y))
  4432. return scm_is_less_than (x, y) ? x : y;
  4433. x = SCM_REALP (x) ? x : scm_exact_to_inexact (x);
  4434. y = SCM_REALP (y) ? y : scm_exact_to_inexact (y);
  4435. double xx = SCM_REAL_VALUE (x);
  4436. double yy = SCM_REAL_VALUE (y);
  4437. if (isnan (xx))
  4438. return x;
  4439. if (isnan (yy))
  4440. return y;
  4441. if (xx < yy)
  4442. return x;
  4443. if (xx > yy)
  4444. return y;
  4445. // Distinguish -0.0 from 0.0.
  4446. return (copysign (1.0, xx) < 0) ? x : y;
  4447. }
  4448. SCM_PRIMITIVE_GENERIC (scm_i_sum, "+", 0, 2, 1,
  4449. (SCM x, SCM y, SCM rest),
  4450. "Return the sum of all parameter values. Return 0 if called without\n"
  4451. "any parameters." )
  4452. #define FUNC_NAME s_scm_i_sum
  4453. {
  4454. while (!scm_is_null (rest))
  4455. { x = scm_sum (x, y);
  4456. y = scm_car (rest);
  4457. rest = scm_cdr (rest);
  4458. }
  4459. return scm_sum (x, y);
  4460. }
  4461. #undef FUNC_NAME
  4462. static SCM
  4463. sum (SCM x, SCM y)
  4464. {
  4465. if (SCM_I_INUMP (x))
  4466. {
  4467. if (SCM_I_INUMP (y))
  4468. return scm_integer_add_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  4469. else if (SCM_BIGP (y))
  4470. return scm_integer_add_zi (scm_bignum (y), SCM_I_INUM (x));
  4471. else if (SCM_REALP (y))
  4472. return scm_i_from_double (SCM_I_INUM (x) + SCM_REAL_VALUE (y));
  4473. else if (SCM_COMPLEXP (y))
  4474. return scm_c_make_rectangular (SCM_I_INUM (x) + SCM_COMPLEX_REAL (y),
  4475. SCM_COMPLEX_IMAG (y));
  4476. else if (SCM_FRACTIONP (y))
  4477. return scm_i_make_ratio
  4478. (scm_sum (SCM_FRACTION_NUMERATOR (y),
  4479. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  4480. SCM_FRACTION_DENOMINATOR (y));
  4481. abort (); /* Unreachable. */
  4482. }
  4483. else if (SCM_BIGP (x))
  4484. {
  4485. if (SCM_BIGP (y))
  4486. return scm_integer_add_zz (scm_bignum (x), scm_bignum (y));
  4487. else if (SCM_REALP (y))
  4488. return scm_i_from_double (scm_integer_to_double_z (scm_bignum (x))
  4489. + SCM_REAL_VALUE (y));
  4490. else if (SCM_COMPLEXP (y))
  4491. return scm_c_make_rectangular (scm_integer_to_double_z (scm_bignum (x))
  4492. + SCM_COMPLEX_REAL (y),
  4493. SCM_COMPLEX_IMAG (y));
  4494. else if (SCM_FRACTIONP (y))
  4495. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  4496. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  4497. SCM_FRACTION_DENOMINATOR (y));
  4498. else
  4499. return sum (y, x);
  4500. }
  4501. else if (SCM_REALP (x))
  4502. {
  4503. if (SCM_REALP (y))
  4504. return scm_i_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
  4505. else if (SCM_COMPLEXP (y))
  4506. return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
  4507. SCM_COMPLEX_IMAG (y));
  4508. else if (SCM_FRACTIONP (y))
  4509. return scm_i_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
  4510. else
  4511. return sum (y, x);
  4512. }
  4513. else if (SCM_COMPLEXP (x))
  4514. {
  4515. if (SCM_COMPLEXP (y))
  4516. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
  4517. SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
  4518. else if (SCM_FRACTIONP (y))
  4519. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
  4520. SCM_COMPLEX_IMAG (x));
  4521. else
  4522. return sum (y, x);
  4523. }
  4524. else if (SCM_FRACTIONP (x))
  4525. {
  4526. if (SCM_FRACTIONP (y))
  4527. {
  4528. SCM nx = SCM_FRACTION_NUMERATOR (x);
  4529. SCM ny = SCM_FRACTION_NUMERATOR (y);
  4530. SCM dx = SCM_FRACTION_DENOMINATOR (x);
  4531. SCM dy = SCM_FRACTION_DENOMINATOR (y);
  4532. return scm_i_make_ratio (scm_sum (scm_product (nx, dy),
  4533. scm_product (ny, dx)),
  4534. scm_product (dx, dy));
  4535. }
  4536. else
  4537. return sum (y, x);
  4538. }
  4539. else
  4540. abort (); /* Unreachable. */
  4541. }
  4542. SCM
  4543. scm_sum (SCM x, SCM y)
  4544. {
  4545. if (SCM_UNBNDP (y))
  4546. {
  4547. if (SCM_NUMBERP (x)) return x;
  4548. if (SCM_UNBNDP (x)) return SCM_INUM0;
  4549. return scm_wta_dispatch_1 (g_scm_i_sum, x, SCM_ARG1, s_scm_i_sum);
  4550. }
  4551. if (!SCM_NUMBERP (x))
  4552. return scm_wta_dispatch_2 (g_scm_i_sum, x, y, SCM_ARG1, s_scm_i_sum);
  4553. if (!SCM_NUMBERP (y))
  4554. return scm_wta_dispatch_2 (g_scm_i_sum, x, y, SCM_ARG2, s_scm_i_sum);
  4555. return sum (x, y);
  4556. }
  4557. SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
  4558. (SCM x),
  4559. "Return @math{@var{x}+1}.")
  4560. #define FUNC_NAME s_scm_oneplus
  4561. {
  4562. return scm_sum (x, SCM_INUM1);
  4563. }
  4564. #undef FUNC_NAME
  4565. static SCM
  4566. negate (SCM x)
  4567. {
  4568. if (SCM_I_INUMP (x))
  4569. return scm_integer_negate_i (SCM_I_INUM (x));
  4570. else if (SCM_BIGP (x))
  4571. return scm_integer_negate_z (scm_bignum (x));
  4572. else if (SCM_REALP (x))
  4573. return scm_i_from_double (-SCM_REAL_VALUE (x));
  4574. else if (SCM_COMPLEXP (x))
  4575. return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
  4576. -SCM_COMPLEX_IMAG (x));
  4577. else if (SCM_FRACTIONP (x))
  4578. return scm_i_make_ratio_already_reduced
  4579. (negate (SCM_FRACTION_NUMERATOR (x)), SCM_FRACTION_DENOMINATOR (x));
  4580. else
  4581. abort (); /* Unreachable. */
  4582. }
  4583. static SCM
  4584. difference (SCM x, SCM y)
  4585. {
  4586. if (SCM_I_INUMP (x))
  4587. {
  4588. if (SCM_I_INUM (x) == 0)
  4589. /* We need to handle x == exact 0 specially because R6RS states
  4590. that:
  4591. (- 0.0) ==> -0.0 and
  4592. (- 0.0 0.0) ==> 0.0
  4593. and the scheme compiler changes
  4594. (- 0.0) into (- 0 0.0)
  4595. So we need to treat (- 0 0.0) like (- 0.0).
  4596. At the C level, (-x) is different than (0.0 - x).
  4597. (0.0 - 0.0) ==> 0.0, but (- 0.0) ==> -0.0. */
  4598. return negate (y);
  4599. if (SCM_I_INUMP (y))
  4600. return scm_integer_sub_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  4601. else if (SCM_BIGP (y))
  4602. return scm_integer_sub_iz (SCM_I_INUM (x), scm_bignum (y));
  4603. else if (SCM_REALP (y))
  4604. return scm_i_from_double (SCM_I_INUM (x) - SCM_REAL_VALUE (y));
  4605. else if (SCM_COMPLEXP (y))
  4606. return scm_c_make_rectangular (SCM_I_INUM (x) - SCM_COMPLEX_REAL (y),
  4607. - SCM_COMPLEX_IMAG (y));
  4608. else if (SCM_FRACTIONP (y))
  4609. /* a - b/c = (ac - b) / c */
  4610. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4611. SCM_FRACTION_NUMERATOR (y)),
  4612. SCM_FRACTION_DENOMINATOR (y));
  4613. else
  4614. abort (); /* Unreachable. */
  4615. }
  4616. else if (SCM_BIGP (x))
  4617. {
  4618. if (SCM_I_INUMP (y))
  4619. return scm_integer_sub_zi (scm_bignum (x), SCM_I_INUM (y));
  4620. else if (SCM_BIGP (y))
  4621. return scm_integer_sub_zz (scm_bignum (x), scm_bignum (y));
  4622. else if (SCM_REALP (y))
  4623. return scm_i_from_double (scm_integer_to_double_z (scm_bignum (x))
  4624. - SCM_REAL_VALUE (y));
  4625. else if (SCM_COMPLEXP (y))
  4626. return scm_c_make_rectangular
  4627. (scm_integer_to_double_z (scm_bignum (x)) - SCM_COMPLEX_REAL (y),
  4628. -SCM_COMPLEX_IMAG (y));
  4629. else if (SCM_FRACTIONP (y))
  4630. return scm_i_make_ratio
  4631. (difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4632. SCM_FRACTION_NUMERATOR (y)),
  4633. SCM_FRACTION_DENOMINATOR (y));
  4634. else
  4635. abort (); /* Unreachable. */
  4636. }
  4637. else if (SCM_REALP (x))
  4638. {
  4639. double r = SCM_REAL_VALUE (x);
  4640. if (SCM_I_INUMP (y))
  4641. return scm_i_from_double (r - SCM_I_INUM (y));
  4642. else if (SCM_BIGP (y))
  4643. return scm_i_from_double (r - scm_integer_to_double_z (scm_bignum (y)));
  4644. else if (SCM_REALP (y))
  4645. return scm_i_from_double (r - SCM_REAL_VALUE (y));
  4646. else if (SCM_COMPLEXP (y))
  4647. return scm_c_make_rectangular (r - SCM_COMPLEX_REAL (y),
  4648. -SCM_COMPLEX_IMAG (y));
  4649. else if (SCM_FRACTIONP (y))
  4650. return scm_i_from_double (r - scm_i_fraction2double (y));
  4651. else
  4652. abort (); /* Unreachable. */
  4653. }
  4654. else if (SCM_COMPLEXP (x))
  4655. {
  4656. double r = SCM_COMPLEX_REAL (x);
  4657. double i = SCM_COMPLEX_IMAG (x);
  4658. if (SCM_I_INUMP (y))
  4659. r -= SCM_I_INUM (y);
  4660. else if (SCM_BIGP (y))
  4661. r -= scm_integer_to_double_z (scm_bignum (y));
  4662. else if (SCM_REALP (y))
  4663. r -= SCM_REAL_VALUE (y);
  4664. else if (SCM_COMPLEXP (y))
  4665. r -= SCM_COMPLEX_REAL (y), i -= SCM_COMPLEX_IMAG (y);
  4666. else if (SCM_FRACTIONP (y))
  4667. r -= scm_i_fraction2double (y);
  4668. else
  4669. abort (); /* Unreachable. */
  4670. return scm_c_make_rectangular (r, i);
  4671. }
  4672. else if (SCM_FRACTIONP (x))
  4673. {
  4674. if (scm_is_exact (y))
  4675. {
  4676. /* a/b - c/d = (ad - bc) / bd */
  4677. SCM n = scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x),
  4678. scm_denominator (y)),
  4679. scm_product (scm_numerator (y),
  4680. SCM_FRACTION_DENOMINATOR (x)));
  4681. SCM d = scm_product (SCM_FRACTION_DENOMINATOR (x),
  4682. scm_denominator (y));
  4683. return scm_i_make_ratio (n, d);
  4684. }
  4685. double xx = scm_i_fraction2double (x);
  4686. if (SCM_REALP (y))
  4687. return scm_i_from_double (xx - SCM_REAL_VALUE (y));
  4688. else if (SCM_COMPLEXP (y))
  4689. return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
  4690. -SCM_COMPLEX_IMAG (y));
  4691. else
  4692. abort (); /* Unreachable. */
  4693. }
  4694. else
  4695. abort (); /* Unreachable. */
  4696. }
  4697. SCM_PRIMITIVE_GENERIC (scm_i_difference, "-", 0, 2, 1,
  4698. (SCM x, SCM y, SCM rest),
  4699. "If called with one argument @var{z1}, -@var{z1} returned. Otherwise\n"
  4700. "the sum of all but the first argument are subtracted from the first\n"
  4701. "argument.")
  4702. #define FUNC_NAME s_scm_i_difference
  4703. {
  4704. while (!scm_is_null (rest))
  4705. { x = scm_difference (x, y);
  4706. y = scm_car (rest);
  4707. rest = scm_cdr (rest);
  4708. }
  4709. return scm_difference (x, y);
  4710. }
  4711. #undef FUNC_NAME
  4712. SCM
  4713. scm_difference (SCM x, SCM y)
  4714. {
  4715. if (SCM_UNBNDP (y))
  4716. {
  4717. if (SCM_NUMBERP (x)) return negate (x);
  4718. if (SCM_UNBNDP (x))
  4719. return scm_wta_dispatch_0 (g_scm_i_difference, s_scm_i_difference);
  4720. return scm_wta_dispatch_1 (g_scm_i_difference, x, SCM_ARG1,
  4721. s_scm_i_difference);
  4722. }
  4723. if (!SCM_NUMBERP (x))
  4724. return scm_wta_dispatch_2 (g_scm_i_difference, x, y, SCM_ARG1,
  4725. s_scm_i_difference);
  4726. if (!SCM_NUMBERP (y))
  4727. return scm_wta_dispatch_2 (g_scm_i_difference, x, y, SCM_ARG2,
  4728. s_scm_i_difference);
  4729. return difference (x, y);
  4730. }
  4731. SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
  4732. (SCM x),
  4733. "Return @math{@var{x}-1}.")
  4734. #define FUNC_NAME s_scm_oneminus
  4735. {
  4736. return scm_difference (x, SCM_INUM1);
  4737. }
  4738. #undef FUNC_NAME
  4739. static SCM
  4740. product (SCM x, SCM y)
  4741. {
  4742. if (SCM_I_INUMP (x))
  4743. {
  4744. if (scm_is_eq (x, SCM_I_MAKINUM (-1)))
  4745. return negate (y);
  4746. else if (SCM_I_INUMP (y))
  4747. return scm_integer_mul_ii (SCM_I_INUM (x), SCM_I_INUM (y));
  4748. else if (SCM_BIGP (y))
  4749. return scm_integer_mul_zi (scm_bignum (y), SCM_I_INUM (x));
  4750. else if (SCM_REALP (y))
  4751. return scm_i_from_double (SCM_I_INUM (x) * SCM_REAL_VALUE (y));
  4752. else if (SCM_COMPLEXP (y))
  4753. return scm_c_make_rectangular (SCM_I_INUM (x) * SCM_COMPLEX_REAL (y),
  4754. SCM_I_INUM (x) * SCM_COMPLEX_IMAG (y));
  4755. else if (SCM_FRACTIONP (y))
  4756. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  4757. SCM_FRACTION_DENOMINATOR (y));
  4758. abort (); /* Unreachable. */
  4759. }
  4760. else if (SCM_BIGP (x))
  4761. {
  4762. if (SCM_BIGP (y))
  4763. return scm_integer_mul_zz (scm_bignum (x), scm_bignum (y));
  4764. else if (SCM_REALP (y))
  4765. return scm_from_double (scm_integer_to_double_z (scm_bignum (x))
  4766. * SCM_REAL_VALUE (y));
  4767. else if (SCM_COMPLEXP (y))
  4768. {
  4769. double z = scm_integer_to_double_z (scm_bignum (x));
  4770. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
  4771. z * SCM_COMPLEX_IMAG (y));
  4772. }
  4773. else if (SCM_FRACTIONP (y))
  4774. return scm_i_make_ratio (product (x, SCM_FRACTION_NUMERATOR (y)),
  4775. SCM_FRACTION_DENOMINATOR (y));
  4776. else
  4777. return product (y, x);
  4778. }
  4779. else if (SCM_REALP (x))
  4780. {
  4781. if (SCM_REALP (y))
  4782. return scm_i_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
  4783. else if (SCM_COMPLEXP (y))
  4784. return scm_c_make_rectangular
  4785. (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
  4786. SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
  4787. else if (SCM_FRACTIONP (y))
  4788. return scm_i_from_double
  4789. (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
  4790. else
  4791. return product (y, x);
  4792. }
  4793. else if (SCM_COMPLEXP (x))
  4794. {
  4795. if (SCM_COMPLEXP (y))
  4796. {
  4797. double rx = SCM_COMPLEX_REAL (x), ry = SCM_COMPLEX_REAL (y);
  4798. double ix = SCM_COMPLEX_IMAG (x), iy = SCM_COMPLEX_IMAG (y);
  4799. return scm_c_make_rectangular (rx * ry - ix * iy, rx * iy + ix * ry);
  4800. }
  4801. else if (SCM_FRACTIONP (y))
  4802. {
  4803. double yy = scm_i_fraction2double (y);
  4804. return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
  4805. yy * SCM_COMPLEX_IMAG (x));
  4806. }
  4807. else
  4808. return product (y, x);
  4809. }
  4810. else if (SCM_FRACTIONP (x))
  4811. {
  4812. if (SCM_FRACTIONP (y))
  4813. /* a/b * c/d = ac / bd */
  4814. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  4815. SCM_FRACTION_NUMERATOR (y)),
  4816. scm_product (SCM_FRACTION_DENOMINATOR (x),
  4817. SCM_FRACTION_DENOMINATOR (y)));
  4818. else
  4819. return product (y, x);
  4820. }
  4821. else
  4822. abort (); /* Unreachable. */
  4823. }
  4824. SCM_PRIMITIVE_GENERIC (scm_i_product, "*", 0, 2, 1,
  4825. (SCM x, SCM y, SCM rest),
  4826. "Return the product of all arguments. If called without arguments,\n"
  4827. "1 is returned.")
  4828. #define FUNC_NAME s_scm_i_product
  4829. {
  4830. while (!scm_is_null (rest))
  4831. { x = scm_product (x, y);
  4832. y = scm_car (rest);
  4833. rest = scm_cdr (rest);
  4834. }
  4835. return scm_product (x, y);
  4836. }
  4837. #undef FUNC_NAME
  4838. SCM
  4839. scm_product (SCM x, SCM y)
  4840. {
  4841. if (SCM_UNBNDP (y))
  4842. {
  4843. if (SCM_UNBNDP (x))
  4844. return SCM_I_MAKINUM (1L);
  4845. else if (SCM_NUMBERP (x))
  4846. return x;
  4847. else
  4848. return scm_wta_dispatch_1 (g_scm_i_product, x, SCM_ARG1,
  4849. s_scm_i_product);
  4850. }
  4851. /* This is pretty gross! But (* 1 X) is apparently X in Guile, for
  4852. any type of X, even a pair. */
  4853. if (scm_is_eq (x, SCM_INUM1))
  4854. return y;
  4855. if (scm_is_eq (y, SCM_INUM1))
  4856. return x;
  4857. if (!SCM_NUMBERP (x))
  4858. return scm_wta_dispatch_2 (g_scm_i_product, x, y, SCM_ARG1,
  4859. s_scm_i_product);
  4860. if (!SCM_NUMBERP (y))
  4861. return scm_wta_dispatch_2 (g_scm_i_product, x, y, SCM_ARG2,
  4862. s_scm_i_product);
  4863. return product (x, y);
  4864. }
  4865. /* The code below for complex division is adapted from the GNU
  4866. libstdc++, which adapted it from f2c's libF77, and is subject to
  4867. this copyright: */
  4868. /****************************************************************
  4869. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  4870. Permission to use, copy, modify, and distribute this software
  4871. and its documentation for any purpose and without fee is hereby
  4872. granted, provided that the above copyright notice appear in all
  4873. copies and that both that the copyright notice and this
  4874. permission notice and warranty disclaimer appear in supporting
  4875. documentation, and that the names of AT&T Bell Laboratories or
  4876. Bellcore or any of their entities not be used in advertising or
  4877. publicity pertaining to distribution of the software without
  4878. specific, written prior permission.
  4879. AT&T and Bellcore disclaim all warranties with regard to this
  4880. software, including all implied warranties of merchantability
  4881. and fitness. In no event shall AT&T or Bellcore be liable for
  4882. any special, indirect or consequential damages or any damages
  4883. whatsoever resulting from loss of use, data or profits, whether
  4884. in an action of contract, negligence or other tortious action,
  4885. arising out of or in connection with the use or performance of
  4886. this software.
  4887. ****************************************************************/
  4888. static SCM
  4889. invert (SCM x)
  4890. {
  4891. if (SCM_I_INUMP (x))
  4892. switch (SCM_I_INUM (x))
  4893. {
  4894. case -1: return x;
  4895. case 0: scm_num_overflow ("divide");
  4896. case 1: return x;
  4897. default: return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  4898. }
  4899. else if (SCM_BIGP (x))
  4900. return scm_i_make_ratio_already_reduced (SCM_INUM1, x);
  4901. else if (SCM_REALP (x))
  4902. return scm_i_from_double (1.0 / SCM_REAL_VALUE (x));
  4903. else if (SCM_COMPLEXP (x))
  4904. {
  4905. double r = SCM_COMPLEX_REAL (x);
  4906. double i = SCM_COMPLEX_IMAG (x);
  4907. if (fabs(r) <= fabs(i))
  4908. {
  4909. double t = r / i;
  4910. double d = i * (1.0 + t * t);
  4911. return scm_c_make_rectangular (t / d, -1.0 / d);
  4912. }
  4913. else
  4914. {
  4915. double t = i / r;
  4916. double d = r * (1.0 + t * t);
  4917. return scm_c_make_rectangular (1.0 / d, -t / d);
  4918. }
  4919. }
  4920. else if (SCM_FRACTIONP (x))
  4921. return scm_i_make_ratio_already_reduced (SCM_FRACTION_DENOMINATOR (x),
  4922. SCM_FRACTION_NUMERATOR (x));
  4923. else
  4924. abort (); /* Unreachable. */
  4925. }
  4926. static SCM
  4927. complex_div (double a, SCM y)
  4928. {
  4929. double r = SCM_COMPLEX_REAL (y);
  4930. double i = SCM_COMPLEX_IMAG (y);
  4931. if (fabs(r) <= fabs(i))
  4932. {
  4933. double t = r / i;
  4934. double d = i * (1.0 + t * t);
  4935. return scm_c_make_rectangular ((a * t) / d, -a / d);
  4936. }
  4937. else
  4938. {
  4939. double t = i / r;
  4940. double d = r * (1.0 + t * t);
  4941. return scm_c_make_rectangular (a / d, -(a * t) / d);
  4942. }
  4943. }
  4944. static SCM
  4945. divide (SCM x, SCM y)
  4946. {
  4947. if (scm_is_eq (y, SCM_INUM0))
  4948. scm_num_overflow ("divide");
  4949. if (SCM_I_INUMP (x))
  4950. {
  4951. if (scm_is_eq (x, SCM_INUM1))
  4952. return invert (y);
  4953. if (SCM_I_INUMP (y))
  4954. return scm_is_integer_divisible_ii (SCM_I_INUM (x), SCM_I_INUM (y))
  4955. ? scm_integer_exact_quotient_ii (SCM_I_INUM (x), SCM_I_INUM (y))
  4956. : scm_i_make_ratio (x, y);
  4957. else if (SCM_BIGP (y))
  4958. return scm_i_make_ratio (x, y);
  4959. else if (SCM_REALP (y))
  4960. /* FIXME: Precision may be lost here due to:
  4961. (1) The cast from 'scm_t_inum' to 'double'
  4962. (2) Double rounding */
  4963. return scm_i_from_double ((double) SCM_I_INUM (x) / SCM_REAL_VALUE (y));
  4964. else if (SCM_COMPLEXP (y))
  4965. return complex_div (SCM_I_INUM (x), y);
  4966. else if (SCM_FRACTIONP (y))
  4967. /* a / b/c = ac / b */
  4968. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4969. SCM_FRACTION_NUMERATOR (y));
  4970. else
  4971. abort (); /* Unreachable. */
  4972. }
  4973. else if (SCM_BIGP (x))
  4974. {
  4975. if (SCM_I_INUMP (y))
  4976. return scm_is_integer_divisible_zi (scm_bignum (x), SCM_I_INUM (y))
  4977. ? scm_integer_exact_quotient_zi (scm_bignum (x), SCM_I_INUM (y))
  4978. : scm_i_make_ratio (x, y);
  4979. else if (SCM_BIGP (y))
  4980. return scm_is_integer_divisible_zz (scm_bignum (x), scm_bignum (y))
  4981. ? scm_integer_exact_quotient_zz (scm_bignum (x), scm_bignum (y))
  4982. : scm_i_make_ratio (x, y);
  4983. else if (SCM_REALP (y))
  4984. /* FIXME: Precision may be lost here due to:
  4985. (1) scm_integer_to_double_z (2) Double rounding */
  4986. return scm_i_from_double (scm_integer_to_double_z (scm_bignum (x))
  4987. / SCM_REAL_VALUE (y));
  4988. else if (SCM_COMPLEXP (y))
  4989. return complex_div (scm_integer_to_double_z (scm_bignum (x)), y);
  4990. else if (SCM_FRACTIONP (y))
  4991. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4992. SCM_FRACTION_NUMERATOR (y));
  4993. else
  4994. abort (); /* Unreachable. */
  4995. }
  4996. else if (SCM_REALP (x))
  4997. {
  4998. double rx = SCM_REAL_VALUE (x);
  4999. if (SCM_I_INUMP (y))
  5000. /* FIXME: Precision may be lost here due to:
  5001. (1) The cast from 'scm_t_inum' to 'double'
  5002. (2) Double rounding */
  5003. return scm_i_from_double (rx / (double) SCM_I_INUM (y));
  5004. else if (SCM_BIGP (y))
  5005. /* FIXME: Precision may be lost here due to:
  5006. (1) The conversion from bignum to double
  5007. (2) Double rounding */
  5008. return scm_i_from_double (rx / scm_integer_to_double_z (scm_bignum (y)));
  5009. else if (SCM_REALP (y))
  5010. return scm_i_from_double (rx / SCM_REAL_VALUE (y));
  5011. else if (SCM_COMPLEXP (y))
  5012. return complex_div (rx, y);
  5013. else if (SCM_FRACTIONP (y))
  5014. return scm_i_from_double (rx / scm_i_fraction2double (y));
  5015. else
  5016. abort () ; /* Unreachable. */
  5017. }
  5018. else if (SCM_COMPLEXP (x))
  5019. {
  5020. double rx = SCM_COMPLEX_REAL (x);
  5021. double ix = SCM_COMPLEX_IMAG (x);
  5022. if (SCM_I_INUMP (y))
  5023. {
  5024. /* FIXME: Precision may be lost here due to:
  5025. (1) The conversion from 'scm_t_inum' to double
  5026. (2) Double rounding */
  5027. double d = SCM_I_INUM (y);
  5028. return scm_c_make_rectangular (rx / d, ix / d);
  5029. }
  5030. else if (SCM_BIGP (y))
  5031. {
  5032. /* FIXME: Precision may be lost here due to:
  5033. (1) The conversion from bignum to double
  5034. (2) Double rounding */
  5035. double d = scm_integer_to_double_z (scm_bignum (y));
  5036. return scm_c_make_rectangular (rx / d, ix / d);
  5037. }
  5038. else if (SCM_REALP (y))
  5039. {
  5040. double d = SCM_REAL_VALUE (y);
  5041. return scm_c_make_rectangular (rx / d, ix / d);
  5042. }
  5043. else if (SCM_COMPLEXP (y))
  5044. {
  5045. double ry = SCM_COMPLEX_REAL (y);
  5046. double iy = SCM_COMPLEX_IMAG (y);
  5047. if (fabs(ry) <= fabs(iy))
  5048. {
  5049. double t = ry / iy;
  5050. double d = iy * (1.0 + t * t);
  5051. return scm_c_make_rectangular ((rx * t + ix) / d,
  5052. (ix * t - rx) / d);
  5053. }
  5054. else
  5055. {
  5056. double t = iy / ry;
  5057. double d = ry * (1.0 + t * t);
  5058. return scm_c_make_rectangular ((rx + ix * t) / d,
  5059. (ix - rx * t) / d);
  5060. }
  5061. }
  5062. else if (SCM_FRACTIONP (y))
  5063. {
  5064. /* FIXME: Precision may be lost here due to:
  5065. (1) The conversion from fraction to double
  5066. (2) Double rounding */
  5067. double d = scm_i_fraction2double (y);
  5068. return scm_c_make_rectangular (rx / d, ix / d);
  5069. }
  5070. else
  5071. abort (); /* Unreachable. */
  5072. }
  5073. else if (SCM_FRACTIONP (x))
  5074. {
  5075. if (scm_is_exact_integer (y))
  5076. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  5077. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  5078. else if (SCM_REALP (y))
  5079. /* FIXME: Precision may be lost here due to:
  5080. (1) The conversion from fraction to double
  5081. (2) Double rounding */
  5082. return scm_i_from_double (scm_i_fraction2double (x) /
  5083. SCM_REAL_VALUE (y));
  5084. else if (SCM_COMPLEXP (y))
  5085. /* FIXME: Precision may be lost here due to:
  5086. (1) The conversion from fraction to double
  5087. (2) Double rounding */
  5088. return complex_div (scm_i_fraction2double (x), y);
  5089. else if (SCM_FRACTIONP (y))
  5090. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  5091. SCM_FRACTION_DENOMINATOR (y)),
  5092. scm_product (SCM_FRACTION_NUMERATOR (y),
  5093. SCM_FRACTION_DENOMINATOR (x)));
  5094. else
  5095. abort (); /* Unreachable. */
  5096. }
  5097. else
  5098. abort (); /* Unreachable. */
  5099. }
  5100. SCM_PRIMITIVE_GENERIC (scm_i_divide, "/", 0, 2, 1,
  5101. (SCM x, SCM y, SCM rest),
  5102. "Divide the first argument by the product of the remaining\n"
  5103. "arguments. If called with one argument @var{z1}, 1/@var{z1} is\n"
  5104. "returned.")
  5105. #define FUNC_NAME s_scm_i_divide
  5106. {
  5107. while (!scm_is_null (rest))
  5108. { x = scm_divide (x, y);
  5109. y = scm_car (rest);
  5110. rest = scm_cdr (rest);
  5111. }
  5112. return scm_divide (x, y);
  5113. }
  5114. #undef FUNC_NAME
  5115. SCM
  5116. scm_divide (SCM x, SCM y)
  5117. {
  5118. if (SCM_UNBNDP (y))
  5119. {
  5120. if (SCM_UNBNDP (x))
  5121. return scm_wta_dispatch_0 (g_scm_i_divide, s_scm_i_divide);
  5122. if (SCM_NUMBERP (x))
  5123. return invert (x);
  5124. else
  5125. return scm_wta_dispatch_1 (g_scm_i_divide, x, SCM_ARG1,
  5126. s_scm_i_divide);
  5127. }
  5128. if (!SCM_NUMBERP (x))
  5129. return scm_wta_dispatch_2 (g_scm_i_divide, x, y, SCM_ARG1,
  5130. s_scm_i_divide);
  5131. if (!SCM_NUMBERP (y))
  5132. return scm_wta_dispatch_2 (g_scm_i_divide, x, y, SCM_ARG2,
  5133. s_scm_i_divide);
  5134. return divide (x, y);
  5135. }
  5136. double
  5137. scm_c_truncate (double x)
  5138. {
  5139. return trunc (x);
  5140. }
  5141. /* scm_c_round is done using floor(x+0.5) to round to nearest and with
  5142. half-way case (ie. when x is an integer plus 0.5) going upwards.
  5143. Then half-way cases are identified and adjusted down if the
  5144. round-upwards didn't give the desired even integer.
  5145. "plus_half == result" identifies a half-way case. If plus_half, which is
  5146. x + 0.5, is an integer then x must be an integer plus 0.5.
  5147. An odd "result" value is identified with result/2 != floor(result/2).
  5148. This is done with plus_half, since that value is ready for use sooner in
  5149. a pipelined cpu, and we're already requiring plus_half == result.
  5150. Note however that we need to be careful when x is big and already an
  5151. integer. In that case "x+0.5" may round to an adjacent integer, causing
  5152. us to return such a value, incorrectly. For instance if the hardware is
  5153. in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
  5154. (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
  5155. returned. Or if the hardware is in round-upwards mode, then other bigger
  5156. values like say x == 2^128 will see x+0.5 rounding up to the next higher
  5157. representable value, 2^128+2^76 (or whatever), again incorrect.
  5158. These bad roundings of x+0.5 are avoided by testing at the start whether
  5159. x is already an integer. If it is then clearly that's the desired result
  5160. already. And if it's not then the exponent must be small enough to allow
  5161. an 0.5 to be represented, and hence added without a bad rounding. */
  5162. double
  5163. scm_c_round (double x)
  5164. {
  5165. double plus_half, result;
  5166. if (x == floor (x))
  5167. return x;
  5168. plus_half = x + 0.5;
  5169. result = floor (plus_half);
  5170. /* Adjust so that the rounding is towards even. */
  5171. return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
  5172. ? result - 1
  5173. : result);
  5174. }
  5175. SCM_PRIMITIVE_GENERIC (scm_truncate_number, "truncate", 1, 0, 0,
  5176. (SCM x),
  5177. "Round the number @var{x} towards zero.")
  5178. #define FUNC_NAME s_scm_truncate_number
  5179. {
  5180. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  5181. return x;
  5182. else if (SCM_REALP (x))
  5183. return scm_i_from_double (trunc (SCM_REAL_VALUE (x)));
  5184. else if (SCM_FRACTIONP (x))
  5185. return scm_truncate_quotient (SCM_FRACTION_NUMERATOR (x),
  5186. SCM_FRACTION_DENOMINATOR (x));
  5187. else
  5188. return scm_wta_dispatch_1 (g_scm_truncate_number, x, SCM_ARG1,
  5189. s_scm_truncate_number);
  5190. }
  5191. #undef FUNC_NAME
  5192. SCM_PRIMITIVE_GENERIC (scm_round_number, "round", 1, 0, 0,
  5193. (SCM x),
  5194. "Round the number @var{x} towards the nearest integer. "
  5195. "When it is exactly halfway between two integers, "
  5196. "round towards the even one.")
  5197. #define FUNC_NAME s_scm_round_number
  5198. {
  5199. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  5200. return x;
  5201. else if (SCM_REALP (x))
  5202. return scm_i_from_double (scm_c_round (SCM_REAL_VALUE (x)));
  5203. else if (SCM_FRACTIONP (x))
  5204. return scm_round_quotient (SCM_FRACTION_NUMERATOR (x),
  5205. SCM_FRACTION_DENOMINATOR (x));
  5206. else
  5207. return scm_wta_dispatch_1 (g_scm_round_number, x, SCM_ARG1,
  5208. s_scm_round_number);
  5209. }
  5210. #undef FUNC_NAME
  5211. SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
  5212. (SCM x),
  5213. "Round the number @var{x} towards minus infinity.")
  5214. #define FUNC_NAME s_scm_floor
  5215. {
  5216. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  5217. return x;
  5218. else if (SCM_REALP (x))
  5219. return scm_i_from_double (floor (SCM_REAL_VALUE (x)));
  5220. else if (SCM_FRACTIONP (x))
  5221. return scm_floor_quotient (SCM_FRACTION_NUMERATOR (x),
  5222. SCM_FRACTION_DENOMINATOR (x));
  5223. else
  5224. return scm_wta_dispatch_1 (g_scm_floor, x, 1, s_scm_floor);
  5225. }
  5226. #undef FUNC_NAME
  5227. SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
  5228. (SCM x),
  5229. "Round the number @var{x} towards infinity.")
  5230. #define FUNC_NAME s_scm_ceiling
  5231. {
  5232. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  5233. return x;
  5234. else if (SCM_REALP (x))
  5235. return scm_i_from_double (ceil (SCM_REAL_VALUE (x)));
  5236. else if (SCM_FRACTIONP (x))
  5237. return scm_ceiling_quotient (SCM_FRACTION_NUMERATOR (x),
  5238. SCM_FRACTION_DENOMINATOR (x));
  5239. else
  5240. return scm_wta_dispatch_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
  5241. }
  5242. #undef FUNC_NAME
  5243. SCM_PRIMITIVE_GENERIC (scm_expt, "expt", 2, 0, 0,
  5244. (SCM x, SCM y),
  5245. "Return @var{x} raised to the power of @var{y}.")
  5246. #define FUNC_NAME s_scm_expt
  5247. {
  5248. if (scm_is_integer (y))
  5249. {
  5250. if (scm_is_true (scm_exact_p (y)))
  5251. return scm_integer_expt (x, y);
  5252. else
  5253. {
  5254. /* Here we handle the case where the exponent is an inexact
  5255. integer. We make the exponent exact in order to use
  5256. scm_integer_expt, and thus avoid the spurious imaginary
  5257. parts that may result from round-off errors in the general
  5258. e^(y log x) method below (for example when squaring a large
  5259. negative number). In this case, we must return an inexact
  5260. result for correctness. We also make the base inexact so
  5261. that scm_integer_expt will use fast inexact arithmetic
  5262. internally. Note that making the base inexact is not
  5263. sufficient to guarantee an inexact result, because
  5264. scm_integer_expt will return an exact 1 when the exponent
  5265. is 0, even if the base is inexact. */
  5266. return scm_exact_to_inexact
  5267. (scm_integer_expt (scm_exact_to_inexact (x),
  5268. scm_inexact_to_exact (y)));
  5269. }
  5270. }
  5271. else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0)
  5272. {
  5273. return scm_i_from_double (pow (scm_to_double (x), scm_to_double (y)));
  5274. }
  5275. else if (scm_is_complex (x) && scm_is_complex (y))
  5276. return scm_exp (scm_product (scm_log (x), y));
  5277. else if (scm_is_complex (x))
  5278. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG2, s_scm_expt);
  5279. else
  5280. return scm_wta_dispatch_2 (g_scm_expt, x, y, SCM_ARG1, s_scm_expt);
  5281. }
  5282. #undef FUNC_NAME
  5283. /* sin/cos/tan/asin/acos/atan
  5284. sinh/cosh/tanh/asinh/acosh/atanh
  5285. Derived from "Transcen.scm", Complex trancendental functions for SCM.
  5286. Written by Jerry D. Hedden, (C) FSF.
  5287. See the file `COPYING' for terms applying to this program. */
  5288. SCM_PRIMITIVE_GENERIC (scm_sin, "sin", 1, 0, 0,
  5289. (SCM z),
  5290. "Compute the sine of @var{z}.")
  5291. #define FUNC_NAME s_scm_sin
  5292. {
  5293. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5294. return z; /* sin(exact0) = exact0 */
  5295. else if (scm_is_real (z))
  5296. return scm_i_from_double (sin (scm_to_double (z)));
  5297. else if (SCM_COMPLEXP (z))
  5298. { double x, y;
  5299. x = SCM_COMPLEX_REAL (z);
  5300. y = SCM_COMPLEX_IMAG (z);
  5301. return scm_c_make_rectangular (sin (x) * cosh (y),
  5302. cos (x) * sinh (y));
  5303. }
  5304. else
  5305. return scm_wta_dispatch_1 (g_scm_sin, z, 1, s_scm_sin);
  5306. }
  5307. #undef FUNC_NAME
  5308. SCM_PRIMITIVE_GENERIC (scm_cos, "cos", 1, 0, 0,
  5309. (SCM z),
  5310. "Compute the cosine of @var{z}.")
  5311. #define FUNC_NAME s_scm_cos
  5312. {
  5313. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5314. return SCM_INUM1; /* cos(exact0) = exact1 */
  5315. else if (scm_is_real (z))
  5316. return scm_i_from_double (cos (scm_to_double (z)));
  5317. else if (SCM_COMPLEXP (z))
  5318. { double x, y;
  5319. x = SCM_COMPLEX_REAL (z);
  5320. y = SCM_COMPLEX_IMAG (z);
  5321. return scm_c_make_rectangular (cos (x) * cosh (y),
  5322. -sin (x) * sinh (y));
  5323. }
  5324. else
  5325. return scm_wta_dispatch_1 (g_scm_cos, z, 1, s_scm_cos);
  5326. }
  5327. #undef FUNC_NAME
  5328. SCM_PRIMITIVE_GENERIC (scm_tan, "tan", 1, 0, 0,
  5329. (SCM z),
  5330. "Compute the tangent of @var{z}.")
  5331. #define FUNC_NAME s_scm_tan
  5332. {
  5333. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5334. return z; /* tan(exact0) = exact0 */
  5335. else if (scm_is_real (z))
  5336. return scm_i_from_double (tan (scm_to_double (z)));
  5337. else if (SCM_COMPLEXP (z))
  5338. { double x, y, w;
  5339. x = 2.0 * SCM_COMPLEX_REAL (z);
  5340. y = 2.0 * SCM_COMPLEX_IMAG (z);
  5341. w = cos (x) + cosh (y);
  5342. return scm_c_make_rectangular (sin (x) / w, sinh (y) / w);
  5343. }
  5344. else
  5345. return scm_wta_dispatch_1 (g_scm_tan, z, 1, s_scm_tan);
  5346. }
  5347. #undef FUNC_NAME
  5348. SCM_PRIMITIVE_GENERIC (scm_sinh, "sinh", 1, 0, 0,
  5349. (SCM z),
  5350. "Compute the hyperbolic sine of @var{z}.")
  5351. #define FUNC_NAME s_scm_sinh
  5352. {
  5353. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5354. return z; /* sinh(exact0) = exact0 */
  5355. else if (scm_is_real (z))
  5356. return scm_i_from_double (sinh (scm_to_double (z)));
  5357. else if (SCM_COMPLEXP (z))
  5358. { double x, y;
  5359. x = SCM_COMPLEX_REAL (z);
  5360. y = SCM_COMPLEX_IMAG (z);
  5361. return scm_c_make_rectangular (sinh (x) * cos (y),
  5362. cosh (x) * sin (y));
  5363. }
  5364. else
  5365. return scm_wta_dispatch_1 (g_scm_sinh, z, 1, s_scm_sinh);
  5366. }
  5367. #undef FUNC_NAME
  5368. SCM_PRIMITIVE_GENERIC (scm_cosh, "cosh", 1, 0, 0,
  5369. (SCM z),
  5370. "Compute the hyperbolic cosine of @var{z}.")
  5371. #define FUNC_NAME s_scm_cosh
  5372. {
  5373. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5374. return SCM_INUM1; /* cosh(exact0) = exact1 */
  5375. else if (scm_is_real (z))
  5376. return scm_i_from_double (cosh (scm_to_double (z)));
  5377. else if (SCM_COMPLEXP (z))
  5378. { double x, y;
  5379. x = SCM_COMPLEX_REAL (z);
  5380. y = SCM_COMPLEX_IMAG (z);
  5381. return scm_c_make_rectangular (cosh (x) * cos (y),
  5382. sinh (x) * sin (y));
  5383. }
  5384. else
  5385. return scm_wta_dispatch_1 (g_scm_cosh, z, 1, s_scm_cosh);
  5386. }
  5387. #undef FUNC_NAME
  5388. SCM_PRIMITIVE_GENERIC (scm_tanh, "tanh", 1, 0, 0,
  5389. (SCM z),
  5390. "Compute the hyperbolic tangent of @var{z}.")
  5391. #define FUNC_NAME s_scm_tanh
  5392. {
  5393. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5394. return z; /* tanh(exact0) = exact0 */
  5395. else if (scm_is_real (z))
  5396. return scm_i_from_double (tanh (scm_to_double (z)));
  5397. else if (SCM_COMPLEXP (z))
  5398. { double x, y, w;
  5399. x = 2.0 * SCM_COMPLEX_REAL (z);
  5400. y = 2.0 * SCM_COMPLEX_IMAG (z);
  5401. w = cosh (x) + cos (y);
  5402. return scm_c_make_rectangular (sinh (x) / w, sin (y) / w);
  5403. }
  5404. else
  5405. return scm_wta_dispatch_1 (g_scm_tanh, z, 1, s_scm_tanh);
  5406. }
  5407. #undef FUNC_NAME
  5408. SCM_PRIMITIVE_GENERIC (scm_asin, "asin", 1, 0, 0,
  5409. (SCM z),
  5410. "Compute the arc sine of @var{z}.")
  5411. #define FUNC_NAME s_scm_asin
  5412. {
  5413. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5414. return z; /* asin(exact0) = exact0 */
  5415. else if (scm_is_real (z))
  5416. {
  5417. double w = scm_to_double (z);
  5418. if (w >= -1.0 && w <= 1.0)
  5419. return scm_i_from_double (asin (w));
  5420. else
  5421. return scm_product (scm_c_make_rectangular (0, -1),
  5422. scm_sys_asinh (scm_c_make_rectangular (0, w)));
  5423. }
  5424. else if (SCM_COMPLEXP (z))
  5425. { double x, y;
  5426. x = SCM_COMPLEX_REAL (z);
  5427. y = SCM_COMPLEX_IMAG (z);
  5428. return scm_product (scm_c_make_rectangular (0, -1),
  5429. scm_sys_asinh (scm_c_make_rectangular (-y, x)));
  5430. }
  5431. else
  5432. return scm_wta_dispatch_1 (g_scm_asin, z, 1, s_scm_asin);
  5433. }
  5434. #undef FUNC_NAME
  5435. SCM_PRIMITIVE_GENERIC (scm_acos, "acos", 1, 0, 0,
  5436. (SCM z),
  5437. "Compute the arc cosine of @var{z}.")
  5438. #define FUNC_NAME s_scm_acos
  5439. {
  5440. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  5441. return SCM_INUM0; /* acos(exact1) = exact0 */
  5442. else if (scm_is_real (z))
  5443. {
  5444. double w = scm_to_double (z);
  5445. if (w >= -1.0 && w <= 1.0)
  5446. return scm_i_from_double (acos (w));
  5447. else
  5448. return scm_sum (scm_i_from_double (acos (0.0)),
  5449. scm_product (scm_c_make_rectangular (0, 1),
  5450. scm_sys_asinh (scm_c_make_rectangular (0, w))));
  5451. }
  5452. else if (SCM_COMPLEXP (z))
  5453. { double x, y;
  5454. x = SCM_COMPLEX_REAL (z);
  5455. y = SCM_COMPLEX_IMAG (z);
  5456. return scm_sum (scm_i_from_double (acos (0.0)),
  5457. scm_product (scm_c_make_rectangular (0, 1),
  5458. scm_sys_asinh (scm_c_make_rectangular (-y, x))));
  5459. }
  5460. else
  5461. return scm_wta_dispatch_1 (g_scm_acos, z, 1, s_scm_acos);
  5462. }
  5463. #undef FUNC_NAME
  5464. SCM_PRIMITIVE_GENERIC (scm_atan, "atan", 1, 1, 0,
  5465. (SCM z, SCM y),
  5466. "With one argument, compute the arc tangent of @var{z}.\n"
  5467. "If @var{y} is present, compute the arc tangent of @var{z}/@var{y},\n"
  5468. "using the sign of @var{z} and @var{y} to determine the quadrant.")
  5469. #define FUNC_NAME s_scm_atan
  5470. {
  5471. if (SCM_UNBNDP (y))
  5472. {
  5473. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5474. return z; /* atan(exact0) = exact0 */
  5475. else if (scm_is_real (z))
  5476. return scm_i_from_double (atan (scm_to_double (z)));
  5477. else if (SCM_COMPLEXP (z))
  5478. {
  5479. double v, w;
  5480. v = SCM_COMPLEX_REAL (z);
  5481. w = SCM_COMPLEX_IMAG (z);
  5482. return scm_divide (scm_log (scm_divide (scm_c_make_rectangular (-v, 1.0 - w),
  5483. scm_c_make_rectangular ( v, 1.0 + w))),
  5484. scm_c_make_rectangular (0, 2));
  5485. }
  5486. else
  5487. return scm_wta_dispatch_1 (g_scm_atan, z, SCM_ARG1, s_scm_atan);
  5488. }
  5489. else if (scm_is_real (z))
  5490. {
  5491. if (scm_is_real (y))
  5492. return scm_i_from_double (atan2 (scm_to_double (z), scm_to_double (y)));
  5493. else
  5494. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG2, s_scm_atan);
  5495. }
  5496. else
  5497. return scm_wta_dispatch_2 (g_scm_atan, z, y, SCM_ARG1, s_scm_atan);
  5498. }
  5499. #undef FUNC_NAME
  5500. SCM_PRIMITIVE_GENERIC (scm_sys_asinh, "asinh", 1, 0, 0,
  5501. (SCM z),
  5502. "Compute the inverse hyperbolic sine of @var{z}.")
  5503. #define FUNC_NAME s_scm_sys_asinh
  5504. {
  5505. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5506. return z; /* asinh(exact0) = exact0 */
  5507. else if (scm_is_real (z))
  5508. return scm_i_from_double (asinh (scm_to_double (z)));
  5509. else if (scm_is_number (z))
  5510. return scm_log (scm_sum (z,
  5511. scm_sqrt (scm_sum (scm_product (z, z),
  5512. SCM_INUM1))));
  5513. else
  5514. return scm_wta_dispatch_1 (g_scm_sys_asinh, z, 1, s_scm_sys_asinh);
  5515. }
  5516. #undef FUNC_NAME
  5517. SCM_PRIMITIVE_GENERIC (scm_sys_acosh, "acosh", 1, 0, 0,
  5518. (SCM z),
  5519. "Compute the inverse hyperbolic cosine of @var{z}.")
  5520. #define FUNC_NAME s_scm_sys_acosh
  5521. {
  5522. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM1)))
  5523. return SCM_INUM0; /* acosh(exact1) = exact0 */
  5524. else if (scm_is_real (z) && scm_to_double (z) >= 1.0)
  5525. return scm_i_from_double (acosh (scm_to_double (z)));
  5526. else if (scm_is_number (z))
  5527. return scm_log (scm_sum (z,
  5528. scm_sqrt (scm_difference (scm_product (z, z),
  5529. SCM_INUM1))));
  5530. else
  5531. return scm_wta_dispatch_1 (g_scm_sys_acosh, z, 1, s_scm_sys_acosh);
  5532. }
  5533. #undef FUNC_NAME
  5534. SCM_PRIMITIVE_GENERIC (scm_sys_atanh, "atanh", 1, 0, 0,
  5535. (SCM z),
  5536. "Compute the inverse hyperbolic tangent of @var{z}.")
  5537. #define FUNC_NAME s_scm_sys_atanh
  5538. {
  5539. if (SCM_UNLIKELY (scm_is_eq (z, SCM_INUM0)))
  5540. return z; /* atanh(exact0) = exact0 */
  5541. else if (scm_is_real (z) && scm_to_double (z) >= -1.0 && scm_to_double (z) <= 1.0)
  5542. return scm_i_from_double (atanh (scm_to_double (z)));
  5543. else if (scm_is_number (z))
  5544. return scm_divide (scm_log (scm_divide (scm_sum (SCM_INUM1, z),
  5545. scm_difference (SCM_INUM1, z))),
  5546. SCM_I_MAKINUM (2));
  5547. else
  5548. return scm_wta_dispatch_1 (g_scm_sys_atanh, z, 1, s_scm_sys_atanh);
  5549. }
  5550. #undef FUNC_NAME
  5551. SCM
  5552. scm_c_make_rectangular (double re, double im)
  5553. {
  5554. SCM z;
  5555. z = SCM_PACK_POINTER (scm_gc_malloc_pointerless (sizeof (scm_t_complex),
  5556. "complex"));
  5557. SCM_SET_CELL_TYPE (z, scm_tc16_complex);
  5558. SCM_COMPLEX_REAL (z) = re;
  5559. SCM_COMPLEX_IMAG (z) = im;
  5560. return z;
  5561. }
  5562. SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
  5563. (SCM real_part, SCM imaginary_part),
  5564. "Return a complex number constructed of the given @var{real_part} "
  5565. "and @var{imaginary_part} parts.")
  5566. #define FUNC_NAME s_scm_make_rectangular
  5567. {
  5568. SCM_ASSERT_TYPE (scm_is_real (real_part), real_part,
  5569. SCM_ARG1, FUNC_NAME, "real");
  5570. SCM_ASSERT_TYPE (scm_is_real (imaginary_part), imaginary_part,
  5571. SCM_ARG2, FUNC_NAME, "real");
  5572. /* Return a real if and only if the imaginary_part is an _exact_ 0 */
  5573. if (scm_is_eq (imaginary_part, SCM_INUM0))
  5574. return real_part;
  5575. else
  5576. return scm_c_make_rectangular (scm_to_double (real_part),
  5577. scm_to_double (imaginary_part));
  5578. }
  5579. #undef FUNC_NAME
  5580. SCM
  5581. scm_c_make_polar (double mag, double ang)
  5582. {
  5583. double s, c;
  5584. /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
  5585. use it on Glibc-based systems that have it (it's a GNU extension). See
  5586. http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
  5587. details. */
  5588. #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
  5589. sincos (ang, &s, &c);
  5590. #elif (defined HAVE___SINCOS)
  5591. __sincos (ang, &s, &c);
  5592. #else
  5593. s = sin (ang);
  5594. c = cos (ang);
  5595. #endif
  5596. /* If s and c are NaNs, this indicates that the angle is a NaN,
  5597. infinite, or perhaps simply too large to determine its value
  5598. mod 2*pi. However, we know something that the floating-point
  5599. implementation doesn't know: We know that s and c are finite.
  5600. Therefore, if the magnitude is zero, return a complex zero.
  5601. The reason we check for the NaNs instead of using this case
  5602. whenever mag == 0.0 is because when the angle is known, we'd
  5603. like to return the correct kind of non-real complex zero:
  5604. +0.0+0.0i, -0.0+0.0i, -0.0-0.0i, or +0.0-0.0i, depending
  5605. on which quadrant the angle is in.
  5606. */
  5607. if (SCM_UNLIKELY (isnan(s)) && isnan(c) && (mag == 0.0))
  5608. return scm_c_make_rectangular (0.0, 0.0);
  5609. else
  5610. return scm_c_make_rectangular (mag * c, mag * s);
  5611. }
  5612. SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
  5613. (SCM mag, SCM ang),
  5614. "Return the complex number @var{mag} * e^(i * @var{ang}).")
  5615. #define FUNC_NAME s_scm_make_polar
  5616. {
  5617. SCM_ASSERT_TYPE (scm_is_real (mag), mag, SCM_ARG1, FUNC_NAME, "real");
  5618. SCM_ASSERT_TYPE (scm_is_real (ang), ang, SCM_ARG2, FUNC_NAME, "real");
  5619. /* If mag is exact0, return exact0 */
  5620. if (scm_is_eq (mag, SCM_INUM0))
  5621. return SCM_INUM0;
  5622. /* Return a real if ang is exact0 */
  5623. else if (scm_is_eq (ang, SCM_INUM0))
  5624. return mag;
  5625. else
  5626. return scm_c_make_polar (scm_to_double (mag), scm_to_double (ang));
  5627. }
  5628. #undef FUNC_NAME
  5629. SCM_PRIMITIVE_GENERIC (scm_real_part, "real-part", 1, 0, 0,
  5630. (SCM z),
  5631. "Return the real part of the number @var{z}.")
  5632. #define FUNC_NAME s_scm_real_part
  5633. {
  5634. if (SCM_COMPLEXP (z))
  5635. return scm_i_from_double (SCM_COMPLEX_REAL (z));
  5636. else if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_REALP (z) || SCM_FRACTIONP (z))
  5637. return z;
  5638. else
  5639. return scm_wta_dispatch_1 (g_scm_real_part, z, SCM_ARG1, s_scm_real_part);
  5640. }
  5641. #undef FUNC_NAME
  5642. SCM_PRIMITIVE_GENERIC (scm_imag_part, "imag-part", 1, 0, 0,
  5643. (SCM z),
  5644. "Return the imaginary part of the number @var{z}.")
  5645. #define FUNC_NAME s_scm_imag_part
  5646. {
  5647. if (SCM_COMPLEXP (z))
  5648. return scm_i_from_double (SCM_COMPLEX_IMAG (z));
  5649. else if (SCM_I_INUMP (z) || SCM_REALP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  5650. return SCM_INUM0;
  5651. else
  5652. return scm_wta_dispatch_1 (g_scm_imag_part, z, SCM_ARG1, s_scm_imag_part);
  5653. }
  5654. #undef FUNC_NAME
  5655. SCM_PRIMITIVE_GENERIC (scm_numerator, "numerator", 1, 0, 0,
  5656. (SCM z),
  5657. "Return the numerator of the number @var{z}.")
  5658. #define FUNC_NAME s_scm_numerator
  5659. {
  5660. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  5661. return z;
  5662. else if (SCM_FRACTIONP (z))
  5663. return SCM_FRACTION_NUMERATOR (z);
  5664. else if (SCM_REALP (z))
  5665. {
  5666. double zz = SCM_REAL_VALUE (z);
  5667. if (zz == floor (zz))
  5668. /* Handle -0.0 and infinities in accordance with R6RS
  5669. flnumerator, and optimize handling of integers. */
  5670. return z;
  5671. else
  5672. return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
  5673. }
  5674. else
  5675. return scm_wta_dispatch_1 (g_scm_numerator, z, SCM_ARG1, s_scm_numerator);
  5676. }
  5677. #undef FUNC_NAME
  5678. SCM_PRIMITIVE_GENERIC (scm_denominator, "denominator", 1, 0, 0,
  5679. (SCM z),
  5680. "Return the denominator of the number @var{z}.")
  5681. #define FUNC_NAME s_scm_denominator
  5682. {
  5683. if (SCM_I_INUMP (z) || SCM_BIGP (z))
  5684. return SCM_INUM1;
  5685. else if (SCM_FRACTIONP (z))
  5686. return SCM_FRACTION_DENOMINATOR (z);
  5687. else if (SCM_REALP (z))
  5688. {
  5689. double zz = SCM_REAL_VALUE (z);
  5690. if (zz == floor (zz))
  5691. /* Handle infinities in accordance with R6RS fldenominator, and
  5692. optimize handling of integers. */
  5693. return scm_i_from_double (1.0);
  5694. else
  5695. return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
  5696. }
  5697. else
  5698. return scm_wta_dispatch_1 (g_scm_denominator, z, SCM_ARG1,
  5699. s_scm_denominator);
  5700. }
  5701. #undef FUNC_NAME
  5702. SCM_PRIMITIVE_GENERIC (scm_magnitude, "magnitude", 1, 0, 0,
  5703. (SCM z),
  5704. "Return the magnitude of the number @var{z}. This is the same as\n"
  5705. "@code{abs} for real arguments, but also allows complex numbers.")
  5706. #define FUNC_NAME s_scm_magnitude
  5707. {
  5708. if (SCM_COMPLEXP (z))
  5709. return scm_i_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
  5710. else if (SCM_NUMBERP (z))
  5711. return scm_abs (z);
  5712. else
  5713. return scm_wta_dispatch_1 (g_scm_magnitude, z, SCM_ARG1,
  5714. s_scm_magnitude);
  5715. }
  5716. #undef FUNC_NAME
  5717. SCM_PRIMITIVE_GENERIC (scm_angle, "angle", 1, 0, 0,
  5718. (SCM z),
  5719. "Return the angle of the complex number @var{z}.")
  5720. #define FUNC_NAME s_scm_angle
  5721. {
  5722. /* atan(0,-1) is pi and it'd be possible to have that as a constant like
  5723. flo0 to save allocating a new flonum with scm_i_from_double each time.
  5724. But if atan2 follows the floating point rounding mode, then the value
  5725. is not a constant. Maybe it'd be close enough though. */
  5726. if (SCM_COMPLEXP (z))
  5727. return scm_i_from_double (atan2 (SCM_COMPLEX_IMAG (z),
  5728. SCM_COMPLEX_REAL (z)));
  5729. else if (SCM_NUMBERP (z))
  5730. return (SCM_REALP (z)
  5731. ? copysign (1.0, SCM_REAL_VALUE (z)) < 0.0
  5732. : scm_is_true (scm_negative_p (z)))
  5733. ? scm_i_from_double (atan2 (0.0, -1.0))
  5734. : flo0;
  5735. else
  5736. return scm_wta_dispatch_1 (g_scm_angle, z, SCM_ARG1, s_scm_angle);
  5737. }
  5738. #undef FUNC_NAME
  5739. SCM_PRIMITIVE_GENERIC (scm_exact_to_inexact, "exact->inexact", 1, 0, 0,
  5740. (SCM z),
  5741. "Convert the number @var{z} to its inexact representation.\n")
  5742. #define FUNC_NAME s_scm_exact_to_inexact
  5743. {
  5744. if (SCM_I_INUMP (z))
  5745. return scm_i_from_double ((double) SCM_I_INUM (z));
  5746. else if (SCM_BIGP (z))
  5747. return scm_i_from_double (scm_integer_to_double_z (scm_bignum (z)));
  5748. else if (SCM_FRACTIONP (z))
  5749. return scm_i_from_double (scm_i_fraction2double (z));
  5750. else if (SCM_INEXACTP (z))
  5751. return z;
  5752. else
  5753. return scm_wta_dispatch_1 (g_scm_exact_to_inexact, z, 1,
  5754. s_scm_exact_to_inexact);
  5755. }
  5756. #undef FUNC_NAME
  5757. SCM_PRIMITIVE_GENERIC (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
  5758. (SCM z),
  5759. "Return an exact number that is numerically closest to @var{z}.")
  5760. #define FUNC_NAME s_scm_inexact_to_exact
  5761. {
  5762. if (SCM_I_INUMP (z) || SCM_BIGP (z) || SCM_FRACTIONP (z))
  5763. return z;
  5764. double val;
  5765. if (SCM_REALP (z))
  5766. val = SCM_REAL_VALUE (z);
  5767. else if (SCM_COMPLEXP (z) && SCM_COMPLEX_IMAG (z) == 0.0)
  5768. val = SCM_COMPLEX_REAL (z);
  5769. else
  5770. return scm_wta_dispatch_1 (g_scm_inexact_to_exact, z, 1,
  5771. s_scm_inexact_to_exact);
  5772. if (!SCM_LIKELY (isfinite (val)))
  5773. SCM_OUT_OF_RANGE (1, z);
  5774. if (val == 0)
  5775. return SCM_INUM0;
  5776. int expon;
  5777. mpz_t zn;
  5778. mpz_init_set_d (zn, ldexp (frexp (val, &expon), DBL_MANT_DIG));
  5779. expon -= DBL_MANT_DIG;
  5780. if (expon < 0)
  5781. {
  5782. int shift = mpz_scan1 (zn, 0);
  5783. if (shift > -expon)
  5784. shift = -expon;
  5785. mpz_fdiv_q_2exp (zn, zn, shift);
  5786. expon += shift;
  5787. }
  5788. SCM numerator = scm_integer_from_mpz (zn);
  5789. mpz_clear (zn);
  5790. if (expon < 0)
  5791. return scm_i_make_ratio_already_reduced
  5792. (numerator, scm_integer_lsh_iu (1, -expon));
  5793. else if (expon > 0)
  5794. return lsh (numerator, scm_from_int (expon), FUNC_NAME);
  5795. else
  5796. return numerator;
  5797. }
  5798. #undef FUNC_NAME
  5799. SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
  5800. (SCM x, SCM eps),
  5801. "Returns the @emph{simplest} rational number differing\n"
  5802. "from @var{x} by no more than @var{eps}.\n"
  5803. "\n"
  5804. "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
  5805. "exact result when both its arguments are exact. Thus, you might need\n"
  5806. "to use @code{inexact->exact} on the arguments.\n"
  5807. "\n"
  5808. "@lisp\n"
  5809. "(rationalize (inexact->exact 1.2) 1/100)\n"
  5810. "@result{} 6/5\n"
  5811. "@end lisp")
  5812. #define FUNC_NAME s_scm_rationalize
  5813. {
  5814. SCM_ASSERT_TYPE (scm_is_real (x), x, SCM_ARG1, FUNC_NAME, "real");
  5815. SCM_ASSERT_TYPE (scm_is_real (eps), eps, SCM_ARG2, FUNC_NAME, "real");
  5816. if (SCM_UNLIKELY (!scm_is_exact (eps) || !scm_is_exact (x)))
  5817. {
  5818. if (SCM_UNLIKELY (scm_is_false (scm_finite_p (eps))))
  5819. {
  5820. if (scm_is_false (scm_nan_p (eps)) && scm_is_true (scm_finite_p (x)))
  5821. return flo0;
  5822. else
  5823. return scm_nan ();
  5824. }
  5825. else if (SCM_UNLIKELY (scm_is_false (scm_finite_p (x))))
  5826. return x;
  5827. else
  5828. return scm_exact_to_inexact
  5829. (scm_rationalize (scm_inexact_to_exact (x),
  5830. scm_inexact_to_exact (eps)));
  5831. }
  5832. else
  5833. {
  5834. /* X and EPS are exact rationals.
  5835. The code that follows is equivalent to the following Scheme code:
  5836. (define (exact-rationalize x eps)
  5837. (let ((n1 (if (negative? x) -1 1))
  5838. (x (abs x))
  5839. (eps (abs eps)))
  5840. (let ((lo (- x eps))
  5841. (hi (+ x eps)))
  5842. (if (<= lo 0)
  5843. 0
  5844. (let loop ((nlo (numerator lo)) (dlo (denominator lo))
  5845. (nhi (numerator hi)) (dhi (denominator hi))
  5846. (n1 n1) (d1 0) (n2 0) (d2 1))
  5847. (let-values (((qlo rlo) (floor/ nlo dlo))
  5848. ((qhi rhi) (floor/ nhi dhi)))
  5849. (let ((n0 (+ n2 (* n1 qlo)))
  5850. (d0 (+ d2 (* d1 qlo))))
  5851. (cond ((zero? rlo) (/ n0 d0))
  5852. ((< qlo qhi) (/ (+ n0 n1) (+ d0 d1)))
  5853. (else (loop dhi rhi dlo rlo n0 d0 n1 d1))))))))))
  5854. */
  5855. int n1_init = 1;
  5856. SCM lo, hi;
  5857. eps = scm_abs (eps);
  5858. if (scm_is_true (scm_negative_p (x)))
  5859. {
  5860. n1_init = -1;
  5861. x = scm_difference (x, SCM_UNDEFINED);
  5862. }
  5863. /* X and EPS are non-negative exact rationals. */
  5864. lo = scm_difference (x, eps);
  5865. hi = scm_sum (x, eps);
  5866. if (scm_is_false (scm_positive_p (lo)))
  5867. /* If zero is included in the interval, return it.
  5868. It is the simplest rational of all. */
  5869. return SCM_INUM0;
  5870. else
  5871. {
  5872. SCM result;
  5873. mpz_t n0, d0, n1, d1, n2, d2;
  5874. mpz_t nlo, dlo, nhi, dhi;
  5875. mpz_t qlo, rlo, qhi, rhi;
  5876. /* LO and HI are positive exact rationals. */
  5877. /* Our approach here follows the method described by Alan
  5878. Bawden in a message entitled "(rationalize x y)" on the
  5879. rrrs-authors mailing list, dated 16 Feb 1988 14:08:28 EST:
  5880. http://groups.csail.mit.edu/mac/ftpdir/scheme-mail/HTML/rrrs-1988/msg00063.html
  5881. In brief, we compute the continued fractions of the two
  5882. endpoints of the interval (LO and HI). The continued
  5883. fraction of the result consists of the common prefix of the
  5884. continued fractions of LO and HI, plus one final term. The
  5885. final term of the result is the smallest integer contained
  5886. in the interval between the remainders of LO and HI after
  5887. the common prefix has been removed.
  5888. The following code lazily computes the continued fraction
  5889. representations of LO and HI, and simultaneously converts
  5890. the continued fraction of the result into a rational
  5891. number. We use MPZ functions directly to avoid type
  5892. dispatch and GC allocation during the loop. */
  5893. mpz_inits (n0, d0, n1, d1, n2, d2,
  5894. nlo, dlo, nhi, dhi,
  5895. qlo, rlo, qhi, rhi,
  5896. NULL);
  5897. /* The variables N1, D1, N2 and D2 are used to compute the
  5898. resulting rational from its continued fraction. At each
  5899. step, N2/D2 and N1/D1 are the last two convergents. They
  5900. are normally initialized to 0/1 and 1/0, respectively.
  5901. However, if we negated X then we must negate the result as
  5902. well, and we do that by initializing N1/D1 to -1/0. */
  5903. mpz_set_si (n1, n1_init);
  5904. mpz_set_ui (d1, 0);
  5905. mpz_set_ui (n2, 0);
  5906. mpz_set_ui (d2, 1);
  5907. /* The variables NLO, DLO, NHI, and DHI are used to lazily
  5908. compute the continued fraction representations of LO and HI
  5909. using Euclid's algorithm. Initially, NLO/DLO == LO and
  5910. NHI/DHI == HI. */
  5911. scm_to_mpz (scm_numerator (lo), nlo);
  5912. scm_to_mpz (scm_denominator (lo), dlo);
  5913. scm_to_mpz (scm_numerator (hi), nhi);
  5914. scm_to_mpz (scm_denominator (hi), dhi);
  5915. /* As long as we're using exact arithmetic, the following loop
  5916. is guaranteed to terminate. */
  5917. for (;;)
  5918. {
  5919. /* Compute the next terms (QLO and QHI) of the continued
  5920. fractions of LO and HI. */
  5921. mpz_fdiv_qr (qlo, rlo, nlo, dlo); /* QLO <-- floor (NLO/DLO), RLO <-- NLO - QLO * DLO */
  5922. mpz_fdiv_qr (qhi, rhi, nhi, dhi); /* QHI <-- floor (NHI/DHI), RHI <-- NHI - QHI * DHI */
  5923. /* The next term of the result will be either QLO or
  5924. QLO+1. Here we compute the next convergent of the
  5925. result based on the assumption that QLO is the next
  5926. term. If that turns out to be wrong, we'll adjust
  5927. these later by adding N1 to N0 and D1 to D0. */
  5928. mpz_set (n0, n2); mpz_addmul (n0, n1, qlo); /* N0 <-- N2 + (QLO * N1) */
  5929. mpz_set (d0, d2); mpz_addmul (d0, d1, qlo); /* D0 <-- D2 + (QLO * D1) */
  5930. /* We stop iterating when an integer is contained in the
  5931. interval between the remainders NLO/DLO and NHI/DHI.
  5932. There are two cases to consider: either NLO/DLO == QLO
  5933. is an integer (indicated by RLO == 0), or QLO < QHI. */
  5934. if (mpz_sgn (rlo) == 0 || mpz_cmp (qlo, qhi) != 0)
  5935. break;
  5936. /* Efficiently shuffle variables around for the next
  5937. iteration. First we shift the recent convergents. */
  5938. mpz_swap (n2, n1); mpz_swap (n1, n0); /* N2 <-- N1 <-- N0 */
  5939. mpz_swap (d2, d1); mpz_swap (d1, d0); /* D2 <-- D1 <-- D0 */
  5940. /* The following shuffling is a bit confusing, so some
  5941. explanation is in order. Conceptually, we're doing a
  5942. couple of things here. After substracting the floor of
  5943. NLO/DLO, the remainder is RLO/DLO. The rest of the
  5944. continued fraction will represent the remainder's
  5945. reciprocal DLO/RLO. Similarly for the HI endpoint.
  5946. So in the next iteration, the new endpoints will be
  5947. DLO/RLO and DHI/RHI. However, when we take the
  5948. reciprocals of these endpoints, their order is
  5949. switched. So in summary, we want NLO/DLO <-- DHI/RHI
  5950. and NHI/DHI <-- DLO/RLO. */
  5951. mpz_swap (nlo, dhi); mpz_swap (dhi, rlo); /* NLO <-- DHI <-- RLO */
  5952. mpz_swap (nhi, dlo); mpz_swap (dlo, rhi); /* NHI <-- DLO <-- RHI */
  5953. }
  5954. /* There is now an integer in the interval [NLO/DLO NHI/DHI].
  5955. The last term of the result will be the smallest integer in
  5956. that interval, which is ceiling(NLO/DLO). We have already
  5957. computed floor(NLO/DLO) in QLO, so now we adjust QLO to be
  5958. equal to the ceiling. */
  5959. if (mpz_sgn (rlo) != 0)
  5960. {
  5961. /* If RLO is non-zero, then NLO/DLO is not an integer and
  5962. the next term will be QLO+1. QLO was used in the
  5963. computation of N0 and D0 above. Here we adjust N0 and
  5964. D0 to be based on QLO+1 instead of QLO. */
  5965. mpz_add (n0, n0, n1); /* N0 <-- N0 + N1 */
  5966. mpz_add (d0, d0, d1); /* D0 <-- D0 + D1 */
  5967. }
  5968. /* The simplest rational in the interval is N0/D0 */
  5969. result = scm_i_make_ratio_already_reduced (scm_from_mpz (n0),
  5970. scm_from_mpz (d0));
  5971. mpz_clears (n0, d0, n1, d1, n2, d2,
  5972. nlo, dlo, nhi, dhi,
  5973. qlo, rlo, qhi, rhi,
  5974. NULL);
  5975. return result;
  5976. }
  5977. }
  5978. }
  5979. #undef FUNC_NAME
  5980. /* conversion functions */
  5981. int
  5982. scm_is_integer (SCM val)
  5983. {
  5984. if (scm_is_exact_integer (val))
  5985. return 1;
  5986. if (SCM_REALP (val))
  5987. {
  5988. double x = SCM_REAL_VALUE (val);
  5989. return !isinf (x) && (x == floor (x));
  5990. }
  5991. return 0;
  5992. }
  5993. int
  5994. scm_is_exact_integer (SCM val)
  5995. {
  5996. return SCM_I_INUMP (val) || SCM_BIGP (val);
  5997. }
  5998. // Given that there is no way to extend intmax_t to encompass types
  5999. // larger than int64, and that we must have int64, intmax will always be
  6000. // 8 bytes wide, and we can treat intmax arguments as int64's.
  6001. verify(SCM_SIZEOF_INTMAX == 8);
  6002. int
  6003. scm_is_signed_integer (SCM val, intmax_t min, intmax_t max)
  6004. {
  6005. if (SCM_I_INUMP (val))
  6006. {
  6007. scm_t_signed_bits n = SCM_I_INUM (val);
  6008. return min <= n && n <= max;
  6009. }
  6010. else if (SCM_BIGP (val))
  6011. {
  6012. int64_t n;
  6013. return scm_integer_to_int64_z (scm_bignum (val), &n)
  6014. && min <= n && n <= max;
  6015. }
  6016. else
  6017. return 0;
  6018. }
  6019. int
  6020. scm_is_unsigned_integer (SCM val, uintmax_t min, uintmax_t max)
  6021. {
  6022. if (SCM_I_INUMP (val))
  6023. {
  6024. scm_t_signed_bits n = SCM_I_INUM (val);
  6025. return n >= 0 && ((uintmax_t)n) >= min && ((uintmax_t)n) <= max;
  6026. }
  6027. else if (SCM_BIGP (val))
  6028. {
  6029. uint64_t n;
  6030. return scm_integer_to_uint64_z (scm_bignum (val), &n)
  6031. && min <= n && n <= max;
  6032. }
  6033. else
  6034. return 0;
  6035. }
  6036. static void range_error (SCM bad_val, SCM min, SCM max) SCM_NORETURN;
  6037. static void
  6038. range_error (SCM bad_val, SCM min, SCM max)
  6039. {
  6040. scm_error (scm_out_of_range_key,
  6041. NULL,
  6042. "Value out of range ~S to< ~S: ~S",
  6043. scm_list_3 (min, max, bad_val),
  6044. scm_list_1 (bad_val));
  6045. }
  6046. #define scm_i_range_error range_error
  6047. static scm_t_inum
  6048. inum_in_range (SCM x, scm_t_inum min, scm_t_inum max)
  6049. {
  6050. if (SCM_LIKELY (SCM_I_INUMP (x)))
  6051. {
  6052. scm_t_inum val = SCM_I_INUM (x);
  6053. if (min <= val && val <= max)
  6054. return val;
  6055. }
  6056. else if (!SCM_BIGP (x))
  6057. scm_wrong_type_arg_msg (NULL, 0, x, "exact integer");
  6058. range_error (x, scm_from_long (min), scm_from_long (max));
  6059. }
  6060. SCM
  6061. scm_from_signed_integer (intmax_t arg)
  6062. {
  6063. return scm_integer_from_int64 (arg);
  6064. }
  6065. intmax_t
  6066. scm_to_signed_integer (SCM arg, intmax_t min, intmax_t max)
  6067. {
  6068. int64_t ret;
  6069. if (SCM_I_INUMP (arg))
  6070. ret = SCM_I_INUM (arg);
  6071. else if (SCM_BIGP (arg))
  6072. {
  6073. if (!scm_integer_to_int64_z (scm_bignum (arg), &ret))
  6074. goto out_of_range;
  6075. }
  6076. else
  6077. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6078. if (min <= ret && ret <= max)
  6079. return ret;
  6080. out_of_range:
  6081. range_error (arg, scm_from_intmax (min), scm_from_intmax (max));
  6082. }
  6083. SCM
  6084. scm_from_unsigned_integer (uintmax_t arg)
  6085. {
  6086. return scm_integer_from_uint64 (arg);
  6087. }
  6088. uintmax_t
  6089. scm_to_unsigned_integer (SCM arg, uintmax_t min, uintmax_t max)
  6090. {
  6091. uint64_t ret;
  6092. if (SCM_I_INUMP (arg))
  6093. {
  6094. scm_t_inum n = SCM_I_INUM (arg);
  6095. if (n < 0)
  6096. goto out_of_range;
  6097. ret = n;
  6098. }
  6099. else if (SCM_BIGP (arg))
  6100. {
  6101. if (!scm_integer_to_uint64_z (scm_bignum (arg), &ret))
  6102. goto out_of_range;
  6103. }
  6104. else
  6105. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6106. if (min <= ret && ret <= max)
  6107. return ret;
  6108. out_of_range:
  6109. range_error (arg, scm_from_uintmax (min), scm_from_uintmax (max));
  6110. }
  6111. int8_t
  6112. scm_to_int8 (SCM arg)
  6113. {
  6114. return inum_in_range (arg, INT8_MIN, INT8_MAX);
  6115. }
  6116. SCM
  6117. scm_from_int8 (int8_t arg)
  6118. {
  6119. return SCM_I_MAKINUM (arg);
  6120. }
  6121. uint8_t
  6122. scm_to_uint8 (SCM arg)
  6123. {
  6124. return inum_in_range (arg, 0, UINT8_MAX);
  6125. }
  6126. SCM
  6127. scm_from_uint8 (uint8_t arg)
  6128. {
  6129. return SCM_I_MAKINUM (arg);
  6130. }
  6131. int16_t
  6132. scm_to_int16 (SCM arg)
  6133. {
  6134. return inum_in_range (arg, INT16_MIN, INT16_MAX);
  6135. }
  6136. SCM
  6137. scm_from_int16 (int16_t arg)
  6138. {
  6139. return SCM_I_MAKINUM (arg);
  6140. }
  6141. uint16_t
  6142. scm_to_uint16 (SCM arg)
  6143. {
  6144. return inum_in_range (arg, 0, UINT16_MAX);
  6145. }
  6146. SCM
  6147. scm_from_uint16 (uint16_t arg)
  6148. {
  6149. return SCM_I_MAKINUM (arg);
  6150. }
  6151. int32_t
  6152. scm_to_int32 (SCM arg)
  6153. {
  6154. #if SCM_SIZEOF_LONG == 4
  6155. if (SCM_I_INUMP (arg))
  6156. return SCM_I_INUM (arg);
  6157. else if (!SCM_BIGP (arg))
  6158. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6159. int32_t ret;
  6160. if (scm_integer_to_int32_z (scm_bignum (arg), &ret))
  6161. return ret;
  6162. range_error (arg, scm_integer_from_int32 (INT32_MIN),
  6163. scm_integer_from_int32 (INT32_MAX));
  6164. #elif SCM_SIZEOF_LONG == 8
  6165. return inum_in_range (arg, INT32_MIN, INT32_MAX);
  6166. #else
  6167. #error bad inum size
  6168. #endif
  6169. }
  6170. SCM
  6171. scm_from_int32 (int32_t arg)
  6172. {
  6173. #if SCM_SIZEOF_LONG == 4
  6174. return scm_integer_from_int32 (arg);
  6175. #elif SCM_SIZEOF_LONG == 8
  6176. return SCM_I_MAKINUM (arg);
  6177. #else
  6178. #error bad inum size
  6179. #endif
  6180. }
  6181. uint32_t
  6182. scm_to_uint32 (SCM arg)
  6183. {
  6184. #if SCM_SIZEOF_LONG == 4
  6185. if (SCM_I_INUMP (arg))
  6186. {
  6187. if (SCM_I_INUM (arg) >= 0)
  6188. return SCM_I_INUM (arg);
  6189. }
  6190. else if (SCM_BIGP (arg))
  6191. {
  6192. uint32_t ret;
  6193. if (scm_integer_to_uint32_z (scm_bignum (arg), &ret))
  6194. return ret;
  6195. }
  6196. else
  6197. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6198. range_error (arg, scm_integer_from_uint32 (0), scm_integer_from_uint32 (UINT32_MAX));
  6199. #elif SCM_SIZEOF_LONG == 8
  6200. return inum_in_range (arg, 0, UINT32_MAX);
  6201. #else
  6202. #error bad inum size
  6203. #endif
  6204. }
  6205. SCM
  6206. scm_from_uint32 (uint32_t arg)
  6207. {
  6208. #if SCM_SIZEOF_LONG == 4
  6209. return scm_integer_from_uint32 (arg);
  6210. #elif SCM_SIZEOF_LONG == 8
  6211. return SCM_I_MAKINUM (arg);
  6212. #else
  6213. #error bad inum size
  6214. #endif
  6215. }
  6216. int64_t
  6217. scm_to_int64 (SCM arg)
  6218. {
  6219. if (SCM_I_INUMP (arg))
  6220. return SCM_I_INUM (arg);
  6221. else if (!SCM_BIGP (arg))
  6222. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6223. int64_t ret;
  6224. if (scm_integer_to_int64_z (scm_bignum (arg), &ret))
  6225. return ret;
  6226. range_error (arg, scm_integer_from_int64 (INT64_MIN),
  6227. scm_integer_from_int64 (INT64_MAX));
  6228. }
  6229. SCM
  6230. scm_from_int64 (int64_t arg)
  6231. {
  6232. return scm_integer_from_int64 (arg);
  6233. }
  6234. uint64_t
  6235. scm_to_uint64 (SCM arg)
  6236. {
  6237. if (SCM_I_INUMP (arg))
  6238. {
  6239. if (SCM_I_INUM (arg) >= 0)
  6240. return SCM_I_INUM (arg);
  6241. }
  6242. else if (SCM_BIGP (arg))
  6243. {
  6244. uint64_t ret;
  6245. if (scm_integer_to_uint64_z (scm_bignum (arg), &ret))
  6246. return ret;
  6247. }
  6248. else
  6249. scm_wrong_type_arg_msg (NULL, 0, arg, "exact integer");
  6250. range_error (arg, scm_integer_from_uint64(0), scm_integer_from_uint64 (UINT64_MAX));
  6251. }
  6252. SCM
  6253. scm_from_uint64 (uint64_t arg)
  6254. {
  6255. return scm_integer_from_uint64 (arg);
  6256. }
  6257. scm_t_wchar
  6258. scm_to_wchar (SCM arg)
  6259. {
  6260. return inum_in_range (arg, -1, 0x10ffff);
  6261. }
  6262. SCM
  6263. scm_from_wchar (scm_t_wchar arg)
  6264. {
  6265. return SCM_I_MAKINUM (arg);
  6266. }
  6267. void
  6268. scm_to_mpz (SCM val, mpz_t rop)
  6269. {
  6270. if (SCM_I_INUMP (val))
  6271. mpz_set_si (rop, SCM_I_INUM (val));
  6272. else if (SCM_BIGP (val))
  6273. scm_integer_set_mpz_z (scm_bignum (val), rop);
  6274. else
  6275. scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
  6276. }
  6277. SCM
  6278. scm_from_mpz (mpz_t val)
  6279. {
  6280. return scm_integer_from_mpz (val);
  6281. }
  6282. int
  6283. scm_is_real (SCM val)
  6284. {
  6285. return scm_is_true (scm_real_p (val));
  6286. }
  6287. int
  6288. scm_is_rational (SCM val)
  6289. {
  6290. return scm_is_true (scm_rational_p (val));
  6291. }
  6292. double
  6293. scm_to_double (SCM val)
  6294. {
  6295. if (SCM_I_INUMP (val))
  6296. return SCM_I_INUM (val);
  6297. else if (SCM_BIGP (val))
  6298. return scm_integer_to_double_z (scm_bignum (val));
  6299. else if (SCM_FRACTIONP (val))
  6300. return scm_i_fraction2double (val);
  6301. else if (SCM_REALP (val))
  6302. return SCM_REAL_VALUE (val);
  6303. else
  6304. scm_wrong_type_arg_msg (NULL, 0, val, "real number");
  6305. }
  6306. SCM
  6307. scm_from_double (double val)
  6308. {
  6309. return scm_i_from_double (val);
  6310. }
  6311. int
  6312. scm_is_complex (SCM val)
  6313. {
  6314. return scm_is_true (scm_complex_p (val));
  6315. }
  6316. double
  6317. scm_c_real_part (SCM z)
  6318. {
  6319. if (SCM_COMPLEXP (z))
  6320. return SCM_COMPLEX_REAL (z);
  6321. else
  6322. {
  6323. /* Use the scm_real_part to get proper error checking and
  6324. dispatching.
  6325. */
  6326. return scm_to_double (scm_real_part (z));
  6327. }
  6328. }
  6329. double
  6330. scm_c_imag_part (SCM z)
  6331. {
  6332. if (SCM_COMPLEXP (z))
  6333. return SCM_COMPLEX_IMAG (z);
  6334. else
  6335. {
  6336. /* Use the scm_imag_part to get proper error checking and
  6337. dispatching. The result will almost always be 0.0, but not
  6338. always.
  6339. */
  6340. return scm_to_double (scm_imag_part (z));
  6341. }
  6342. }
  6343. double
  6344. scm_c_magnitude (SCM z)
  6345. {
  6346. return scm_to_double (scm_magnitude (z));
  6347. }
  6348. double
  6349. scm_c_angle (SCM z)
  6350. {
  6351. return scm_to_double (scm_angle (z));
  6352. }
  6353. int
  6354. scm_is_number (SCM z)
  6355. {
  6356. return scm_is_true (scm_number_p (z));
  6357. }
  6358. /* Returns log(x * 2^shift) */
  6359. static SCM
  6360. log_of_shifted_double (double x, long shift)
  6361. {
  6362. /* cf scm_log10 */
  6363. double ans = log (fabs (x)) + shift * M_LN2;
  6364. if (signbit (x) && SCM_LIKELY (!isnan (x)))
  6365. return scm_c_make_rectangular (ans, M_PI);
  6366. else
  6367. return scm_i_from_double (ans);
  6368. }
  6369. /* Returns log(n), for exact integer n */
  6370. static SCM
  6371. log_of_exact_integer (SCM n)
  6372. {
  6373. if (SCM_I_INUMP (n))
  6374. return log_of_shifted_double (SCM_I_INUM (n), 0);
  6375. else if (SCM_BIGP (n))
  6376. {
  6377. long expon;
  6378. double signif = scm_integer_frexp_z (scm_bignum (n), &expon);
  6379. return log_of_shifted_double (signif, expon);
  6380. }
  6381. else
  6382. abort ();
  6383. }
  6384. /* Returns log(n/d), for exact non-zero integers n and d */
  6385. static SCM
  6386. log_of_fraction (SCM n, SCM d)
  6387. {
  6388. long n_size = scm_to_long (scm_integer_length (n));
  6389. long d_size = scm_to_long (scm_integer_length (d));
  6390. if (labs (n_size - d_size) > 1)
  6391. return (scm_difference (log_of_exact_integer (n),
  6392. log_of_exact_integer (d)));
  6393. else if (scm_is_false (scm_negative_p (n)))
  6394. return scm_i_from_double
  6395. (log1p (scm_i_divide2double (scm_difference (n, d), d)));
  6396. else
  6397. return scm_c_make_rectangular
  6398. (log1p (scm_i_divide2double (scm_difference (scm_abs (n), d),
  6399. d)),
  6400. M_PI);
  6401. }
  6402. /* In the following functions we dispatch to the real-arg funcs like log()
  6403. when we know the arg is real, instead of just handing everything to
  6404. clog() for instance. This is in case clog() doesn't optimize for a
  6405. real-only case, and because we have to test SCM_COMPLEXP anyway so may as
  6406. well use it to go straight to the applicable C func. */
  6407. SCM_PRIMITIVE_GENERIC (scm_log, "log", 1, 0, 0,
  6408. (SCM z),
  6409. "Return the natural logarithm of @var{z}.")
  6410. #define FUNC_NAME s_scm_log
  6411. {
  6412. if (SCM_COMPLEXP (z))
  6413. {
  6414. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG \
  6415. && defined (SCM_COMPLEX_VALUE)
  6416. return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
  6417. #else
  6418. double re = SCM_COMPLEX_REAL (z);
  6419. double im = SCM_COMPLEX_IMAG (z);
  6420. return scm_c_make_rectangular (log (hypot (re, im)),
  6421. atan2 (im, re));
  6422. #endif
  6423. }
  6424. else if (SCM_REALP (z))
  6425. return log_of_shifted_double (SCM_REAL_VALUE (z), 0);
  6426. else if (SCM_I_INUMP (z))
  6427. {
  6428. if (scm_is_eq (z, SCM_INUM0))
  6429. scm_num_overflow (s_scm_log);
  6430. return log_of_shifted_double (SCM_I_INUM (z), 0);
  6431. }
  6432. else if (SCM_BIGP (z))
  6433. return log_of_exact_integer (z);
  6434. else if (SCM_FRACTIONP (z))
  6435. return log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  6436. SCM_FRACTION_DENOMINATOR (z));
  6437. else
  6438. return scm_wta_dispatch_1 (g_scm_log, z, 1, s_scm_log);
  6439. }
  6440. #undef FUNC_NAME
  6441. SCM_PRIMITIVE_GENERIC (scm_log10, "log10", 1, 0, 0,
  6442. (SCM z),
  6443. "Return the base 10 logarithm of @var{z}.")
  6444. #define FUNC_NAME s_scm_log10
  6445. {
  6446. if (SCM_COMPLEXP (z))
  6447. {
  6448. /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
  6449. clog() and a multiply by M_LOG10E, rather than the fallback
  6450. log10+hypot+atan2.) */
  6451. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CLOG10 \
  6452. && defined SCM_COMPLEX_VALUE
  6453. return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
  6454. #else
  6455. double re = SCM_COMPLEX_REAL (z);
  6456. double im = SCM_COMPLEX_IMAG (z);
  6457. return scm_c_make_rectangular (log10 (hypot (re, im)),
  6458. M_LOG10E * atan2 (im, re));
  6459. #endif
  6460. }
  6461. else if (SCM_REALP (z) || SCM_I_INUMP (z))
  6462. {
  6463. if (scm_is_eq (z, SCM_INUM0))
  6464. scm_num_overflow (s_scm_log10);
  6465. {
  6466. double re = scm_to_double (z);
  6467. double l = log10 (fabs (re));
  6468. /* cf log_of_shifted_double */
  6469. if (signbit (re) && SCM_LIKELY (!isnan (re)))
  6470. return scm_c_make_rectangular (l, M_LOG10E * M_PI);
  6471. else
  6472. return scm_i_from_double (l);
  6473. }
  6474. }
  6475. else if (SCM_BIGP (z))
  6476. return scm_product (flo_log10e, log_of_exact_integer (z));
  6477. else if (SCM_FRACTIONP (z))
  6478. return scm_product (flo_log10e,
  6479. log_of_fraction (SCM_FRACTION_NUMERATOR (z),
  6480. SCM_FRACTION_DENOMINATOR (z)));
  6481. else
  6482. return scm_wta_dispatch_1 (g_scm_log10, z, 1, s_scm_log10);
  6483. }
  6484. #undef FUNC_NAME
  6485. SCM_PRIMITIVE_GENERIC (scm_exp, "exp", 1, 0, 0,
  6486. (SCM z),
  6487. "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
  6488. "base of natural logarithms (2.71828@dots{}).")
  6489. #define FUNC_NAME s_scm_exp
  6490. {
  6491. if (SCM_COMPLEXP (z))
  6492. {
  6493. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_CEXP \
  6494. && defined (SCM_COMPLEX_VALUE)
  6495. return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
  6496. #else
  6497. return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
  6498. SCM_COMPLEX_IMAG (z));
  6499. #endif
  6500. }
  6501. else if (SCM_NUMBERP (z))
  6502. {
  6503. /* When z is a negative bignum the conversion to double overflows,
  6504. giving -infinity, but that's ok, the exp is still 0.0. */
  6505. return scm_i_from_double (exp (scm_to_double (z)));
  6506. }
  6507. else
  6508. return scm_wta_dispatch_1 (g_scm_exp, z, 1, s_scm_exp);
  6509. }
  6510. #undef FUNC_NAME
  6511. SCM_DEFINE (scm_i_exact_integer_sqrt, "exact-integer-sqrt", 1, 0, 0,
  6512. (SCM k),
  6513. "Return two exact non-negative integers @var{s} and @var{r}\n"
  6514. "such that @math{@var{k} = @var{s}^2 + @var{r}} and\n"
  6515. "@math{@var{s}^2 <= @var{k} < (@var{s} + 1)^2}.\n"
  6516. "An error is raised if @var{k} is not an exact non-negative integer.\n"
  6517. "\n"
  6518. "@lisp\n"
  6519. "(exact-integer-sqrt 10) @result{} 3 and 1\n"
  6520. "@end lisp")
  6521. #define FUNC_NAME s_scm_i_exact_integer_sqrt
  6522. {
  6523. SCM s, r;
  6524. scm_exact_integer_sqrt (k, &s, &r);
  6525. return scm_values_2 (s, r);
  6526. }
  6527. #undef FUNC_NAME
  6528. void
  6529. scm_exact_integer_sqrt (SCM k, SCM *sp, SCM *rp)
  6530. {
  6531. if (SCM_I_INUMP (k))
  6532. {
  6533. scm_t_inum kk = SCM_I_INUM (k);
  6534. if (kk >= 0)
  6535. return scm_integer_exact_sqrt_i (kk, sp, rp);
  6536. }
  6537. else if (SCM_BIGP (k))
  6538. {
  6539. struct scm_bignum *zk = scm_bignum (k);
  6540. if (!scm_is_integer_negative_z (zk))
  6541. return scm_integer_exact_sqrt_z (zk, sp, rp);
  6542. }
  6543. scm_wrong_type_arg_msg ("exact-integer-sqrt", SCM_ARG1, k,
  6544. "exact non-negative integer");
  6545. }
  6546. SCM_PRIMITIVE_GENERIC (scm_sqrt, "sqrt", 1, 0, 0,
  6547. (SCM z),
  6548. "Return the square root of @var{z}. Of the two possible roots\n"
  6549. "(positive and negative), the one with positive real part\n"
  6550. "is returned, or if that's zero then a positive imaginary part.\n"
  6551. "Thus,\n"
  6552. "\n"
  6553. "@example\n"
  6554. "(sqrt 9.0) @result{} 3.0\n"
  6555. "(sqrt -9.0) @result{} 0.0+3.0i\n"
  6556. "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
  6557. "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
  6558. "@end example")
  6559. #define FUNC_NAME s_scm_sqrt
  6560. {
  6561. if (SCM_I_INUMP (z))
  6562. {
  6563. scm_t_inum i = SCM_I_INUM (z);
  6564. if (scm_is_integer_perfect_square_i (i))
  6565. return scm_integer_floor_sqrt_i (i);
  6566. double root = scm_integer_inexact_sqrt_i (i);
  6567. return (root < 0)
  6568. ? scm_c_make_rectangular (0.0, -root)
  6569. : scm_i_from_double (root);
  6570. }
  6571. else if (SCM_BIGP (z))
  6572. {
  6573. struct scm_bignum *k = scm_bignum (z);
  6574. if (scm_is_integer_perfect_square_z (k))
  6575. return scm_integer_floor_sqrt_z (k);
  6576. double root = scm_integer_inexact_sqrt_z (k);
  6577. return (root < 0)
  6578. ? scm_c_make_rectangular (0.0, -root)
  6579. : scm_i_from_double (root);
  6580. }
  6581. else if (SCM_REALP (z))
  6582. {
  6583. double xx = SCM_REAL_VALUE (z);
  6584. if (xx < 0)
  6585. return scm_c_make_rectangular (0.0, sqrt (-xx));
  6586. else
  6587. return scm_i_from_double (sqrt (xx));
  6588. }
  6589. else if (SCM_COMPLEXP (z))
  6590. {
  6591. #if defined HAVE_COMPLEX_DOUBLE && defined HAVE_USABLE_CSQRT \
  6592. && defined SCM_COMPLEX_VALUE
  6593. return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (z)));
  6594. #else
  6595. double re = SCM_COMPLEX_REAL (z);
  6596. double im = SCM_COMPLEX_IMAG (z);
  6597. return scm_c_make_polar (sqrt (hypot (re, im)),
  6598. 0.5 * atan2 (im, re));
  6599. #endif
  6600. }
  6601. else if (SCM_FRACTIONP (z))
  6602. {
  6603. SCM n = SCM_FRACTION_NUMERATOR (z);
  6604. SCM d = SCM_FRACTION_DENOMINATOR (z);
  6605. SCM nr = scm_sqrt (n);
  6606. SCM dr = scm_sqrt (d);
  6607. if (scm_is_exact_integer (nr) && scm_is_exact_integer (dr))
  6608. return scm_i_make_ratio_already_reduced (nr, dr);
  6609. double xx = scm_i_divide2double (n, d);
  6610. double abs_xx = fabs (xx);
  6611. long shift = 0;
  6612. if (abs_xx > DBL_MAX || abs_xx < DBL_MIN)
  6613. {
  6614. shift = (scm_to_long (scm_integer_length (n))
  6615. - scm_to_long (scm_integer_length (d))) / 2;
  6616. if (shift > 0)
  6617. d = lsh (d, scm_from_long (2 * shift), FUNC_NAME);
  6618. else
  6619. n = lsh (n, scm_from_long (-2 * shift), FUNC_NAME);
  6620. xx = scm_i_divide2double (n, d);
  6621. }
  6622. if (xx < 0)
  6623. return scm_c_make_rectangular (0.0, ldexp (sqrt (-xx), shift));
  6624. else
  6625. return scm_i_from_double (ldexp (sqrt (xx), shift));
  6626. }
  6627. else
  6628. return scm_wta_dispatch_1 (g_scm_sqrt, z, 1, s_scm_sqrt);
  6629. }
  6630. #undef FUNC_NAME
  6631. void
  6632. scm_init_numbers ()
  6633. {
  6634. /* It may be possible to tune the performance of some algorithms by using
  6635. * the following constants to avoid the creation of bignums. Please, before
  6636. * using these values, remember the two rules of program optimization:
  6637. * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
  6638. scm_c_define ("most-positive-fixnum",
  6639. SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
  6640. scm_c_define ("most-negative-fixnum",
  6641. SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
  6642. scm_add_feature ("complex");
  6643. scm_add_feature ("inexact");
  6644. flo0 = scm_i_from_double (0.0);
  6645. flo_log10e = scm_i_from_double (M_LOG10E);
  6646. exactly_one_half = scm_divide (SCM_INUM1, SCM_I_MAKINUM (2));
  6647. {
  6648. /* Set scm_i_divide2double_lo2b to (2 b^p - 1) */
  6649. mpz_init_set_ui (scm_i_divide2double_lo2b, 1);
  6650. mpz_mul_2exp (scm_i_divide2double_lo2b,
  6651. scm_i_divide2double_lo2b,
  6652. DBL_MANT_DIG + 1); /* 2 b^p */
  6653. mpz_sub_ui (scm_i_divide2double_lo2b, scm_i_divide2double_lo2b, 1);
  6654. }
  6655. {
  6656. /* Set dbl_minimum_normal_mantissa to b^{p-1} */
  6657. mpz_init_set_ui (dbl_minimum_normal_mantissa, 1);
  6658. mpz_mul_2exp (dbl_minimum_normal_mantissa,
  6659. dbl_minimum_normal_mantissa,
  6660. DBL_MANT_DIG - 1);
  6661. }
  6662. #include "numbers.x"
  6663. }