numbers.c 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208
  1. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  2. *
  3. * Portions Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories
  4. * and Bellcore. See scm_divide.
  5. *
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /* General assumptions:
  22. * All objects satisfying SCM_COMPLEXP() have a non-zero complex component.
  23. * All objects satisfying SCM_BIGP() are too large to fit in a fixnum.
  24. * If an object satisfies integer?, it's either an inum, a bignum, or a real.
  25. * If floor (r) == r, r is an int, and mpz_set_d will DTRT.
  26. * All objects satisfying SCM_FRACTIONP are never an integer.
  27. */
  28. /* TODO:
  29. - see if special casing bignums and reals in integer-exponent when
  30. possible (to use mpz_pow and mpf_pow_ui) is faster.
  31. - look in to better short-circuiting of common cases in
  32. integer-expt and elsewhere.
  33. - see if direct mpz operations can help in ash and elsewhere.
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. # include <config.h>
  37. #endif
  38. #include <math.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #if HAVE_COMPLEX_H
  42. #include <complex.h>
  43. #endif
  44. #include "libguile/_scm.h"
  45. #include "libguile/feature.h"
  46. #include "libguile/ports.h"
  47. #include "libguile/root.h"
  48. #include "libguile/smob.h"
  49. #include "libguile/strings.h"
  50. #include "libguile/validate.h"
  51. #include "libguile/numbers.h"
  52. #include "libguile/deprecation.h"
  53. #include "libguile/eq.h"
  54. #include "libguile/discouraged.h"
  55. /* values per glibc, if not already defined */
  56. #ifndef M_LOG10E
  57. #define M_LOG10E 0.43429448190325182765
  58. #endif
  59. #ifndef M_PI
  60. #define M_PI 3.14159265358979323846
  61. #endif
  62. /*
  63. Wonder if this might be faster for some of our code? A switch on
  64. the numtag would jump directly to the right case, and the
  65. SCM_I_NUMTAG code might be faster than repeated SCM_FOOP tests...
  66. #define SCM_I_NUMTAG_NOTNUM 0
  67. #define SCM_I_NUMTAG_INUM 1
  68. #define SCM_I_NUMTAG_BIG scm_tc16_big
  69. #define SCM_I_NUMTAG_REAL scm_tc16_real
  70. #define SCM_I_NUMTAG_COMPLEX scm_tc16_complex
  71. #define SCM_I_NUMTAG(x) \
  72. (SCM_I_INUMP(x) ? SCM_I_NUMTAG_INUM \
  73. : (SCM_IMP(x) ? SCM_I_NUMTAG_NOTNUM \
  74. : (((0xfcff & SCM_CELL_TYPE (x)) == scm_tc7_number) ? SCM_TYP16(x) \
  75. : SCM_I_NUMTAG_NOTNUM)))
  76. */
  77. /* the macro above will not work as is with fractions */
  78. #define SCM_SWAP(x, y) do { SCM __t = x; x = y; y = __t; } while (0)
  79. /* FLOBUFLEN is the maximum number of characters neccessary for the
  80. * printed or scm_string representation of an inexact number.
  81. */
  82. #define FLOBUFLEN (40+2*(sizeof(double)/sizeof(char)*SCM_CHAR_BIT*3+9)/10)
  83. #if defined (SCO)
  84. #if ! defined (HAVE_ISNAN)
  85. #define HAVE_ISNAN
  86. static int
  87. isnan (double x)
  88. {
  89. return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0;
  90. }
  91. #endif
  92. #if ! defined (HAVE_ISINF)
  93. #define HAVE_ISINF
  94. static int
  95. isinf (double x)
  96. {
  97. return (IsNANorINF (x) && IsINF (x)) ? 1 : 0;
  98. }
  99. #endif
  100. #endif
  101. /* mpz_cmp_d in gmp 4.1.3 doesn't recognise infinities, so xmpz_cmp_d uses
  102. an explicit check. In some future gmp (don't know what version number),
  103. mpz_cmp_d is supposed to do this itself. */
  104. #if 1
  105. #define xmpz_cmp_d(z, d) \
  106. (xisinf (d) ? (d < 0.0 ? 1 : -1) : mpz_cmp_d (z, d))
  107. #else
  108. #define xmpz_cmp_d(z, d) mpz_cmp_d (z, d)
  109. #endif
  110. /* For reference, sparc solaris 7 has infinities (IEEE) but doesn't have
  111. isinf. It does have finite and isnan though, hence the use of those.
  112. fpclass would be a possibility on that system too. */
  113. static int
  114. xisinf (double x)
  115. {
  116. #if defined (HAVE_ISINF)
  117. return isinf (x);
  118. #elif defined (HAVE_FINITE) && defined (HAVE_ISNAN)
  119. return (! (finite (x) || isnan (x)));
  120. #else
  121. return 0;
  122. #endif
  123. }
  124. static int
  125. xisnan (double x)
  126. {
  127. #if defined (HAVE_ISNAN)
  128. return isnan (x);
  129. #else
  130. return 0;
  131. #endif
  132. }
  133. #if defined (GUILE_I)
  134. #if HAVE_COMPLEX_DOUBLE
  135. /* For an SCM object Z which is a complex number (ie. satisfies
  136. SCM_COMPLEXP), return its value as a C level "complex double". */
  137. #define SCM_COMPLEX_VALUE(z) \
  138. (SCM_COMPLEX_REAL (z) + GUILE_I * SCM_COMPLEX_IMAG (z))
  139. static inline SCM scm_from_complex_double (complex double z) SCM_UNUSED;
  140. /* Convert a C "complex double" to an SCM value. */
  141. static inline SCM
  142. scm_from_complex_double (complex double z)
  143. {
  144. return scm_c_make_rectangular (creal (z), cimag (z));
  145. }
  146. #endif /* HAVE_COMPLEX_DOUBLE */
  147. #endif /* GUILE_I */
  148. static mpz_t z_negative_one;
  149. SCM
  150. scm_i_mkbig ()
  151. {
  152. /* Return a newly created bignum. */
  153. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  154. mpz_init (SCM_I_BIG_MPZ (z));
  155. return z;
  156. }
  157. SCM
  158. scm_i_long2big (long x)
  159. {
  160. /* Return a newly created bignum initialized to X. */
  161. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  162. mpz_init_set_si (SCM_I_BIG_MPZ (z), x);
  163. return z;
  164. }
  165. SCM
  166. scm_i_ulong2big (unsigned long x)
  167. {
  168. /* Return a newly created bignum initialized to X. */
  169. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  170. mpz_init_set_ui (SCM_I_BIG_MPZ (z), x);
  171. return z;
  172. }
  173. SCM
  174. scm_i_clonebig (SCM src_big, int same_sign_p)
  175. {
  176. /* Copy src_big's value, negate it if same_sign_p is false, and return. */
  177. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  178. mpz_init_set (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (src_big));
  179. if (!same_sign_p)
  180. mpz_neg (SCM_I_BIG_MPZ (z), SCM_I_BIG_MPZ (z));
  181. return z;
  182. }
  183. int
  184. scm_i_bigcmp (SCM x, SCM y)
  185. {
  186. /* Return neg if x < y, pos if x > y, and 0 if x == y */
  187. /* presume we already know x and y are bignums */
  188. int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  189. scm_remember_upto_here_2 (x, y);
  190. return result;
  191. }
  192. SCM
  193. scm_i_dbl2big (double d)
  194. {
  195. /* results are only defined if d is an integer */
  196. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  197. mpz_init_set_d (SCM_I_BIG_MPZ (z), d);
  198. return z;
  199. }
  200. /* Convert a integer in double representation to a SCM number. */
  201. SCM
  202. scm_i_dbl2num (double u)
  203. {
  204. /* SCM_MOST_POSITIVE_FIXNUM+1 and SCM_MOST_NEGATIVE_FIXNUM are both
  205. powers of 2, so there's no rounding when making "double" values
  206. from them. If plain SCM_MOST_POSITIVE_FIXNUM was used it could
  207. get rounded on a 64-bit machine, hence the "+1".
  208. The use of floor() to force to an integer value ensures we get a
  209. "numerically closest" value without depending on how a
  210. double->long cast or how mpz_set_d will round. For reference,
  211. double->long probably follows the hardware rounding mode,
  212. mpz_set_d truncates towards zero. */
  213. /* XXX - what happens when SCM_MOST_POSITIVE_FIXNUM etc is not
  214. representable as a double? */
  215. if (u < (double) (SCM_MOST_POSITIVE_FIXNUM+1)
  216. && u >= (double) SCM_MOST_NEGATIVE_FIXNUM)
  217. return SCM_I_MAKINUM ((long) u);
  218. else
  219. return scm_i_dbl2big (u);
  220. }
  221. /* scm_i_big2dbl() rounds to the closest representable double, in accordance
  222. with R5RS exact->inexact.
  223. The approach is to use mpz_get_d to pick out the high DBL_MANT_DIG bits
  224. (ie. truncate towards zero), then adjust to get the closest double by
  225. examining the next lower bit and adding 1 (to the absolute value) if
  226. necessary.
  227. Bignums exactly half way between representable doubles are rounded to the
  228. next higher absolute value (ie. away from zero). This seems like an
  229. adequate interpretation of R5RS "numerically closest", and it's easier
  230. and faster than a full "nearest-even" style.
  231. The bit test must be done on the absolute value of the mpz_t, which means
  232. we need to use mpz_getlimbn. mpz_tstbit is not right, it treats
  233. negatives as twos complement.
  234. In current gmp 4.1.3, mpz_get_d rounding is unspecified. It ends up
  235. following the hardware rounding mode, but applied to the absolute value
  236. of the mpz_t operand. This is not what we want so we put the high
  237. DBL_MANT_DIG bits into a temporary. In some future gmp, don't know when,
  238. mpz_get_d is supposed to always truncate towards zero.
  239. ENHANCE-ME: The temporary init+clear to force the rounding in gmp 4.1.3
  240. is a slowdown. It'd be faster to pick out the relevant high bits with
  241. mpz_getlimbn if we could be bothered coding that, and if the new
  242. truncating gmp doesn't come out. */
  243. double
  244. scm_i_big2dbl (SCM b)
  245. {
  246. double result;
  247. size_t bits;
  248. bits = mpz_sizeinbase (SCM_I_BIG_MPZ (b), 2);
  249. #if 1
  250. {
  251. /* Current GMP, eg. 4.1.3, force truncation towards zero */
  252. mpz_t tmp;
  253. if (bits > DBL_MANT_DIG)
  254. {
  255. size_t shift = bits - DBL_MANT_DIG;
  256. mpz_init2 (tmp, DBL_MANT_DIG);
  257. mpz_tdiv_q_2exp (tmp, SCM_I_BIG_MPZ (b), shift);
  258. result = ldexp (mpz_get_d (tmp), shift);
  259. mpz_clear (tmp);
  260. }
  261. else
  262. {
  263. result = mpz_get_d (SCM_I_BIG_MPZ (b));
  264. }
  265. }
  266. #else
  267. /* Future GMP */
  268. result = mpz_get_d (SCM_I_BIG_MPZ (b));
  269. #endif
  270. if (bits > DBL_MANT_DIG)
  271. {
  272. unsigned long pos = bits - DBL_MANT_DIG - 1;
  273. /* test bit number "pos" in absolute value */
  274. if (mpz_getlimbn (SCM_I_BIG_MPZ (b), pos / GMP_NUMB_BITS)
  275. & ((mp_limb_t) 1 << (pos % GMP_NUMB_BITS)))
  276. {
  277. result += ldexp ((double) mpz_sgn (SCM_I_BIG_MPZ (b)), pos + 1);
  278. }
  279. }
  280. scm_remember_upto_here_1 (b);
  281. return result;
  282. }
  283. SCM
  284. scm_i_normbig (SCM b)
  285. {
  286. /* convert a big back to a fixnum if it'll fit */
  287. /* presume b is a bignum */
  288. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (b)))
  289. {
  290. long val = mpz_get_si (SCM_I_BIG_MPZ (b));
  291. if (SCM_FIXABLE (val))
  292. b = SCM_I_MAKINUM (val);
  293. }
  294. return b;
  295. }
  296. static SCM_C_INLINE_KEYWORD SCM
  297. scm_i_mpz2num (mpz_t b)
  298. {
  299. /* convert a mpz number to a SCM number. */
  300. if (mpz_fits_slong_p (b))
  301. {
  302. long val = mpz_get_si (b);
  303. if (SCM_FIXABLE (val))
  304. return SCM_I_MAKINUM (val);
  305. }
  306. {
  307. SCM z = scm_double_cell (scm_tc16_big, 0, 0, 0);
  308. mpz_init_set (SCM_I_BIG_MPZ (z), b);
  309. return z;
  310. }
  311. }
  312. /* this is needed when we want scm_divide to make a float, not a ratio, even if passed two ints */
  313. static SCM scm_divide2real (SCM x, SCM y);
  314. static SCM
  315. scm_i_make_ratio (SCM numerator, SCM denominator)
  316. #define FUNC_NAME "make-ratio"
  317. {
  318. /* First make sure the arguments are proper.
  319. */
  320. if (SCM_I_INUMP (denominator))
  321. {
  322. if (scm_is_eq (denominator, SCM_INUM0))
  323. scm_num_overflow ("make-ratio");
  324. if (scm_is_eq (denominator, SCM_I_MAKINUM(1)))
  325. return numerator;
  326. }
  327. else
  328. {
  329. if (!(SCM_BIGP(denominator)))
  330. SCM_WRONG_TYPE_ARG (2, denominator);
  331. }
  332. if (!SCM_I_INUMP (numerator) && !SCM_BIGP (numerator))
  333. SCM_WRONG_TYPE_ARG (1, numerator);
  334. /* Then flip signs so that the denominator is positive.
  335. */
  336. if (scm_is_true (scm_negative_p (denominator)))
  337. {
  338. numerator = scm_difference (numerator, SCM_UNDEFINED);
  339. denominator = scm_difference (denominator, SCM_UNDEFINED);
  340. }
  341. /* Now consider for each of the four fixnum/bignum combinations
  342. whether the rational number is really an integer.
  343. */
  344. if (SCM_I_INUMP (numerator))
  345. {
  346. long x = SCM_I_INUM (numerator);
  347. if (scm_is_eq (numerator, SCM_INUM0))
  348. return SCM_INUM0;
  349. if (SCM_I_INUMP (denominator))
  350. {
  351. long y;
  352. y = SCM_I_INUM (denominator);
  353. if (x == y)
  354. return SCM_I_MAKINUM(1);
  355. if ((x % y) == 0)
  356. return SCM_I_MAKINUM (x / y);
  357. }
  358. else
  359. {
  360. /* When x == SCM_MOST_NEGATIVE_FIXNUM we could have the negative
  361. of that value for the denominator, as a bignum. Apart from
  362. that case, abs(bignum) > abs(inum) so inum/bignum is not an
  363. integer. */
  364. if (x == SCM_MOST_NEGATIVE_FIXNUM
  365. && mpz_cmp_ui (SCM_I_BIG_MPZ (denominator),
  366. - SCM_MOST_NEGATIVE_FIXNUM) == 0)
  367. return SCM_I_MAKINUM(-1);
  368. }
  369. }
  370. else if (SCM_BIGP (numerator))
  371. {
  372. if (SCM_I_INUMP (denominator))
  373. {
  374. long yy = SCM_I_INUM (denominator);
  375. if (mpz_divisible_ui_p (SCM_I_BIG_MPZ (numerator), yy))
  376. return scm_divide (numerator, denominator);
  377. }
  378. else
  379. {
  380. if (scm_is_eq (numerator, denominator))
  381. return SCM_I_MAKINUM(1);
  382. if (mpz_divisible_p (SCM_I_BIG_MPZ (numerator),
  383. SCM_I_BIG_MPZ (denominator)))
  384. return scm_divide(numerator, denominator);
  385. }
  386. }
  387. /* No, it's a proper fraction.
  388. */
  389. {
  390. SCM divisor = scm_gcd (numerator, denominator);
  391. if (!(scm_is_eq (divisor, SCM_I_MAKINUM(1))))
  392. {
  393. numerator = scm_divide (numerator, divisor);
  394. denominator = scm_divide (denominator, divisor);
  395. }
  396. return scm_double_cell (scm_tc16_fraction,
  397. SCM_UNPACK (numerator),
  398. SCM_UNPACK (denominator), 0);
  399. }
  400. }
  401. #undef FUNC_NAME
  402. double
  403. scm_i_fraction2double (SCM z)
  404. {
  405. return scm_to_double (scm_divide2real (SCM_FRACTION_NUMERATOR (z),
  406. SCM_FRACTION_DENOMINATOR (z)));
  407. }
  408. SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0,
  409. (SCM x),
  410. "Return @code{#t} if @var{x} is an exact number, @code{#f}\n"
  411. "otherwise.")
  412. #define FUNC_NAME s_scm_exact_p
  413. {
  414. if (SCM_I_INUMP (x))
  415. return SCM_BOOL_T;
  416. if (SCM_BIGP (x))
  417. return SCM_BOOL_T;
  418. if (SCM_FRACTIONP (x))
  419. return SCM_BOOL_T;
  420. if (SCM_NUMBERP (x))
  421. return SCM_BOOL_F;
  422. SCM_WRONG_TYPE_ARG (1, x);
  423. }
  424. #undef FUNC_NAME
  425. SCM_DEFINE (scm_odd_p, "odd?", 1, 0, 0,
  426. (SCM n),
  427. "Return @code{#t} if @var{n} is an odd number, @code{#f}\n"
  428. "otherwise.")
  429. #define FUNC_NAME s_scm_odd_p
  430. {
  431. if (SCM_I_INUMP (n))
  432. {
  433. long val = SCM_I_INUM (n);
  434. return scm_from_bool ((val & 1L) != 0);
  435. }
  436. else if (SCM_BIGP (n))
  437. {
  438. int odd_p = mpz_odd_p (SCM_I_BIG_MPZ (n));
  439. scm_remember_upto_here_1 (n);
  440. return scm_from_bool (odd_p);
  441. }
  442. else if (scm_is_true (scm_inf_p (n)))
  443. return SCM_BOOL_T;
  444. else if (SCM_REALP (n))
  445. {
  446. double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0));
  447. if (rem == 1.0)
  448. return SCM_BOOL_T;
  449. else if (rem == 0.0)
  450. return SCM_BOOL_F;
  451. else
  452. SCM_WRONG_TYPE_ARG (1, n);
  453. }
  454. else
  455. SCM_WRONG_TYPE_ARG (1, n);
  456. }
  457. #undef FUNC_NAME
  458. SCM_DEFINE (scm_even_p, "even?", 1, 0, 0,
  459. (SCM n),
  460. "Return @code{#t} if @var{n} is an even number, @code{#f}\n"
  461. "otherwise.")
  462. #define FUNC_NAME s_scm_even_p
  463. {
  464. if (SCM_I_INUMP (n))
  465. {
  466. long val = SCM_I_INUM (n);
  467. return scm_from_bool ((val & 1L) == 0);
  468. }
  469. else if (SCM_BIGP (n))
  470. {
  471. int even_p = mpz_even_p (SCM_I_BIG_MPZ (n));
  472. scm_remember_upto_here_1 (n);
  473. return scm_from_bool (even_p);
  474. }
  475. else if (scm_is_true (scm_inf_p (n)))
  476. return SCM_BOOL_T;
  477. else if (SCM_REALP (n))
  478. {
  479. double rem = fabs (fmod (SCM_REAL_VALUE(n), 2.0));
  480. if (rem == 1.0)
  481. return SCM_BOOL_F;
  482. else if (rem == 0.0)
  483. return SCM_BOOL_T;
  484. else
  485. SCM_WRONG_TYPE_ARG (1, n);
  486. }
  487. else
  488. SCM_WRONG_TYPE_ARG (1, n);
  489. }
  490. #undef FUNC_NAME
  491. SCM_DEFINE (scm_inf_p, "inf?", 1, 0, 0,
  492. (SCM x),
  493. "Return @code{#t} if @var{x} is either @samp{+inf.0}\n"
  494. "or @samp{-inf.0}, @code{#f} otherwise.")
  495. #define FUNC_NAME s_scm_inf_p
  496. {
  497. if (SCM_REALP (x))
  498. return scm_from_bool (xisinf (SCM_REAL_VALUE (x)));
  499. else if (SCM_COMPLEXP (x))
  500. return scm_from_bool (xisinf (SCM_COMPLEX_REAL (x))
  501. || xisinf (SCM_COMPLEX_IMAG (x)));
  502. else
  503. return SCM_BOOL_F;
  504. }
  505. #undef FUNC_NAME
  506. SCM_DEFINE (scm_nan_p, "nan?", 1, 0, 0,
  507. (SCM n),
  508. "Return @code{#t} if @var{n} is a NaN, @code{#f}\n"
  509. "otherwise.")
  510. #define FUNC_NAME s_scm_nan_p
  511. {
  512. if (SCM_REALP (n))
  513. return scm_from_bool (xisnan (SCM_REAL_VALUE (n)));
  514. else if (SCM_COMPLEXP (n))
  515. return scm_from_bool (xisnan (SCM_COMPLEX_REAL (n))
  516. || xisnan (SCM_COMPLEX_IMAG (n)));
  517. else
  518. return SCM_BOOL_F;
  519. }
  520. #undef FUNC_NAME
  521. /* Guile's idea of infinity. */
  522. static double guile_Inf;
  523. /* Guile's idea of not a number. */
  524. static double guile_NaN;
  525. static void
  526. guile_ieee_init (void)
  527. {
  528. #if defined (HAVE_ISINF) || defined (HAVE_FINITE)
  529. /* Some version of gcc on some old version of Linux used to crash when
  530. trying to make Inf and NaN. */
  531. #ifdef INFINITY
  532. /* C99 INFINITY, when available.
  533. FIXME: The standard allows for INFINITY to be something that overflows
  534. at compile time. We ought to have a configure test to check for that
  535. before trying to use it. (But in practice we believe this is not a
  536. problem on any system guile is likely to target.) */
  537. guile_Inf = INFINITY;
  538. #elif HAVE_DINFINITY
  539. /* OSF */
  540. extern unsigned int DINFINITY[2];
  541. guile_Inf = (*((double *) (DINFINITY)));
  542. #else
  543. double tmp = 1e+10;
  544. guile_Inf = tmp;
  545. for (;;)
  546. {
  547. guile_Inf *= 1e+10;
  548. if (guile_Inf == tmp)
  549. break;
  550. tmp = guile_Inf;
  551. }
  552. #endif
  553. #endif
  554. #if defined (HAVE_ISNAN)
  555. #if defined __GNUC__ && defined __alpha__ && !defined _IEEE_FP
  556. /* On Alpha GCC must be passed `-mieee' to provide proper NaN handling.
  557. See http://lists.gnu.org/archive/html/bug-gnulib/2009-05/msg00010.html
  558. for more details. */
  559. # error NaN handling will not work when compiling without -mieee
  560. #endif
  561. #ifdef NAN
  562. /* C99 NAN, when available */
  563. guile_NaN = NAN;
  564. #elif HAVE_DQNAN
  565. {
  566. /* OSF */
  567. extern unsigned int DQNAN[2];
  568. guile_NaN = (*((double *)(DQNAN)));
  569. }
  570. #else
  571. guile_NaN = guile_Inf / guile_Inf;
  572. #endif
  573. #endif
  574. }
  575. SCM_DEFINE (scm_inf, "inf", 0, 0, 0,
  576. (void),
  577. "Return Inf.")
  578. #define FUNC_NAME s_scm_inf
  579. {
  580. static int initialized = 0;
  581. if (! initialized)
  582. {
  583. guile_ieee_init ();
  584. initialized = 1;
  585. }
  586. return scm_from_double (guile_Inf);
  587. }
  588. #undef FUNC_NAME
  589. SCM_DEFINE (scm_nan, "nan", 0, 0, 0,
  590. (void),
  591. "Return NaN.")
  592. #define FUNC_NAME s_scm_nan
  593. {
  594. static int initialized = 0;
  595. if (!initialized)
  596. {
  597. guile_ieee_init ();
  598. initialized = 1;
  599. }
  600. return scm_from_double (guile_NaN);
  601. }
  602. #undef FUNC_NAME
  603. SCM_PRIMITIVE_GENERIC (scm_abs, "abs", 1, 0, 0,
  604. (SCM x),
  605. "Return the absolute value of @var{x}.")
  606. #define FUNC_NAME
  607. {
  608. if (SCM_I_INUMP (x))
  609. {
  610. long int xx = SCM_I_INUM (x);
  611. if (xx >= 0)
  612. return x;
  613. else if (SCM_POSFIXABLE (-xx))
  614. return SCM_I_MAKINUM (-xx);
  615. else
  616. return scm_i_long2big (-xx);
  617. }
  618. else if (SCM_BIGP (x))
  619. {
  620. const int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  621. if (sgn < 0)
  622. return scm_i_clonebig (x, 0);
  623. else
  624. return x;
  625. }
  626. else if (SCM_REALP (x))
  627. {
  628. /* note that if x is a NaN then xx<0 is false so we return x unchanged */
  629. double xx = SCM_REAL_VALUE (x);
  630. if (xx < 0.0)
  631. return scm_from_double (-xx);
  632. else
  633. return x;
  634. }
  635. else if (SCM_FRACTIONP (x))
  636. {
  637. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (x))))
  638. return x;
  639. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  640. SCM_FRACTION_DENOMINATOR (x));
  641. }
  642. else
  643. SCM_WTA_DISPATCH_1 (g_scm_abs, x, 1, s_scm_abs);
  644. }
  645. #undef FUNC_NAME
  646. SCM_GPROC (s_quotient, "quotient", 2, 0, 0, scm_quotient, g_quotient);
  647. /* "Return the quotient of the numbers @var{x} and @var{y}."
  648. */
  649. SCM
  650. scm_quotient (SCM x, SCM y)
  651. {
  652. if (SCM_I_INUMP (x))
  653. {
  654. long xx = SCM_I_INUM (x);
  655. if (SCM_I_INUMP (y))
  656. {
  657. long yy = SCM_I_INUM (y);
  658. if (yy == 0)
  659. scm_num_overflow (s_quotient);
  660. else
  661. {
  662. long z = xx / yy;
  663. if (SCM_FIXABLE (z))
  664. return SCM_I_MAKINUM (z);
  665. else
  666. return scm_i_long2big (z);
  667. }
  668. }
  669. else if (SCM_BIGP (y))
  670. {
  671. if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM)
  672. && (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  673. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  674. {
  675. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  676. scm_remember_upto_here_1 (y);
  677. return SCM_I_MAKINUM (-1);
  678. }
  679. else
  680. return SCM_I_MAKINUM (0);
  681. }
  682. else
  683. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
  684. }
  685. else if (SCM_BIGP (x))
  686. {
  687. if (SCM_I_INUMP (y))
  688. {
  689. long yy = SCM_I_INUM (y);
  690. if (yy == 0)
  691. scm_num_overflow (s_quotient);
  692. else if (yy == 1)
  693. return x;
  694. else
  695. {
  696. SCM result = scm_i_mkbig ();
  697. if (yy < 0)
  698. {
  699. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result),
  700. SCM_I_BIG_MPZ (x),
  701. - yy);
  702. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  703. }
  704. else
  705. mpz_tdiv_q_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  706. scm_remember_upto_here_1 (x);
  707. return scm_i_normbig (result);
  708. }
  709. }
  710. else if (SCM_BIGP (y))
  711. {
  712. SCM result = scm_i_mkbig ();
  713. mpz_tdiv_q (SCM_I_BIG_MPZ (result),
  714. SCM_I_BIG_MPZ (x),
  715. SCM_I_BIG_MPZ (y));
  716. scm_remember_upto_here_2 (x, y);
  717. return scm_i_normbig (result);
  718. }
  719. else
  720. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG2, s_quotient);
  721. }
  722. else
  723. SCM_WTA_DISPATCH_2 (g_quotient, x, y, SCM_ARG1, s_quotient);
  724. }
  725. SCM_GPROC (s_remainder, "remainder", 2, 0, 0, scm_remainder, g_remainder);
  726. /* "Return the remainder of the numbers @var{x} and @var{y}.\n"
  727. * "@lisp\n"
  728. * "(remainder 13 4) @result{} 1\n"
  729. * "(remainder -13 4) @result{} -1\n"
  730. * "@end lisp"
  731. */
  732. SCM
  733. scm_remainder (SCM x, SCM y)
  734. {
  735. if (SCM_I_INUMP (x))
  736. {
  737. if (SCM_I_INUMP (y))
  738. {
  739. long yy = SCM_I_INUM (y);
  740. if (yy == 0)
  741. scm_num_overflow (s_remainder);
  742. else
  743. {
  744. long z = SCM_I_INUM (x) % yy;
  745. return SCM_I_MAKINUM (z);
  746. }
  747. }
  748. else if (SCM_BIGP (y))
  749. {
  750. if ((SCM_I_INUM (x) == SCM_MOST_NEGATIVE_FIXNUM)
  751. && (mpz_cmp_ui (SCM_I_BIG_MPZ (y),
  752. - SCM_MOST_NEGATIVE_FIXNUM) == 0))
  753. {
  754. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  755. scm_remember_upto_here_1 (y);
  756. return SCM_I_MAKINUM (0);
  757. }
  758. else
  759. return x;
  760. }
  761. else
  762. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
  763. }
  764. else if (SCM_BIGP (x))
  765. {
  766. if (SCM_I_INUMP (y))
  767. {
  768. long yy = SCM_I_INUM (y);
  769. if (yy == 0)
  770. scm_num_overflow (s_remainder);
  771. else
  772. {
  773. SCM result = scm_i_mkbig ();
  774. if (yy < 0)
  775. yy = - yy;
  776. mpz_tdiv_r_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ(x), yy);
  777. scm_remember_upto_here_1 (x);
  778. return scm_i_normbig (result);
  779. }
  780. }
  781. else if (SCM_BIGP (y))
  782. {
  783. SCM result = scm_i_mkbig ();
  784. mpz_tdiv_r (SCM_I_BIG_MPZ (result),
  785. SCM_I_BIG_MPZ (x),
  786. SCM_I_BIG_MPZ (y));
  787. scm_remember_upto_here_2 (x, y);
  788. return scm_i_normbig (result);
  789. }
  790. else
  791. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG2, s_remainder);
  792. }
  793. else
  794. SCM_WTA_DISPATCH_2 (g_remainder, x, y, SCM_ARG1, s_remainder);
  795. }
  796. SCM_GPROC (s_modulo, "modulo", 2, 0, 0, scm_modulo, g_modulo);
  797. /* "Return the modulo of the numbers @var{x} and @var{y}.\n"
  798. * "@lisp\n"
  799. * "(modulo 13 4) @result{} 1\n"
  800. * "(modulo -13 4) @result{} 3\n"
  801. * "@end lisp"
  802. */
  803. SCM
  804. scm_modulo (SCM x, SCM y)
  805. {
  806. if (SCM_I_INUMP (x))
  807. {
  808. long xx = SCM_I_INUM (x);
  809. if (SCM_I_INUMP (y))
  810. {
  811. long yy = SCM_I_INUM (y);
  812. if (yy == 0)
  813. scm_num_overflow (s_modulo);
  814. else
  815. {
  816. /* C99 specifies that "%" is the remainder corresponding to a
  817. quotient rounded towards zero, and that's also traditional
  818. for machine division, so z here should be well defined. */
  819. long z = xx % yy;
  820. long result;
  821. if (yy < 0)
  822. {
  823. if (z > 0)
  824. result = z + yy;
  825. else
  826. result = z;
  827. }
  828. else
  829. {
  830. if (z < 0)
  831. result = z + yy;
  832. else
  833. result = z;
  834. }
  835. return SCM_I_MAKINUM (result);
  836. }
  837. }
  838. else if (SCM_BIGP (y))
  839. {
  840. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  841. {
  842. mpz_t z_x;
  843. SCM result;
  844. if (sgn_y < 0)
  845. {
  846. SCM pos_y = scm_i_clonebig (y, 0);
  847. /* do this after the last scm_op */
  848. mpz_init_set_si (z_x, xx);
  849. result = pos_y; /* re-use this bignum */
  850. mpz_mod (SCM_I_BIG_MPZ (result),
  851. z_x,
  852. SCM_I_BIG_MPZ (pos_y));
  853. scm_remember_upto_here_1 (pos_y);
  854. }
  855. else
  856. {
  857. result = scm_i_mkbig ();
  858. /* do this after the last scm_op */
  859. mpz_init_set_si (z_x, xx);
  860. mpz_mod (SCM_I_BIG_MPZ (result),
  861. z_x,
  862. SCM_I_BIG_MPZ (y));
  863. scm_remember_upto_here_1 (y);
  864. }
  865. if ((sgn_y < 0) && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
  866. mpz_add (SCM_I_BIG_MPZ (result),
  867. SCM_I_BIG_MPZ (y),
  868. SCM_I_BIG_MPZ (result));
  869. scm_remember_upto_here_1 (y);
  870. /* and do this before the next one */
  871. mpz_clear (z_x);
  872. return scm_i_normbig (result);
  873. }
  874. }
  875. else
  876. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
  877. }
  878. else if (SCM_BIGP (x))
  879. {
  880. if (SCM_I_INUMP (y))
  881. {
  882. long yy = SCM_I_INUM (y);
  883. if (yy == 0)
  884. scm_num_overflow (s_modulo);
  885. else
  886. {
  887. SCM result = scm_i_mkbig ();
  888. mpz_mod_ui (SCM_I_BIG_MPZ (result),
  889. SCM_I_BIG_MPZ (x),
  890. (yy < 0) ? - yy : yy);
  891. scm_remember_upto_here_1 (x);
  892. if ((yy < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0))
  893. mpz_sub_ui (SCM_I_BIG_MPZ (result),
  894. SCM_I_BIG_MPZ (result),
  895. - yy);
  896. return scm_i_normbig (result);
  897. }
  898. }
  899. else if (SCM_BIGP (y))
  900. {
  901. {
  902. SCM result = scm_i_mkbig ();
  903. int y_sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  904. SCM pos_y = scm_i_clonebig (y, y_sgn >= 0);
  905. mpz_mod (SCM_I_BIG_MPZ (result),
  906. SCM_I_BIG_MPZ (x),
  907. SCM_I_BIG_MPZ (pos_y));
  908. scm_remember_upto_here_1 (x);
  909. if ((y_sgn < 0) && (mpz_sgn (SCM_I_BIG_MPZ (result)) != 0))
  910. mpz_add (SCM_I_BIG_MPZ (result),
  911. SCM_I_BIG_MPZ (y),
  912. SCM_I_BIG_MPZ (result));
  913. scm_remember_upto_here_2 (y, pos_y);
  914. return scm_i_normbig (result);
  915. }
  916. }
  917. else
  918. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG2, s_modulo);
  919. }
  920. else
  921. SCM_WTA_DISPATCH_2 (g_modulo, x, y, SCM_ARG1, s_modulo);
  922. }
  923. SCM_GPROC1 (s_gcd, "gcd", scm_tc7_asubr, scm_gcd, g_gcd);
  924. /* "Return the greatest common divisor of all arguments.\n"
  925. * "If called without arguments, 0 is returned."
  926. */
  927. SCM
  928. scm_gcd (SCM x, SCM y)
  929. {
  930. if (SCM_UNBNDP (y))
  931. return SCM_UNBNDP (x) ? SCM_INUM0 : scm_abs (x);
  932. if (SCM_I_INUMP (x))
  933. {
  934. if (SCM_I_INUMP (y))
  935. {
  936. long xx = SCM_I_INUM (x);
  937. long yy = SCM_I_INUM (y);
  938. long u = xx < 0 ? -xx : xx;
  939. long v = yy < 0 ? -yy : yy;
  940. long result;
  941. if (xx == 0)
  942. result = v;
  943. else if (yy == 0)
  944. result = u;
  945. else
  946. {
  947. long k = 1;
  948. long t;
  949. /* Determine a common factor 2^k */
  950. while (!(1 & (u | v)))
  951. {
  952. k <<= 1;
  953. u >>= 1;
  954. v >>= 1;
  955. }
  956. /* Now, any factor 2^n can be eliminated */
  957. if (u & 1)
  958. t = -v;
  959. else
  960. {
  961. t = u;
  962. b3:
  963. t = SCM_SRS (t, 1);
  964. }
  965. if (!(1 & t))
  966. goto b3;
  967. if (t > 0)
  968. u = t;
  969. else
  970. v = -t;
  971. t = u - v;
  972. if (t != 0)
  973. goto b3;
  974. result = u * k;
  975. }
  976. return (SCM_POSFIXABLE (result)
  977. ? SCM_I_MAKINUM (result)
  978. : scm_i_long2big (result));
  979. }
  980. else if (SCM_BIGP (y))
  981. {
  982. SCM_SWAP (x, y);
  983. goto big_inum;
  984. }
  985. else
  986. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  987. }
  988. else if (SCM_BIGP (x))
  989. {
  990. if (SCM_I_INUMP (y))
  991. {
  992. unsigned long result;
  993. long yy;
  994. big_inum:
  995. yy = SCM_I_INUM (y);
  996. if (yy == 0)
  997. return scm_abs (x);
  998. if (yy < 0)
  999. yy = -yy;
  1000. result = mpz_gcd_ui (NULL, SCM_I_BIG_MPZ (x), yy);
  1001. scm_remember_upto_here_1 (x);
  1002. return (SCM_POSFIXABLE (result)
  1003. ? SCM_I_MAKINUM (result)
  1004. : scm_from_ulong (result));
  1005. }
  1006. else if (SCM_BIGP (y))
  1007. {
  1008. SCM result = scm_i_mkbig ();
  1009. mpz_gcd (SCM_I_BIG_MPZ (result),
  1010. SCM_I_BIG_MPZ (x),
  1011. SCM_I_BIG_MPZ (y));
  1012. scm_remember_upto_here_2 (x, y);
  1013. return scm_i_normbig (result);
  1014. }
  1015. else
  1016. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG2, s_gcd);
  1017. }
  1018. else
  1019. SCM_WTA_DISPATCH_2 (g_gcd, x, y, SCM_ARG1, s_gcd);
  1020. }
  1021. SCM_GPROC1 (s_lcm, "lcm", scm_tc7_asubr, scm_lcm, g_lcm);
  1022. /* "Return the least common multiple of the arguments.\n"
  1023. * "If called without arguments, 1 is returned."
  1024. */
  1025. SCM
  1026. scm_lcm (SCM n1, SCM n2)
  1027. {
  1028. if (SCM_UNBNDP (n2))
  1029. {
  1030. if (SCM_UNBNDP (n1))
  1031. return SCM_I_MAKINUM (1L);
  1032. n2 = SCM_I_MAKINUM (1L);
  1033. }
  1034. SCM_GASSERT2 (SCM_I_INUMP (n1) || SCM_BIGP (n1),
  1035. g_lcm, n1, n2, SCM_ARG1, s_lcm);
  1036. SCM_GASSERT2 (SCM_I_INUMP (n2) || SCM_BIGP (n2),
  1037. g_lcm, n1, n2, SCM_ARGn, s_lcm);
  1038. if (SCM_I_INUMP (n1))
  1039. {
  1040. if (SCM_I_INUMP (n2))
  1041. {
  1042. SCM d = scm_gcd (n1, n2);
  1043. if (scm_is_eq (d, SCM_INUM0))
  1044. return d;
  1045. else
  1046. return scm_abs (scm_product (n1, scm_quotient (n2, d)));
  1047. }
  1048. else
  1049. {
  1050. /* inum n1, big n2 */
  1051. inumbig:
  1052. {
  1053. SCM result = scm_i_mkbig ();
  1054. long nn1 = SCM_I_INUM (n1);
  1055. if (nn1 == 0) return SCM_INUM0;
  1056. if (nn1 < 0) nn1 = - nn1;
  1057. mpz_lcm_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n2), nn1);
  1058. scm_remember_upto_here_1 (n2);
  1059. return result;
  1060. }
  1061. }
  1062. }
  1063. else
  1064. {
  1065. /* big n1 */
  1066. if (SCM_I_INUMP (n2))
  1067. {
  1068. SCM_SWAP (n1, n2);
  1069. goto inumbig;
  1070. }
  1071. else
  1072. {
  1073. SCM result = scm_i_mkbig ();
  1074. mpz_lcm(SCM_I_BIG_MPZ (result),
  1075. SCM_I_BIG_MPZ (n1),
  1076. SCM_I_BIG_MPZ (n2));
  1077. scm_remember_upto_here_2(n1, n2);
  1078. /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
  1079. return result;
  1080. }
  1081. }
  1082. }
  1083. /* Emulating 2's complement bignums with sign magnitude arithmetic:
  1084. Logand:
  1085. X Y Result Method:
  1086. (len)
  1087. + + + x (map digit:logand X Y)
  1088. + - + x (map digit:logand X (lognot (+ -1 Y)))
  1089. - + + y (map digit:logand (lognot (+ -1 X)) Y)
  1090. - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
  1091. Logior:
  1092. X Y Result Method:
  1093. + + + (map digit:logior X Y)
  1094. + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
  1095. - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
  1096. - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
  1097. Logxor:
  1098. X Y Result Method:
  1099. + + + (map digit:logxor X Y)
  1100. + - - (+ 1 (map digit:logxor X (+ -1 Y)))
  1101. - + - (+ 1 (map digit:logxor (+ -1 X) Y))
  1102. - - + (map digit:logxor (+ -1 X) (+ -1 Y))
  1103. Logtest:
  1104. X Y Result
  1105. + + (any digit:logand X Y)
  1106. + - (any digit:logand X (lognot (+ -1 Y)))
  1107. - + (any digit:logand (lognot (+ -1 X)) Y)
  1108. - - #t
  1109. */
  1110. SCM_DEFINE1 (scm_logand, "logand", scm_tc7_asubr,
  1111. (SCM n1, SCM n2),
  1112. "Return the bitwise AND of the integer arguments.\n\n"
  1113. "@lisp\n"
  1114. "(logand) @result{} -1\n"
  1115. "(logand 7) @result{} 7\n"
  1116. "(logand #b111 #b011 #b001) @result{} 1\n"
  1117. "@end lisp")
  1118. #define FUNC_NAME s_scm_logand
  1119. {
  1120. long int nn1;
  1121. if (SCM_UNBNDP (n2))
  1122. {
  1123. if (SCM_UNBNDP (n1))
  1124. return SCM_I_MAKINUM (-1);
  1125. else if (!SCM_NUMBERP (n1))
  1126. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1127. else if (SCM_NUMBERP (n1))
  1128. return n1;
  1129. else
  1130. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1131. }
  1132. if (SCM_I_INUMP (n1))
  1133. {
  1134. nn1 = SCM_I_INUM (n1);
  1135. if (SCM_I_INUMP (n2))
  1136. {
  1137. long nn2 = SCM_I_INUM (n2);
  1138. return SCM_I_MAKINUM (nn1 & nn2);
  1139. }
  1140. else if SCM_BIGP (n2)
  1141. {
  1142. intbig:
  1143. if (n1 == 0)
  1144. return SCM_INUM0;
  1145. {
  1146. SCM result_z = scm_i_mkbig ();
  1147. mpz_t nn1_z;
  1148. mpz_init_set_si (nn1_z, nn1);
  1149. mpz_and (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1150. scm_remember_upto_here_1 (n2);
  1151. mpz_clear (nn1_z);
  1152. return scm_i_normbig (result_z);
  1153. }
  1154. }
  1155. else
  1156. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1157. }
  1158. else if (SCM_BIGP (n1))
  1159. {
  1160. if (SCM_I_INUMP (n2))
  1161. {
  1162. SCM_SWAP (n1, n2);
  1163. nn1 = SCM_I_INUM (n1);
  1164. goto intbig;
  1165. }
  1166. else if (SCM_BIGP (n2))
  1167. {
  1168. SCM result_z = scm_i_mkbig ();
  1169. mpz_and (SCM_I_BIG_MPZ (result_z),
  1170. SCM_I_BIG_MPZ (n1),
  1171. SCM_I_BIG_MPZ (n2));
  1172. scm_remember_upto_here_2 (n1, n2);
  1173. return scm_i_normbig (result_z);
  1174. }
  1175. else
  1176. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1177. }
  1178. else
  1179. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1180. }
  1181. #undef FUNC_NAME
  1182. SCM_DEFINE1 (scm_logior, "logior", scm_tc7_asubr,
  1183. (SCM n1, SCM n2),
  1184. "Return the bitwise OR of the integer arguments.\n\n"
  1185. "@lisp\n"
  1186. "(logior) @result{} 0\n"
  1187. "(logior 7) @result{} 7\n"
  1188. "(logior #b000 #b001 #b011) @result{} 3\n"
  1189. "@end lisp")
  1190. #define FUNC_NAME s_scm_logior
  1191. {
  1192. long int nn1;
  1193. if (SCM_UNBNDP (n2))
  1194. {
  1195. if (SCM_UNBNDP (n1))
  1196. return SCM_INUM0;
  1197. else if (SCM_NUMBERP (n1))
  1198. return n1;
  1199. else
  1200. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1201. }
  1202. if (SCM_I_INUMP (n1))
  1203. {
  1204. nn1 = SCM_I_INUM (n1);
  1205. if (SCM_I_INUMP (n2))
  1206. {
  1207. long nn2 = SCM_I_INUM (n2);
  1208. return SCM_I_MAKINUM (nn1 | nn2);
  1209. }
  1210. else if (SCM_BIGP (n2))
  1211. {
  1212. intbig:
  1213. if (nn1 == 0)
  1214. return n2;
  1215. {
  1216. SCM result_z = scm_i_mkbig ();
  1217. mpz_t nn1_z;
  1218. mpz_init_set_si (nn1_z, nn1);
  1219. mpz_ior (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1220. scm_remember_upto_here_1 (n2);
  1221. mpz_clear (nn1_z);
  1222. return scm_i_normbig (result_z);
  1223. }
  1224. }
  1225. else
  1226. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1227. }
  1228. else if (SCM_BIGP (n1))
  1229. {
  1230. if (SCM_I_INUMP (n2))
  1231. {
  1232. SCM_SWAP (n1, n2);
  1233. nn1 = SCM_I_INUM (n1);
  1234. goto intbig;
  1235. }
  1236. else if (SCM_BIGP (n2))
  1237. {
  1238. SCM result_z = scm_i_mkbig ();
  1239. mpz_ior (SCM_I_BIG_MPZ (result_z),
  1240. SCM_I_BIG_MPZ (n1),
  1241. SCM_I_BIG_MPZ (n2));
  1242. scm_remember_upto_here_2 (n1, n2);
  1243. return scm_i_normbig (result_z);
  1244. }
  1245. else
  1246. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1247. }
  1248. else
  1249. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1250. }
  1251. #undef FUNC_NAME
  1252. SCM_DEFINE1 (scm_logxor, "logxor", scm_tc7_asubr,
  1253. (SCM n1, SCM n2),
  1254. "Return the bitwise XOR of the integer arguments. A bit is\n"
  1255. "set in the result if it is set in an odd number of arguments.\n"
  1256. "@lisp\n"
  1257. "(logxor) @result{} 0\n"
  1258. "(logxor 7) @result{} 7\n"
  1259. "(logxor #b000 #b001 #b011) @result{} 2\n"
  1260. "(logxor #b000 #b001 #b011 #b011) @result{} 1\n"
  1261. "@end lisp")
  1262. #define FUNC_NAME s_scm_logxor
  1263. {
  1264. long int nn1;
  1265. if (SCM_UNBNDP (n2))
  1266. {
  1267. if (SCM_UNBNDP (n1))
  1268. return SCM_INUM0;
  1269. else if (SCM_NUMBERP (n1))
  1270. return n1;
  1271. else
  1272. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1273. }
  1274. if (SCM_I_INUMP (n1))
  1275. {
  1276. nn1 = SCM_I_INUM (n1);
  1277. if (SCM_I_INUMP (n2))
  1278. {
  1279. long nn2 = SCM_I_INUM (n2);
  1280. return SCM_I_MAKINUM (nn1 ^ nn2);
  1281. }
  1282. else if (SCM_BIGP (n2))
  1283. {
  1284. intbig:
  1285. {
  1286. SCM result_z = scm_i_mkbig ();
  1287. mpz_t nn1_z;
  1288. mpz_init_set_si (nn1_z, nn1);
  1289. mpz_xor (SCM_I_BIG_MPZ (result_z), nn1_z, SCM_I_BIG_MPZ (n2));
  1290. scm_remember_upto_here_1 (n2);
  1291. mpz_clear (nn1_z);
  1292. return scm_i_normbig (result_z);
  1293. }
  1294. }
  1295. else
  1296. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1297. }
  1298. else if (SCM_BIGP (n1))
  1299. {
  1300. if (SCM_I_INUMP (n2))
  1301. {
  1302. SCM_SWAP (n1, n2);
  1303. nn1 = SCM_I_INUM (n1);
  1304. goto intbig;
  1305. }
  1306. else if (SCM_BIGP (n2))
  1307. {
  1308. SCM result_z = scm_i_mkbig ();
  1309. mpz_xor (SCM_I_BIG_MPZ (result_z),
  1310. SCM_I_BIG_MPZ (n1),
  1311. SCM_I_BIG_MPZ (n2));
  1312. scm_remember_upto_here_2 (n1, n2);
  1313. return scm_i_normbig (result_z);
  1314. }
  1315. else
  1316. SCM_WRONG_TYPE_ARG (SCM_ARG2, n2);
  1317. }
  1318. else
  1319. SCM_WRONG_TYPE_ARG (SCM_ARG1, n1);
  1320. }
  1321. #undef FUNC_NAME
  1322. SCM_DEFINE (scm_logtest, "logtest", 2, 0, 0,
  1323. (SCM j, SCM k),
  1324. "Test whether @var{j} and @var{k} have any 1 bits in common.\n"
  1325. "This is equivalent to @code{(not (zero? (logand j k)))}, but\n"
  1326. "without actually calculating the @code{logand}, just testing\n"
  1327. "for non-zero.\n"
  1328. "\n"
  1329. "@lisp\n"
  1330. "(logtest #b0100 #b1011) @result{} #f\n"
  1331. "(logtest #b0100 #b0111) @result{} #t\n"
  1332. "@end lisp")
  1333. #define FUNC_NAME s_scm_logtest
  1334. {
  1335. long int nj;
  1336. if (SCM_I_INUMP (j))
  1337. {
  1338. nj = SCM_I_INUM (j);
  1339. if (SCM_I_INUMP (k))
  1340. {
  1341. long nk = SCM_I_INUM (k);
  1342. return scm_from_bool (nj & nk);
  1343. }
  1344. else if (SCM_BIGP (k))
  1345. {
  1346. intbig:
  1347. if (nj == 0)
  1348. return SCM_BOOL_F;
  1349. {
  1350. SCM result;
  1351. mpz_t nj_z;
  1352. mpz_init_set_si (nj_z, nj);
  1353. mpz_and (nj_z, nj_z, SCM_I_BIG_MPZ (k));
  1354. scm_remember_upto_here_1 (k);
  1355. result = scm_from_bool (mpz_sgn (nj_z) != 0);
  1356. mpz_clear (nj_z);
  1357. return result;
  1358. }
  1359. }
  1360. else
  1361. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  1362. }
  1363. else if (SCM_BIGP (j))
  1364. {
  1365. if (SCM_I_INUMP (k))
  1366. {
  1367. SCM_SWAP (j, k);
  1368. nj = SCM_I_INUM (j);
  1369. goto intbig;
  1370. }
  1371. else if (SCM_BIGP (k))
  1372. {
  1373. SCM result;
  1374. mpz_t result_z;
  1375. mpz_init (result_z);
  1376. mpz_and (result_z,
  1377. SCM_I_BIG_MPZ (j),
  1378. SCM_I_BIG_MPZ (k));
  1379. scm_remember_upto_here_2 (j, k);
  1380. result = scm_from_bool (mpz_sgn (result_z) != 0);
  1381. mpz_clear (result_z);
  1382. return result;
  1383. }
  1384. else
  1385. SCM_WRONG_TYPE_ARG (SCM_ARG2, k);
  1386. }
  1387. else
  1388. SCM_WRONG_TYPE_ARG (SCM_ARG1, j);
  1389. }
  1390. #undef FUNC_NAME
  1391. SCM_DEFINE (scm_logbit_p, "logbit?", 2, 0, 0,
  1392. (SCM index, SCM j),
  1393. "Test whether bit number @var{index} in @var{j} is set.\n"
  1394. "@var{index} starts from 0 for the least significant bit.\n"
  1395. "\n"
  1396. "@lisp\n"
  1397. "(logbit? 0 #b1101) @result{} #t\n"
  1398. "(logbit? 1 #b1101) @result{} #f\n"
  1399. "(logbit? 2 #b1101) @result{} #t\n"
  1400. "(logbit? 3 #b1101) @result{} #t\n"
  1401. "(logbit? 4 #b1101) @result{} #f\n"
  1402. "@end lisp")
  1403. #define FUNC_NAME s_scm_logbit_p
  1404. {
  1405. unsigned long int iindex;
  1406. iindex = scm_to_ulong (index);
  1407. if (SCM_I_INUMP (j))
  1408. {
  1409. /* bits above what's in an inum follow the sign bit */
  1410. iindex = min (iindex, SCM_LONG_BIT - 1);
  1411. return scm_from_bool ((1L << iindex) & SCM_I_INUM (j));
  1412. }
  1413. else if (SCM_BIGP (j))
  1414. {
  1415. int val = mpz_tstbit (SCM_I_BIG_MPZ (j), iindex);
  1416. scm_remember_upto_here_1 (j);
  1417. return scm_from_bool (val);
  1418. }
  1419. else
  1420. SCM_WRONG_TYPE_ARG (SCM_ARG2, j);
  1421. }
  1422. #undef FUNC_NAME
  1423. SCM_DEFINE (scm_lognot, "lognot", 1, 0, 0,
  1424. (SCM n),
  1425. "Return the integer which is the ones-complement of the integer\n"
  1426. "argument.\n"
  1427. "\n"
  1428. "@lisp\n"
  1429. "(number->string (lognot #b10000000) 2)\n"
  1430. " @result{} \"-10000001\"\n"
  1431. "(number->string (lognot #b0) 2)\n"
  1432. " @result{} \"-1\"\n"
  1433. "@end lisp")
  1434. #define FUNC_NAME s_scm_lognot
  1435. {
  1436. if (SCM_I_INUMP (n)) {
  1437. /* No overflow here, just need to toggle all the bits making up the inum.
  1438. Enhancement: No need to strip the tag and add it back, could just xor
  1439. a block of 1 bits, if that worked with the various debug versions of
  1440. the SCM typedef. */
  1441. return SCM_I_MAKINUM (~ SCM_I_INUM (n));
  1442. } else if (SCM_BIGP (n)) {
  1443. SCM result = scm_i_mkbig ();
  1444. mpz_com (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n));
  1445. scm_remember_upto_here_1 (n);
  1446. return result;
  1447. } else {
  1448. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1449. }
  1450. }
  1451. #undef FUNC_NAME
  1452. /* returns 0 if IN is not an integer. OUT must already be
  1453. initialized. */
  1454. static int
  1455. coerce_to_big (SCM in, mpz_t out)
  1456. {
  1457. if (SCM_BIGP (in))
  1458. mpz_set (out, SCM_I_BIG_MPZ (in));
  1459. else if (SCM_I_INUMP (in))
  1460. mpz_set_si (out, SCM_I_INUM (in));
  1461. else
  1462. return 0;
  1463. return 1;
  1464. }
  1465. SCM_DEFINE (scm_modulo_expt, "modulo-expt", 3, 0, 0,
  1466. (SCM n, SCM k, SCM m),
  1467. "Return @var{n} raised to the integer exponent\n"
  1468. "@var{k}, modulo @var{m}.\n"
  1469. "\n"
  1470. "@lisp\n"
  1471. "(modulo-expt 2 3 5)\n"
  1472. " @result{} 3\n"
  1473. "@end lisp")
  1474. #define FUNC_NAME s_scm_modulo_expt
  1475. {
  1476. mpz_t n_tmp;
  1477. mpz_t k_tmp;
  1478. mpz_t m_tmp;
  1479. /* There are two classes of error we might encounter --
  1480. 1) Math errors, which we'll report by calling scm_num_overflow,
  1481. and
  1482. 2) wrong-type errors, which of course we'll report by calling
  1483. SCM_WRONG_TYPE_ARG.
  1484. We don't report those errors immediately, however; instead we do
  1485. some cleanup first. These variables tell us which error (if
  1486. any) we should report after cleaning up.
  1487. */
  1488. int report_overflow = 0;
  1489. int position_of_wrong_type = 0;
  1490. SCM value_of_wrong_type = SCM_INUM0;
  1491. SCM result = SCM_UNDEFINED;
  1492. mpz_init (n_tmp);
  1493. mpz_init (k_tmp);
  1494. mpz_init (m_tmp);
  1495. if (scm_is_eq (m, SCM_INUM0))
  1496. {
  1497. report_overflow = 1;
  1498. goto cleanup;
  1499. }
  1500. if (!coerce_to_big (n, n_tmp))
  1501. {
  1502. value_of_wrong_type = n;
  1503. position_of_wrong_type = 1;
  1504. goto cleanup;
  1505. }
  1506. if (!coerce_to_big (k, k_tmp))
  1507. {
  1508. value_of_wrong_type = k;
  1509. position_of_wrong_type = 2;
  1510. goto cleanup;
  1511. }
  1512. if (!coerce_to_big (m, m_tmp))
  1513. {
  1514. value_of_wrong_type = m;
  1515. position_of_wrong_type = 3;
  1516. goto cleanup;
  1517. }
  1518. /* if the exponent K is negative, and we simply call mpz_powm, we
  1519. will get a divide-by-zero exception when an inverse 1/n mod m
  1520. doesn't exist (or is not unique). Since exceptions are hard to
  1521. handle, we'll attempt the inversion "by hand" -- that way, we get
  1522. a simple failure code, which is easy to handle. */
  1523. if (-1 == mpz_sgn (k_tmp))
  1524. {
  1525. if (!mpz_invert (n_tmp, n_tmp, m_tmp))
  1526. {
  1527. report_overflow = 1;
  1528. goto cleanup;
  1529. }
  1530. mpz_neg (k_tmp, k_tmp);
  1531. }
  1532. result = scm_i_mkbig ();
  1533. mpz_powm (SCM_I_BIG_MPZ (result),
  1534. n_tmp,
  1535. k_tmp,
  1536. m_tmp);
  1537. if (mpz_sgn (m_tmp) < 0 && mpz_sgn (SCM_I_BIG_MPZ (result)) != 0)
  1538. mpz_add (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), m_tmp);
  1539. cleanup:
  1540. mpz_clear (m_tmp);
  1541. mpz_clear (k_tmp);
  1542. mpz_clear (n_tmp);
  1543. if (report_overflow)
  1544. scm_num_overflow (FUNC_NAME);
  1545. if (position_of_wrong_type)
  1546. SCM_WRONG_TYPE_ARG (position_of_wrong_type,
  1547. value_of_wrong_type);
  1548. return scm_i_normbig (result);
  1549. }
  1550. #undef FUNC_NAME
  1551. SCM_DEFINE (scm_integer_expt, "integer-expt", 2, 0, 0,
  1552. (SCM n, SCM k),
  1553. "Return @var{n} raised to the power @var{k}. @var{k} must be an\n"
  1554. "exact integer, @var{n} can be any number.\n"
  1555. "\n"
  1556. "Negative @var{k} is supported, and results in @math{1/n^abs(k)}\n"
  1557. "in the usual way. @math{@var{n}^0} is 1, as usual, and that\n"
  1558. "includes @math{0^0} is 1.\n"
  1559. "\n"
  1560. "@lisp\n"
  1561. "(integer-expt 2 5) @result{} 32\n"
  1562. "(integer-expt -3 3) @result{} -27\n"
  1563. "(integer-expt 5 -3) @result{} 1/125\n"
  1564. "(integer-expt 0 0) @result{} 1\n"
  1565. "@end lisp")
  1566. #define FUNC_NAME s_scm_integer_expt
  1567. {
  1568. long i2 = 0;
  1569. SCM z_i2 = SCM_BOOL_F;
  1570. int i2_is_big = 0;
  1571. SCM acc = SCM_I_MAKINUM (1L);
  1572. /* 0^0 == 1 according to R5RS */
  1573. if (scm_is_eq (n, SCM_INUM0) || scm_is_eq (n, acc))
  1574. return scm_is_false (scm_zero_p(k)) ? n : acc;
  1575. else if (scm_is_eq (n, SCM_I_MAKINUM (-1L)))
  1576. return scm_is_false (scm_even_p (k)) ? n : acc;
  1577. if (SCM_I_INUMP (k))
  1578. i2 = SCM_I_INUM (k);
  1579. else if (SCM_BIGP (k))
  1580. {
  1581. z_i2 = scm_i_clonebig (k, 1);
  1582. scm_remember_upto_here_1 (k);
  1583. i2_is_big = 1;
  1584. }
  1585. else
  1586. SCM_WRONG_TYPE_ARG (2, k);
  1587. if (i2_is_big)
  1588. {
  1589. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == -1)
  1590. {
  1591. mpz_neg (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2));
  1592. n = scm_divide (n, SCM_UNDEFINED);
  1593. }
  1594. while (1)
  1595. {
  1596. if (mpz_sgn(SCM_I_BIG_MPZ (z_i2)) == 0)
  1597. {
  1598. return acc;
  1599. }
  1600. if (mpz_cmp_ui(SCM_I_BIG_MPZ (z_i2), 1) == 0)
  1601. {
  1602. return scm_product (acc, n);
  1603. }
  1604. if (mpz_tstbit(SCM_I_BIG_MPZ (z_i2), 0))
  1605. acc = scm_product (acc, n);
  1606. n = scm_product (n, n);
  1607. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (z_i2), SCM_I_BIG_MPZ (z_i2), 1);
  1608. }
  1609. }
  1610. else
  1611. {
  1612. if (i2 < 0)
  1613. {
  1614. i2 = -i2;
  1615. n = scm_divide (n, SCM_UNDEFINED);
  1616. }
  1617. while (1)
  1618. {
  1619. if (0 == i2)
  1620. return acc;
  1621. if (1 == i2)
  1622. return scm_product (acc, n);
  1623. if (i2 & 1)
  1624. acc = scm_product (acc, n);
  1625. n = scm_product (n, n);
  1626. i2 >>= 1;
  1627. }
  1628. }
  1629. }
  1630. #undef FUNC_NAME
  1631. SCM_DEFINE (scm_ash, "ash", 2, 0, 0,
  1632. (SCM n, SCM cnt),
  1633. "Return @var{n} shifted left by @var{cnt} bits, or shifted right\n"
  1634. "if @var{cnt} is negative. This is an ``arithmetic'' shift.\n"
  1635. "\n"
  1636. "This is effectively a multiplication by 2^@var{cnt}, and when\n"
  1637. "@var{cnt} is negative it's a division, rounded towards negative\n"
  1638. "infinity. (Note that this is not the same rounding as\n"
  1639. "@code{quotient} does.)\n"
  1640. "\n"
  1641. "With @var{n} viewed as an infinite precision twos complement,\n"
  1642. "@code{ash} means a left shift introducing zero bits, or a right\n"
  1643. "shift dropping bits.\n"
  1644. "\n"
  1645. "@lisp\n"
  1646. "(number->string (ash #b1 3) 2) @result{} \"1000\"\n"
  1647. "(number->string (ash #b1010 -1) 2) @result{} \"101\"\n"
  1648. "\n"
  1649. ";; -23 is bits ...11101001, -6 is bits ...111010\n"
  1650. "(ash -23 -2) @result{} -6\n"
  1651. "@end lisp")
  1652. #define FUNC_NAME s_scm_ash
  1653. {
  1654. long bits_to_shift;
  1655. bits_to_shift = scm_to_long (cnt);
  1656. if (SCM_I_INUMP (n))
  1657. {
  1658. long nn = SCM_I_INUM (n);
  1659. if (bits_to_shift > 0)
  1660. {
  1661. /* Left shift of bits_to_shift >= SCM_I_FIXNUM_BIT-1 will always
  1662. overflow a non-zero fixnum. For smaller shifts we check the
  1663. bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
  1664. all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
  1665. Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 -
  1666. bits_to_shift)". */
  1667. if (nn == 0)
  1668. return n;
  1669. if (bits_to_shift < SCM_I_FIXNUM_BIT-1
  1670. && ((unsigned long)
  1671. (SCM_SRS (nn, (SCM_I_FIXNUM_BIT-1 - bits_to_shift)) + 1)
  1672. <= 1))
  1673. {
  1674. return SCM_I_MAKINUM (nn << bits_to_shift);
  1675. }
  1676. else
  1677. {
  1678. SCM result = scm_i_long2big (nn);
  1679. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  1680. bits_to_shift);
  1681. return result;
  1682. }
  1683. }
  1684. else
  1685. {
  1686. bits_to_shift = -bits_to_shift;
  1687. if (bits_to_shift >= SCM_LONG_BIT)
  1688. return (nn >= 0 ? SCM_I_MAKINUM (0) : SCM_I_MAKINUM(-1));
  1689. else
  1690. return SCM_I_MAKINUM (SCM_SRS (nn, bits_to_shift));
  1691. }
  1692. }
  1693. else if (SCM_BIGP (n))
  1694. {
  1695. SCM result;
  1696. if (bits_to_shift == 0)
  1697. return n;
  1698. result = scm_i_mkbig ();
  1699. if (bits_to_shift >= 0)
  1700. {
  1701. mpz_mul_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
  1702. bits_to_shift);
  1703. return result;
  1704. }
  1705. else
  1706. {
  1707. /* GMP doesn't have an fdiv_q_2exp variant returning just a long, so
  1708. we have to allocate a bignum even if the result is going to be a
  1709. fixnum. */
  1710. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (n),
  1711. -bits_to_shift);
  1712. return scm_i_normbig (result);
  1713. }
  1714. }
  1715. else
  1716. {
  1717. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1718. }
  1719. }
  1720. #undef FUNC_NAME
  1721. SCM_DEFINE (scm_bit_extract, "bit-extract", 3, 0, 0,
  1722. (SCM n, SCM start, SCM end),
  1723. "Return the integer composed of the @var{start} (inclusive)\n"
  1724. "through @var{end} (exclusive) bits of @var{n}. The\n"
  1725. "@var{start}th bit becomes the 0-th bit in the result.\n"
  1726. "\n"
  1727. "@lisp\n"
  1728. "(number->string (bit-extract #b1101101010 0 4) 2)\n"
  1729. " @result{} \"1010\"\n"
  1730. "(number->string (bit-extract #b1101101010 4 9) 2)\n"
  1731. " @result{} \"10110\"\n"
  1732. "@end lisp")
  1733. #define FUNC_NAME s_scm_bit_extract
  1734. {
  1735. unsigned long int istart, iend, bits;
  1736. istart = scm_to_ulong (start);
  1737. iend = scm_to_ulong (end);
  1738. SCM_ASSERT_RANGE (3, end, (iend >= istart));
  1739. /* how many bits to keep */
  1740. bits = iend - istart;
  1741. if (SCM_I_INUMP (n))
  1742. {
  1743. long int in = SCM_I_INUM (n);
  1744. /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
  1745. SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "in". */
  1746. in = SCM_SRS (in, min (istart, SCM_I_FIXNUM_BIT-1));
  1747. if (in < 0 && bits >= SCM_I_FIXNUM_BIT)
  1748. {
  1749. /* Since we emulate two's complement encoded numbers, this
  1750. * special case requires us to produce a result that has
  1751. * more bits than can be stored in a fixnum.
  1752. */
  1753. SCM result = scm_i_long2big (in);
  1754. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result),
  1755. bits);
  1756. return result;
  1757. }
  1758. /* mask down to requisite bits */
  1759. bits = min (bits, SCM_I_FIXNUM_BIT);
  1760. return SCM_I_MAKINUM (in & ((1L << bits) - 1));
  1761. }
  1762. else if (SCM_BIGP (n))
  1763. {
  1764. SCM result;
  1765. if (bits == 1)
  1766. {
  1767. result = SCM_I_MAKINUM (mpz_tstbit (SCM_I_BIG_MPZ (n), istart));
  1768. }
  1769. else
  1770. {
  1771. /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
  1772. bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
  1773. such bits into a ulong. */
  1774. result = scm_i_mkbig ();
  1775. mpz_fdiv_q_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(n), istart);
  1776. mpz_fdiv_r_2exp (SCM_I_BIG_MPZ(result), SCM_I_BIG_MPZ(result), bits);
  1777. result = scm_i_normbig (result);
  1778. }
  1779. scm_remember_upto_here_1 (n);
  1780. return result;
  1781. }
  1782. else
  1783. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1784. }
  1785. #undef FUNC_NAME
  1786. static const char scm_logtab[] = {
  1787. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  1788. };
  1789. SCM_DEFINE (scm_logcount, "logcount", 1, 0, 0,
  1790. (SCM n),
  1791. "Return the number of bits in integer @var{n}. If integer is\n"
  1792. "positive, the 1-bits in its binary representation are counted.\n"
  1793. "If negative, the 0-bits in its two's-complement binary\n"
  1794. "representation are counted. If 0, 0 is returned.\n"
  1795. "\n"
  1796. "@lisp\n"
  1797. "(logcount #b10101010)\n"
  1798. " @result{} 4\n"
  1799. "(logcount 0)\n"
  1800. " @result{} 0\n"
  1801. "(logcount -2)\n"
  1802. " @result{} 1\n"
  1803. "@end lisp")
  1804. #define FUNC_NAME s_scm_logcount
  1805. {
  1806. if (SCM_I_INUMP (n))
  1807. {
  1808. unsigned long int c = 0;
  1809. long int nn = SCM_I_INUM (n);
  1810. if (nn < 0)
  1811. nn = -1 - nn;
  1812. while (nn)
  1813. {
  1814. c += scm_logtab[15 & nn];
  1815. nn >>= 4;
  1816. }
  1817. return SCM_I_MAKINUM (c);
  1818. }
  1819. else if (SCM_BIGP (n))
  1820. {
  1821. unsigned long count;
  1822. if (mpz_sgn (SCM_I_BIG_MPZ (n)) >= 0)
  1823. count = mpz_popcount (SCM_I_BIG_MPZ (n));
  1824. else
  1825. count = mpz_hamdist (SCM_I_BIG_MPZ (n), z_negative_one);
  1826. scm_remember_upto_here_1 (n);
  1827. return SCM_I_MAKINUM (count);
  1828. }
  1829. else
  1830. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1831. }
  1832. #undef FUNC_NAME
  1833. static const char scm_ilentab[] = {
  1834. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
  1835. };
  1836. SCM_DEFINE (scm_integer_length, "integer-length", 1, 0, 0,
  1837. (SCM n),
  1838. "Return the number of bits necessary to represent @var{n}.\n"
  1839. "\n"
  1840. "@lisp\n"
  1841. "(integer-length #b10101010)\n"
  1842. " @result{} 8\n"
  1843. "(integer-length 0)\n"
  1844. " @result{} 0\n"
  1845. "(integer-length #b1111)\n"
  1846. " @result{} 4\n"
  1847. "@end lisp")
  1848. #define FUNC_NAME s_scm_integer_length
  1849. {
  1850. if (SCM_I_INUMP (n))
  1851. {
  1852. unsigned long int c = 0;
  1853. unsigned int l = 4;
  1854. long int nn = SCM_I_INUM (n);
  1855. if (nn < 0)
  1856. nn = -1 - nn;
  1857. while (nn)
  1858. {
  1859. c += 4;
  1860. l = scm_ilentab [15 & nn];
  1861. nn >>= 4;
  1862. }
  1863. return SCM_I_MAKINUM (c - 4 + l);
  1864. }
  1865. else if (SCM_BIGP (n))
  1866. {
  1867. /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
  1868. want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
  1869. 1 too big, so check for that and adjust. */
  1870. size_t size = mpz_sizeinbase (SCM_I_BIG_MPZ (n), 2);
  1871. if (mpz_sgn (SCM_I_BIG_MPZ (n)) < 0
  1872. && mpz_scan0 (SCM_I_BIG_MPZ (n), /* no 0 bits above the lowest 1 */
  1873. mpz_scan1 (SCM_I_BIG_MPZ (n), 0)) == ULONG_MAX)
  1874. size--;
  1875. scm_remember_upto_here_1 (n);
  1876. return SCM_I_MAKINUM (size);
  1877. }
  1878. else
  1879. SCM_WRONG_TYPE_ARG (SCM_ARG1, n);
  1880. }
  1881. #undef FUNC_NAME
  1882. /*** NUMBERS -> STRINGS ***/
  1883. #define SCM_MAX_DBL_PREC 60
  1884. #define SCM_MAX_DBL_RADIX 36
  1885. /* this is an array starting with radix 2, and ending with radix SCM_MAX_DBL_RADIX */
  1886. static int scm_dblprec[SCM_MAX_DBL_RADIX - 1];
  1887. static double fx_per_radix[SCM_MAX_DBL_RADIX - 1][SCM_MAX_DBL_PREC];
  1888. static
  1889. void init_dblprec(int *prec, int radix) {
  1890. /* determine floating point precision by adding successively
  1891. smaller increments to 1.0 until it is considered == 1.0 */
  1892. double f = ((double)1.0)/radix;
  1893. double fsum = 1.0 + f;
  1894. *prec = 0;
  1895. while (fsum != 1.0)
  1896. {
  1897. if (++(*prec) > SCM_MAX_DBL_PREC)
  1898. fsum = 1.0;
  1899. else
  1900. {
  1901. f /= radix;
  1902. fsum = f + 1.0;
  1903. }
  1904. }
  1905. (*prec) -= 1;
  1906. }
  1907. static
  1908. void init_fx_radix(double *fx_list, int radix)
  1909. {
  1910. /* initialize a per-radix list of tolerances. When added
  1911. to a number < 1.0, we can determine if we should raund
  1912. up and quit converting a number to a string. */
  1913. int i;
  1914. fx_list[0] = 0.0;
  1915. fx_list[1] = 0.5;
  1916. for( i = 2 ; i < SCM_MAX_DBL_PREC; ++i )
  1917. fx_list[i] = (fx_list[i-1] / radix);
  1918. }
  1919. /* use this array as a way to generate a single digit */
  1920. static const char*number_chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  1921. static size_t
  1922. idbl2str (double f, char *a, int radix)
  1923. {
  1924. int efmt, dpt, d, i, wp;
  1925. double *fx;
  1926. #ifdef DBL_MIN_10_EXP
  1927. double f_cpy;
  1928. int exp_cpy;
  1929. #endif /* DBL_MIN_10_EXP */
  1930. size_t ch = 0;
  1931. int exp = 0;
  1932. if(radix < 2 ||
  1933. radix > SCM_MAX_DBL_RADIX)
  1934. {
  1935. /* revert to existing behavior */
  1936. radix = 10;
  1937. }
  1938. wp = scm_dblprec[radix-2];
  1939. fx = fx_per_radix[radix-2];
  1940. if (f == 0.0)
  1941. {
  1942. #ifdef HAVE_COPYSIGN
  1943. double sgn = copysign (1.0, f);
  1944. if (sgn < 0.0)
  1945. a[ch++] = '-';
  1946. #endif
  1947. goto zero; /*{a[0]='0'; a[1]='.'; a[2]='0'; return 3;} */
  1948. }
  1949. if (xisinf (f))
  1950. {
  1951. if (f < 0)
  1952. strcpy (a, "-inf.0");
  1953. else
  1954. strcpy (a, "+inf.0");
  1955. return ch+6;
  1956. }
  1957. else if (xisnan (f))
  1958. {
  1959. strcpy (a, "+nan.0");
  1960. return ch+6;
  1961. }
  1962. if (f < 0.0)
  1963. {
  1964. f = -f;
  1965. a[ch++] = '-';
  1966. }
  1967. #ifdef DBL_MIN_10_EXP /* Prevent unnormalized values, as from
  1968. make-uniform-vector, from causing infinite loops. */
  1969. /* just do the checking...if it passes, we do the conversion for our
  1970. radix again below */
  1971. f_cpy = f;
  1972. exp_cpy = exp;
  1973. while (f_cpy < 1.0)
  1974. {
  1975. f_cpy *= 10.0;
  1976. if (exp_cpy-- < DBL_MIN_10_EXP)
  1977. {
  1978. a[ch++] = '#';
  1979. a[ch++] = '.';
  1980. a[ch++] = '#';
  1981. return ch;
  1982. }
  1983. }
  1984. while (f_cpy > 10.0)
  1985. {
  1986. f_cpy *= 0.10;
  1987. if (exp_cpy++ > DBL_MAX_10_EXP)
  1988. {
  1989. a[ch++] = '#';
  1990. a[ch++] = '.';
  1991. a[ch++] = '#';
  1992. return ch;
  1993. }
  1994. }
  1995. #endif
  1996. while (f < 1.0)
  1997. {
  1998. f *= radix;
  1999. exp--;
  2000. }
  2001. while (f > radix)
  2002. {
  2003. f /= radix;
  2004. exp++;
  2005. }
  2006. if (f + fx[wp] >= radix)
  2007. {
  2008. f = 1.0;
  2009. exp++;
  2010. }
  2011. zero:
  2012. #ifdef ENGNOT
  2013. /* adding 9999 makes this equivalent to abs(x) % 3 */
  2014. dpt = (exp + 9999) % 3;
  2015. exp -= dpt++;
  2016. efmt = 1;
  2017. #else
  2018. efmt = (exp < -3) || (exp > wp + 2);
  2019. if (!efmt)
  2020. {
  2021. if (exp < 0)
  2022. {
  2023. a[ch++] = '0';
  2024. a[ch++] = '.';
  2025. dpt = exp;
  2026. while (++dpt)
  2027. a[ch++] = '0';
  2028. }
  2029. else
  2030. dpt = exp + 1;
  2031. }
  2032. else
  2033. dpt = 1;
  2034. #endif
  2035. do
  2036. {
  2037. d = f;
  2038. f -= d;
  2039. a[ch++] = number_chars[d];
  2040. if (f < fx[wp])
  2041. break;
  2042. if (f + fx[wp] >= 1.0)
  2043. {
  2044. a[ch - 1] = number_chars[d+1];
  2045. break;
  2046. }
  2047. f *= radix;
  2048. if (!(--dpt))
  2049. a[ch++] = '.';
  2050. }
  2051. while (wp--);
  2052. if (dpt > 0)
  2053. {
  2054. #ifndef ENGNOT
  2055. if ((dpt > 4) && (exp > 6))
  2056. {
  2057. d = (a[0] == '-' ? 2 : 1);
  2058. for (i = ch++; i > d; i--)
  2059. a[i] = a[i - 1];
  2060. a[d] = '.';
  2061. efmt = 1;
  2062. }
  2063. else
  2064. #endif
  2065. {
  2066. while (--dpt)
  2067. a[ch++] = '0';
  2068. a[ch++] = '.';
  2069. }
  2070. }
  2071. if (a[ch - 1] == '.')
  2072. a[ch++] = '0'; /* trailing zero */
  2073. if (efmt && exp)
  2074. {
  2075. a[ch++] = 'e';
  2076. if (exp < 0)
  2077. {
  2078. exp = -exp;
  2079. a[ch++] = '-';
  2080. }
  2081. for (i = radix; i <= exp; i *= radix);
  2082. for (i /= radix; i; i /= radix)
  2083. {
  2084. a[ch++] = number_chars[exp / i];
  2085. exp %= i;
  2086. }
  2087. }
  2088. return ch;
  2089. }
  2090. static size_t
  2091. icmplx2str (double real, double imag, char *str, int radix)
  2092. {
  2093. size_t i;
  2094. i = idbl2str (real, str, radix);
  2095. if (imag != 0.0)
  2096. {
  2097. /* Don't output a '+' for negative numbers or for Inf and
  2098. NaN. They will provide their own sign. */
  2099. if (0 <= imag && !xisinf (imag) && !xisnan (imag))
  2100. str[i++] = '+';
  2101. i += idbl2str (imag, &str[i], radix);
  2102. str[i++] = 'i';
  2103. }
  2104. return i;
  2105. }
  2106. static size_t
  2107. iflo2str (SCM flt, char *str, int radix)
  2108. {
  2109. size_t i;
  2110. if (SCM_REALP (flt))
  2111. i = idbl2str (SCM_REAL_VALUE (flt), str, radix);
  2112. else
  2113. i = icmplx2str (SCM_COMPLEX_REAL (flt), SCM_COMPLEX_IMAG (flt),
  2114. str, radix);
  2115. return i;
  2116. }
  2117. /* convert a scm_t_intmax to a string (unterminated). returns the number of
  2118. characters in the result.
  2119. rad is output base
  2120. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  2121. size_t
  2122. scm_iint2str (scm_t_intmax num, int rad, char *p)
  2123. {
  2124. if (num < 0)
  2125. {
  2126. *p++ = '-';
  2127. return scm_iuint2str (-num, rad, p) + 1;
  2128. }
  2129. else
  2130. return scm_iuint2str (num, rad, p);
  2131. }
  2132. /* convert a scm_t_intmax to a string (unterminated). returns the number of
  2133. characters in the result.
  2134. rad is output base
  2135. p is destination: worst case (base 2) is SCM_INTBUFLEN */
  2136. size_t
  2137. scm_iuint2str (scm_t_uintmax num, int rad, char *p)
  2138. {
  2139. size_t j = 1;
  2140. size_t i;
  2141. scm_t_uintmax n = num;
  2142. for (n /= rad; n > 0; n /= rad)
  2143. j++;
  2144. i = j;
  2145. n = num;
  2146. while (i--)
  2147. {
  2148. int d = n % rad;
  2149. n /= rad;
  2150. p[i] = d + ((d < 10) ? '0' : 'a' - 10);
  2151. }
  2152. return j;
  2153. }
  2154. SCM_DEFINE (scm_number_to_string, "number->string", 1, 1, 0,
  2155. (SCM n, SCM radix),
  2156. "Return a string holding the external representation of the\n"
  2157. "number @var{n} in the given @var{radix}. If @var{n} is\n"
  2158. "inexact, a radix of 10 will be used.")
  2159. #define FUNC_NAME s_scm_number_to_string
  2160. {
  2161. int base;
  2162. if (SCM_UNBNDP (radix))
  2163. base = 10;
  2164. else
  2165. base = scm_to_signed_integer (radix, 2, 36);
  2166. if (SCM_I_INUMP (n))
  2167. {
  2168. char num_buf [SCM_INTBUFLEN];
  2169. size_t length = scm_iint2str (SCM_I_INUM (n), base, num_buf);
  2170. return scm_from_locale_stringn (num_buf, length);
  2171. }
  2172. else if (SCM_BIGP (n))
  2173. {
  2174. char *str = mpz_get_str (NULL, base, SCM_I_BIG_MPZ (n));
  2175. scm_remember_upto_here_1 (n);
  2176. return scm_take_locale_string (str);
  2177. }
  2178. else if (SCM_FRACTIONP (n))
  2179. {
  2180. return scm_string_append (scm_list_3 (scm_number_to_string (SCM_FRACTION_NUMERATOR (n), radix),
  2181. scm_from_locale_string ("/"),
  2182. scm_number_to_string (SCM_FRACTION_DENOMINATOR (n), radix)));
  2183. }
  2184. else if (SCM_INEXACTP (n))
  2185. {
  2186. char num_buf [FLOBUFLEN];
  2187. return scm_from_locale_stringn (num_buf, iflo2str (n, num_buf, base));
  2188. }
  2189. else
  2190. SCM_WRONG_TYPE_ARG (1, n);
  2191. }
  2192. #undef FUNC_NAME
  2193. /* These print routines used to be stubbed here so that scm_repl.c
  2194. wouldn't need SCM_BIGDIG conditionals (pre GMP) */
  2195. int
  2196. scm_print_real (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2197. {
  2198. char num_buf[FLOBUFLEN];
  2199. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  2200. return !0;
  2201. }
  2202. void
  2203. scm_i_print_double (double val, SCM port)
  2204. {
  2205. char num_buf[FLOBUFLEN];
  2206. scm_lfwrite (num_buf, idbl2str (val, num_buf, 10), port);
  2207. }
  2208. int
  2209. scm_print_complex (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2210. {
  2211. char num_buf[FLOBUFLEN];
  2212. scm_lfwrite (num_buf, iflo2str (sexp, num_buf, 10), port);
  2213. return !0;
  2214. }
  2215. void
  2216. scm_i_print_complex (double real, double imag, SCM port)
  2217. {
  2218. char num_buf[FLOBUFLEN];
  2219. scm_lfwrite (num_buf, icmplx2str (real, imag, num_buf, 10), port);
  2220. }
  2221. int
  2222. scm_i_print_fraction (SCM sexp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2223. {
  2224. SCM str;
  2225. str = scm_number_to_string (sexp, SCM_UNDEFINED);
  2226. scm_lfwrite (scm_i_string_chars (str), scm_i_string_length (str), port);
  2227. scm_remember_upto_here_1 (str);
  2228. return !0;
  2229. }
  2230. int
  2231. scm_bigprint (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  2232. {
  2233. char *str = mpz_get_str (NULL, 10, SCM_I_BIG_MPZ (exp));
  2234. scm_remember_upto_here_1 (exp);
  2235. scm_lfwrite (str, (size_t) strlen (str), port);
  2236. free (str);
  2237. return !0;
  2238. }
  2239. /*** END nums->strs ***/
  2240. /*** STRINGS -> NUMBERS ***/
  2241. /* The following functions implement the conversion from strings to numbers.
  2242. * The implementation somehow follows the grammar for numbers as it is given
  2243. * in R5RS. Thus, the functions resemble syntactic units (<ureal R>,
  2244. * <uinteger R>, ...) that are used to build up numbers in the grammar. Some
  2245. * points should be noted about the implementation:
  2246. * * Each function keeps a local index variable 'idx' that points at the
  2247. * current position within the parsed string. The global index is only
  2248. * updated if the function could parse the corresponding syntactic unit
  2249. * successfully.
  2250. * * Similarly, the functions keep track of indicators of inexactness ('#',
  2251. * '.' or exponents) using local variables ('hash_seen', 'x'). Again, the
  2252. * global exactness information is only updated after each part has been
  2253. * successfully parsed.
  2254. * * Sequences of digits are parsed into temporary variables holding fixnums.
  2255. * Only if these fixnums would overflow, the result variables are updated
  2256. * using the standard functions scm_add, scm_product, scm_divide etc. Then,
  2257. * the temporary variables holding the fixnums are cleared, and the process
  2258. * starts over again. If for example fixnums were able to store five decimal
  2259. * digits, a number 1234567890 would be parsed in two parts 12345 and 67890,
  2260. * and the result was computed as 12345 * 100000 + 67890. In other words,
  2261. * only every five digits two bignum operations were performed.
  2262. */
  2263. enum t_exactness {NO_EXACTNESS, INEXACT, EXACT};
  2264. /* R5RS, section 7.1.1, lexical structure of numbers: <uinteger R>. */
  2265. /* In non ASCII-style encodings the following macro might not work. */
  2266. #define XDIGIT2UINT(d) \
  2267. (isdigit ((int) (unsigned char) d) \
  2268. ? (d) - '0' \
  2269. : tolower ((int) (unsigned char) d) - 'a' + 10)
  2270. static SCM
  2271. mem2uinteger (const char* mem, size_t len, unsigned int *p_idx,
  2272. unsigned int radix, enum t_exactness *p_exactness)
  2273. {
  2274. unsigned int idx = *p_idx;
  2275. unsigned int hash_seen = 0;
  2276. scm_t_bits shift = 1;
  2277. scm_t_bits add = 0;
  2278. unsigned int digit_value;
  2279. SCM result;
  2280. char c;
  2281. if (idx == len)
  2282. return SCM_BOOL_F;
  2283. c = mem[idx];
  2284. if (!isxdigit ((int) (unsigned char) c))
  2285. return SCM_BOOL_F;
  2286. digit_value = XDIGIT2UINT (c);
  2287. if (digit_value >= radix)
  2288. return SCM_BOOL_F;
  2289. idx++;
  2290. result = SCM_I_MAKINUM (digit_value);
  2291. while (idx != len)
  2292. {
  2293. char c = mem[idx];
  2294. if (isxdigit ((int) (unsigned char) c))
  2295. {
  2296. if (hash_seen)
  2297. break;
  2298. digit_value = XDIGIT2UINT (c);
  2299. if (digit_value >= radix)
  2300. break;
  2301. }
  2302. else if (c == '#')
  2303. {
  2304. hash_seen = 1;
  2305. digit_value = 0;
  2306. }
  2307. else
  2308. break;
  2309. idx++;
  2310. if (SCM_MOST_POSITIVE_FIXNUM / radix < shift)
  2311. {
  2312. result = scm_product (result, SCM_I_MAKINUM (shift));
  2313. if (add > 0)
  2314. result = scm_sum (result, SCM_I_MAKINUM (add));
  2315. shift = radix;
  2316. add = digit_value;
  2317. }
  2318. else
  2319. {
  2320. shift = shift * radix;
  2321. add = add * radix + digit_value;
  2322. }
  2323. };
  2324. if (shift > 1)
  2325. result = scm_product (result, SCM_I_MAKINUM (shift));
  2326. if (add > 0)
  2327. result = scm_sum (result, SCM_I_MAKINUM (add));
  2328. *p_idx = idx;
  2329. if (hash_seen)
  2330. *p_exactness = INEXACT;
  2331. return result;
  2332. }
  2333. /* R5RS, section 7.1.1, lexical structure of numbers: <decimal 10>. Only
  2334. * covers the parts of the rules that start at a potential point. The value
  2335. * of the digits up to the point have been parsed by the caller and are given
  2336. * in variable result. The content of *p_exactness indicates, whether a hash
  2337. * has already been seen in the digits before the point.
  2338. */
  2339. /* In non ASCII-style encodings the following macro might not work. */
  2340. #define DIGIT2UINT(d) ((d) - '0')
  2341. static SCM
  2342. mem2decimal_from_point (SCM result, const char* mem, size_t len,
  2343. unsigned int *p_idx, enum t_exactness *p_exactness)
  2344. {
  2345. unsigned int idx = *p_idx;
  2346. enum t_exactness x = *p_exactness;
  2347. if (idx == len)
  2348. return result;
  2349. if (mem[idx] == '.')
  2350. {
  2351. scm_t_bits shift = 1;
  2352. scm_t_bits add = 0;
  2353. unsigned int digit_value;
  2354. SCM big_shift = SCM_I_MAKINUM (1);
  2355. idx++;
  2356. while (idx != len)
  2357. {
  2358. char c = mem[idx];
  2359. if (isdigit ((int) (unsigned char) c))
  2360. {
  2361. if (x == INEXACT)
  2362. return SCM_BOOL_F;
  2363. else
  2364. digit_value = DIGIT2UINT (c);
  2365. }
  2366. else if (c == '#')
  2367. {
  2368. x = INEXACT;
  2369. digit_value = 0;
  2370. }
  2371. else
  2372. break;
  2373. idx++;
  2374. if (SCM_MOST_POSITIVE_FIXNUM / 10 < shift)
  2375. {
  2376. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  2377. result = scm_product (result, SCM_I_MAKINUM (shift));
  2378. if (add > 0)
  2379. result = scm_sum (result, SCM_I_MAKINUM (add));
  2380. shift = 10;
  2381. add = digit_value;
  2382. }
  2383. else
  2384. {
  2385. shift = shift * 10;
  2386. add = add * 10 + digit_value;
  2387. }
  2388. };
  2389. if (add > 0)
  2390. {
  2391. big_shift = scm_product (big_shift, SCM_I_MAKINUM (shift));
  2392. result = scm_product (result, SCM_I_MAKINUM (shift));
  2393. result = scm_sum (result, SCM_I_MAKINUM (add));
  2394. }
  2395. result = scm_divide (result, big_shift);
  2396. /* We've seen a decimal point, thus the value is implicitly inexact. */
  2397. x = INEXACT;
  2398. }
  2399. if (idx != len)
  2400. {
  2401. int sign = 1;
  2402. unsigned int start;
  2403. char c;
  2404. int exponent;
  2405. SCM e;
  2406. /* R5RS, section 7.1.1, lexical structure of numbers: <suffix> */
  2407. switch (mem[idx])
  2408. {
  2409. case 'd': case 'D':
  2410. case 'e': case 'E':
  2411. case 'f': case 'F':
  2412. case 'l': case 'L':
  2413. case 's': case 'S':
  2414. idx++;
  2415. start = idx;
  2416. c = mem[idx];
  2417. if (c == '-')
  2418. {
  2419. idx++;
  2420. sign = -1;
  2421. c = mem[idx];
  2422. }
  2423. else if (c == '+')
  2424. {
  2425. idx++;
  2426. sign = 1;
  2427. c = mem[idx];
  2428. }
  2429. else
  2430. sign = 1;
  2431. if (!isdigit ((int) (unsigned char) c))
  2432. return SCM_BOOL_F;
  2433. idx++;
  2434. exponent = DIGIT2UINT (c);
  2435. while (idx != len)
  2436. {
  2437. char c = mem[idx];
  2438. if (isdigit ((int) (unsigned char) c))
  2439. {
  2440. idx++;
  2441. if (exponent <= SCM_MAXEXP)
  2442. exponent = exponent * 10 + DIGIT2UINT (c);
  2443. }
  2444. else
  2445. break;
  2446. }
  2447. if (exponent > SCM_MAXEXP)
  2448. {
  2449. size_t exp_len = idx - start;
  2450. SCM exp_string = scm_from_locale_stringn (&mem[start], exp_len);
  2451. SCM exp_num = scm_string_to_number (exp_string, SCM_UNDEFINED);
  2452. scm_out_of_range ("string->number", exp_num);
  2453. }
  2454. e = scm_integer_expt (SCM_I_MAKINUM (10), SCM_I_MAKINUM (exponent));
  2455. if (sign == 1)
  2456. result = scm_product (result, e);
  2457. else
  2458. result = scm_divide2real (result, e);
  2459. /* We've seen an exponent, thus the value is implicitly inexact. */
  2460. x = INEXACT;
  2461. break;
  2462. default:
  2463. break;
  2464. }
  2465. }
  2466. *p_idx = idx;
  2467. if (x == INEXACT)
  2468. *p_exactness = x;
  2469. return result;
  2470. }
  2471. /* R5RS, section 7.1.1, lexical structure of numbers: <ureal R> */
  2472. static SCM
  2473. mem2ureal (const char* mem, size_t len, unsigned int *p_idx,
  2474. unsigned int radix, enum t_exactness *p_exactness)
  2475. {
  2476. unsigned int idx = *p_idx;
  2477. SCM result;
  2478. /* Start off believing that the number will be exact. This changes
  2479. to INEXACT if we see a decimal point or a hash. */
  2480. enum t_exactness x = EXACT;
  2481. if (idx == len)
  2482. return SCM_BOOL_F;
  2483. if (idx+5 <= len && !strncmp (mem+idx, "inf.0", 5))
  2484. {
  2485. *p_idx = idx+5;
  2486. return scm_inf ();
  2487. }
  2488. if (idx+4 < len && !strncmp (mem+idx, "nan.", 4))
  2489. {
  2490. /* Cobble up the fractional part. We might want to set the
  2491. NaN's mantissa from it. */
  2492. idx += 4;
  2493. mem2uinteger (mem, len, &idx, 10, &x);
  2494. *p_idx = idx;
  2495. return scm_nan ();
  2496. }
  2497. if (mem[idx] == '.')
  2498. {
  2499. if (radix != 10)
  2500. return SCM_BOOL_F;
  2501. else if (idx + 1 == len)
  2502. return SCM_BOOL_F;
  2503. else if (!isdigit ((int) (unsigned char) mem[idx + 1]))
  2504. return SCM_BOOL_F;
  2505. else
  2506. result = mem2decimal_from_point (SCM_I_MAKINUM (0), mem, len,
  2507. p_idx, &x);
  2508. }
  2509. else
  2510. {
  2511. SCM uinteger;
  2512. uinteger = mem2uinteger (mem, len, &idx, radix, &x);
  2513. if (scm_is_false (uinteger))
  2514. return SCM_BOOL_F;
  2515. if (idx == len)
  2516. result = uinteger;
  2517. else if (mem[idx] == '/')
  2518. {
  2519. SCM divisor;
  2520. idx++;
  2521. divisor = mem2uinteger (mem, len, &idx, radix, &x);
  2522. if (scm_is_false (divisor))
  2523. return SCM_BOOL_F;
  2524. /* both are int/big here, I assume */
  2525. result = scm_i_make_ratio (uinteger, divisor);
  2526. }
  2527. else if (radix == 10)
  2528. {
  2529. result = mem2decimal_from_point (uinteger, mem, len, &idx, &x);
  2530. if (scm_is_false (result))
  2531. return SCM_BOOL_F;
  2532. }
  2533. else
  2534. result = uinteger;
  2535. *p_idx = idx;
  2536. }
  2537. /* Update *p_exactness if the number just read was inexact. This is
  2538. important for complex numbers, so that a complex number is
  2539. treated as inexact overall if either its real or imaginary part
  2540. is inexact.
  2541. */
  2542. if (x == INEXACT)
  2543. *p_exactness = x;
  2544. /* When returning an inexact zero, make sure it is represented as a
  2545. floating point value so that we can change its sign.
  2546. */
  2547. if (scm_is_eq (result, SCM_I_MAKINUM(0)) && *p_exactness == INEXACT)
  2548. result = scm_from_double (0.0);
  2549. return result;
  2550. }
  2551. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  2552. static SCM
  2553. mem2complex (const char* mem, size_t len, unsigned int idx,
  2554. unsigned int radix, enum t_exactness *p_exactness)
  2555. {
  2556. char c;
  2557. int sign = 0;
  2558. SCM ureal;
  2559. if (idx == len)
  2560. return SCM_BOOL_F;
  2561. c = mem[idx];
  2562. if (c == '+')
  2563. {
  2564. idx++;
  2565. sign = 1;
  2566. }
  2567. else if (c == '-')
  2568. {
  2569. idx++;
  2570. sign = -1;
  2571. }
  2572. if (idx == len)
  2573. return SCM_BOOL_F;
  2574. ureal = mem2ureal (mem, len, &idx, radix, p_exactness);
  2575. if (scm_is_false (ureal))
  2576. {
  2577. /* input must be either +i or -i */
  2578. if (sign == 0)
  2579. return SCM_BOOL_F;
  2580. if (mem[idx] == 'i' || mem[idx] == 'I')
  2581. {
  2582. idx++;
  2583. if (idx != len)
  2584. return SCM_BOOL_F;
  2585. return scm_make_rectangular (SCM_I_MAKINUM (0), SCM_I_MAKINUM (sign));
  2586. }
  2587. else
  2588. return SCM_BOOL_F;
  2589. }
  2590. else
  2591. {
  2592. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2593. ureal = scm_difference (ureal, SCM_UNDEFINED);
  2594. if (idx == len)
  2595. return ureal;
  2596. c = mem[idx];
  2597. switch (c)
  2598. {
  2599. case 'i': case 'I':
  2600. /* either +<ureal>i or -<ureal>i */
  2601. idx++;
  2602. if (sign == 0)
  2603. return SCM_BOOL_F;
  2604. if (idx != len)
  2605. return SCM_BOOL_F;
  2606. return scm_make_rectangular (SCM_I_MAKINUM (0), ureal);
  2607. case '@':
  2608. /* polar input: <real>@<real>. */
  2609. idx++;
  2610. if (idx == len)
  2611. return SCM_BOOL_F;
  2612. else
  2613. {
  2614. int sign;
  2615. SCM angle;
  2616. SCM result;
  2617. c = mem[idx];
  2618. if (c == '+')
  2619. {
  2620. idx++;
  2621. sign = 1;
  2622. }
  2623. else if (c == '-')
  2624. {
  2625. idx++;
  2626. sign = -1;
  2627. }
  2628. else
  2629. sign = 1;
  2630. angle = mem2ureal (mem, len, &idx, radix, p_exactness);
  2631. if (scm_is_false (angle))
  2632. return SCM_BOOL_F;
  2633. if (idx != len)
  2634. return SCM_BOOL_F;
  2635. if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2636. angle = scm_difference (angle, SCM_UNDEFINED);
  2637. result = scm_make_polar (ureal, angle);
  2638. return result;
  2639. }
  2640. case '+':
  2641. case '-':
  2642. /* expecting input matching <real>[+-]<ureal>?i */
  2643. idx++;
  2644. if (idx == len)
  2645. return SCM_BOOL_F;
  2646. else
  2647. {
  2648. int sign = (c == '+') ? 1 : -1;
  2649. SCM imag = mem2ureal (mem, len, &idx, radix, p_exactness);
  2650. if (scm_is_false (imag))
  2651. imag = SCM_I_MAKINUM (sign);
  2652. else if (sign == -1 && scm_is_false (scm_nan_p (ureal)))
  2653. imag = scm_difference (imag, SCM_UNDEFINED);
  2654. if (idx == len)
  2655. return SCM_BOOL_F;
  2656. if (mem[idx] != 'i' && mem[idx] != 'I')
  2657. return SCM_BOOL_F;
  2658. idx++;
  2659. if (idx != len)
  2660. return SCM_BOOL_F;
  2661. return scm_make_rectangular (ureal, imag);
  2662. }
  2663. default:
  2664. return SCM_BOOL_F;
  2665. }
  2666. }
  2667. }
  2668. /* R5RS, section 7.1.1, lexical structure of numbers: <number> */
  2669. enum t_radix {NO_RADIX=0, DUAL=2, OCT=8, DEC=10, HEX=16};
  2670. SCM
  2671. scm_c_locale_stringn_to_number (const char* mem, size_t len,
  2672. unsigned int default_radix)
  2673. {
  2674. unsigned int idx = 0;
  2675. unsigned int radix = NO_RADIX;
  2676. enum t_exactness forced_x = NO_EXACTNESS;
  2677. enum t_exactness implicit_x = EXACT;
  2678. SCM result;
  2679. /* R5RS, section 7.1.1, lexical structure of numbers: <prefix R> */
  2680. while (idx + 2 < len && mem[idx] == '#')
  2681. {
  2682. switch (mem[idx + 1])
  2683. {
  2684. case 'b': case 'B':
  2685. if (radix != NO_RADIX)
  2686. return SCM_BOOL_F;
  2687. radix = DUAL;
  2688. break;
  2689. case 'd': case 'D':
  2690. if (radix != NO_RADIX)
  2691. return SCM_BOOL_F;
  2692. radix = DEC;
  2693. break;
  2694. case 'i': case 'I':
  2695. if (forced_x != NO_EXACTNESS)
  2696. return SCM_BOOL_F;
  2697. forced_x = INEXACT;
  2698. break;
  2699. case 'e': case 'E':
  2700. if (forced_x != NO_EXACTNESS)
  2701. return SCM_BOOL_F;
  2702. forced_x = EXACT;
  2703. break;
  2704. case 'o': case 'O':
  2705. if (radix != NO_RADIX)
  2706. return SCM_BOOL_F;
  2707. radix = OCT;
  2708. break;
  2709. case 'x': case 'X':
  2710. if (radix != NO_RADIX)
  2711. return SCM_BOOL_F;
  2712. radix = HEX;
  2713. break;
  2714. default:
  2715. return SCM_BOOL_F;
  2716. }
  2717. idx += 2;
  2718. }
  2719. /* R5RS, section 7.1.1, lexical structure of numbers: <complex R> */
  2720. if (radix == NO_RADIX)
  2721. result = mem2complex (mem, len, idx, default_radix, &implicit_x);
  2722. else
  2723. result = mem2complex (mem, len, idx, (unsigned int) radix, &implicit_x);
  2724. if (scm_is_false (result))
  2725. return SCM_BOOL_F;
  2726. switch (forced_x)
  2727. {
  2728. case EXACT:
  2729. if (SCM_INEXACTP (result))
  2730. return scm_inexact_to_exact (result);
  2731. else
  2732. return result;
  2733. case INEXACT:
  2734. if (SCM_INEXACTP (result))
  2735. return result;
  2736. else
  2737. return scm_exact_to_inexact (result);
  2738. case NO_EXACTNESS:
  2739. default:
  2740. if (implicit_x == INEXACT)
  2741. {
  2742. if (SCM_INEXACTP (result))
  2743. return result;
  2744. else
  2745. return scm_exact_to_inexact (result);
  2746. }
  2747. else
  2748. return result;
  2749. }
  2750. }
  2751. SCM_DEFINE (scm_string_to_number, "string->number", 1, 1, 0,
  2752. (SCM string, SCM radix),
  2753. "Return a number of the maximally precise representation\n"
  2754. "expressed by the given @var{string}. @var{radix} must be an\n"
  2755. "exact integer, either 2, 8, 10, or 16. If supplied, @var{radix}\n"
  2756. "is a default radix that may be overridden by an explicit radix\n"
  2757. "prefix in @var{string} (e.g. \"#o177\"). If @var{radix} is not\n"
  2758. "supplied, then the default radix is 10. If string is not a\n"
  2759. "syntactically valid notation for a number, then\n"
  2760. "@code{string->number} returns @code{#f}.")
  2761. #define FUNC_NAME s_scm_string_to_number
  2762. {
  2763. SCM answer;
  2764. unsigned int base;
  2765. SCM_VALIDATE_STRING (1, string);
  2766. if (SCM_UNBNDP (radix))
  2767. base = 10;
  2768. else
  2769. base = scm_to_unsigned_integer (radix, 2, INT_MAX);
  2770. answer = scm_c_locale_stringn_to_number (scm_i_string_chars (string),
  2771. scm_i_string_length (string),
  2772. base);
  2773. scm_remember_upto_here_1 (string);
  2774. return answer;
  2775. }
  2776. #undef FUNC_NAME
  2777. /*** END strs->nums ***/
  2778. SCM
  2779. scm_bigequal (SCM x, SCM y)
  2780. {
  2781. int result = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2782. scm_remember_upto_here_2 (x, y);
  2783. return scm_from_bool (0 == result);
  2784. }
  2785. SCM
  2786. scm_real_equalp (SCM x, SCM y)
  2787. {
  2788. return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  2789. }
  2790. SCM
  2791. scm_complex_equalp (SCM x, SCM y)
  2792. {
  2793. return scm_from_bool (SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y)
  2794. && SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y));
  2795. }
  2796. SCM
  2797. scm_i_fraction_equalp (SCM x, SCM y)
  2798. {
  2799. if (scm_is_false (scm_equal_p (SCM_FRACTION_NUMERATOR (x),
  2800. SCM_FRACTION_NUMERATOR (y)))
  2801. || scm_is_false (scm_equal_p (SCM_FRACTION_DENOMINATOR (x),
  2802. SCM_FRACTION_DENOMINATOR (y))))
  2803. return SCM_BOOL_F;
  2804. else
  2805. return SCM_BOOL_T;
  2806. }
  2807. SCM_DEFINE (scm_number_p, "number?", 1, 0, 0,
  2808. (SCM x),
  2809. "Return @code{#t} if @var{x} is a number, @code{#f}\n"
  2810. "otherwise.")
  2811. #define FUNC_NAME s_scm_number_p
  2812. {
  2813. return scm_from_bool (SCM_NUMBERP (x));
  2814. }
  2815. #undef FUNC_NAME
  2816. SCM_DEFINE (scm_complex_p, "complex?", 1, 0, 0,
  2817. (SCM x),
  2818. "Return @code{#t} if @var{x} is a complex number, @code{#f}\n"
  2819. "otherwise. Note that the sets of real, rational and integer\n"
  2820. "values form subsets of the set of complex numbers, i. e. the\n"
  2821. "predicate will also be fulfilled if @var{x} is a real,\n"
  2822. "rational or integer number.")
  2823. #define FUNC_NAME s_scm_complex_p
  2824. {
  2825. /* all numbers are complex. */
  2826. return scm_number_p (x);
  2827. }
  2828. #undef FUNC_NAME
  2829. SCM_DEFINE (scm_real_p, "real?", 1, 0, 0,
  2830. (SCM x),
  2831. "Return @code{#t} if @var{x} is a real number, @code{#f}\n"
  2832. "otherwise. Note that the set of integer values forms a subset of\n"
  2833. "the set of real numbers, i. e. the predicate will also be\n"
  2834. "fulfilled if @var{x} is an integer number.")
  2835. #define FUNC_NAME s_scm_real_p
  2836. {
  2837. /* we can't represent irrational numbers. */
  2838. return scm_rational_p (x);
  2839. }
  2840. #undef FUNC_NAME
  2841. SCM_DEFINE (scm_rational_p, "rational?", 1, 0, 0,
  2842. (SCM x),
  2843. "Return @code{#t} if @var{x} is a rational number, @code{#f}\n"
  2844. "otherwise. Note that the set of integer values forms a subset of\n"
  2845. "the set of rational numbers, i. e. the predicate will also be\n"
  2846. "fulfilled if @var{x} is an integer number.")
  2847. #define FUNC_NAME s_scm_rational_p
  2848. {
  2849. if (SCM_I_INUMP (x))
  2850. return SCM_BOOL_T;
  2851. else if (SCM_IMP (x))
  2852. return SCM_BOOL_F;
  2853. else if (SCM_BIGP (x))
  2854. return SCM_BOOL_T;
  2855. else if (SCM_FRACTIONP (x))
  2856. return SCM_BOOL_T;
  2857. else if (SCM_REALP (x))
  2858. /* due to their limited precision, all floating point numbers are
  2859. rational as well. */
  2860. return SCM_BOOL_T;
  2861. else
  2862. return SCM_BOOL_F;
  2863. }
  2864. #undef FUNC_NAME
  2865. SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0,
  2866. (SCM x),
  2867. "Return @code{#t} if @var{x} is an integer number, @code{#f}\n"
  2868. "else.")
  2869. #define FUNC_NAME s_scm_integer_p
  2870. {
  2871. double r;
  2872. if (SCM_I_INUMP (x))
  2873. return SCM_BOOL_T;
  2874. if (SCM_IMP (x))
  2875. return SCM_BOOL_F;
  2876. if (SCM_BIGP (x))
  2877. return SCM_BOOL_T;
  2878. if (!SCM_INEXACTP (x))
  2879. return SCM_BOOL_F;
  2880. if (SCM_COMPLEXP (x))
  2881. return SCM_BOOL_F;
  2882. r = SCM_REAL_VALUE (x);
  2883. /* +/-inf passes r==floor(r), making those #t */
  2884. if (r == floor (r))
  2885. return SCM_BOOL_T;
  2886. return SCM_BOOL_F;
  2887. }
  2888. #undef FUNC_NAME
  2889. SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0,
  2890. (SCM x),
  2891. "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n"
  2892. "else.")
  2893. #define FUNC_NAME s_scm_inexact_p
  2894. {
  2895. if (SCM_INEXACTP (x))
  2896. return SCM_BOOL_T;
  2897. if (SCM_NUMBERP (x))
  2898. return SCM_BOOL_F;
  2899. SCM_WRONG_TYPE_ARG (1, x);
  2900. }
  2901. #undef FUNC_NAME
  2902. SCM_GPROC1 (s_eq_p, "=", scm_tc7_rpsubr, scm_num_eq_p, g_eq_p);
  2903. /* "Return @code{#t} if all parameters are numerically equal." */
  2904. SCM
  2905. scm_num_eq_p (SCM x, SCM y)
  2906. {
  2907. again:
  2908. if (SCM_I_INUMP (x))
  2909. {
  2910. long xx = SCM_I_INUM (x);
  2911. if (SCM_I_INUMP (y))
  2912. {
  2913. long yy = SCM_I_INUM (y);
  2914. return scm_from_bool (xx == yy);
  2915. }
  2916. else if (SCM_BIGP (y))
  2917. return SCM_BOOL_F;
  2918. else if (SCM_REALP (y))
  2919. {
  2920. /* On a 32-bit system an inum fits a double, we can cast the inum
  2921. to a double and compare.
  2922. But on a 64-bit system an inum is bigger than a double and
  2923. casting it to a double (call that dxx) will round. dxx is at
  2924. worst 1 bigger or smaller than xx, so if dxx==yy we know yy is
  2925. an integer and fits a long. So we cast yy to a long and
  2926. compare with plain xx.
  2927. An alternative (for any size system actually) would be to check
  2928. yy is an integer (with floor) and is in range of an inum
  2929. (compare against appropriate powers of 2) then test
  2930. xx==(long)yy. It's just a matter of which casts/comparisons
  2931. might be fastest or easiest for the cpu. */
  2932. double yy = SCM_REAL_VALUE (y);
  2933. return scm_from_bool ((double) xx == yy
  2934. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  2935. || xx == (long) yy));
  2936. }
  2937. else if (SCM_COMPLEXP (y))
  2938. return scm_from_bool (((double) xx == SCM_COMPLEX_REAL (y))
  2939. && (0.0 == SCM_COMPLEX_IMAG (y)));
  2940. else if (SCM_FRACTIONP (y))
  2941. return SCM_BOOL_F;
  2942. else
  2943. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  2944. }
  2945. else if (SCM_BIGP (x))
  2946. {
  2947. if (SCM_I_INUMP (y))
  2948. return SCM_BOOL_F;
  2949. else if (SCM_BIGP (y))
  2950. {
  2951. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  2952. scm_remember_upto_here_2 (x, y);
  2953. return scm_from_bool (0 == cmp);
  2954. }
  2955. else if (SCM_REALP (y))
  2956. {
  2957. int cmp;
  2958. if (xisnan (SCM_REAL_VALUE (y)))
  2959. return SCM_BOOL_F;
  2960. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  2961. scm_remember_upto_here_1 (x);
  2962. return scm_from_bool (0 == cmp);
  2963. }
  2964. else if (SCM_COMPLEXP (y))
  2965. {
  2966. int cmp;
  2967. if (0.0 != SCM_COMPLEX_IMAG (y))
  2968. return SCM_BOOL_F;
  2969. if (xisnan (SCM_COMPLEX_REAL (y)))
  2970. return SCM_BOOL_F;
  2971. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_COMPLEX_REAL (y));
  2972. scm_remember_upto_here_1 (x);
  2973. return scm_from_bool (0 == cmp);
  2974. }
  2975. else if (SCM_FRACTIONP (y))
  2976. return SCM_BOOL_F;
  2977. else
  2978. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  2979. }
  2980. else if (SCM_REALP (x))
  2981. {
  2982. double xx = SCM_REAL_VALUE (x);
  2983. if (SCM_I_INUMP (y))
  2984. {
  2985. /* see comments with inum/real above */
  2986. long yy = SCM_I_INUM (y);
  2987. return scm_from_bool (xx == (double) yy
  2988. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1
  2989. || (long) xx == yy));
  2990. }
  2991. else if (SCM_BIGP (y))
  2992. {
  2993. int cmp;
  2994. if (xisnan (SCM_REAL_VALUE (x)))
  2995. return SCM_BOOL_F;
  2996. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  2997. scm_remember_upto_here_1 (y);
  2998. return scm_from_bool (0 == cmp);
  2999. }
  3000. else if (SCM_REALP (y))
  3001. return scm_from_bool (SCM_REAL_VALUE (x) == SCM_REAL_VALUE (y));
  3002. else if (SCM_COMPLEXP (y))
  3003. return scm_from_bool ((SCM_REAL_VALUE (x) == SCM_COMPLEX_REAL (y))
  3004. && (0.0 == SCM_COMPLEX_IMAG (y)));
  3005. else if (SCM_FRACTIONP (y))
  3006. {
  3007. double xx = SCM_REAL_VALUE (x);
  3008. if (xisnan (xx))
  3009. return SCM_BOOL_F;
  3010. if (xisinf (xx))
  3011. return scm_from_bool (xx < 0.0);
  3012. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3013. goto again;
  3014. }
  3015. else
  3016. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3017. }
  3018. else if (SCM_COMPLEXP (x))
  3019. {
  3020. if (SCM_I_INUMP (y))
  3021. return scm_from_bool ((SCM_COMPLEX_REAL (x) == (double) SCM_I_INUM (y))
  3022. && (SCM_COMPLEX_IMAG (x) == 0.0));
  3023. else if (SCM_BIGP (y))
  3024. {
  3025. int cmp;
  3026. if (0.0 != SCM_COMPLEX_IMAG (x))
  3027. return SCM_BOOL_F;
  3028. if (xisnan (SCM_COMPLEX_REAL (x)))
  3029. return SCM_BOOL_F;
  3030. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_COMPLEX_REAL (x));
  3031. scm_remember_upto_here_1 (y);
  3032. return scm_from_bool (0 == cmp);
  3033. }
  3034. else if (SCM_REALP (y))
  3035. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_REAL_VALUE (y))
  3036. && (SCM_COMPLEX_IMAG (x) == 0.0));
  3037. else if (SCM_COMPLEXP (y))
  3038. return scm_from_bool ((SCM_COMPLEX_REAL (x) == SCM_COMPLEX_REAL (y))
  3039. && (SCM_COMPLEX_IMAG (x) == SCM_COMPLEX_IMAG (y)));
  3040. else if (SCM_FRACTIONP (y))
  3041. {
  3042. double xx;
  3043. if (SCM_COMPLEX_IMAG (x) != 0.0)
  3044. return SCM_BOOL_F;
  3045. xx = SCM_COMPLEX_REAL (x);
  3046. if (xisnan (xx))
  3047. return SCM_BOOL_F;
  3048. if (xisinf (xx))
  3049. return scm_from_bool (xx < 0.0);
  3050. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3051. goto again;
  3052. }
  3053. else
  3054. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3055. }
  3056. else if (SCM_FRACTIONP (x))
  3057. {
  3058. if (SCM_I_INUMP (y))
  3059. return SCM_BOOL_F;
  3060. else if (SCM_BIGP (y))
  3061. return SCM_BOOL_F;
  3062. else if (SCM_REALP (y))
  3063. {
  3064. double yy = SCM_REAL_VALUE (y);
  3065. if (xisnan (yy))
  3066. return SCM_BOOL_F;
  3067. if (xisinf (yy))
  3068. return scm_from_bool (0.0 < yy);
  3069. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3070. goto again;
  3071. }
  3072. else if (SCM_COMPLEXP (y))
  3073. {
  3074. double yy;
  3075. if (SCM_COMPLEX_IMAG (y) != 0.0)
  3076. return SCM_BOOL_F;
  3077. yy = SCM_COMPLEX_REAL (y);
  3078. if (xisnan (yy))
  3079. return SCM_BOOL_F;
  3080. if (xisinf (yy))
  3081. return scm_from_bool (0.0 < yy);
  3082. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3083. goto again;
  3084. }
  3085. else if (SCM_FRACTIONP (y))
  3086. return scm_i_fraction_equalp (x, y);
  3087. else
  3088. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARGn, s_eq_p);
  3089. }
  3090. else
  3091. SCM_WTA_DISPATCH_2 (g_eq_p, x, y, SCM_ARG1, s_eq_p);
  3092. }
  3093. /* OPTIMIZE-ME: For int/frac and frac/frac compares, the multiplications
  3094. done are good for inums, but for bignums an answer can almost always be
  3095. had by just examining a few high bits of the operands, as done by GMP in
  3096. mpq_cmp. flonum/frac compares likewise, but with the slight complication
  3097. of the float exponent to take into account. */
  3098. SCM_GPROC1 (s_less_p, "<", scm_tc7_rpsubr, scm_less_p, g_less_p);
  3099. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3100. * "increasing."
  3101. */
  3102. SCM
  3103. scm_less_p (SCM x, SCM y)
  3104. {
  3105. again:
  3106. if (SCM_I_INUMP (x))
  3107. {
  3108. long xx = SCM_I_INUM (x);
  3109. if (SCM_I_INUMP (y))
  3110. {
  3111. long yy = SCM_I_INUM (y);
  3112. return scm_from_bool (xx < yy);
  3113. }
  3114. else if (SCM_BIGP (y))
  3115. {
  3116. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3117. scm_remember_upto_here_1 (y);
  3118. return scm_from_bool (sgn > 0);
  3119. }
  3120. else if (SCM_REALP (y))
  3121. return scm_from_bool ((double) xx < SCM_REAL_VALUE (y));
  3122. else if (SCM_FRACTIONP (y))
  3123. {
  3124. /* "x < a/b" becomes "x*b < a" */
  3125. int_frac:
  3126. x = scm_product (x, SCM_FRACTION_DENOMINATOR (y));
  3127. y = SCM_FRACTION_NUMERATOR (y);
  3128. goto again;
  3129. }
  3130. else
  3131. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3132. }
  3133. else if (SCM_BIGP (x))
  3134. {
  3135. if (SCM_I_INUMP (y))
  3136. {
  3137. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3138. scm_remember_upto_here_1 (x);
  3139. return scm_from_bool (sgn < 0);
  3140. }
  3141. else if (SCM_BIGP (y))
  3142. {
  3143. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3144. scm_remember_upto_here_2 (x, y);
  3145. return scm_from_bool (cmp < 0);
  3146. }
  3147. else if (SCM_REALP (y))
  3148. {
  3149. int cmp;
  3150. if (xisnan (SCM_REAL_VALUE (y)))
  3151. return SCM_BOOL_F;
  3152. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (x), SCM_REAL_VALUE (y));
  3153. scm_remember_upto_here_1 (x);
  3154. return scm_from_bool (cmp < 0);
  3155. }
  3156. else if (SCM_FRACTIONP (y))
  3157. goto int_frac;
  3158. else
  3159. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3160. }
  3161. else if (SCM_REALP (x))
  3162. {
  3163. if (SCM_I_INUMP (y))
  3164. return scm_from_bool (SCM_REAL_VALUE (x) < (double) SCM_I_INUM (y));
  3165. else if (SCM_BIGP (y))
  3166. {
  3167. int cmp;
  3168. if (xisnan (SCM_REAL_VALUE (x)))
  3169. return SCM_BOOL_F;
  3170. cmp = xmpz_cmp_d (SCM_I_BIG_MPZ (y), SCM_REAL_VALUE (x));
  3171. scm_remember_upto_here_1 (y);
  3172. return scm_from_bool (cmp > 0);
  3173. }
  3174. else if (SCM_REALP (y))
  3175. return scm_from_bool (SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y));
  3176. else if (SCM_FRACTIONP (y))
  3177. {
  3178. double xx = SCM_REAL_VALUE (x);
  3179. if (xisnan (xx))
  3180. return SCM_BOOL_F;
  3181. if (xisinf (xx))
  3182. return scm_from_bool (xx < 0.0);
  3183. x = scm_inexact_to_exact (x); /* with x as frac or int */
  3184. goto again;
  3185. }
  3186. else
  3187. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3188. }
  3189. else if (SCM_FRACTIONP (x))
  3190. {
  3191. if (SCM_I_INUMP (y) || SCM_BIGP (y))
  3192. {
  3193. /* "a/b < y" becomes "a < y*b" */
  3194. y = scm_product (y, SCM_FRACTION_DENOMINATOR (x));
  3195. x = SCM_FRACTION_NUMERATOR (x);
  3196. goto again;
  3197. }
  3198. else if (SCM_REALP (y))
  3199. {
  3200. double yy = SCM_REAL_VALUE (y);
  3201. if (xisnan (yy))
  3202. return SCM_BOOL_F;
  3203. if (xisinf (yy))
  3204. return scm_from_bool (0.0 < yy);
  3205. y = scm_inexact_to_exact (y); /* with y as frac or int */
  3206. goto again;
  3207. }
  3208. else if (SCM_FRACTIONP (y))
  3209. {
  3210. /* "a/b < c/d" becomes "a*d < c*b" */
  3211. SCM new_x = scm_product (SCM_FRACTION_NUMERATOR (x),
  3212. SCM_FRACTION_DENOMINATOR (y));
  3213. SCM new_y = scm_product (SCM_FRACTION_NUMERATOR (y),
  3214. SCM_FRACTION_DENOMINATOR (x));
  3215. x = new_x;
  3216. y = new_y;
  3217. goto again;
  3218. }
  3219. else
  3220. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARGn, s_less_p);
  3221. }
  3222. else
  3223. SCM_WTA_DISPATCH_2 (g_less_p, x, y, SCM_ARG1, s_less_p);
  3224. }
  3225. SCM_GPROC1 (s_scm_gr_p, ">", scm_tc7_rpsubr, scm_gr_p, g_gr_p);
  3226. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3227. * "decreasing."
  3228. */
  3229. #define FUNC_NAME s_scm_gr_p
  3230. SCM
  3231. scm_gr_p (SCM x, SCM y)
  3232. {
  3233. if (!SCM_NUMBERP (x))
  3234. SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG1, FUNC_NAME);
  3235. else if (!SCM_NUMBERP (y))
  3236. SCM_WTA_DISPATCH_2 (g_gr_p, x, y, SCM_ARG2, FUNC_NAME);
  3237. else
  3238. return scm_less_p (y, x);
  3239. }
  3240. #undef FUNC_NAME
  3241. SCM_GPROC1 (s_scm_leq_p, "<=", scm_tc7_rpsubr, scm_leq_p, g_leq_p);
  3242. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3243. * "non-decreasing."
  3244. */
  3245. #define FUNC_NAME s_scm_leq_p
  3246. SCM
  3247. scm_leq_p (SCM x, SCM y)
  3248. {
  3249. if (!SCM_NUMBERP (x))
  3250. SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG1, FUNC_NAME);
  3251. else if (!SCM_NUMBERP (y))
  3252. SCM_WTA_DISPATCH_2 (g_leq_p, x, y, SCM_ARG2, FUNC_NAME);
  3253. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  3254. return SCM_BOOL_F;
  3255. else
  3256. return scm_not (scm_less_p (y, x));
  3257. }
  3258. #undef FUNC_NAME
  3259. SCM_GPROC1 (s_scm_geq_p, ">=", scm_tc7_rpsubr, scm_geq_p, g_geq_p);
  3260. /* "Return @code{#t} if the list of parameters is monotonically\n"
  3261. * "non-increasing."
  3262. */
  3263. #define FUNC_NAME s_scm_geq_p
  3264. SCM
  3265. scm_geq_p (SCM x, SCM y)
  3266. {
  3267. if (!SCM_NUMBERP (x))
  3268. SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG1, FUNC_NAME);
  3269. else if (!SCM_NUMBERP (y))
  3270. SCM_WTA_DISPATCH_2 (g_geq_p, x, y, SCM_ARG2, FUNC_NAME);
  3271. else if (scm_is_true (scm_nan_p (x)) || scm_is_true (scm_nan_p (y)))
  3272. return SCM_BOOL_F;
  3273. else
  3274. return scm_not (scm_less_p (x, y));
  3275. }
  3276. #undef FUNC_NAME
  3277. SCM_GPROC (s_zero_p, "zero?", 1, 0, 0, scm_zero_p, g_zero_p);
  3278. /* "Return @code{#t} if @var{z} is an exact or inexact number equal to\n"
  3279. * "zero."
  3280. */
  3281. SCM
  3282. scm_zero_p (SCM z)
  3283. {
  3284. if (SCM_I_INUMP (z))
  3285. return scm_from_bool (scm_is_eq (z, SCM_INUM0));
  3286. else if (SCM_BIGP (z))
  3287. return SCM_BOOL_F;
  3288. else if (SCM_REALP (z))
  3289. return scm_from_bool (SCM_REAL_VALUE (z) == 0.0);
  3290. else if (SCM_COMPLEXP (z))
  3291. return scm_from_bool (SCM_COMPLEX_REAL (z) == 0.0
  3292. && SCM_COMPLEX_IMAG (z) == 0.0);
  3293. else if (SCM_FRACTIONP (z))
  3294. return SCM_BOOL_F;
  3295. else
  3296. SCM_WTA_DISPATCH_1 (g_zero_p, z, SCM_ARG1, s_zero_p);
  3297. }
  3298. SCM_GPROC (s_positive_p, "positive?", 1, 0, 0, scm_positive_p, g_positive_p);
  3299. /* "Return @code{#t} if @var{x} is an exact or inexact number greater than\n"
  3300. * "zero."
  3301. */
  3302. SCM
  3303. scm_positive_p (SCM x)
  3304. {
  3305. if (SCM_I_INUMP (x))
  3306. return scm_from_bool (SCM_I_INUM (x) > 0);
  3307. else if (SCM_BIGP (x))
  3308. {
  3309. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3310. scm_remember_upto_here_1 (x);
  3311. return scm_from_bool (sgn > 0);
  3312. }
  3313. else if (SCM_REALP (x))
  3314. return scm_from_bool(SCM_REAL_VALUE (x) > 0.0);
  3315. else if (SCM_FRACTIONP (x))
  3316. return scm_positive_p (SCM_FRACTION_NUMERATOR (x));
  3317. else
  3318. SCM_WTA_DISPATCH_1 (g_positive_p, x, SCM_ARG1, s_positive_p);
  3319. }
  3320. SCM_GPROC (s_negative_p, "negative?", 1, 0, 0, scm_negative_p, g_negative_p);
  3321. /* "Return @code{#t} if @var{x} is an exact or inexact number less than\n"
  3322. * "zero."
  3323. */
  3324. SCM
  3325. scm_negative_p (SCM x)
  3326. {
  3327. if (SCM_I_INUMP (x))
  3328. return scm_from_bool (SCM_I_INUM (x) < 0);
  3329. else if (SCM_BIGP (x))
  3330. {
  3331. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3332. scm_remember_upto_here_1 (x);
  3333. return scm_from_bool (sgn < 0);
  3334. }
  3335. else if (SCM_REALP (x))
  3336. return scm_from_bool(SCM_REAL_VALUE (x) < 0.0);
  3337. else if (SCM_FRACTIONP (x))
  3338. return scm_negative_p (SCM_FRACTION_NUMERATOR (x));
  3339. else
  3340. SCM_WTA_DISPATCH_1 (g_negative_p, x, SCM_ARG1, s_negative_p);
  3341. }
  3342. /* scm_min and scm_max return an inexact when either argument is inexact, as
  3343. required by r5rs. On that basis, for exact/inexact combinations the
  3344. exact is converted to inexact to compare and possibly return. This is
  3345. unlike scm_less_p above which takes some trouble to preserve all bits in
  3346. its test, such trouble is not required for min and max. */
  3347. SCM_GPROC1 (s_max, "max", scm_tc7_asubr, scm_max, g_max);
  3348. /* "Return the maximum of all parameter values."
  3349. */
  3350. SCM
  3351. scm_max (SCM x, SCM y)
  3352. {
  3353. if (SCM_UNBNDP (y))
  3354. {
  3355. if (SCM_UNBNDP (x))
  3356. SCM_WTA_DISPATCH_0 (g_max, s_max);
  3357. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  3358. return x;
  3359. else
  3360. SCM_WTA_DISPATCH_1 (g_max, x, SCM_ARG1, s_max);
  3361. }
  3362. if (SCM_I_INUMP (x))
  3363. {
  3364. long xx = SCM_I_INUM (x);
  3365. if (SCM_I_INUMP (y))
  3366. {
  3367. long yy = SCM_I_INUM (y);
  3368. return (xx < yy) ? y : x;
  3369. }
  3370. else if (SCM_BIGP (y))
  3371. {
  3372. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3373. scm_remember_upto_here_1 (y);
  3374. return (sgn < 0) ? x : y;
  3375. }
  3376. else if (SCM_REALP (y))
  3377. {
  3378. double z = xx;
  3379. /* if y==NaN then ">" is false and we return NaN */
  3380. return (z > SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
  3381. }
  3382. else if (SCM_FRACTIONP (y))
  3383. {
  3384. use_less:
  3385. return (scm_is_false (scm_less_p (x, y)) ? x : y);
  3386. }
  3387. else
  3388. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3389. }
  3390. else if (SCM_BIGP (x))
  3391. {
  3392. if (SCM_I_INUMP (y))
  3393. {
  3394. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3395. scm_remember_upto_here_1 (x);
  3396. return (sgn < 0) ? y : x;
  3397. }
  3398. else if (SCM_BIGP (y))
  3399. {
  3400. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3401. scm_remember_upto_here_2 (x, y);
  3402. return (cmp > 0) ? x : y;
  3403. }
  3404. else if (SCM_REALP (y))
  3405. {
  3406. /* if y==NaN then xx>yy is false, so we return the NaN y */
  3407. double xx, yy;
  3408. big_real:
  3409. xx = scm_i_big2dbl (x);
  3410. yy = SCM_REAL_VALUE (y);
  3411. return (xx > yy ? scm_from_double (xx) : y);
  3412. }
  3413. else if (SCM_FRACTIONP (y))
  3414. {
  3415. goto use_less;
  3416. }
  3417. else
  3418. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3419. }
  3420. else if (SCM_REALP (x))
  3421. {
  3422. if (SCM_I_INUMP (y))
  3423. {
  3424. double z = SCM_I_INUM (y);
  3425. /* if x==NaN then "<" is false and we return NaN */
  3426. return (SCM_REAL_VALUE (x) < z) ? scm_from_double (z) : x;
  3427. }
  3428. else if (SCM_BIGP (y))
  3429. {
  3430. SCM_SWAP (x, y);
  3431. goto big_real;
  3432. }
  3433. else if (SCM_REALP (y))
  3434. {
  3435. /* if x==NaN then our explicit check means we return NaN
  3436. if y==NaN then ">" is false and we return NaN
  3437. calling isnan is unavoidable, since it's the only way to know
  3438. which of x or y causes any compares to be false */
  3439. double xx = SCM_REAL_VALUE (x);
  3440. return (xisnan (xx) || xx > SCM_REAL_VALUE (y)) ? x : y;
  3441. }
  3442. else if (SCM_FRACTIONP (y))
  3443. {
  3444. double yy = scm_i_fraction2double (y);
  3445. double xx = SCM_REAL_VALUE (x);
  3446. return (xx < yy) ? scm_from_double (yy) : x;
  3447. }
  3448. else
  3449. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3450. }
  3451. else if (SCM_FRACTIONP (x))
  3452. {
  3453. if (SCM_I_INUMP (y))
  3454. {
  3455. goto use_less;
  3456. }
  3457. else if (SCM_BIGP (y))
  3458. {
  3459. goto use_less;
  3460. }
  3461. else if (SCM_REALP (y))
  3462. {
  3463. double xx = scm_i_fraction2double (x);
  3464. return (xx < SCM_REAL_VALUE (y)) ? y : scm_from_double (xx);
  3465. }
  3466. else if (SCM_FRACTIONP (y))
  3467. {
  3468. goto use_less;
  3469. }
  3470. else
  3471. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3472. }
  3473. else
  3474. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARG1, s_max);
  3475. }
  3476. SCM_GPROC1 (s_min, "min", scm_tc7_asubr, scm_min, g_min);
  3477. /* "Return the minium of all parameter values."
  3478. */
  3479. SCM
  3480. scm_min (SCM x, SCM y)
  3481. {
  3482. if (SCM_UNBNDP (y))
  3483. {
  3484. if (SCM_UNBNDP (x))
  3485. SCM_WTA_DISPATCH_0 (g_min, s_min);
  3486. else if (SCM_I_INUMP(x) || SCM_BIGP(x) || SCM_REALP(x) || SCM_FRACTIONP(x))
  3487. return x;
  3488. else
  3489. SCM_WTA_DISPATCH_1 (g_min, x, SCM_ARG1, s_min);
  3490. }
  3491. if (SCM_I_INUMP (x))
  3492. {
  3493. long xx = SCM_I_INUM (x);
  3494. if (SCM_I_INUMP (y))
  3495. {
  3496. long yy = SCM_I_INUM (y);
  3497. return (xx < yy) ? x : y;
  3498. }
  3499. else if (SCM_BIGP (y))
  3500. {
  3501. int sgn = mpz_sgn (SCM_I_BIG_MPZ (y));
  3502. scm_remember_upto_here_1 (y);
  3503. return (sgn < 0) ? y : x;
  3504. }
  3505. else if (SCM_REALP (y))
  3506. {
  3507. double z = xx;
  3508. /* if y==NaN then "<" is false and we return NaN */
  3509. return (z < SCM_REAL_VALUE (y)) ? scm_from_double (z) : y;
  3510. }
  3511. else if (SCM_FRACTIONP (y))
  3512. {
  3513. use_less:
  3514. return (scm_is_false (scm_less_p (x, y)) ? y : x);
  3515. }
  3516. else
  3517. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3518. }
  3519. else if (SCM_BIGP (x))
  3520. {
  3521. if (SCM_I_INUMP (y))
  3522. {
  3523. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3524. scm_remember_upto_here_1 (x);
  3525. return (sgn < 0) ? x : y;
  3526. }
  3527. else if (SCM_BIGP (y))
  3528. {
  3529. int cmp = mpz_cmp (SCM_I_BIG_MPZ (x), SCM_I_BIG_MPZ (y));
  3530. scm_remember_upto_here_2 (x, y);
  3531. return (cmp > 0) ? y : x;
  3532. }
  3533. else if (SCM_REALP (y))
  3534. {
  3535. /* if y==NaN then xx<yy is false, so we return the NaN y */
  3536. double xx, yy;
  3537. big_real:
  3538. xx = scm_i_big2dbl (x);
  3539. yy = SCM_REAL_VALUE (y);
  3540. return (xx < yy ? scm_from_double (xx) : y);
  3541. }
  3542. else if (SCM_FRACTIONP (y))
  3543. {
  3544. goto use_less;
  3545. }
  3546. else
  3547. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3548. }
  3549. else if (SCM_REALP (x))
  3550. {
  3551. if (SCM_I_INUMP (y))
  3552. {
  3553. double z = SCM_I_INUM (y);
  3554. /* if x==NaN then "<" is false and we return NaN */
  3555. return (z < SCM_REAL_VALUE (x)) ? scm_from_double (z) : x;
  3556. }
  3557. else if (SCM_BIGP (y))
  3558. {
  3559. SCM_SWAP (x, y);
  3560. goto big_real;
  3561. }
  3562. else if (SCM_REALP (y))
  3563. {
  3564. /* if x==NaN then our explicit check means we return NaN
  3565. if y==NaN then "<" is false and we return NaN
  3566. calling isnan is unavoidable, since it's the only way to know
  3567. which of x or y causes any compares to be false */
  3568. double xx = SCM_REAL_VALUE (x);
  3569. return (xisnan (xx) || xx < SCM_REAL_VALUE (y)) ? x : y;
  3570. }
  3571. else if (SCM_FRACTIONP (y))
  3572. {
  3573. double yy = scm_i_fraction2double (y);
  3574. double xx = SCM_REAL_VALUE (x);
  3575. return (yy < xx) ? scm_from_double (yy) : x;
  3576. }
  3577. else
  3578. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARGn, s_min);
  3579. }
  3580. else if (SCM_FRACTIONP (x))
  3581. {
  3582. if (SCM_I_INUMP (y))
  3583. {
  3584. goto use_less;
  3585. }
  3586. else if (SCM_BIGP (y))
  3587. {
  3588. goto use_less;
  3589. }
  3590. else if (SCM_REALP (y))
  3591. {
  3592. double xx = scm_i_fraction2double (x);
  3593. return (SCM_REAL_VALUE (y) < xx) ? y : scm_from_double (xx);
  3594. }
  3595. else if (SCM_FRACTIONP (y))
  3596. {
  3597. goto use_less;
  3598. }
  3599. else
  3600. SCM_WTA_DISPATCH_2 (g_max, x, y, SCM_ARGn, s_max);
  3601. }
  3602. else
  3603. SCM_WTA_DISPATCH_2 (g_min, x, y, SCM_ARG1, s_min);
  3604. }
  3605. SCM_GPROC1 (s_sum, "+", scm_tc7_asubr, scm_sum, g_sum);
  3606. /* "Return the sum of all parameter values. Return 0 if called without\n"
  3607. * "any parameters."
  3608. */
  3609. SCM
  3610. scm_sum (SCM x, SCM y)
  3611. {
  3612. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  3613. {
  3614. if (SCM_NUMBERP (x)) return x;
  3615. if (SCM_UNBNDP (x)) return SCM_INUM0;
  3616. SCM_WTA_DISPATCH_1 (g_sum, x, SCM_ARG1, s_sum);
  3617. }
  3618. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3619. {
  3620. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3621. {
  3622. long xx = SCM_I_INUM (x);
  3623. long yy = SCM_I_INUM (y);
  3624. long int z = xx + yy;
  3625. return SCM_FIXABLE (z) ? SCM_I_MAKINUM (z) : scm_i_long2big (z);
  3626. }
  3627. else if (SCM_BIGP (y))
  3628. {
  3629. SCM_SWAP (x, y);
  3630. goto add_big_inum;
  3631. }
  3632. else if (SCM_REALP (y))
  3633. {
  3634. long int xx = SCM_I_INUM (x);
  3635. return scm_from_double (xx + SCM_REAL_VALUE (y));
  3636. }
  3637. else if (SCM_COMPLEXP (y))
  3638. {
  3639. long int xx = SCM_I_INUM (x);
  3640. return scm_c_make_rectangular (xx + SCM_COMPLEX_REAL (y),
  3641. SCM_COMPLEX_IMAG (y));
  3642. }
  3643. else if (SCM_FRACTIONP (y))
  3644. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  3645. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  3646. SCM_FRACTION_DENOMINATOR (y));
  3647. else
  3648. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3649. } else if (SCM_BIGP (x))
  3650. {
  3651. if (SCM_I_INUMP (y))
  3652. {
  3653. long int inum;
  3654. int bigsgn;
  3655. add_big_inum:
  3656. inum = SCM_I_INUM (y);
  3657. if (inum == 0)
  3658. return x;
  3659. bigsgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  3660. if (inum < 0)
  3661. {
  3662. SCM result = scm_i_mkbig ();
  3663. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), - inum);
  3664. scm_remember_upto_here_1 (x);
  3665. /* we know the result will have to be a bignum */
  3666. if (bigsgn == -1)
  3667. return result;
  3668. return scm_i_normbig (result);
  3669. }
  3670. else
  3671. {
  3672. SCM result = scm_i_mkbig ();
  3673. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), inum);
  3674. scm_remember_upto_here_1 (x);
  3675. /* we know the result will have to be a bignum */
  3676. if (bigsgn == 1)
  3677. return result;
  3678. return scm_i_normbig (result);
  3679. }
  3680. }
  3681. else if (SCM_BIGP (y))
  3682. {
  3683. SCM result = scm_i_mkbig ();
  3684. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3685. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3686. mpz_add (SCM_I_BIG_MPZ (result),
  3687. SCM_I_BIG_MPZ (x),
  3688. SCM_I_BIG_MPZ (y));
  3689. scm_remember_upto_here_2 (x, y);
  3690. /* we know the result will have to be a bignum */
  3691. if (sgn_x == sgn_y)
  3692. return result;
  3693. return scm_i_normbig (result);
  3694. }
  3695. else if (SCM_REALP (y))
  3696. {
  3697. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) + SCM_REAL_VALUE (y);
  3698. scm_remember_upto_here_1 (x);
  3699. return scm_from_double (result);
  3700. }
  3701. else if (SCM_COMPLEXP (y))
  3702. {
  3703. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  3704. + SCM_COMPLEX_REAL (y));
  3705. scm_remember_upto_here_1 (x);
  3706. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  3707. }
  3708. else if (SCM_FRACTIONP (y))
  3709. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (y),
  3710. scm_product (x, SCM_FRACTION_DENOMINATOR (y))),
  3711. SCM_FRACTION_DENOMINATOR (y));
  3712. else
  3713. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3714. }
  3715. else if (SCM_REALP (x))
  3716. {
  3717. if (SCM_I_INUMP (y))
  3718. return scm_from_double (SCM_REAL_VALUE (x) + SCM_I_INUM (y));
  3719. else if (SCM_BIGP (y))
  3720. {
  3721. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) + SCM_REAL_VALUE (x);
  3722. scm_remember_upto_here_1 (y);
  3723. return scm_from_double (result);
  3724. }
  3725. else if (SCM_REALP (y))
  3726. return scm_from_double (SCM_REAL_VALUE (x) + SCM_REAL_VALUE (y));
  3727. else if (SCM_COMPLEXP (y))
  3728. return scm_c_make_rectangular (SCM_REAL_VALUE (x) + SCM_COMPLEX_REAL (y),
  3729. SCM_COMPLEX_IMAG (y));
  3730. else if (SCM_FRACTIONP (y))
  3731. return scm_from_double (SCM_REAL_VALUE (x) + scm_i_fraction2double (y));
  3732. else
  3733. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3734. }
  3735. else if (SCM_COMPLEXP (x))
  3736. {
  3737. if (SCM_I_INUMP (y))
  3738. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_I_INUM (y),
  3739. SCM_COMPLEX_IMAG (x));
  3740. else if (SCM_BIGP (y))
  3741. {
  3742. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (y))
  3743. + SCM_COMPLEX_REAL (x));
  3744. scm_remember_upto_here_1 (y);
  3745. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (x));
  3746. }
  3747. else if (SCM_REALP (y))
  3748. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_REAL_VALUE (y),
  3749. SCM_COMPLEX_IMAG (x));
  3750. else if (SCM_COMPLEXP (y))
  3751. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + SCM_COMPLEX_REAL (y),
  3752. SCM_COMPLEX_IMAG (x) + SCM_COMPLEX_IMAG (y));
  3753. else if (SCM_FRACTIONP (y))
  3754. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) + scm_i_fraction2double (y),
  3755. SCM_COMPLEX_IMAG (x));
  3756. else
  3757. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3758. }
  3759. else if (SCM_FRACTIONP (x))
  3760. {
  3761. if (SCM_I_INUMP (y))
  3762. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  3763. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  3764. SCM_FRACTION_DENOMINATOR (x));
  3765. else if (SCM_BIGP (y))
  3766. return scm_i_make_ratio (scm_sum (SCM_FRACTION_NUMERATOR (x),
  3767. scm_product (y, SCM_FRACTION_DENOMINATOR (x))),
  3768. SCM_FRACTION_DENOMINATOR (x));
  3769. else if (SCM_REALP (y))
  3770. return scm_from_double (SCM_REAL_VALUE (y) + scm_i_fraction2double (x));
  3771. else if (SCM_COMPLEXP (y))
  3772. return scm_c_make_rectangular (SCM_COMPLEX_REAL (y) + scm_i_fraction2double (x),
  3773. SCM_COMPLEX_IMAG (y));
  3774. else if (SCM_FRACTIONP (y))
  3775. /* a/b + c/d = (ad + bc) / bd */
  3776. return scm_i_make_ratio (scm_sum (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  3777. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  3778. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  3779. else
  3780. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARGn, s_sum);
  3781. }
  3782. else
  3783. SCM_WTA_DISPATCH_2 (g_sum, x, y, SCM_ARG1, s_sum);
  3784. }
  3785. SCM_DEFINE (scm_oneplus, "1+", 1, 0, 0,
  3786. (SCM x),
  3787. "Return @math{@var{x}+1}.")
  3788. #define FUNC_NAME s_scm_oneplus
  3789. {
  3790. return scm_sum (x, SCM_I_MAKINUM (1));
  3791. }
  3792. #undef FUNC_NAME
  3793. SCM_GPROC1 (s_difference, "-", scm_tc7_asubr, scm_difference, g_difference);
  3794. /* If called with one argument @var{z1}, -@var{z1} returned. Otherwise
  3795. * the sum of all but the first argument are subtracted from the first
  3796. * argument. */
  3797. #define FUNC_NAME s_difference
  3798. SCM
  3799. scm_difference (SCM x, SCM y)
  3800. {
  3801. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  3802. {
  3803. if (SCM_UNBNDP (x))
  3804. SCM_WTA_DISPATCH_0 (g_difference, s_difference);
  3805. else
  3806. if (SCM_I_INUMP (x))
  3807. {
  3808. long xx = -SCM_I_INUM (x);
  3809. if (SCM_FIXABLE (xx))
  3810. return SCM_I_MAKINUM (xx);
  3811. else
  3812. return scm_i_long2big (xx);
  3813. }
  3814. else if (SCM_BIGP (x))
  3815. /* Must scm_i_normbig here because -SCM_MOST_NEGATIVE_FIXNUM is a
  3816. bignum, but negating that gives a fixnum. */
  3817. return scm_i_normbig (scm_i_clonebig (x, 0));
  3818. else if (SCM_REALP (x))
  3819. return scm_from_double (-SCM_REAL_VALUE (x));
  3820. else if (SCM_COMPLEXP (x))
  3821. return scm_c_make_rectangular (-SCM_COMPLEX_REAL (x),
  3822. -SCM_COMPLEX_IMAG (x));
  3823. else if (SCM_FRACTIONP (x))
  3824. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x), SCM_UNDEFINED),
  3825. SCM_FRACTION_DENOMINATOR (x));
  3826. else
  3827. SCM_WTA_DISPATCH_1 (g_difference, x, SCM_ARG1, s_difference);
  3828. }
  3829. if (SCM_LIKELY (SCM_I_INUMP (x)))
  3830. {
  3831. if (SCM_LIKELY (SCM_I_INUMP (y)))
  3832. {
  3833. long int xx = SCM_I_INUM (x);
  3834. long int yy = SCM_I_INUM (y);
  3835. long int z = xx - yy;
  3836. if (SCM_FIXABLE (z))
  3837. return SCM_I_MAKINUM (z);
  3838. else
  3839. return scm_i_long2big (z);
  3840. }
  3841. else if (SCM_BIGP (y))
  3842. {
  3843. /* inum-x - big-y */
  3844. long xx = SCM_I_INUM (x);
  3845. if (xx == 0)
  3846. return scm_i_clonebig (y, 0);
  3847. else
  3848. {
  3849. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3850. SCM result = scm_i_mkbig ();
  3851. if (xx >= 0)
  3852. mpz_ui_sub (SCM_I_BIG_MPZ (result), xx, SCM_I_BIG_MPZ (y));
  3853. else
  3854. {
  3855. /* x - y == -(y + -x) */
  3856. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), -xx);
  3857. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  3858. }
  3859. scm_remember_upto_here_1 (y);
  3860. if ((xx < 0 && (sgn_y > 0)) || ((xx > 0) && sgn_y < 0))
  3861. /* we know the result will have to be a bignum */
  3862. return result;
  3863. else
  3864. return scm_i_normbig (result);
  3865. }
  3866. }
  3867. else if (SCM_REALP (y))
  3868. {
  3869. long int xx = SCM_I_INUM (x);
  3870. return scm_from_double (xx - SCM_REAL_VALUE (y));
  3871. }
  3872. else if (SCM_COMPLEXP (y))
  3873. {
  3874. long int xx = SCM_I_INUM (x);
  3875. return scm_c_make_rectangular (xx - SCM_COMPLEX_REAL (y),
  3876. - SCM_COMPLEX_IMAG (y));
  3877. }
  3878. else if (SCM_FRACTIONP (y))
  3879. /* a - b/c = (ac - b) / c */
  3880. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  3881. SCM_FRACTION_NUMERATOR (y)),
  3882. SCM_FRACTION_DENOMINATOR (y));
  3883. else
  3884. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3885. }
  3886. else if (SCM_BIGP (x))
  3887. {
  3888. if (SCM_I_INUMP (y))
  3889. {
  3890. /* big-x - inum-y */
  3891. long yy = SCM_I_INUM (y);
  3892. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3893. scm_remember_upto_here_1 (x);
  3894. if (sgn_x == 0)
  3895. return (SCM_FIXABLE (-yy) ?
  3896. SCM_I_MAKINUM (-yy) : scm_from_long (-yy));
  3897. else
  3898. {
  3899. SCM result = scm_i_mkbig ();
  3900. if (yy >= 0)
  3901. mpz_sub_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), yy);
  3902. else
  3903. mpz_add_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), -yy);
  3904. scm_remember_upto_here_1 (x);
  3905. if ((sgn_x < 0 && (yy > 0)) || ((sgn_x > 0) && yy < 0))
  3906. /* we know the result will have to be a bignum */
  3907. return result;
  3908. else
  3909. return scm_i_normbig (result);
  3910. }
  3911. }
  3912. else if (SCM_BIGP (y))
  3913. {
  3914. int sgn_x = mpz_sgn (SCM_I_BIG_MPZ (x));
  3915. int sgn_y = mpz_sgn (SCM_I_BIG_MPZ (y));
  3916. SCM result = scm_i_mkbig ();
  3917. mpz_sub (SCM_I_BIG_MPZ (result),
  3918. SCM_I_BIG_MPZ (x),
  3919. SCM_I_BIG_MPZ (y));
  3920. scm_remember_upto_here_2 (x, y);
  3921. /* we know the result will have to be a bignum */
  3922. if ((sgn_x == 1) && (sgn_y == -1))
  3923. return result;
  3924. if ((sgn_x == -1) && (sgn_y == 1))
  3925. return result;
  3926. return scm_i_normbig (result);
  3927. }
  3928. else if (SCM_REALP (y))
  3929. {
  3930. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) - SCM_REAL_VALUE (y);
  3931. scm_remember_upto_here_1 (x);
  3932. return scm_from_double (result);
  3933. }
  3934. else if (SCM_COMPLEXP (y))
  3935. {
  3936. double real_part = (mpz_get_d (SCM_I_BIG_MPZ (x))
  3937. - SCM_COMPLEX_REAL (y));
  3938. scm_remember_upto_here_1 (x);
  3939. return scm_c_make_rectangular (real_part, - SCM_COMPLEX_IMAG (y));
  3940. }
  3941. else if (SCM_FRACTIONP (y))
  3942. return scm_i_make_ratio (scm_difference (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  3943. SCM_FRACTION_NUMERATOR (y)),
  3944. SCM_FRACTION_DENOMINATOR (y));
  3945. else SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3946. }
  3947. else if (SCM_REALP (x))
  3948. {
  3949. if (SCM_I_INUMP (y))
  3950. return scm_from_double (SCM_REAL_VALUE (x) - SCM_I_INUM (y));
  3951. else if (SCM_BIGP (y))
  3952. {
  3953. double result = SCM_REAL_VALUE (x) - mpz_get_d (SCM_I_BIG_MPZ (y));
  3954. scm_remember_upto_here_1 (x);
  3955. return scm_from_double (result);
  3956. }
  3957. else if (SCM_REALP (y))
  3958. return scm_from_double (SCM_REAL_VALUE (x) - SCM_REAL_VALUE (y));
  3959. else if (SCM_COMPLEXP (y))
  3960. return scm_c_make_rectangular (SCM_REAL_VALUE (x) - SCM_COMPLEX_REAL (y),
  3961. -SCM_COMPLEX_IMAG (y));
  3962. else if (SCM_FRACTIONP (y))
  3963. return scm_from_double (SCM_REAL_VALUE (x) - scm_i_fraction2double (y));
  3964. else
  3965. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3966. }
  3967. else if (SCM_COMPLEXP (x))
  3968. {
  3969. if (SCM_I_INUMP (y))
  3970. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_I_INUM (y),
  3971. SCM_COMPLEX_IMAG (x));
  3972. else if (SCM_BIGP (y))
  3973. {
  3974. double real_part = (SCM_COMPLEX_REAL (x)
  3975. - mpz_get_d (SCM_I_BIG_MPZ (y)));
  3976. scm_remember_upto_here_1 (x);
  3977. return scm_c_make_rectangular (real_part, SCM_COMPLEX_IMAG (y));
  3978. }
  3979. else if (SCM_REALP (y))
  3980. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_REAL_VALUE (y),
  3981. SCM_COMPLEX_IMAG (x));
  3982. else if (SCM_COMPLEXP (y))
  3983. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - SCM_COMPLEX_REAL (y),
  3984. SCM_COMPLEX_IMAG (x) - SCM_COMPLEX_IMAG (y));
  3985. else if (SCM_FRACTIONP (y))
  3986. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) - scm_i_fraction2double (y),
  3987. SCM_COMPLEX_IMAG (x));
  3988. else
  3989. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  3990. }
  3991. else if (SCM_FRACTIONP (x))
  3992. {
  3993. if (SCM_I_INUMP (y))
  3994. /* a/b - c = (a - cb) / b */
  3995. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  3996. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  3997. SCM_FRACTION_DENOMINATOR (x));
  3998. else if (SCM_BIGP (y))
  3999. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (x),
  4000. scm_product(y, SCM_FRACTION_DENOMINATOR (x))),
  4001. SCM_FRACTION_DENOMINATOR (x));
  4002. else if (SCM_REALP (y))
  4003. return scm_from_double (scm_i_fraction2double (x) - SCM_REAL_VALUE (y));
  4004. else if (SCM_COMPLEXP (y))
  4005. return scm_c_make_rectangular (scm_i_fraction2double (x) - SCM_COMPLEX_REAL (y),
  4006. -SCM_COMPLEX_IMAG (y));
  4007. else if (SCM_FRACTIONP (y))
  4008. /* a/b - c/d = (ad - bc) / bd */
  4009. return scm_i_make_ratio (scm_difference (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  4010. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x))),
  4011. scm_product (SCM_FRACTION_DENOMINATOR (x), SCM_FRACTION_DENOMINATOR (y)));
  4012. else
  4013. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARGn, s_difference);
  4014. }
  4015. else
  4016. SCM_WTA_DISPATCH_2 (g_difference, x, y, SCM_ARG1, s_difference);
  4017. }
  4018. #undef FUNC_NAME
  4019. SCM_DEFINE (scm_oneminus, "1-", 1, 0, 0,
  4020. (SCM x),
  4021. "Return @math{@var{x}-1}.")
  4022. #define FUNC_NAME s_scm_oneminus
  4023. {
  4024. return scm_difference (x, SCM_I_MAKINUM (1));
  4025. }
  4026. #undef FUNC_NAME
  4027. SCM_GPROC1 (s_product, "*", scm_tc7_asubr, scm_product, g_product);
  4028. /* "Return the product of all arguments. If called without arguments,\n"
  4029. * "1 is returned."
  4030. */
  4031. SCM
  4032. scm_product (SCM x, SCM y)
  4033. {
  4034. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  4035. {
  4036. if (SCM_UNBNDP (x))
  4037. return SCM_I_MAKINUM (1L);
  4038. else if (SCM_NUMBERP (x))
  4039. return x;
  4040. else
  4041. SCM_WTA_DISPATCH_1 (g_product, x, SCM_ARG1, s_product);
  4042. }
  4043. if (SCM_LIKELY (SCM_I_INUMP (x)))
  4044. {
  4045. long xx;
  4046. intbig:
  4047. xx = SCM_I_INUM (x);
  4048. switch (xx)
  4049. {
  4050. case 0: return x; break;
  4051. case 1: return y; break;
  4052. }
  4053. if (SCM_LIKELY (SCM_I_INUMP (y)))
  4054. {
  4055. long yy = SCM_I_INUM (y);
  4056. long kk = xx * yy;
  4057. SCM k = SCM_I_MAKINUM (kk);
  4058. if ((kk == SCM_I_INUM (k)) && (kk / xx == yy))
  4059. return k;
  4060. else
  4061. {
  4062. SCM result = scm_i_long2big (xx);
  4063. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result), yy);
  4064. return scm_i_normbig (result);
  4065. }
  4066. }
  4067. else if (SCM_BIGP (y))
  4068. {
  4069. SCM result = scm_i_mkbig ();
  4070. mpz_mul_si (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (y), xx);
  4071. scm_remember_upto_here_1 (y);
  4072. return result;
  4073. }
  4074. else if (SCM_REALP (y))
  4075. return scm_from_double (xx * SCM_REAL_VALUE (y));
  4076. else if (SCM_COMPLEXP (y))
  4077. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  4078. xx * SCM_COMPLEX_IMAG (y));
  4079. else if (SCM_FRACTIONP (y))
  4080. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  4081. SCM_FRACTION_DENOMINATOR (y));
  4082. else
  4083. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4084. }
  4085. else if (SCM_BIGP (x))
  4086. {
  4087. if (SCM_I_INUMP (y))
  4088. {
  4089. SCM_SWAP (x, y);
  4090. goto intbig;
  4091. }
  4092. else if (SCM_BIGP (y))
  4093. {
  4094. SCM result = scm_i_mkbig ();
  4095. mpz_mul (SCM_I_BIG_MPZ (result),
  4096. SCM_I_BIG_MPZ (x),
  4097. SCM_I_BIG_MPZ (y));
  4098. scm_remember_upto_here_2 (x, y);
  4099. return result;
  4100. }
  4101. else if (SCM_REALP (y))
  4102. {
  4103. double result = mpz_get_d (SCM_I_BIG_MPZ (x)) * SCM_REAL_VALUE (y);
  4104. scm_remember_upto_here_1 (x);
  4105. return scm_from_double (result);
  4106. }
  4107. else if (SCM_COMPLEXP (y))
  4108. {
  4109. double z = mpz_get_d (SCM_I_BIG_MPZ (x));
  4110. scm_remember_upto_here_1 (x);
  4111. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (y),
  4112. z * SCM_COMPLEX_IMAG (y));
  4113. }
  4114. else if (SCM_FRACTIONP (y))
  4115. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_NUMERATOR (y)),
  4116. SCM_FRACTION_DENOMINATOR (y));
  4117. else
  4118. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4119. }
  4120. else if (SCM_REALP (x))
  4121. {
  4122. if (SCM_I_INUMP (y))
  4123. {
  4124. /* inexact*exact0 => exact 0, per R5RS "Exactness" section */
  4125. if (scm_is_eq (y, SCM_INUM0))
  4126. return y;
  4127. return scm_from_double (SCM_I_INUM (y) * SCM_REAL_VALUE (x));
  4128. }
  4129. else if (SCM_BIGP (y))
  4130. {
  4131. double result = mpz_get_d (SCM_I_BIG_MPZ (y)) * SCM_REAL_VALUE (x);
  4132. scm_remember_upto_here_1 (y);
  4133. return scm_from_double (result);
  4134. }
  4135. else if (SCM_REALP (y))
  4136. return scm_from_double (SCM_REAL_VALUE (x) * SCM_REAL_VALUE (y));
  4137. else if (SCM_COMPLEXP (y))
  4138. return scm_c_make_rectangular (SCM_REAL_VALUE (x) * SCM_COMPLEX_REAL (y),
  4139. SCM_REAL_VALUE (x) * SCM_COMPLEX_IMAG (y));
  4140. else if (SCM_FRACTIONP (y))
  4141. return scm_from_double (SCM_REAL_VALUE (x) * scm_i_fraction2double (y));
  4142. else
  4143. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4144. }
  4145. else if (SCM_COMPLEXP (x))
  4146. {
  4147. if (SCM_I_INUMP (y))
  4148. {
  4149. /* inexact*exact0 => exact 0, per R5RS "Exactness" section */
  4150. if (scm_is_eq (y, SCM_INUM0))
  4151. return y;
  4152. return scm_c_make_rectangular (SCM_I_INUM (y) * SCM_COMPLEX_REAL (x),
  4153. SCM_I_INUM (y) * SCM_COMPLEX_IMAG (x));
  4154. }
  4155. else if (SCM_BIGP (y))
  4156. {
  4157. double z = mpz_get_d (SCM_I_BIG_MPZ (y));
  4158. scm_remember_upto_here_1 (y);
  4159. return scm_c_make_rectangular (z * SCM_COMPLEX_REAL (x),
  4160. z * SCM_COMPLEX_IMAG (x));
  4161. }
  4162. else if (SCM_REALP (y))
  4163. return scm_c_make_rectangular (SCM_REAL_VALUE (y) * SCM_COMPLEX_REAL (x),
  4164. SCM_REAL_VALUE (y) * SCM_COMPLEX_IMAG (x));
  4165. else if (SCM_COMPLEXP (y))
  4166. {
  4167. return scm_c_make_rectangular (SCM_COMPLEX_REAL (x) * SCM_COMPLEX_REAL (y)
  4168. - SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_IMAG (y),
  4169. SCM_COMPLEX_REAL (x) * SCM_COMPLEX_IMAG (y)
  4170. + SCM_COMPLEX_IMAG (x) * SCM_COMPLEX_REAL (y));
  4171. }
  4172. else if (SCM_FRACTIONP (y))
  4173. {
  4174. double yy = scm_i_fraction2double (y);
  4175. return scm_c_make_rectangular (yy * SCM_COMPLEX_REAL (x),
  4176. yy * SCM_COMPLEX_IMAG (x));
  4177. }
  4178. else
  4179. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4180. }
  4181. else if (SCM_FRACTIONP (x))
  4182. {
  4183. if (SCM_I_INUMP (y))
  4184. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  4185. SCM_FRACTION_DENOMINATOR (x));
  4186. else if (SCM_BIGP (y))
  4187. return scm_i_make_ratio (scm_product (y, SCM_FRACTION_NUMERATOR (x)),
  4188. SCM_FRACTION_DENOMINATOR (x));
  4189. else if (SCM_REALP (y))
  4190. return scm_from_double (scm_i_fraction2double (x) * SCM_REAL_VALUE (y));
  4191. else if (SCM_COMPLEXP (y))
  4192. {
  4193. double xx = scm_i_fraction2double (x);
  4194. return scm_c_make_rectangular (xx * SCM_COMPLEX_REAL (y),
  4195. xx * SCM_COMPLEX_IMAG (y));
  4196. }
  4197. else if (SCM_FRACTIONP (y))
  4198. /* a/b * c/d = ac / bd */
  4199. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x),
  4200. SCM_FRACTION_NUMERATOR (y)),
  4201. scm_product (SCM_FRACTION_DENOMINATOR (x),
  4202. SCM_FRACTION_DENOMINATOR (y)));
  4203. else
  4204. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARGn, s_product);
  4205. }
  4206. else
  4207. SCM_WTA_DISPATCH_2 (g_product, x, y, SCM_ARG1, s_product);
  4208. }
  4209. #if ((defined (HAVE_ISINF) && defined (HAVE_ISNAN)) \
  4210. || (defined (HAVE_FINITE) && defined (HAVE_ISNAN)))
  4211. #define ALLOW_DIVIDE_BY_ZERO
  4212. /* #define ALLOW_DIVIDE_BY_EXACT_ZERO */
  4213. #endif
  4214. /* The code below for complex division is adapted from the GNU
  4215. libstdc++, which adapted it from f2c's libF77, and is subject to
  4216. this copyright: */
  4217. /****************************************************************
  4218. Copyright 1990, 1991, 1992, 1993 by AT&T Bell Laboratories and Bellcore.
  4219. Permission to use, copy, modify, and distribute this software
  4220. and its documentation for any purpose and without fee is hereby
  4221. granted, provided that the above copyright notice appear in all
  4222. copies and that both that the copyright notice and this
  4223. permission notice and warranty disclaimer appear in supporting
  4224. documentation, and that the names of AT&T Bell Laboratories or
  4225. Bellcore or any of their entities not be used in advertising or
  4226. publicity pertaining to distribution of the software without
  4227. specific, written prior permission.
  4228. AT&T and Bellcore disclaim all warranties with regard to this
  4229. software, including all implied warranties of merchantability
  4230. and fitness. In no event shall AT&T or Bellcore be liable for
  4231. any special, indirect or consequential damages or any damages
  4232. whatsoever resulting from loss of use, data or profits, whether
  4233. in an action of contract, negligence or other tortious action,
  4234. arising out of or in connection with the use or performance of
  4235. this software.
  4236. ****************************************************************/
  4237. SCM_GPROC1 (s_divide, "/", scm_tc7_asubr, scm_divide, g_divide);
  4238. /* Divide the first argument by the product of the remaining
  4239. arguments. If called with one argument @var{z1}, 1/@var{z1} is
  4240. returned. */
  4241. #define FUNC_NAME s_divide
  4242. static SCM
  4243. scm_i_divide (SCM x, SCM y, int inexact)
  4244. {
  4245. double a;
  4246. if (SCM_UNLIKELY (SCM_UNBNDP (y)))
  4247. {
  4248. if (SCM_UNBNDP (x))
  4249. SCM_WTA_DISPATCH_0 (g_divide, s_divide);
  4250. else if (SCM_I_INUMP (x))
  4251. {
  4252. long xx = SCM_I_INUM (x);
  4253. if (xx == 1 || xx == -1)
  4254. return x;
  4255. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4256. else if (xx == 0)
  4257. scm_num_overflow (s_divide);
  4258. #endif
  4259. else
  4260. {
  4261. if (inexact)
  4262. return scm_from_double (1.0 / (double) xx);
  4263. else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
  4264. }
  4265. }
  4266. else if (SCM_BIGP (x))
  4267. {
  4268. if (inexact)
  4269. return scm_from_double (1.0 / scm_i_big2dbl (x));
  4270. else return scm_i_make_ratio (SCM_I_MAKINUM(1), x);
  4271. }
  4272. else if (SCM_REALP (x))
  4273. {
  4274. double xx = SCM_REAL_VALUE (x);
  4275. #ifndef ALLOW_DIVIDE_BY_ZERO
  4276. if (xx == 0.0)
  4277. scm_num_overflow (s_divide);
  4278. else
  4279. #endif
  4280. return scm_from_double (1.0 / xx);
  4281. }
  4282. else if (SCM_COMPLEXP (x))
  4283. {
  4284. double r = SCM_COMPLEX_REAL (x);
  4285. double i = SCM_COMPLEX_IMAG (x);
  4286. if (fabs(r) <= fabs(i))
  4287. {
  4288. double t = r / i;
  4289. double d = i * (1.0 + t * t);
  4290. return scm_c_make_rectangular (t / d, -1.0 / d);
  4291. }
  4292. else
  4293. {
  4294. double t = i / r;
  4295. double d = r * (1.0 + t * t);
  4296. return scm_c_make_rectangular (1.0 / d, -t / d);
  4297. }
  4298. }
  4299. else if (SCM_FRACTIONP (x))
  4300. return scm_i_make_ratio (SCM_FRACTION_DENOMINATOR (x),
  4301. SCM_FRACTION_NUMERATOR (x));
  4302. else
  4303. SCM_WTA_DISPATCH_1 (g_divide, x, SCM_ARG1, s_divide);
  4304. }
  4305. if (SCM_LIKELY (SCM_I_INUMP (x)))
  4306. {
  4307. long xx = SCM_I_INUM (x);
  4308. if (SCM_LIKELY (SCM_I_INUMP (y)))
  4309. {
  4310. long yy = SCM_I_INUM (y);
  4311. if (yy == 0)
  4312. {
  4313. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4314. scm_num_overflow (s_divide);
  4315. #else
  4316. return scm_from_double ((double) xx / (double) yy);
  4317. #endif
  4318. }
  4319. else if (xx % yy != 0)
  4320. {
  4321. if (inexact)
  4322. return scm_from_double ((double) xx / (double) yy);
  4323. else return scm_i_make_ratio (x, y);
  4324. }
  4325. else
  4326. {
  4327. long z = xx / yy;
  4328. if (SCM_FIXABLE (z))
  4329. return SCM_I_MAKINUM (z);
  4330. else
  4331. return scm_i_long2big (z);
  4332. }
  4333. }
  4334. else if (SCM_BIGP (y))
  4335. {
  4336. if (inexact)
  4337. return scm_from_double ((double) xx / scm_i_big2dbl (y));
  4338. else return scm_i_make_ratio (x, y);
  4339. }
  4340. else if (SCM_REALP (y))
  4341. {
  4342. double yy = SCM_REAL_VALUE (y);
  4343. #ifndef ALLOW_DIVIDE_BY_ZERO
  4344. if (yy == 0.0)
  4345. scm_num_overflow (s_divide);
  4346. else
  4347. #endif
  4348. return scm_from_double ((double) xx / yy);
  4349. }
  4350. else if (SCM_COMPLEXP (y))
  4351. {
  4352. a = xx;
  4353. complex_div: /* y _must_ be a complex number */
  4354. {
  4355. double r = SCM_COMPLEX_REAL (y);
  4356. double i = SCM_COMPLEX_IMAG (y);
  4357. if (fabs(r) <= fabs(i))
  4358. {
  4359. double t = r / i;
  4360. double d = i * (1.0 + t * t);
  4361. return scm_c_make_rectangular ((a * t) / d, -a / d);
  4362. }
  4363. else
  4364. {
  4365. double t = i / r;
  4366. double d = r * (1.0 + t * t);
  4367. return scm_c_make_rectangular (a / d, -(a * t) / d);
  4368. }
  4369. }
  4370. }
  4371. else if (SCM_FRACTIONP (y))
  4372. /* a / b/c = ac / b */
  4373. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4374. SCM_FRACTION_NUMERATOR (y));
  4375. else
  4376. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4377. }
  4378. else if (SCM_BIGP (x))
  4379. {
  4380. if (SCM_I_INUMP (y))
  4381. {
  4382. long int yy = SCM_I_INUM (y);
  4383. if (yy == 0)
  4384. {
  4385. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4386. scm_num_overflow (s_divide);
  4387. #else
  4388. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  4389. scm_remember_upto_here_1 (x);
  4390. return (sgn == 0) ? scm_nan () : scm_inf ();
  4391. #endif
  4392. }
  4393. else if (yy == 1)
  4394. return x;
  4395. else
  4396. {
  4397. /* FIXME: HMM, what are the relative performance issues here?
  4398. We need to test. Is it faster on average to test
  4399. divisible_p, then perform whichever operation, or is it
  4400. faster to perform the integer div opportunistically and
  4401. switch to real if there's a remainder? For now we take the
  4402. middle ground: test, then if divisible, use the faster div
  4403. func. */
  4404. long abs_yy = yy < 0 ? -yy : yy;
  4405. int divisible_p = mpz_divisible_ui_p (SCM_I_BIG_MPZ (x), abs_yy);
  4406. if (divisible_p)
  4407. {
  4408. SCM result = scm_i_mkbig ();
  4409. mpz_divexact_ui (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (x), abs_yy);
  4410. scm_remember_upto_here_1 (x);
  4411. if (yy < 0)
  4412. mpz_neg (SCM_I_BIG_MPZ (result), SCM_I_BIG_MPZ (result));
  4413. return scm_i_normbig (result);
  4414. }
  4415. else
  4416. {
  4417. if (inexact)
  4418. return scm_from_double (scm_i_big2dbl (x) / (double) yy);
  4419. else return scm_i_make_ratio (x, y);
  4420. }
  4421. }
  4422. }
  4423. else if (SCM_BIGP (y))
  4424. {
  4425. int y_is_zero = (mpz_sgn (SCM_I_BIG_MPZ (y)) == 0);
  4426. if (y_is_zero)
  4427. {
  4428. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4429. scm_num_overflow (s_divide);
  4430. #else
  4431. int sgn = mpz_sgn (SCM_I_BIG_MPZ (x));
  4432. scm_remember_upto_here_1 (x);
  4433. return (sgn == 0) ? scm_nan () : scm_inf ();
  4434. #endif
  4435. }
  4436. else
  4437. {
  4438. /* big_x / big_y */
  4439. if (inexact)
  4440. {
  4441. /* It's easily possible for the ratio x/y to fit a double
  4442. but one or both x and y be too big to fit a double,
  4443. hence the use of mpq_get_d rather than converting and
  4444. dividing. */
  4445. mpq_t q;
  4446. *mpq_numref(q) = *SCM_I_BIG_MPZ (x);
  4447. *mpq_denref(q) = *SCM_I_BIG_MPZ (y);
  4448. return scm_from_double (mpq_get_d (q));
  4449. }
  4450. else
  4451. {
  4452. int divisible_p = mpz_divisible_p (SCM_I_BIG_MPZ (x),
  4453. SCM_I_BIG_MPZ (y));
  4454. if (divisible_p)
  4455. {
  4456. SCM result = scm_i_mkbig ();
  4457. mpz_divexact (SCM_I_BIG_MPZ (result),
  4458. SCM_I_BIG_MPZ (x),
  4459. SCM_I_BIG_MPZ (y));
  4460. scm_remember_upto_here_2 (x, y);
  4461. return scm_i_normbig (result);
  4462. }
  4463. else
  4464. return scm_i_make_ratio (x, y);
  4465. }
  4466. }
  4467. }
  4468. else if (SCM_REALP (y))
  4469. {
  4470. double yy = SCM_REAL_VALUE (y);
  4471. #ifndef ALLOW_DIVIDE_BY_ZERO
  4472. if (yy == 0.0)
  4473. scm_num_overflow (s_divide);
  4474. else
  4475. #endif
  4476. return scm_from_double (scm_i_big2dbl (x) / yy);
  4477. }
  4478. else if (SCM_COMPLEXP (y))
  4479. {
  4480. a = scm_i_big2dbl (x);
  4481. goto complex_div;
  4482. }
  4483. else if (SCM_FRACTIONP (y))
  4484. return scm_i_make_ratio (scm_product (x, SCM_FRACTION_DENOMINATOR (y)),
  4485. SCM_FRACTION_NUMERATOR (y));
  4486. else
  4487. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4488. }
  4489. else if (SCM_REALP (x))
  4490. {
  4491. double rx = SCM_REAL_VALUE (x);
  4492. if (SCM_I_INUMP (y))
  4493. {
  4494. long int yy = SCM_I_INUM (y);
  4495. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4496. if (yy == 0)
  4497. scm_num_overflow (s_divide);
  4498. else
  4499. #endif
  4500. return scm_from_double (rx / (double) yy);
  4501. }
  4502. else if (SCM_BIGP (y))
  4503. {
  4504. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  4505. scm_remember_upto_here_1 (y);
  4506. return scm_from_double (rx / dby);
  4507. }
  4508. else if (SCM_REALP (y))
  4509. {
  4510. double yy = SCM_REAL_VALUE (y);
  4511. #ifndef ALLOW_DIVIDE_BY_ZERO
  4512. if (yy == 0.0)
  4513. scm_num_overflow (s_divide);
  4514. else
  4515. #endif
  4516. return scm_from_double (rx / yy);
  4517. }
  4518. else if (SCM_COMPLEXP (y))
  4519. {
  4520. a = rx;
  4521. goto complex_div;
  4522. }
  4523. else if (SCM_FRACTIONP (y))
  4524. return scm_from_double (rx / scm_i_fraction2double (y));
  4525. else
  4526. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4527. }
  4528. else if (SCM_COMPLEXP (x))
  4529. {
  4530. double rx = SCM_COMPLEX_REAL (x);
  4531. double ix = SCM_COMPLEX_IMAG (x);
  4532. if (SCM_I_INUMP (y))
  4533. {
  4534. long int yy = SCM_I_INUM (y);
  4535. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4536. if (yy == 0)
  4537. scm_num_overflow (s_divide);
  4538. else
  4539. #endif
  4540. {
  4541. double d = yy;
  4542. return scm_c_make_rectangular (rx / d, ix / d);
  4543. }
  4544. }
  4545. else if (SCM_BIGP (y))
  4546. {
  4547. double dby = mpz_get_d (SCM_I_BIG_MPZ (y));
  4548. scm_remember_upto_here_1 (y);
  4549. return scm_c_make_rectangular (rx / dby, ix / dby);
  4550. }
  4551. else if (SCM_REALP (y))
  4552. {
  4553. double yy = SCM_REAL_VALUE (y);
  4554. #ifndef ALLOW_DIVIDE_BY_ZERO
  4555. if (yy == 0.0)
  4556. scm_num_overflow (s_divide);
  4557. else
  4558. #endif
  4559. return scm_c_make_rectangular (rx / yy, ix / yy);
  4560. }
  4561. else if (SCM_COMPLEXP (y))
  4562. {
  4563. double ry = SCM_COMPLEX_REAL (y);
  4564. double iy = SCM_COMPLEX_IMAG (y);
  4565. if (fabs(ry) <= fabs(iy))
  4566. {
  4567. double t = ry / iy;
  4568. double d = iy * (1.0 + t * t);
  4569. return scm_c_make_rectangular ((rx * t + ix) / d, (ix * t - rx) / d);
  4570. }
  4571. else
  4572. {
  4573. double t = iy / ry;
  4574. double d = ry * (1.0 + t * t);
  4575. return scm_c_make_rectangular ((rx + ix * t) / d, (ix - rx * t) / d);
  4576. }
  4577. }
  4578. else if (SCM_FRACTIONP (y))
  4579. {
  4580. double yy = scm_i_fraction2double (y);
  4581. return scm_c_make_rectangular (rx / yy, ix / yy);
  4582. }
  4583. else
  4584. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4585. }
  4586. else if (SCM_FRACTIONP (x))
  4587. {
  4588. if (SCM_I_INUMP (y))
  4589. {
  4590. long int yy = SCM_I_INUM (y);
  4591. #ifndef ALLOW_DIVIDE_BY_EXACT_ZERO
  4592. if (yy == 0)
  4593. scm_num_overflow (s_divide);
  4594. else
  4595. #endif
  4596. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  4597. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  4598. }
  4599. else if (SCM_BIGP (y))
  4600. {
  4601. return scm_i_make_ratio (SCM_FRACTION_NUMERATOR (x),
  4602. scm_product (SCM_FRACTION_DENOMINATOR (x), y));
  4603. }
  4604. else if (SCM_REALP (y))
  4605. {
  4606. double yy = SCM_REAL_VALUE (y);
  4607. #ifndef ALLOW_DIVIDE_BY_ZERO
  4608. if (yy == 0.0)
  4609. scm_num_overflow (s_divide);
  4610. else
  4611. #endif
  4612. return scm_from_double (scm_i_fraction2double (x) / yy);
  4613. }
  4614. else if (SCM_COMPLEXP (y))
  4615. {
  4616. a = scm_i_fraction2double (x);
  4617. goto complex_div;
  4618. }
  4619. else if (SCM_FRACTIONP (y))
  4620. return scm_i_make_ratio (scm_product (SCM_FRACTION_NUMERATOR (x), SCM_FRACTION_DENOMINATOR (y)),
  4621. scm_product (SCM_FRACTION_NUMERATOR (y), SCM_FRACTION_DENOMINATOR (x)));
  4622. else
  4623. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARGn, s_divide);
  4624. }
  4625. else
  4626. SCM_WTA_DISPATCH_2 (g_divide, x, y, SCM_ARG1, s_divide);
  4627. }
  4628. SCM
  4629. scm_divide (SCM x, SCM y)
  4630. {
  4631. return scm_i_divide (x, y, 0);
  4632. }
  4633. static SCM scm_divide2real (SCM x, SCM y)
  4634. {
  4635. return scm_i_divide (x, y, 1);
  4636. }
  4637. #undef FUNC_NAME
  4638. double
  4639. scm_asinh (double x)
  4640. {
  4641. #if HAVE_ASINH
  4642. return asinh (x);
  4643. #else
  4644. #define asinh scm_asinh
  4645. return log (x + sqrt (x * x + 1));
  4646. #endif
  4647. }
  4648. SCM_GPROC1 (s_asinh, "$asinh", scm_tc7_dsubr, (SCM (*)()) asinh, g_asinh);
  4649. /* "Return the inverse hyperbolic sine of @var{x}."
  4650. */
  4651. double
  4652. scm_acosh (double x)
  4653. {
  4654. #if HAVE_ACOSH
  4655. return acosh (x);
  4656. #else
  4657. #define acosh scm_acosh
  4658. return log (x + sqrt (x * x - 1));
  4659. #endif
  4660. }
  4661. SCM_GPROC1 (s_acosh, "$acosh", scm_tc7_dsubr, (SCM (*)()) acosh, g_acosh);
  4662. /* "Return the inverse hyperbolic cosine of @var{x}."
  4663. */
  4664. double
  4665. scm_atanh (double x)
  4666. {
  4667. #if HAVE_ATANH
  4668. return atanh (x);
  4669. #else
  4670. #define atanh scm_atanh
  4671. return 0.5 * log ((1 + x) / (1 - x));
  4672. #endif
  4673. }
  4674. SCM_GPROC1 (s_atanh, "$atanh", scm_tc7_dsubr, (SCM (*)()) atanh, g_atanh);
  4675. /* "Return the inverse hyperbolic tangent of @var{x}."
  4676. */
  4677. double
  4678. scm_c_truncate (double x)
  4679. {
  4680. #if HAVE_TRUNC
  4681. return trunc (x);
  4682. #else
  4683. if (x < 0.0)
  4684. return -floor (-x);
  4685. return floor (x);
  4686. #endif
  4687. }
  4688. /* scm_c_round is done using floor(x+0.5) to round to nearest and with
  4689. half-way case (ie. when x is an integer plus 0.5) going upwards.
  4690. Then half-way cases are identified and adjusted down if the
  4691. round-upwards didn't give the desired even integer.
  4692. "plus_half == result" identifies a half-way case. If plus_half, which is
  4693. x + 0.5, is an integer then x must be an integer plus 0.5.
  4694. An odd "result" value is identified with result/2 != floor(result/2).
  4695. This is done with plus_half, since that value is ready for use sooner in
  4696. a pipelined cpu, and we're already requiring plus_half == result.
  4697. Note however that we need to be careful when x is big and already an
  4698. integer. In that case "x+0.5" may round to an adjacent integer, causing
  4699. us to return such a value, incorrectly. For instance if the hardware is
  4700. in the usual default nearest-even rounding, then for x = 0x1FFFFFFFFFFFFF
  4701. (ie. 53 one bits) we will have x+0.5 = 0x20000000000000 and that value
  4702. returned. Or if the hardware is in round-upwards mode, then other bigger
  4703. values like say x == 2^128 will see x+0.5 rounding up to the next higher
  4704. representable value, 2^128+2^76 (or whatever), again incorrect.
  4705. These bad roundings of x+0.5 are avoided by testing at the start whether
  4706. x is already an integer. If it is then clearly that's the desired result
  4707. already. And if it's not then the exponent must be small enough to allow
  4708. an 0.5 to be represented, and hence added without a bad rounding. */
  4709. double
  4710. scm_c_round (double x)
  4711. {
  4712. double plus_half, result;
  4713. if (x == floor (x))
  4714. return x;
  4715. plus_half = x + 0.5;
  4716. result = floor (plus_half);
  4717. /* Adjust so that the rounding is towards even. */
  4718. return ((plus_half == result && plus_half / 2 != floor (plus_half / 2))
  4719. ? result - 1
  4720. : result);
  4721. }
  4722. SCM_DEFINE (scm_truncate_number, "truncate", 1, 0, 0,
  4723. (SCM x),
  4724. "Round the number @var{x} towards zero.")
  4725. #define FUNC_NAME s_scm_truncate_number
  4726. {
  4727. if (scm_is_false (scm_negative_p (x)))
  4728. return scm_floor (x);
  4729. else
  4730. return scm_ceiling (x);
  4731. }
  4732. #undef FUNC_NAME
  4733. static SCM exactly_one_half;
  4734. SCM_DEFINE (scm_round_number, "round", 1, 0, 0,
  4735. (SCM x),
  4736. "Round the number @var{x} towards the nearest integer. "
  4737. "When it is exactly halfway between two integers, "
  4738. "round towards the even one.")
  4739. #define FUNC_NAME s_scm_round_number
  4740. {
  4741. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4742. return x;
  4743. else if (SCM_REALP (x))
  4744. return scm_from_double (scm_c_round (SCM_REAL_VALUE (x)));
  4745. else
  4746. {
  4747. /* OPTIMIZE-ME: Fraction case could be done more efficiently by a
  4748. single quotient+remainder division then examining to see which way
  4749. the rounding should go. */
  4750. SCM plus_half = scm_sum (x, exactly_one_half);
  4751. SCM result = scm_floor (plus_half);
  4752. /* Adjust so that the rounding is towards even. */
  4753. if (scm_is_true (scm_num_eq_p (plus_half, result))
  4754. && scm_is_true (scm_odd_p (result)))
  4755. return scm_difference (result, SCM_I_MAKINUM (1));
  4756. else
  4757. return result;
  4758. }
  4759. }
  4760. #undef FUNC_NAME
  4761. SCM_PRIMITIVE_GENERIC (scm_floor, "floor", 1, 0, 0,
  4762. (SCM x),
  4763. "Round the number @var{x} towards minus infinity.")
  4764. #define FUNC_NAME s_scm_floor
  4765. {
  4766. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4767. return x;
  4768. else if (SCM_REALP (x))
  4769. return scm_from_double (floor (SCM_REAL_VALUE (x)));
  4770. else if (SCM_FRACTIONP (x))
  4771. {
  4772. SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
  4773. SCM_FRACTION_DENOMINATOR (x));
  4774. if (scm_is_false (scm_negative_p (x)))
  4775. {
  4776. /* For positive x, rounding towards zero is correct. */
  4777. return q;
  4778. }
  4779. else
  4780. {
  4781. /* For negative x, we need to return q-1 unless x is an
  4782. integer. But fractions are never integer, per our
  4783. assumptions. */
  4784. return scm_difference (q, SCM_I_MAKINUM (1));
  4785. }
  4786. }
  4787. else
  4788. SCM_WTA_DISPATCH_1 (g_scm_floor, x, 1, s_scm_floor);
  4789. }
  4790. #undef FUNC_NAME
  4791. SCM_PRIMITIVE_GENERIC (scm_ceiling, "ceiling", 1, 0, 0,
  4792. (SCM x),
  4793. "Round the number @var{x} towards infinity.")
  4794. #define FUNC_NAME s_scm_ceiling
  4795. {
  4796. if (SCM_I_INUMP (x) || SCM_BIGP (x))
  4797. return x;
  4798. else if (SCM_REALP (x))
  4799. return scm_from_double (ceil (SCM_REAL_VALUE (x)));
  4800. else if (SCM_FRACTIONP (x))
  4801. {
  4802. SCM q = scm_quotient (SCM_FRACTION_NUMERATOR (x),
  4803. SCM_FRACTION_DENOMINATOR (x));
  4804. if (scm_is_false (scm_positive_p (x)))
  4805. {
  4806. /* For negative x, rounding towards zero is correct. */
  4807. return q;
  4808. }
  4809. else
  4810. {
  4811. /* For positive x, we need to return q+1 unless x is an
  4812. integer. But fractions are never integer, per our
  4813. assumptions. */
  4814. return scm_sum (q, SCM_I_MAKINUM (1));
  4815. }
  4816. }
  4817. else
  4818. SCM_WTA_DISPATCH_1 (g_scm_ceiling, x, 1, s_scm_ceiling);
  4819. }
  4820. #undef FUNC_NAME
  4821. SCM_GPROC1 (s_i_sqrt, "$sqrt", scm_tc7_dsubr, (SCM (*)()) sqrt, g_i_sqrt);
  4822. /* "Return the square root of the real number @var{x}."
  4823. */
  4824. SCM_GPROC1 (s_i_abs, "$abs", scm_tc7_dsubr, (SCM (*)()) fabs, g_i_abs);
  4825. /* "Return the absolute value of the real number @var{x}."
  4826. */
  4827. SCM_GPROC1 (s_i_exp, "$exp", scm_tc7_dsubr, (SCM (*)()) exp, g_i_exp);
  4828. /* "Return the @var{x}th power of e."
  4829. */
  4830. SCM_GPROC1 (s_i_log, "$log", scm_tc7_dsubr, (SCM (*)()) log, g_i_log);
  4831. /* "Return the natural logarithm of the real number @var{x}."
  4832. */
  4833. SCM_GPROC1 (s_i_sin, "$sin", scm_tc7_dsubr, (SCM (*)()) sin, g_i_sin);
  4834. /* "Return the sine of the real number @var{x}."
  4835. */
  4836. SCM_GPROC1 (s_i_cos, "$cos", scm_tc7_dsubr, (SCM (*)()) cos, g_i_cos);
  4837. /* "Return the cosine of the real number @var{x}."
  4838. */
  4839. SCM_GPROC1 (s_i_tan, "$tan", scm_tc7_dsubr, (SCM (*)()) tan, g_i_tan);
  4840. /* "Return the tangent of the real number @var{x}."
  4841. */
  4842. SCM_GPROC1 (s_i_asin, "$asin", scm_tc7_dsubr, (SCM (*)()) asin, g_i_asin);
  4843. /* "Return the arc sine of the real number @var{x}."
  4844. */
  4845. SCM_GPROC1 (s_i_acos, "$acos", scm_tc7_dsubr, (SCM (*)()) acos, g_i_acos);
  4846. /* "Return the arc cosine of the real number @var{x}."
  4847. */
  4848. SCM_GPROC1 (s_i_atan, "$atan", scm_tc7_dsubr, (SCM (*)()) atan, g_i_atan);
  4849. /* "Return the arc tangent of the real number @var{x}."
  4850. */
  4851. SCM_GPROC1 (s_i_sinh, "$sinh", scm_tc7_dsubr, (SCM (*)()) sinh, g_i_sinh);
  4852. /* "Return the hyperbolic sine of the real number @var{x}."
  4853. */
  4854. SCM_GPROC1 (s_i_cosh, "$cosh", scm_tc7_dsubr, (SCM (*)()) cosh, g_i_cosh);
  4855. /* "Return the hyperbolic cosine of the real number @var{x}."
  4856. */
  4857. SCM_GPROC1 (s_i_tanh, "$tanh", scm_tc7_dsubr, (SCM (*)()) tanh, g_i_tanh);
  4858. /* "Return the hyperbolic tangent of the real number @var{x}."
  4859. */
  4860. struct dpair
  4861. {
  4862. double x, y;
  4863. };
  4864. static void scm_two_doubles (SCM x,
  4865. SCM y,
  4866. const char *sstring,
  4867. struct dpair * xy);
  4868. static void
  4869. scm_two_doubles (SCM x, SCM y, const char *sstring, struct dpair *xy)
  4870. {
  4871. if (SCM_I_INUMP (x))
  4872. xy->x = SCM_I_INUM (x);
  4873. else if (SCM_BIGP (x))
  4874. xy->x = scm_i_big2dbl (x);
  4875. else if (SCM_REALP (x))
  4876. xy->x = SCM_REAL_VALUE (x);
  4877. else if (SCM_FRACTIONP (x))
  4878. xy->x = scm_i_fraction2double (x);
  4879. else
  4880. scm_wrong_type_arg (sstring, SCM_ARG1, x);
  4881. if (SCM_I_INUMP (y))
  4882. xy->y = SCM_I_INUM (y);
  4883. else if (SCM_BIGP (y))
  4884. xy->y = scm_i_big2dbl (y);
  4885. else if (SCM_REALP (y))
  4886. xy->y = SCM_REAL_VALUE (y);
  4887. else if (SCM_FRACTIONP (y))
  4888. xy->y = scm_i_fraction2double (y);
  4889. else
  4890. scm_wrong_type_arg (sstring, SCM_ARG2, y);
  4891. }
  4892. SCM_DEFINE (scm_sys_expt, "$expt", 2, 0, 0,
  4893. (SCM x, SCM y),
  4894. "Return @var{x} raised to the power of @var{y}. This\n"
  4895. "procedure does not accept complex arguments.")
  4896. #define FUNC_NAME s_scm_sys_expt
  4897. {
  4898. struct dpair xy;
  4899. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4900. return scm_from_double (pow (xy.x, xy.y));
  4901. }
  4902. #undef FUNC_NAME
  4903. SCM_DEFINE (scm_sys_atan2, "$atan2", 2, 0, 0,
  4904. (SCM x, SCM y),
  4905. "Return the arc tangent of the two arguments @var{x} and\n"
  4906. "@var{y}. This is similar to calculating the arc tangent of\n"
  4907. "@var{x} / @var{y}, except that the signs of both arguments\n"
  4908. "are used to determine the quadrant of the result. This\n"
  4909. "procedure does not accept complex arguments.")
  4910. #define FUNC_NAME s_scm_sys_atan2
  4911. {
  4912. struct dpair xy;
  4913. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4914. return scm_from_double (atan2 (xy.x, xy.y));
  4915. }
  4916. #undef FUNC_NAME
  4917. SCM
  4918. scm_c_make_rectangular (double re, double im)
  4919. {
  4920. if (im == 0.0)
  4921. return scm_from_double (re);
  4922. else
  4923. {
  4924. SCM z;
  4925. SCM_NEWSMOB (z, scm_tc16_complex, scm_gc_malloc (sizeof (scm_t_complex),
  4926. "complex"));
  4927. SCM_COMPLEX_REAL (z) = re;
  4928. SCM_COMPLEX_IMAG (z) = im;
  4929. return z;
  4930. }
  4931. }
  4932. SCM_DEFINE (scm_make_rectangular, "make-rectangular", 2, 0, 0,
  4933. (SCM real_part, SCM imaginary_part),
  4934. "Return a complex number constructed of the given @var{real-part} "
  4935. "and @var{imaginary-part} parts.")
  4936. #define FUNC_NAME s_scm_make_rectangular
  4937. {
  4938. struct dpair xy;
  4939. scm_two_doubles (real_part, imaginary_part, FUNC_NAME, &xy);
  4940. return scm_c_make_rectangular (xy.x, xy.y);
  4941. }
  4942. #undef FUNC_NAME
  4943. SCM
  4944. scm_c_make_polar (double mag, double ang)
  4945. {
  4946. double s, c;
  4947. /* The sincos(3) function is undocumented an broken on Tru64. Thus we only
  4948. use it on Glibc-based systems that have it (it's a GNU extension). See
  4949. http://lists.gnu.org/archive/html/guile-user/2009-04/msg00033.html for
  4950. details. */
  4951. #if (defined HAVE_SINCOS) && (defined __GLIBC__) && (defined _GNU_SOURCE)
  4952. sincos (ang, &s, &c);
  4953. #else
  4954. s = sin (ang);
  4955. c = cos (ang);
  4956. #endif
  4957. return scm_c_make_rectangular (mag * c, mag * s);
  4958. }
  4959. SCM_DEFINE (scm_make_polar, "make-polar", 2, 0, 0,
  4960. (SCM x, SCM y),
  4961. "Return the complex number @var{x} * e^(i * @var{y}).")
  4962. #define FUNC_NAME s_scm_make_polar
  4963. {
  4964. struct dpair xy;
  4965. scm_two_doubles (x, y, FUNC_NAME, &xy);
  4966. return scm_c_make_polar (xy.x, xy.y);
  4967. }
  4968. #undef FUNC_NAME
  4969. SCM_GPROC (s_real_part, "real-part", 1, 0, 0, scm_real_part, g_real_part);
  4970. /* "Return the real part of the number @var{z}."
  4971. */
  4972. SCM
  4973. scm_real_part (SCM z)
  4974. {
  4975. if (SCM_I_INUMP (z))
  4976. return z;
  4977. else if (SCM_BIGP (z))
  4978. return z;
  4979. else if (SCM_REALP (z))
  4980. return z;
  4981. else if (SCM_COMPLEXP (z))
  4982. return scm_from_double (SCM_COMPLEX_REAL (z));
  4983. else if (SCM_FRACTIONP (z))
  4984. return z;
  4985. else
  4986. SCM_WTA_DISPATCH_1 (g_real_part, z, SCM_ARG1, s_real_part);
  4987. }
  4988. SCM_GPROC (s_imag_part, "imag-part", 1, 0, 0, scm_imag_part, g_imag_part);
  4989. /* "Return the imaginary part of the number @var{z}."
  4990. */
  4991. SCM
  4992. scm_imag_part (SCM z)
  4993. {
  4994. if (SCM_I_INUMP (z))
  4995. return SCM_INUM0;
  4996. else if (SCM_BIGP (z))
  4997. return SCM_INUM0;
  4998. else if (SCM_REALP (z))
  4999. return scm_flo0;
  5000. else if (SCM_COMPLEXP (z))
  5001. return scm_from_double (SCM_COMPLEX_IMAG (z));
  5002. else if (SCM_FRACTIONP (z))
  5003. return SCM_INUM0;
  5004. else
  5005. SCM_WTA_DISPATCH_1 (g_imag_part, z, SCM_ARG1, s_imag_part);
  5006. }
  5007. SCM_GPROC (s_numerator, "numerator", 1, 0, 0, scm_numerator, g_numerator);
  5008. /* "Return the numerator of the number @var{z}."
  5009. */
  5010. SCM
  5011. scm_numerator (SCM z)
  5012. {
  5013. if (SCM_I_INUMP (z))
  5014. return z;
  5015. else if (SCM_BIGP (z))
  5016. return z;
  5017. else if (SCM_FRACTIONP (z))
  5018. return SCM_FRACTION_NUMERATOR (z);
  5019. else if (SCM_REALP (z))
  5020. return scm_exact_to_inexact (scm_numerator (scm_inexact_to_exact (z)));
  5021. else
  5022. SCM_WTA_DISPATCH_1 (g_numerator, z, SCM_ARG1, s_numerator);
  5023. }
  5024. SCM_GPROC (s_denominator, "denominator", 1, 0, 0, scm_denominator, g_denominator);
  5025. /* "Return the denominator of the number @var{z}."
  5026. */
  5027. SCM
  5028. scm_denominator (SCM z)
  5029. {
  5030. if (SCM_I_INUMP (z))
  5031. return SCM_I_MAKINUM (1);
  5032. else if (SCM_BIGP (z))
  5033. return SCM_I_MAKINUM (1);
  5034. else if (SCM_FRACTIONP (z))
  5035. return SCM_FRACTION_DENOMINATOR (z);
  5036. else if (SCM_REALP (z))
  5037. return scm_exact_to_inexact (scm_denominator (scm_inexact_to_exact (z)));
  5038. else
  5039. SCM_WTA_DISPATCH_1 (g_denominator, z, SCM_ARG1, s_denominator);
  5040. }
  5041. SCM_GPROC (s_magnitude, "magnitude", 1, 0, 0, scm_magnitude, g_magnitude);
  5042. /* "Return the magnitude of the number @var{z}. This is the same as\n"
  5043. * "@code{abs} for real arguments, but also allows complex numbers."
  5044. */
  5045. SCM
  5046. scm_magnitude (SCM z)
  5047. {
  5048. if (SCM_I_INUMP (z))
  5049. {
  5050. long int zz = SCM_I_INUM (z);
  5051. if (zz >= 0)
  5052. return z;
  5053. else if (SCM_POSFIXABLE (-zz))
  5054. return SCM_I_MAKINUM (-zz);
  5055. else
  5056. return scm_i_long2big (-zz);
  5057. }
  5058. else if (SCM_BIGP (z))
  5059. {
  5060. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  5061. scm_remember_upto_here_1 (z);
  5062. if (sgn < 0)
  5063. return scm_i_clonebig (z, 0);
  5064. else
  5065. return z;
  5066. }
  5067. else if (SCM_REALP (z))
  5068. return scm_from_double (fabs (SCM_REAL_VALUE (z)));
  5069. else if (SCM_COMPLEXP (z))
  5070. return scm_from_double (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
  5071. else if (SCM_FRACTIONP (z))
  5072. {
  5073. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  5074. return z;
  5075. return scm_i_make_ratio (scm_difference (SCM_FRACTION_NUMERATOR (z), SCM_UNDEFINED),
  5076. SCM_FRACTION_DENOMINATOR (z));
  5077. }
  5078. else
  5079. SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
  5080. }
  5081. SCM_GPROC (s_angle, "angle", 1, 0, 0, scm_angle, g_angle);
  5082. /* "Return the angle of the complex number @var{z}."
  5083. */
  5084. SCM
  5085. scm_angle (SCM z)
  5086. {
  5087. /* atan(0,-1) is pi and it'd be possible to have that as a constant like
  5088. scm_flo0 to save allocating a new flonum with scm_from_double each time.
  5089. But if atan2 follows the floating point rounding mode, then the value
  5090. is not a constant. Maybe it'd be close enough though. */
  5091. if (SCM_I_INUMP (z))
  5092. {
  5093. if (SCM_I_INUM (z) >= 0)
  5094. return scm_flo0;
  5095. else
  5096. return scm_from_double (atan2 (0.0, -1.0));
  5097. }
  5098. else if (SCM_BIGP (z))
  5099. {
  5100. int sgn = mpz_sgn (SCM_I_BIG_MPZ (z));
  5101. scm_remember_upto_here_1 (z);
  5102. if (sgn < 0)
  5103. return scm_from_double (atan2 (0.0, -1.0));
  5104. else
  5105. return scm_flo0;
  5106. }
  5107. else if (SCM_REALP (z))
  5108. {
  5109. if (SCM_REAL_VALUE (z) >= 0)
  5110. return scm_flo0;
  5111. else
  5112. return scm_from_double (atan2 (0.0, -1.0));
  5113. }
  5114. else if (SCM_COMPLEXP (z))
  5115. return scm_from_double (atan2 (SCM_COMPLEX_IMAG (z), SCM_COMPLEX_REAL (z)));
  5116. else if (SCM_FRACTIONP (z))
  5117. {
  5118. if (scm_is_false (scm_negative_p (SCM_FRACTION_NUMERATOR (z))))
  5119. return scm_flo0;
  5120. else return scm_from_double (atan2 (0.0, -1.0));
  5121. }
  5122. else
  5123. SCM_WTA_DISPATCH_1 (g_angle, z, SCM_ARG1, s_angle);
  5124. }
  5125. SCM_GPROC (s_exact_to_inexact, "exact->inexact", 1, 0, 0, scm_exact_to_inexact, g_exact_to_inexact);
  5126. /* Convert the number @var{x} to its inexact representation.\n"
  5127. */
  5128. SCM
  5129. scm_exact_to_inexact (SCM z)
  5130. {
  5131. if (SCM_I_INUMP (z))
  5132. return scm_from_double ((double) SCM_I_INUM (z));
  5133. else if (SCM_BIGP (z))
  5134. return scm_from_double (scm_i_big2dbl (z));
  5135. else if (SCM_FRACTIONP (z))
  5136. return scm_from_double (scm_i_fraction2double (z));
  5137. else if (SCM_INEXACTP (z))
  5138. return z;
  5139. else
  5140. SCM_WTA_DISPATCH_1 (g_exact_to_inexact, z, 1, s_exact_to_inexact);
  5141. }
  5142. SCM_DEFINE (scm_inexact_to_exact, "inexact->exact", 1, 0, 0,
  5143. (SCM z),
  5144. "Return an exact number that is numerically closest to @var{z}.")
  5145. #define FUNC_NAME s_scm_inexact_to_exact
  5146. {
  5147. if (SCM_I_INUMP (z))
  5148. return z;
  5149. else if (SCM_BIGP (z))
  5150. return z;
  5151. else if (SCM_REALP (z))
  5152. {
  5153. if (xisinf (SCM_REAL_VALUE (z)) || xisnan (SCM_REAL_VALUE (z)))
  5154. SCM_OUT_OF_RANGE (1, z);
  5155. else
  5156. {
  5157. mpq_t frac;
  5158. SCM q;
  5159. mpq_init (frac);
  5160. mpq_set_d (frac, SCM_REAL_VALUE (z));
  5161. q = scm_i_make_ratio (scm_i_mpz2num (mpq_numref (frac)),
  5162. scm_i_mpz2num (mpq_denref (frac)));
  5163. /* When scm_i_make_ratio throws, we leak the memory allocated
  5164. for frac...
  5165. */
  5166. mpq_clear (frac);
  5167. return q;
  5168. }
  5169. }
  5170. else if (SCM_FRACTIONP (z))
  5171. return z;
  5172. else
  5173. SCM_WRONG_TYPE_ARG (1, z);
  5174. }
  5175. #undef FUNC_NAME
  5176. SCM_DEFINE (scm_rationalize, "rationalize", 2, 0, 0,
  5177. (SCM x, SCM eps),
  5178. "Returns the @emph{simplest} rational number differing\n"
  5179. "from @var{x} by no more than @var{eps}.\n"
  5180. "\n"
  5181. "As required by @acronym{R5RS}, @code{rationalize} only returns an\n"
  5182. "exact result when both its arguments are exact. Thus, you might need\n"
  5183. "to use @code{inexact->exact} on the arguments.\n"
  5184. "\n"
  5185. "@lisp\n"
  5186. "(rationalize (inexact->exact 1.2) 1/100)\n"
  5187. "@result{} 6/5\n"
  5188. "@end lisp")
  5189. #define FUNC_NAME s_scm_rationalize
  5190. {
  5191. if (SCM_I_INUMP (x))
  5192. return x;
  5193. else if (SCM_BIGP (x))
  5194. return x;
  5195. else if ((SCM_REALP (x)) || SCM_FRACTIONP (x))
  5196. {
  5197. /* Use continued fractions to find closest ratio. All
  5198. arithmetic is done with exact numbers.
  5199. */
  5200. SCM ex = scm_inexact_to_exact (x);
  5201. SCM int_part = scm_floor (ex);
  5202. SCM tt = SCM_I_MAKINUM (1);
  5203. SCM a1 = SCM_I_MAKINUM (0), a2 = SCM_I_MAKINUM (1), a = SCM_I_MAKINUM (0);
  5204. SCM b1 = SCM_I_MAKINUM (1), b2 = SCM_I_MAKINUM (0), b = SCM_I_MAKINUM (0);
  5205. SCM rx;
  5206. int i = 0;
  5207. if (scm_is_true (scm_num_eq_p (ex, int_part)))
  5208. return ex;
  5209. ex = scm_difference (ex, int_part); /* x = x-int_part */
  5210. rx = scm_divide (ex, SCM_UNDEFINED); /* rx = 1/x */
  5211. /* We stop after a million iterations just to be absolutely sure
  5212. that we don't go into an infinite loop. The process normally
  5213. converges after less than a dozen iterations.
  5214. */
  5215. eps = scm_abs (eps);
  5216. while (++i < 1000000)
  5217. {
  5218. a = scm_sum (scm_product (a1, tt), a2); /* a = a1*tt + a2 */
  5219. b = scm_sum (scm_product (b1, tt), b2); /* b = b1*tt + b2 */
  5220. if (scm_is_false (scm_zero_p (b)) && /* b != 0 */
  5221. scm_is_false
  5222. (scm_gr_p (scm_abs (scm_difference (ex, scm_divide (a, b))),
  5223. eps))) /* abs(x-a/b) <= eps */
  5224. {
  5225. SCM res = scm_sum (int_part, scm_divide (a, b));
  5226. if (scm_is_false (scm_exact_p (x))
  5227. || scm_is_false (scm_exact_p (eps)))
  5228. return scm_exact_to_inexact (res);
  5229. else
  5230. return res;
  5231. }
  5232. rx = scm_divide (scm_difference (rx, tt), /* rx = 1/(rx - tt) */
  5233. SCM_UNDEFINED);
  5234. tt = scm_floor (rx); /* tt = floor (rx) */
  5235. a2 = a1;
  5236. b2 = b1;
  5237. a1 = a;
  5238. b1 = b;
  5239. }
  5240. scm_num_overflow (s_scm_rationalize);
  5241. }
  5242. else
  5243. SCM_WRONG_TYPE_ARG (1, x);
  5244. }
  5245. #undef FUNC_NAME
  5246. /* conversion functions */
  5247. int
  5248. scm_is_integer (SCM val)
  5249. {
  5250. return scm_is_true (scm_integer_p (val));
  5251. }
  5252. int
  5253. scm_is_signed_integer (SCM val, scm_t_intmax min, scm_t_intmax max)
  5254. {
  5255. if (SCM_I_INUMP (val))
  5256. {
  5257. scm_t_signed_bits n = SCM_I_INUM (val);
  5258. return n >= min && n <= max;
  5259. }
  5260. else if (SCM_BIGP (val))
  5261. {
  5262. if (min >= SCM_MOST_NEGATIVE_FIXNUM && max <= SCM_MOST_POSITIVE_FIXNUM)
  5263. return 0;
  5264. else if (min >= LONG_MIN && max <= LONG_MAX)
  5265. {
  5266. if (mpz_fits_slong_p (SCM_I_BIG_MPZ (val)))
  5267. {
  5268. long n = mpz_get_si (SCM_I_BIG_MPZ (val));
  5269. return n >= min && n <= max;
  5270. }
  5271. else
  5272. return 0;
  5273. }
  5274. else
  5275. {
  5276. scm_t_intmax n;
  5277. size_t count;
  5278. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  5279. > CHAR_BIT*sizeof (scm_t_uintmax))
  5280. return 0;
  5281. mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
  5282. SCM_I_BIG_MPZ (val));
  5283. if (mpz_sgn (SCM_I_BIG_MPZ (val)) >= 0)
  5284. {
  5285. if (n < 0)
  5286. return 0;
  5287. }
  5288. else
  5289. {
  5290. n = -n;
  5291. if (n >= 0)
  5292. return 0;
  5293. }
  5294. return n >= min && n <= max;
  5295. }
  5296. }
  5297. else
  5298. return 0;
  5299. }
  5300. int
  5301. scm_is_unsigned_integer (SCM val, scm_t_uintmax min, scm_t_uintmax max)
  5302. {
  5303. if (SCM_I_INUMP (val))
  5304. {
  5305. scm_t_signed_bits n = SCM_I_INUM (val);
  5306. return n >= 0 && ((scm_t_uintmax)n) >= min && ((scm_t_uintmax)n) <= max;
  5307. }
  5308. else if (SCM_BIGP (val))
  5309. {
  5310. if (max <= SCM_MOST_POSITIVE_FIXNUM)
  5311. return 0;
  5312. else if (max <= ULONG_MAX)
  5313. {
  5314. if (mpz_fits_ulong_p (SCM_I_BIG_MPZ (val)))
  5315. {
  5316. unsigned long n = mpz_get_ui (SCM_I_BIG_MPZ (val));
  5317. return n >= min && n <= max;
  5318. }
  5319. else
  5320. return 0;
  5321. }
  5322. else
  5323. {
  5324. scm_t_uintmax n;
  5325. size_t count;
  5326. if (mpz_sgn (SCM_I_BIG_MPZ (val)) < 0)
  5327. return 0;
  5328. if (mpz_sizeinbase (SCM_I_BIG_MPZ (val), 2)
  5329. > CHAR_BIT*sizeof (scm_t_uintmax))
  5330. return 0;
  5331. mpz_export (&n, &count, 1, sizeof (scm_t_uintmax), 0, 0,
  5332. SCM_I_BIG_MPZ (val));
  5333. return n >= min && n <= max;
  5334. }
  5335. }
  5336. else
  5337. return 0;
  5338. }
  5339. static void
  5340. scm_i_range_error (SCM bad_val, SCM min, SCM max)
  5341. {
  5342. scm_error (scm_out_of_range_key,
  5343. NULL,
  5344. "Value out of range ~S to ~S: ~S",
  5345. scm_list_3 (min, max, bad_val),
  5346. scm_list_1 (bad_val));
  5347. }
  5348. #define TYPE scm_t_intmax
  5349. #define TYPE_MIN min
  5350. #define TYPE_MAX max
  5351. #define SIZEOF_TYPE 0
  5352. #define SCM_TO_TYPE_PROTO(arg) scm_to_signed_integer (arg, scm_t_intmax min, scm_t_intmax max)
  5353. #define SCM_FROM_TYPE_PROTO(arg) scm_from_signed_integer (arg)
  5354. #include "libguile/conv-integer.i.c"
  5355. #define TYPE scm_t_uintmax
  5356. #define TYPE_MIN min
  5357. #define TYPE_MAX max
  5358. #define SIZEOF_TYPE 0
  5359. #define SCM_TO_TYPE_PROTO(arg) scm_to_unsigned_integer (arg, scm_t_uintmax min, scm_t_uintmax max)
  5360. #define SCM_FROM_TYPE_PROTO(arg) scm_from_unsigned_integer (arg)
  5361. #include "libguile/conv-uinteger.i.c"
  5362. #define TYPE scm_t_int8
  5363. #define TYPE_MIN SCM_T_INT8_MIN
  5364. #define TYPE_MAX SCM_T_INT8_MAX
  5365. #define SIZEOF_TYPE 1
  5366. #define SCM_TO_TYPE_PROTO(arg) scm_to_int8 (arg)
  5367. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int8 (arg)
  5368. #include "libguile/conv-integer.i.c"
  5369. #define TYPE scm_t_uint8
  5370. #define TYPE_MIN 0
  5371. #define TYPE_MAX SCM_T_UINT8_MAX
  5372. #define SIZEOF_TYPE 1
  5373. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint8 (arg)
  5374. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint8 (arg)
  5375. #include "libguile/conv-uinteger.i.c"
  5376. #define TYPE scm_t_int16
  5377. #define TYPE_MIN SCM_T_INT16_MIN
  5378. #define TYPE_MAX SCM_T_INT16_MAX
  5379. #define SIZEOF_TYPE 2
  5380. #define SCM_TO_TYPE_PROTO(arg) scm_to_int16 (arg)
  5381. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int16 (arg)
  5382. #include "libguile/conv-integer.i.c"
  5383. #define TYPE scm_t_uint16
  5384. #define TYPE_MIN 0
  5385. #define TYPE_MAX SCM_T_UINT16_MAX
  5386. #define SIZEOF_TYPE 2
  5387. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint16 (arg)
  5388. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint16 (arg)
  5389. #include "libguile/conv-uinteger.i.c"
  5390. #define TYPE scm_t_int32
  5391. #define TYPE_MIN SCM_T_INT32_MIN
  5392. #define TYPE_MAX SCM_T_INT32_MAX
  5393. #define SIZEOF_TYPE 4
  5394. #define SCM_TO_TYPE_PROTO(arg) scm_to_int32 (arg)
  5395. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int32 (arg)
  5396. #include "libguile/conv-integer.i.c"
  5397. #define TYPE scm_t_uint32
  5398. #define TYPE_MIN 0
  5399. #define TYPE_MAX SCM_T_UINT32_MAX
  5400. #define SIZEOF_TYPE 4
  5401. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint32 (arg)
  5402. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint32 (arg)
  5403. #include "libguile/conv-uinteger.i.c"
  5404. #if SCM_HAVE_T_INT64
  5405. #define TYPE scm_t_int64
  5406. #define TYPE_MIN SCM_T_INT64_MIN
  5407. #define TYPE_MAX SCM_T_INT64_MAX
  5408. #define SIZEOF_TYPE 8
  5409. #define SCM_TO_TYPE_PROTO(arg) scm_to_int64 (arg)
  5410. #define SCM_FROM_TYPE_PROTO(arg) scm_from_int64 (arg)
  5411. #include "libguile/conv-integer.i.c"
  5412. #define TYPE scm_t_uint64
  5413. #define TYPE_MIN 0
  5414. #define TYPE_MAX SCM_T_UINT64_MAX
  5415. #define SIZEOF_TYPE 8
  5416. #define SCM_TO_TYPE_PROTO(arg) scm_to_uint64 (arg)
  5417. #define SCM_FROM_TYPE_PROTO(arg) scm_from_uint64 (arg)
  5418. #include "libguile/conv-uinteger.i.c"
  5419. #endif
  5420. void
  5421. scm_to_mpz (SCM val, mpz_t rop)
  5422. {
  5423. if (SCM_I_INUMP (val))
  5424. mpz_set_si (rop, SCM_I_INUM (val));
  5425. else if (SCM_BIGP (val))
  5426. mpz_set (rop, SCM_I_BIG_MPZ (val));
  5427. else
  5428. scm_wrong_type_arg_msg (NULL, 0, val, "exact integer");
  5429. }
  5430. SCM
  5431. scm_from_mpz (mpz_t val)
  5432. {
  5433. return scm_i_mpz2num (val);
  5434. }
  5435. int
  5436. scm_is_real (SCM val)
  5437. {
  5438. return scm_is_true (scm_real_p (val));
  5439. }
  5440. int
  5441. scm_is_rational (SCM val)
  5442. {
  5443. return scm_is_true (scm_rational_p (val));
  5444. }
  5445. double
  5446. scm_to_double (SCM val)
  5447. {
  5448. if (SCM_I_INUMP (val))
  5449. return SCM_I_INUM (val);
  5450. else if (SCM_BIGP (val))
  5451. return scm_i_big2dbl (val);
  5452. else if (SCM_FRACTIONP (val))
  5453. return scm_i_fraction2double (val);
  5454. else if (SCM_REALP (val))
  5455. return SCM_REAL_VALUE (val);
  5456. else
  5457. scm_wrong_type_arg_msg (NULL, 0, val, "real number");
  5458. }
  5459. SCM
  5460. scm_from_double (double val)
  5461. {
  5462. SCM z = scm_double_cell (scm_tc16_real, 0, 0, 0);
  5463. SCM_REAL_VALUE (z) = val;
  5464. return z;
  5465. }
  5466. #if SCM_ENABLE_DISCOURAGED == 1
  5467. float
  5468. scm_num2float (SCM num, unsigned long int pos, const char *s_caller)
  5469. {
  5470. if (SCM_BIGP (num))
  5471. {
  5472. float res = mpz_get_d (SCM_I_BIG_MPZ (num));
  5473. if (!xisinf (res))
  5474. return res;
  5475. else
  5476. scm_out_of_range (NULL, num);
  5477. }
  5478. else
  5479. return scm_to_double (num);
  5480. }
  5481. double
  5482. scm_num2double (SCM num, unsigned long int pos, const char *s_caller)
  5483. {
  5484. if (SCM_BIGP (num))
  5485. {
  5486. double res = mpz_get_d (SCM_I_BIG_MPZ (num));
  5487. if (!xisinf (res))
  5488. return res;
  5489. else
  5490. scm_out_of_range (NULL, num);
  5491. }
  5492. else
  5493. return scm_to_double (num);
  5494. }
  5495. #endif
  5496. int
  5497. scm_is_complex (SCM val)
  5498. {
  5499. return scm_is_true (scm_complex_p (val));
  5500. }
  5501. double
  5502. scm_c_real_part (SCM z)
  5503. {
  5504. if (SCM_COMPLEXP (z))
  5505. return SCM_COMPLEX_REAL (z);
  5506. else
  5507. {
  5508. /* Use the scm_real_part to get proper error checking and
  5509. dispatching.
  5510. */
  5511. return scm_to_double (scm_real_part (z));
  5512. }
  5513. }
  5514. double
  5515. scm_c_imag_part (SCM z)
  5516. {
  5517. if (SCM_COMPLEXP (z))
  5518. return SCM_COMPLEX_IMAG (z);
  5519. else
  5520. {
  5521. /* Use the scm_imag_part to get proper error checking and
  5522. dispatching. The result will almost always be 0.0, but not
  5523. always.
  5524. */
  5525. return scm_to_double (scm_imag_part (z));
  5526. }
  5527. }
  5528. double
  5529. scm_c_magnitude (SCM z)
  5530. {
  5531. return scm_to_double (scm_magnitude (z));
  5532. }
  5533. double
  5534. scm_c_angle (SCM z)
  5535. {
  5536. return scm_to_double (scm_angle (z));
  5537. }
  5538. int
  5539. scm_is_number (SCM z)
  5540. {
  5541. return scm_is_true (scm_number_p (z));
  5542. }
  5543. /* In the following functions we dispatch to the real-arg funcs like log()
  5544. when we know the arg is real, instead of just handing everything to
  5545. clog() for instance. This is in case clog() doesn't optimize for a
  5546. real-only case, and because we have to test SCM_COMPLEXP anyway so may as
  5547. well use it to go straight to the applicable C func. */
  5548. SCM_DEFINE (scm_log, "log", 1, 0, 0,
  5549. (SCM z),
  5550. "Return the natural logarithm of @var{z}.")
  5551. #define FUNC_NAME s_scm_log
  5552. {
  5553. if (SCM_COMPLEXP (z))
  5554. {
  5555. #if HAVE_COMPLEX_DOUBLE && HAVE_CLOG && defined (SCM_COMPLEX_VALUE)
  5556. return scm_from_complex_double (clog (SCM_COMPLEX_VALUE (z)));
  5557. #else
  5558. double re = SCM_COMPLEX_REAL (z);
  5559. double im = SCM_COMPLEX_IMAG (z);
  5560. return scm_c_make_rectangular (log (hypot (re, im)),
  5561. atan2 (im, re));
  5562. #endif
  5563. }
  5564. else
  5565. {
  5566. /* ENHANCE-ME: When z is a bignum the logarithm will fit a double
  5567. although the value itself overflows. */
  5568. double re = scm_to_double (z);
  5569. double l = log (fabs (re));
  5570. if (re >= 0.0)
  5571. return scm_from_double (l);
  5572. else
  5573. return scm_c_make_rectangular (l, M_PI);
  5574. }
  5575. }
  5576. #undef FUNC_NAME
  5577. SCM_DEFINE (scm_log10, "log10", 1, 0, 0,
  5578. (SCM z),
  5579. "Return the base 10 logarithm of @var{z}.")
  5580. #define FUNC_NAME s_scm_log10
  5581. {
  5582. if (SCM_COMPLEXP (z))
  5583. {
  5584. /* Mingw has clog() but not clog10(). (Maybe it'd be worth using
  5585. clog() and a multiply by M_LOG10E, rather than the fallback
  5586. log10+hypot+atan2.) */
  5587. #if HAVE_COMPLEX_DOUBLE && HAVE_CLOG10 && defined (SCM_COMPLEX_VALUE)
  5588. return scm_from_complex_double (clog10 (SCM_COMPLEX_VALUE (z)));
  5589. #else
  5590. double re = SCM_COMPLEX_REAL (z);
  5591. double im = SCM_COMPLEX_IMAG (z);
  5592. return scm_c_make_rectangular (log10 (hypot (re, im)),
  5593. M_LOG10E * atan2 (im, re));
  5594. #endif
  5595. }
  5596. else
  5597. {
  5598. /* ENHANCE-ME: When z is a bignum the logarithm will fit a double
  5599. although the value itself overflows. */
  5600. double re = scm_to_double (z);
  5601. double l = log10 (fabs (re));
  5602. if (re >= 0.0)
  5603. return scm_from_double (l);
  5604. else
  5605. return scm_c_make_rectangular (l, M_LOG10E * M_PI);
  5606. }
  5607. }
  5608. #undef FUNC_NAME
  5609. SCM_DEFINE (scm_exp, "exp", 1, 0, 0,
  5610. (SCM z),
  5611. "Return @math{e} to the power of @var{z}, where @math{e} is the\n"
  5612. "base of natural logarithms (2.71828@dots{}).")
  5613. #define FUNC_NAME s_scm_exp
  5614. {
  5615. if (SCM_COMPLEXP (z))
  5616. {
  5617. #if HAVE_COMPLEX_DOUBLE && HAVE_CEXP && defined (SCM_COMPLEX_VALUE)
  5618. return scm_from_complex_double (cexp (SCM_COMPLEX_VALUE (z)));
  5619. #else
  5620. return scm_c_make_polar (exp (SCM_COMPLEX_REAL (z)),
  5621. SCM_COMPLEX_IMAG (z));
  5622. #endif
  5623. }
  5624. else
  5625. {
  5626. /* When z is a negative bignum the conversion to double overflows,
  5627. giving -infinity, but that's ok, the exp is still 0.0. */
  5628. return scm_from_double (exp (scm_to_double (z)));
  5629. }
  5630. }
  5631. #undef FUNC_NAME
  5632. SCM_DEFINE (scm_sqrt, "sqrt", 1, 0, 0,
  5633. (SCM x),
  5634. "Return the square root of @var{z}. Of the two possible roots\n"
  5635. "(positive and negative), the one with the a positive real part\n"
  5636. "is returned, or if that's zero then a positive imaginary part.\n"
  5637. "Thus,\n"
  5638. "\n"
  5639. "@example\n"
  5640. "(sqrt 9.0) @result{} 3.0\n"
  5641. "(sqrt -9.0) @result{} 0.0+3.0i\n"
  5642. "(sqrt 1.0+1.0i) @result{} 1.09868411346781+0.455089860562227i\n"
  5643. "(sqrt -1.0-1.0i) @result{} 0.455089860562227-1.09868411346781i\n"
  5644. "@end example")
  5645. #define FUNC_NAME s_scm_sqrt
  5646. {
  5647. if (SCM_COMPLEXP (x))
  5648. {
  5649. #if HAVE_COMPLEX_DOUBLE && HAVE_USABLE_CSQRT && defined (SCM_COMPLEX_VALUE)
  5650. return scm_from_complex_double (csqrt (SCM_COMPLEX_VALUE (x)));
  5651. #else
  5652. double re = SCM_COMPLEX_REAL (x);
  5653. double im = SCM_COMPLEX_IMAG (x);
  5654. return scm_c_make_polar (sqrt (hypot (re, im)),
  5655. 0.5 * atan2 (im, re));
  5656. #endif
  5657. }
  5658. else
  5659. {
  5660. double xx = scm_to_double (x);
  5661. if (xx < 0)
  5662. return scm_c_make_rectangular (0.0, sqrt (-xx));
  5663. else
  5664. return scm_from_double (sqrt (xx));
  5665. }
  5666. }
  5667. #undef FUNC_NAME
  5668. void
  5669. scm_init_numbers ()
  5670. {
  5671. int i;
  5672. mpz_init_set_si (z_negative_one, -1);
  5673. /* It may be possible to tune the performance of some algorithms by using
  5674. * the following constants to avoid the creation of bignums. Please, before
  5675. * using these values, remember the two rules of program optimization:
  5676. * 1st Rule: Don't do it. 2nd Rule (experts only): Don't do it yet. */
  5677. scm_c_define ("most-positive-fixnum",
  5678. SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM));
  5679. scm_c_define ("most-negative-fixnum",
  5680. SCM_I_MAKINUM (SCM_MOST_NEGATIVE_FIXNUM));
  5681. scm_add_feature ("complex");
  5682. scm_add_feature ("inexact");
  5683. scm_flo0 = scm_from_double (0.0);
  5684. /* determine floating point precision */
  5685. for (i=2; i <= SCM_MAX_DBL_RADIX; ++i)
  5686. {
  5687. init_dblprec(&scm_dblprec[i-2],i);
  5688. init_fx_radix(fx_per_radix[i-2],i);
  5689. }
  5690. #ifdef DBL_DIG
  5691. /* hard code precision for base 10 if the preprocessor tells us to... */
  5692. scm_dblprec[10-2] = (DBL_DIG > 20) ? 20 : DBL_DIG;
  5693. #endif
  5694. exactly_one_half = scm_permanent_object (scm_divide (SCM_I_MAKINUM (1),
  5695. SCM_I_MAKINUM (2)));
  5696. #include "libguile/numbers.x"
  5697. }
  5698. /*
  5699. Local Variables:
  5700. c-file-style: "gnu"
  5701. End:
  5702. */