numbers.c 300 KB

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