numbers.c 224 KB

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