123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481 |
- ;;;; numbers.test --- tests guile's numbers -*- scheme -*-
- ;;;; Copyright (C) 2000, 2001, 2003-2006, 2009-2013,
- ;;;; 2015, 2018 Free Software Foundation, Inc.
- ;;;;
- ;;;; This library is free software; you can redistribute it and/or
- ;;;; modify it under the terms of the GNU Lesser General Public
- ;;;; License as published by the Free Software Foundation; either
- ;;;; version 3 of the License, or (at your option) any later version.
- ;;;;
- ;;;; This library is distributed in the hope that it will be useful,
- ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- ;;;; Lesser General Public License for more details.
- ;;;;
- ;;;; You should have received a copy of the GNU Lesser General Public
- ;;;; License along with this library; if not, write to the Free Software
- ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- (define-module (test-suite test-numbers)
- #:use-module (test-suite lib)
- #:use-module (ice-9 documentation)
- #:use-module (srfi srfi-1) ; list library
- #:use-module (srfi srfi-11)) ; let-values
- ;;;
- ;;; miscellaneous
- ;;;
- (define exception:numerical-overflow
- (cons 'numerical-overflow "^Numerical overflow"))
- (define (documented? object)
- (not (not (object-documentation object))))
- (define fixnum-bit
- (do ((i 0 (+ 1 i))
- (n 1 (* 2 n)))
- ((> n most-positive-fixnum)
- (+ 1 i))))
- (define fixnum-min most-negative-fixnum)
- (define fixnum-max most-positive-fixnum)
- ;; Divine the number of bits in the mantissa of a flonum.
- ;; We look for when 2.0^i+1.0 gets rounded, ie. the difference between that
- ;; value and 2.0^k is not 1.0.
- ;; Of course this assumes flonums have a fixed precision mantissa, but
- ;; that's the case now and probably into the forseeable future.
- ;; On an IEEE system, which means pretty much everywhere, the value here is
- ;; the usual 53.
- ;;
- (define dbl-mant-dig
- (do ((prec 0 (+ prec 1))
- (eps 1.0 (/ eps 2.0)))
- ((begin (when (> prec 1000000)
- (error "Unable to determine dbl-mant-dig"))
- (= 1.0 (+ 1.0 eps)))
- prec)))
- (define dbl-epsilon
- (expt 0.5 (- dbl-mant-dig 1)))
- (define dbl-epsilon-exact
- (expt 1/2 (- dbl-mant-dig 1)))
- (define dbl-min-exp
- (do ((x 1.0 (/ x 2.0))
- (y (+ 1.0 dbl-epsilon) (/ y 2.0))
- (e 2 (- e 1)))
- ((begin (when (< e -100000000)
- (error "Unable to determine dbl-min-exp"))
- (= x y))
- e)))
- (define dbl-max-exp
- (do ((x 1.0 (* x 2.0))
- (e 0 (+ e 1)))
- ((begin (when (> e 100000000)
- (error "Unable to determine dbl-max-exp"))
- (inf? x))
- e)))
- ;; like ash, but working on a flonum
- (define (ash-flo x n)
- (while (> n 0)
- (set! x (* 2.0 x))
- (set! n (1- n)))
- (while (< n 0)
- (set! x (* 0.5 x))
- (set! n (1+ n)))
- x)
- ;; `quotient' but rounded towards -infinity, like `modulo' or `ash' do
- ;; note only positive D supported (that's all that's currently required)
- (define-public (quotient-floor n d)
- (if (negative? n)
- (quotient (- n d -1) d) ;; neg/pos
- (quotient n d))) ;; pos/pos
- ;; return true of X is in the range LO to HI, inclusive
- (define (within-range? lo hi x)
- (and (>= x (min lo hi))
- (<= x (max lo hi))))
- ;; return true if GOT is within +/- 0.01 of GOT
- ;; for a complex number both real and imaginary parts must be in that range
- (define (eqv-loosely? want got)
- (and (within-range? (- (real-part want) 0.01)
- (+ (real-part want) 0.01)
- (real-part got))
- (within-range? (- (imag-part want) 0.01)
- (+ (imag-part want) 0.01)
- (imag-part got))))
- ;; return true if OBJ is negative infinity
- (define (negative-infinity? obj)
- (and (real? obj)
- (negative? obj)
- (inf? obj)))
- ;;
- ;; Tolerance used by test-eqv? for inexact numbers.
- ;;
- (define test-epsilon 1e-10)
- ;;
- ;; Like eqv?, except that inexact finite numbers need only be within
- ;; test-epsilon (1e-10) to be considered equal. For non-real complex
- ;; numbers, each component is tested according to these rules. The
- ;; intent is that the known-correct value will be the first parameter.
- ;;
- (define (test-eqv? x y)
- (cond ((real? x)
- (and (real? y) (test-real-eqv? x y)))
- ((complex? x)
- (and (not (real? y))
- (test-real-eqv? (real-part x) (real-part y))
- (test-real-eqv? (imag-part x) (imag-part y))))
- (else (eqv? x y))))
- ;; Auxiliary predicate used by test-eqv?
- (define (test-real-eqv? x y)
- (cond ((or (exact? x) (nan? x) (inf? x))
- (eqv? x y))
- (else (and (inexact? y) (> test-epsilon (abs (- x y)))))))
- ;; return true if OBJ is a real NaN
- (define (real-nan? obj)
- (and (real? obj)
- (nan? obj)))
- ;; return true if OBJ is a non-real complex number
- ;; whose real part is a nan, and whose imaginary
- ;; part is an inexact zero.
- (define (almost-real-nan? obj)
- (and (not (real? obj))
- (nan? (real-part obj))
- (zero? (imag-part obj))))
- ;; return true if both the real and imaginary
- ;; parts of OBJ are NaNs
- (define (complex-nan? obj)
- (and (nan? (real-part obj))
- (nan? (imag-part obj))))
- ;; return true if the real part of OBJ is zero
- ;; and the imaginary part is a NaN.
- (define (imaginary-nan? obj)
- (and (zero? (real-part obj))
- (nan? (imag-part obj))))
- ;; return true if OBJ is a non-real complex zero
- (define (complex-zero? obj)
- (and (zero? obj)
- (complex? obj)
- (not (real? obj))))
- (define const-e 2.7182818284590452354)
- (define const-e^2 7.3890560989306502274)
- (define const-1/e 0.3678794411714423215)
- ;;;
- ;;; 1+
- ;;;
- (with-test-prefix/c&e "1+"
- (pass-if "documented?"
- (documented? 1+))
- (pass-if "0" (eqv? 1 (1+ 0)))
- (pass-if "-1" (eqv? 0 (1+ -1)))
- (pass-if "100" (eqv? 101 (1+ 100)))
- (pass-if "-100" (eqv? -99 (1+ -100)))
- ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
- (pass-if "1+ fixnum = bignum (32-bit)"
- (eqv? 536870912 (1+ 536870911)))
- ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
- (pass-if "1+ fixnum = bignum (64-bit)"
- (eqv? 2305843009213693952 (1+ 2305843009213693951))))
- ;;;
- ;;; 1-
- ;;;
- (with-test-prefix/c&e "1-"
- (pass-if "documented?"
- (documented? 1-))
- (pass-if "0" (eqv? -1 (1- 0)))
- (pass-if "1" (eqv? 0 (1- 1)))
- (pass-if "100" (eqv? 99 (1- 100)))
- (pass-if "-100" (eqv? -101 (1- -100)))
- ;; The minimum fixnum on a 32-bit architecture: -2^29.
- (pass-if "1- fixnum = bignum (32-bit)"
- (eqv? -536870913 (1- -536870912)))
- ;; The minimum fixnum on a 64-bit architecture: -2^61.
- (pass-if "1- fixnum = bignum (64-bit)"
- (eqv? -2305843009213693953 (1- -2305843009213693952))))
- ;;;
- ;;; exact?
- ;;;
- (with-test-prefix "exact?"
- (pass-if "documented?"
- (documented? exact?))
- (with-test-prefix "integers"
- (pass-if "0"
- (exact? 0))
- (pass-if "fixnum-max"
- (exact? fixnum-max))
- (pass-if "fixnum-max + 1"
- (exact? (+ fixnum-max 1)))
- (pass-if "fixnum-min"
- (exact? fixnum-min))
- (pass-if "fixnum-min - 1"
- (exact? (- fixnum-min 1))))
- (with-test-prefix "reals"
- ;; (FIXME: need better examples.)
- (pass-if "sqrt (fixnum-max^2 - 1)"
- (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1)))))
- (pass-if "sqrt ((fixnum-max+1)^2 - 1)"
- (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))
- (pass-if (not (exact? +inf.0)))
- (pass-if (not (exact? -inf.0)))
- (pass-if (not (exact? +nan.0)))))
- ;;;
- ;;; exp
- ;;;
- (with-test-prefix "exp"
- (pass-if (documented? exp))
- (pass-if-exception "no args" exception:wrong-num-args
- (exp))
- (pass-if-exception "two args" exception:wrong-num-args
- (exp 123 456))
- (pass-if (eqv? 0.0 (exp -inf.0)))
- (pass-if (eqv-loosely? 1.0 (exp 0)))
- (pass-if (eqv-loosely? 1.0 (exp 0.0)))
- (pass-if (eqv-loosely? const-e (exp 1.0)))
- (pass-if (eqv-loosely? const-e^2 (exp 2.0)))
- (pass-if (eqv-loosely? const-1/e (exp -1)))
- (pass-if "exp(pi*i) = -1"
- (eqv-loosely? -1.0 (exp 0+3.14159i)))
- (pass-if "exp(-pi*i) = -1"
- (eqv-loosely? -1.0 (exp 0-3.14159i)))
- (pass-if "exp(2*pi*i) = +1"
- (eqv-loosely? 1.0 (exp 0+6.28318i)))
- (pass-if "exp(2-pi*i) = -e^2"
- (eqv-loosely? (- const-e^2) (exp 2.0-3.14159i))))
- ;;;
- ;;; odd?
- ;;;
- (with-test-prefix "odd?"
- (pass-if (documented? odd?))
- (pass-if (odd? 1))
- (pass-if (odd? -1))
- (pass-if (not (odd? 0)))
- (pass-if (not (odd? 2)))
- (pass-if (not (odd? -2)))
- (pass-if (odd? (+ (* 2 fixnum-max) 1)))
- (pass-if (not (odd? (* 2 fixnum-max))))
- (pass-if (odd? (- (* 2 fixnum-min) 1)))
- (pass-if (not (odd? (* 2 fixnum-min)))))
- ;;;
- ;;; even?
- ;;;
- (with-test-prefix "even?"
- (pass-if (documented? even?))
- (pass-if (even? 2))
- (pass-if (even? -2))
- (pass-if (even? 0))
- (pass-if (not (even? 1)))
- (pass-if (not (even? -1)))
- (pass-if (not (even? (+ (* 2 fixnum-max) 1))))
- (pass-if (even? (* 2 fixnum-max)))
- (pass-if (not (even? (- (* 2 fixnum-min) 1))))
- (pass-if (even? (* 2 fixnum-min))))
- ;;;
- ;;; finite?
- ;;;
- (with-test-prefix "finite?"
- (pass-if (documented? finite?))
- (pass-if (not (finite? (inf))))
- (pass-if (not (finite? +inf.0)))
- (pass-if (not (finite? -inf.0)))
- (pass-if-exception
- "complex numbers not in domain of finite?"
- exception:wrong-type-arg
- (finite? +inf.0+1i))
- (pass-if-exception
- "complex numbers not in domain of finite? (2)"
- exception:wrong-type-arg
- (finite? +1+inf.0i))
- (pass-if-exception
- "complex numbers not in domain of finite? (3)"
- exception:wrong-type-arg
- (finite? +1+1i))
- (pass-if (finite? 3+0i))
- (pass-if (not (finite? (nan))))
- (pass-if (not (finite? +nan.0)))
- (pass-if (finite? 0))
- (pass-if (finite? 0.0))
- (pass-if (finite? -0.0))
- (pass-if (finite? 42.0))
- (pass-if (finite? 1/2))
- (pass-if (finite? (+ fixnum-max 1)))
- (pass-if (finite? (- fixnum-min 1))))
- ;;;
- ;;; inf? and inf
- ;;;
- (with-test-prefix "inf?"
- (pass-if (documented? inf?))
- (pass-if (inf? (inf)))
- ;; FIXME: what are the expected behaviors?
- ;; (pass-if (inf? (/ 1.0 0.0))
- ;; (pass-if (inf? (/ 1 0.0))
- (pass-if-exception
- "complex numbers not in domain of inf?"
- exception:wrong-type-arg
- (inf? +1+inf.0i))
- (pass-if (inf? +inf.0+0i))
- (pass-if (not (inf? 0)))
- (pass-if (not (inf? 42.0)))
- (pass-if (not (inf? (+ fixnum-max 1))))
- (pass-if (not (inf? (- fixnum-min 1)))))
- ;;;
- ;;; nan? and nan
- ;;;
- (with-test-prefix "nan?"
- (pass-if (documented? nan?))
- (pass-if (nan? (nan)))
- ;; FIXME: other ways we should be able to generate NaN?
- (pass-if (not (nan? 0)))
- (pass-if (not (nan? 42.0)))
- (pass-if (not (nan? (+ fixnum-max 1))))
- (pass-if (not (nan? (- fixnum-min 1)))))
- ;;;
- ;;; abs
- ;;;
- (with-test-prefix "abs"
- (pass-if (documented? abs))
- (pass-if (eqv? 0 (abs 0)))
- (pass-if (eqv? 1 (abs 1)))
- (pass-if (eqv? 1 (abs -1)))
- (with-test-prefix "double-negation of fixnum-min"
- (pass-if (eqv? fixnum-min (- (abs fixnum-min)))))
- (pass-if (eqv? (+ fixnum-max 1) (abs (+ fixnum-max 1))))
- (pass-if (eqv? (+ (- fixnum-min) 1) (abs (- fixnum-min 1))))
- (pass-if (eqv? 0.0 (abs 0.0)))
- (pass-if (eqv? 0.0 (abs -0.0)))
- (pass-if (eqv? 1.0 (abs 1.0)))
- (pass-if (eqv? 1.0 (abs -1.0)))
- (pass-if (real-nan? (abs +nan.0)))
- (pass-if (eqv? +inf.0 (abs +inf.0)))
- (pass-if (eqv? +inf.0 (abs -inf.0))))
- ;;;
- ;;; quotient
- ;;;
- (with-test-prefix "quotient"
- (pass-if (documented? quotient))
- (with-test-prefix "0 / n"
- (pass-if "n = 1"
- (eqv? 0 (quotient 0 1)))
- (pass-if "n = -1"
- (eqv? 0 (quotient 0 -1)))
- (pass-if "n = 2"
- (eqv? 0 (quotient 0 2)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (quotient 0 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (quotient 0 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (quotient 0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient 0 (- fixnum-min 1)))))
- (with-test-prefix "1 / n"
- (pass-if "n = 1"
- (eqv? 1 (quotient 1 1)))
- (pass-if "n = -1"
- (eqv? -1 (quotient 1 -1)))
- (pass-if "n = 2"
- (eqv? 0 (quotient 1 2)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (quotient 1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (quotient 1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (quotient 1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient 1 (- fixnum-min 1)))))
- (with-test-prefix "-1 / n"
- (pass-if "n = 1"
- (eqv? -1 (quotient -1 1)))
- (pass-if "n = -1"
- (eqv? 1 (quotient -1 -1)))
- (pass-if "n = 2"
- (eqv? 0 (quotient -1 2)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (quotient -1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (quotient -1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (quotient -1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient -1 (- fixnum-min 1)))))
- (with-test-prefix "fixnum-max / n"
- (pass-if "n = 1"
- (eqv? fixnum-max (quotient fixnum-max 1)))
- (pass-if "n = -1"
- (eqv? (- fixnum-max) (quotient fixnum-max -1)))
- (pass-if "n = 2"
- (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (quotient fixnum-max fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (quotient fixnum-max (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (quotient fixnum-max fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient fixnum-max (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-max + 1) / n"
- (pass-if "n = 1"
- (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1)))
- (pass-if "n = -1"
- (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1)))
- (pass-if "n = 2"
- (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1)))))
- (with-test-prefix "fixnum-min / n"
- (pass-if "n = 1"
- (eqv? fixnum-min (quotient fixnum-min 1)))
- (pass-if "n = -1"
- (eqv? (- fixnum-min) (quotient fixnum-min -1)))
- (pass-if "n = 2"
- (eqv? fixnum-min (* (quotient fixnum-min 2) 2)))
- (pass-if "n = fixnum-max"
- (eqv? -1 (quotient fixnum-min fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? -1 (quotient fixnum-min (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (quotient fixnum-min fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (quotient fixnum-min (- fixnum-min 1))))
- (pass-if "n = - fixnum-min - 1"
- (eqv? -1 (quotient fixnum-min (1- (- fixnum-min)))))
- ;; special case, normally inum/big is zero
- (pass-if "n = - fixnum-min"
- (eqv? -1 (quotient fixnum-min (- fixnum-min))))
- (pass-if "n = - fixnum-min + 1"
- (eqv? 0 (quotient fixnum-min (1+ (- fixnum-min))))))
- (with-test-prefix "(fixnum-min - 1) / n"
- (pass-if "n = 1"
- (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1)))
- (pass-if "n = -1"
- (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1)))
- (pass-if "n = 2"
- (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2)))
- (pass-if "n = fixnum-max"
- (eqv? -1 (quotient (- fixnum-min 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (quotient (- fixnum-min 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1)))))
-
- ;; Inexact integers
- (pass-if (eqv? 5.0 (quotient 35.0 7.0)))
- (pass-if (eqv? 5.0 (quotient 35 7.0)))
- (pass-if (eqv? 5.0 (quotient 35.0 7 )))
- ;; Positive dividend and divisor
- (pass-if "35 / 7"
- (eqv? 5 (quotient 35 7)))
- ;; Negative dividend, positive divisor
- (pass-if "-35 / 7"
- (eqv? -5 (quotient -35 7)))
- ;; Positive dividend, negative divisor
- (pass-if "35 / -7"
- (eqv? -5 (quotient 35 -7)))
- ;; Negative dividend and divisor
- (pass-if "-35 / -7"
- (eqv? 5 (quotient -35 -7)))
- ;; Are numerical overflows detected correctly?
- (with-test-prefix "division by zero"
- (pass-if-exception "(quotient 1 0)"
- exception:numerical-overflow
- (quotient 1 0))
- (pass-if-exception "(quotient bignum 0)"
- exception:numerical-overflow
- (quotient (+ fixnum-max 1) 0)))
- ;; Are wrong type arguments detected correctly?
- )
- ;;;
- ;;; remainder
- ;;;
- (with-test-prefix "remainder"
- (pass-if (documented? remainder))
- (with-test-prefix "0 / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder 0 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder 0 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (remainder 0 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (remainder 0 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (remainder 0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (remainder 0 (- fixnum-min 1)))))
- (with-test-prefix "1 / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder 1 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder 1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (remainder 1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (remainder 1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (remainder 1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (remainder 1 (- fixnum-min 1)))))
- (with-test-prefix "-1 / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder -1 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder -1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? -1 (remainder -1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? -1 (remainder -1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (remainder -1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? -1 (remainder -1 (- fixnum-min 1)))))
- (with-test-prefix "fixnum-max / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder fixnum-max 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder fixnum-max -1)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (remainder fixnum-max fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? fixnum-max (remainder fixnum-max fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-max + 1) / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder (+ fixnum-max 1) 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder (+ fixnum-max 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1)))))
- (with-test-prefix "fixnum-min / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder fixnum-min 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder fixnum-min -1)))
- (pass-if "n = fixnum-max"
- (eqv? -1 (remainder fixnum-min fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (remainder fixnum-min (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (remainder fixnum-min fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1))))
- (pass-if "n = - fixnum-min - 1"
- (eqv? -1 (remainder fixnum-min (1- (- fixnum-min)))))
- ;; special case, normally inum%big is the inum
- (pass-if "n = - fixnum-min"
- (eqv? 0 (remainder fixnum-min (- fixnum-min))))
- (pass-if "n = - fixnum-min + 1"
- (eqv? fixnum-min (remainder fixnum-min (1+ (- fixnum-min))))))
- (with-test-prefix "(fixnum-min - 1) / n"
- (pass-if "n = 1"
- (eqv? 0 (remainder (- fixnum-min 1) 1)))
- (pass-if "n = -1"
- (eqv? 0 (remainder (- fixnum-min 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? -2 (remainder (- fixnum-min 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (remainder (- fixnum-min 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1)))))
- ;; Inexact integers
- (pass-if (eqv? 2.0 (remainder 37.0 7.0)))
- (pass-if (eqv? 2.0 (remainder 37 7.0)))
- (pass-if (eqv? 2.0 (remainder 37.0 7 )))
- ;; Positive dividend and divisor
- (pass-if "35 / 7"
- (eqv? 0 (remainder 35 7)))
- ;; Negative dividend, positive divisor
- (pass-if "-35 / 7"
- (eqv? 0 (remainder -35 7)))
- ;; Positive dividend, negative divisor
- (pass-if "35 / -7"
- (eqv? 0 (remainder 35 -7)))
- ;; Negative dividend and divisor
- (pass-if "-35 / -7"
- (eqv? 0 (remainder -35 -7)))
- ;; Are numerical overflows detected correctly?
- (with-test-prefix "division by zero"
- (pass-if-exception "(remainder 1 0)"
- exception:numerical-overflow
- (remainder 1 0))
- (pass-if-exception "(remainder bignum 0)"
- exception:numerical-overflow
- (remainder (+ fixnum-max 1) 0)))
- ;; Are wrong type arguments detected correctly?
- )
- ;;;
- ;;; modulo
- ;;;
- (with-test-prefix "modulo"
- (pass-if (documented? modulo))
- (with-test-prefix "0 % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo 0 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo 0 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (modulo 0 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (modulo 0 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (modulo 0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (modulo 0 (- fixnum-min 1)))))
- (with-test-prefix "1 % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo 1 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo 1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (modulo 1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (modulo 1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? fixnum-min (modulo 1 (- fixnum-min 1)))))
- (with-test-prefix "-1 % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo -1 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo -1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? (- fixnum-max 1) (modulo -1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? fixnum-max (modulo -1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (modulo -1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? -1 (modulo -1 (- fixnum-min 1)))))
- (with-test-prefix "fixnum-max % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo fixnum-max 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo fixnum-max -1)))
- (pass-if "n = fixnum-max"
- (eqv? 0 (modulo fixnum-max fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (modulo fixnum-max fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? -2 (modulo fixnum-max (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-max + 1) % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo (+ fixnum-max 1) 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo (+ fixnum-max 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1)))))
- (with-test-prefix "fixnum-min % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo fixnum-min 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo fixnum-min -1)))
- (pass-if "n = fixnum-max"
- (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 0 (modulo fixnum-min (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 0 (modulo fixnum-min fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-min - 1) % n"
- (pass-if "n = 1"
- (eqv? 0 (modulo (- fixnum-min 1) 1)))
- (pass-if "n = -1"
- (eqv? 0 (modulo (- fixnum-min 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? -1 (modulo (- fixnum-min 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1)))))
- ;; Inexact integers
- (pass-if (eqv? 1.0 (modulo 13.0 4.0)))
- (pass-if (eqv? 1.0 (modulo 13 4.0)))
- (pass-if (eqv? 1.0 (modulo 13.0 4 )))
- ;; Positive dividend and divisor
- (pass-if "13 % 4"
- (eqv? 1 (modulo 13 4)))
- (pass-if "2177452800 % 86400"
- (eqv? 0 (modulo 2177452800 86400)))
- ;; Negative dividend, positive divisor
- (pass-if "-13 % 4"
- (eqv? 3 (modulo -13 4)))
- (pass-if "-2177452800 % 86400"
- (eqv? 0 (modulo -2177452800 86400)))
- ;; Positive dividend, negative divisor
- (pass-if "13 % -4"
- (eqv? -3 (modulo 13 -4)))
- (pass-if "2177452800 % -86400"
- (eqv? 0 (modulo 2177452800 -86400)))
- ;; Negative dividend and divisor
- (pass-if "-13 % -4"
- (eqv? -1 (modulo -13 -4)))
- (pass-if "-2177452800 % -86400"
- (eqv? 0 (modulo -2177452800 -86400)))
- ;; Are numerical overflows detected correctly?
- (with-test-prefix "division by zero"
- (pass-if-exception "(modulo 1 0)"
- exception:numerical-overflow
- (modulo 1 0))
- (pass-if-exception "(modulo bignum 0)"
- exception:numerical-overflow
- (modulo (+ fixnum-max 1) 0)))
- ;; Are wrong type arguments detected correctly?
- )
- ;;;
- ;;; modulo-expt
- ;;;
- (with-test-prefix "modulo-expt"
- (pass-if (= 1 (modulo-expt 17 23 47)))
-
- (pass-if (= 1 (modulo-expt 17 -23 47)))
-
- (pass-if (= 17 (modulo-expt 17 -22 47)))
-
- (pass-if (= 36 (modulo-expt 17 22 47)))
-
- (pass-if (= 183658794479969134816674175082294846241553725240 (modulo-expt 111122223333444455556666 111122223333444455556666 1153478690012629968439432872520758982731022934717)))
- (pass-if-exception
- "Proper exception with 0 modulus"
- exception:numerical-overflow
- (modulo-expt 17 23 0))
- (pass-if-exception
- "Proper exception when result not invertible"
- exception:numerical-overflow
- (modulo-expt 10 -1 48))
- (pass-if-exception
- "Proper exception with wrong type argument"
- exception:wrong-type-arg
- (modulo-expt "Sam" 23 10))
- (pass-if-exception
- "Proper exception with wrong type argument"
- exception:wrong-type-arg
- (modulo-expt 17 9.9 10))
-
- (pass-if-exception
- "Proper exception with wrong type argument"
- exception:wrong-type-arg
- (modulo-expt 17 23 'Ethel)))
- ;;;
- ;;; numerator
- ;;;
- (with-test-prefix "numerator"
- (pass-if-equal "0" 0 (numerator 0))
- (pass-if-equal "1" 1 (numerator 1))
- (pass-if-equal "2" 2 (numerator 2))
- (pass-if-equal "-1" -1 (numerator -1))
- (pass-if-equal "-2" -2 (numerator -2))
- (pass-if-equal "0.0" 0.0 (numerator 0.0))
- (pass-if-equal "1.0" 1.0 (numerator 1.0))
- (pass-if-equal "2.0" 2.0 (numerator 2.0))
- (pass-if-equal "-0.0" -0.0 (numerator -0.0))
- (pass-if-equal "-1.0" -1.0 (numerator -1.0))
- (pass-if-equal "-2.0" -2.0 (numerator -2.0))
- (pass-if-equal "0.5" 1.0 (numerator 0.5))
- (pass-if-equal "0.25" 1.0 (numerator 0.25))
- (pass-if-equal "0.75" 3.0 (numerator 0.75))
- (pass-if-equal "+inf.0" +inf.0 (numerator +inf.0))
- (pass-if-equal "-inf.0" -inf.0 (numerator -inf.0)))
- ;;;
- ;;; denominator
- ;;;
- (with-test-prefix "denominator"
- (pass-if-equal "0" 1 (denominator 0))
- (pass-if-equal "1" 1 (denominator 1))
- (pass-if-equal "2" 1 (denominator 2))
- (pass-if-equal "-1" 1 (denominator -1))
- (pass-if-equal "-2" 1 (denominator -2))
- (pass-if-equal "0.0" 1.0 (denominator 0.0))
- (pass-if-equal "1.0" 1.0 (denominator 1.0))
- (pass-if-equal "2.0" 1.0 (denominator 2.0))
- (pass-if-equal "-0.0" 1.0 (denominator -0.0))
- (pass-if-equal "-1.0" 1.0 (denominator -1.0))
- (pass-if-equal "-2.0" 1.0 (denominator -2.0))
- (pass-if-equal "0.5" 2.0 (denominator 0.5))
- (pass-if-equal "0.25" 4.0 (denominator 0.25))
- (pass-if-equal "0.75" 4.0 (denominator 0.75))
- (pass-if-equal "+inf.0" 1.0 (denominator +inf.0))
- (pass-if-equal "-inf.0" 1.0 (denominator -inf.0)))
- ;;;
- ;;; gcd
- ;;;
- (with-test-prefix "gcd"
- (pass-if "documented?"
- (documented? gcd))
- (with-test-prefix "(n)"
- (pass-if "n = -2"
- (eqv? 2 (gcd -2))))
- (with-test-prefix "(0 n)"
- (pass-if "n = 0"
- (eqv? 0 (gcd 0 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd 0 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd 0 -1)))
- (pass-if "n = fixnum-max"
- (eqv? fixnum-max (gcd 0 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? (- fixnum-min) (gcd 0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1)))))
- (with-test-prefix "(n 0)"
- (pass-if "n = 2^128 * fixnum-max"
- (eqv? (ash fixnum-max 128) (gcd (ash fixnum-max 128) 0))))
- (with-test-prefix "(1 n)"
- (pass-if "n = 0"
- (eqv? 1 (gcd 1 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd 1 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd 1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (gcd 1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (gcd 1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (gcd 1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (gcd 1 (- fixnum-min 1)))))
- (with-test-prefix "(-1 n)"
- (pass-if "n = 0"
- (eqv? 1 (gcd -1 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd -1 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd -1 -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (gcd -1 fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (gcd -1 (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (gcd -1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (gcd -1 (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-max n)"
- (pass-if "n = 0"
- (eqv? fixnum-max (gcd fixnum-max 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd fixnum-max 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd fixnum-max -1)))
- (pass-if "n = fixnum-max"
- (eqv? fixnum-max (gcd fixnum-max fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (gcd fixnum-max (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (gcd fixnum-max fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (gcd fixnum-max (- fixnum-min 1)))))
- (with-test-prefix "((+ fixnum-max 1) n)"
- (pass-if "n = 0"
- (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd (+ fixnum-max 1) 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd (+ fixnum-max 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1)))))
- (with-test-prefix "(fixnum-min n)"
- (pass-if "n = 0"
- (eqv? (- fixnum-min) (gcd fixnum-min 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd fixnum-min 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd fixnum-min -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (gcd fixnum-min fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? 1 (gcd fixnum-min (- fixnum-min 1)))))
- (with-test-prefix "((- fixnum-min 1) n)"
- (pass-if "n = 0"
- (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0)))
- (pass-if "n = 1"
- (eqv? 1 (gcd (- fixnum-min 1) 1)))
- (pass-if "n = -1"
- (eqv? 1 (gcd (- fixnum-min 1) -1)))
- (pass-if "n = fixnum-max"
- (eqv? 1 (gcd (- fixnum-min 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (eqv? 1 (gcd (- fixnum-min 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1)))))
- (with-test-prefix "flonum arguments"
- (pass-if-equal "flonum"
- 15.0
- (gcd -15.0))
- (pass-if-equal "flonum/flonum"
- 3.0
- (gcd 6.0 -15.0))
- (pass-if-equal "flonum/fixnum"
- 3.0
- (gcd 6.0 -15))
- (pass-if-equal "fixnum/flonum"
- 3.0
- (gcd -6 15.0))
- (pass-if-equal "flonum/bignum"
- 2.0
- (gcd -6.0 (expt 2 fixnum-bit)))
- (pass-if-equal "bignum/flonum"
- 3.0
- (gcd (- (expt 3 fixnum-bit)) 6.0)))
- ;; Are wrong type arguments detected correctly?
- )
- ;;;
- ;;; lcm
- ;;;
- (with-test-prefix "lcm"
- ;; FIXME: more tests?
- ;; (some of these are already in r4rs.test)
- (pass-if (documented? lcm))
- (pass-if-equal 1 (lcm))
- (pass-if-equal 15 (lcm -15))
- (pass-if-equal 288 (lcm 32 -36))
- (with-test-prefix "flonum arguments"
- (pass-if-equal "flonum"
- 15.0
- (lcm -15.0))
- (pass-if-equal "flonum/flonum"
- 30.0
- (lcm 6.0 -15.0))
- (pass-if-equal "flonum/fixnum"
- 30.0
- (lcm 6.0 -15))
- (pass-if-equal "fixnum/flonum"
- 30.0
- (lcm -6 15.0))
- (pass-if "flonum/bignum"
- (let ((want (* 3.0 (expt 2 fixnum-bit)))
- (got (lcm -6.0 (expt 2 fixnum-bit))))
- (and (inexact? got)
- (test-eqv? 1.0 (/ want got)))))
- (pass-if "bignum/flonum"
- (let ((want (* 2.0 (expt 3 fixnum-bit)))
- (got (lcm (- (expt 3 fixnum-bit)) 6.0)))
- (and (inexact? got)
- (test-eqv? 1.0 (/ want got))))))
- (let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256
- (lcm-of-big-n-and-11 1273712981610478149659280835095566986385969831322046204434033424087044426039296))
- (pass-if (= lcm-of-big-n-and-11 (lcm big-n 11)))
- (pass-if (= lcm-of-big-n-and-11 (lcm 11 big-n 11)))))
- ;;;
- ;;; rationalize
- ;;;
- (with-test-prefix "rationalize"
- (pass-if (documented? rationalize))
- (pass-if (eqv? 2 (rationalize 4 2 )))
- (pass-if (eqv? -2 (rationalize -4 2 )))
- (pass-if (eqv? 2.0 (rationalize 4 2.0)))
- (pass-if (eqv? -2.0 (rationalize -4.0 2 )))
- (pass-if (eqv? 0 (rationalize 4 8 )))
- (pass-if (eqv? 0 (rationalize -4 8 )))
- (pass-if (eqv? 0.0 (rationalize 4 8.0)))
- (pass-if (eqv? 0.0 (rationalize -4.0 8 )))
- (pass-if (eqv? 0.0 (rationalize 3 +inf.0)))
- (pass-if (eqv? 0.0 (rationalize -3 +inf.0)))
- (pass-if (real-nan? (rationalize +inf.0 +inf.0)))
- (pass-if (real-nan? (rationalize +nan.0 +inf.0)))
- (pass-if (real-nan? (rationalize +nan.0 4)))
- (pass-if (eqv? +inf.0 (rationalize +inf.0 3)))
- (pass-if (eqv? 3/10 (rationalize 3/10 0)))
- (pass-if (eqv? -3/10 (rationalize -3/10 0)))
- (pass-if (eqv? 1/3 (rationalize 3/10 1/10)))
- (pass-if (eqv? -1/3 (rationalize -3/10 1/10)))
- (pass-if (eqv? 1/3 (rationalize 3/10 -1/10)))
- (pass-if (eqv? -1/3 (rationalize -3/10 -1/10)))
- ;; Prior to Guile 2.0.10, rationalize used a faulty algorithm that
- ;; incorrectly returned 2/3 and -2/3 in the following cases.
- (pass-if (eqv? 1/2 (rationalize #e+0.67 1/4)))
- (pass-if (eqv? -1/2 (rationalize #e-0.67 1/4)))
- (pass-if (eqv? 1 (rationalize #e+0.67 1/3)))
- (pass-if (eqv? -1 (rationalize #e-0.67 1/3)))
- (pass-if (eqv? 1/2 (rationalize #e+0.66 1/3)))
- (pass-if (eqv? -1/2 (rationalize #e-0.66 1/3)))
- (pass-if (eqv? 1 (rationalize #e+0.67 2/3)))
- (pass-if (eqv? -1 (rationalize #e-0.67 2/3)))
- (pass-if (eqv? 0 (rationalize #e+0.66 2/3)))
- (pass-if (eqv? 0 (rationalize #e-0.66 2/3)))
- ;; Prior to Guile 2.0.10, rationalize used a faulty algorithm that
- ;; incorrectly computed the following approximations of PI.
- (with-test-prefix "pi"
- (define *pi* #e3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679)
- (pass-if (eqv? 16/5 (rationalize *pi* 1/10)))
- (pass-if (eqv? 201/64 (rationalize *pi* 1/1000)))
- (pass-if (eqv? 75948/24175 (rationalize *pi* (expt 10 -7))))
- (pass-if (eqv? 100798/32085 (rationalize *pi* (expt 10 -8))))
- (pass-if (eqv? 58466453/18610450 (rationalize *pi* (expt 10 -14))))
- (pass-if (eqv? 2307954651196778721982809475299879198775111361078/734644782339796933783743757007944508986600750685
- (rationalize *pi* (expt 10 -95)))))
- (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 1/10)))
- (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 1/10)))
- (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 -1/10)))
- (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 -1/10))))
- ;;;
- ;;; number->string
- ;;;
- (with-test-prefix "number->string"
- (let ((num->str->num
- (lambda (n radix)
- (string->number (number->string n radix) radix))))
- (define (test num)
- (pass-if-equal (list num 'pos)
- num
- (num->str->num num 10))
- (pass-if-equal (list num 'neg)
- (- num)
- (num->str->num (- num) 10)))
- (pass-if (documented? number->string))
- (pass-if (string=? (number->string 0) "0"))
- (pass-if (string=? (number->string 171) "171"))
- (pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10)))
- (pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10)))
- (test (inf))
- (test 1.3)
- (test (acos -1)) ; pi
- (test (exp 1)) ; e
- (test (/ 3.0))
- (test (/ 7.0))
- (test 2.2250738585072011e-308)
- (test 2.2250738585072012e-308)
- ;; Largest finite inexact
- (test (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
- (pass-if (string=? "0.0" (number->string 0.0)))
- (pass-if (or (eqv? 0.0 -0.0)
- (string=? "-0.0" (number->string -0.0))))
- (pass-if (string=? (number->string 35.25 36) "z.9"))
- (pass-if (string=? (number->string 0.25 2) "0.01"))
- (pass-if (string=? (number->string 255.0625 16) "ff.1"))
- (pass-if (string=? (number->string (/ 1 3) 3) "1/10"))
- (pass-if (string=? (number->string 10) "10"))
- (pass-if (string=? (number->string 10 11) "a"))
- (pass-if (string=? (number->string 36 36) "10"))
- (pass-if (= (num->str->num 36 36) 36))
- (pass-if (= (string->number "z" 36) 35))
- (pass-if (= (string->number "Z" 36) 35))
- (pass-if (not (string->number "Z" 35)))
- (pass-if (string=? (number->string 35 36) "z"))
- (pass-if (= (num->str->num 35 36) 35))
- (pass-if (string=? (number->string 12342342340000.0) "1.234234234e13"))
- (pass-if (string=? (number->string 1234234234000.0) "1234234234000.0"))
- (pass-if (string=? (number->string 1240000.0) "1240000.0"))
- (with-test-prefix "powers of radix"
- (for-each
- (lambda (radix)
- (for-each (lambda (k)
- (let ((val (exact->inexact (expt radix k)))
- (str (if (<= -3 k 6)
- (assoc-ref '((-3 . "0.001")
- (-2 . "0.01")
- (-1 . "0.1")
- ( 0 . "1.0")
- ( 1 . "10.0")
- ( 2 . "100.0")
- ( 3 . "1000.0")
- ( 4 . "10000.0")
- ( 5 . "100000.0")
- ( 6 . "1000000.0"))
- k)
- (string-append "1.0e"
- (number->string k radix)))))
- (pass-if-equal (list radix k 'pos)
- str
- (number->string val radix))
- (pass-if-equal (list radix k 'neg)
- (string-append "-" str)
- (number->string (- val) radix))))
- (iota 41 -20)))
- (iota 35 2)))
- (with-test-prefix "multiples of smallest inexact"
- (for-each (lambda (k)
- (let ((val (* k (expt 2.0 (- dbl-min-exp dbl-mant-dig)))))
- (test val)))
- (iota 40 1)))
- (with-test-prefix "one plus multiples of epsilon"
- (for-each (lambda (k)
- (let ((val (+ 1.0 (* k dbl-epsilon))))
- (test val)))
- (iota 40 1)))
- (with-test-prefix "one minus multiples of 1/2 epsilon"
- (for-each (lambda (k)
- (let ((val (- 1.0 (* k 1/2 dbl-epsilon))))
- (test val)))
- (iota 40 1)))
- ;; Before Guile 2.0.1, even in the presence of a #e forced exactness
- ;; specifier, negative exponents were applied inexactly and then
- ;; later coerced to exact, yielding an incorrect fraction.
- (pass-if (eqv? (string->number "#e1e-10") 1/10000000000))
- (pass-if (string=? (number->string 11.33333333333333333 12)
- "b.4"))
- (pass-if (string=? (number->string 1.324e44 16)
- "5.efe0a14fafdf8e24"))))
-
- ;;;
- ;;; string->number
- ;;;
- (with-test-prefix "string->number"
- (pass-if "documented?"
- (documented? string->number))
- (pass-if "non number strings"
- (for-each (lambda (x) (if (string->number x) (throw 'fail)))
- '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@"
- "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "."
- "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2"
- "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc"
- "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1"
- "#i#i1" "12@12+0i" "3/0" "0/0" "4+3/0i" "4/0-3i" "2+0/0i"
- "nan.0" "inf.0" "#e+nan.0" "#e+inf.0" "#e-inf.0"
- "3@inf.0" "4@nan.0"))
- #t)
- (pass-if "valid number strings"
- (for-each (lambda (couple)
- (apply
- (lambda (x y)
- (let ((xx (string->number x)))
- (if (or (eq? xx #f) (not (eqv? xx y)))
- (begin
- (pk x y)
- (throw 'fail)))))
- couple))
- `(;; Radix:
- ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0)
- ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3)
- ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6)
- ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1)
- ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4)
- ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7)
- ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9)
- ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11)
- ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13)
- ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15)
- ("#b1010" 10)
- ("#o12345670" 2739128)
- ("#d1234567890" 1234567890)
- ("#x1234567890abcdef" 1311768467294899695)
- ;; Exactness:
- ("#e1" 1) ("#e1.2" 12/10)
- ("#i1.1" 1.1) ("#i1" 1.0)
- ;; Integers:
- ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1))
- ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0)
- ("#b#i100" 4.0)
- ;; Fractions:
- ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0)
- ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1)
- ("#i6/8" 0.75) ("#i1/1" 1.0)
- ;; Infinities and NaNs:
- ("+inf.0" ,(inf)) ("-inf.0" ,(- (inf)))
- ("+Inf.0" ,(inf)) ("-Inf.0" ,(- (inf)))
- ("+InF.0" ,(inf)) ("-InF.0" ,(- (inf)))
- ("+INF.0" ,(inf)) ("-INF.0" ,(- (inf)))
- ("#i+InF.0" ,(inf)) ("#i-InF.0" ,(- (inf)))
- ("+nan.0" ,(nan)) ("-nan.0" ,(nan))
- ("#i+nan.0" ,(nan)) ("#i-nan.0" ,(nan))
- ;; Decimal numbers:
- ;; * <uinteger 10> <suffix>
- ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0)
- ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0)
- ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01)
- ;; * . <digit 10>+ #* <suffix>
- (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16)
- (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3)
- ;; * <digit 10>+ . <digit 10>* #* <suffix>
- ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3))
- ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1)
- ("3.1#e0" 3.1)
- ;; * <digit 10>+ #+ . #* <suffix>
- ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0)
- ))
- #t)
- (pass-if "valid complex number strings"
- (for-each (lambda (triple)
- (apply
- (lambda (str re im)
- (let ((z (string->number str)))
- (if (or (eq? z #f)
- (not (and (eqv? (real-part z) re)
- (eqv? (imag-part z) im))))
- (begin
- (pk str re im)
- (throw 'fail)))))
- triple))
- `(("1@0" 1 0) ("1@+0" 1 0) ("1@-0" 1 0) ("1/2@0" 1/2 0)
- ("1.0@0" 1.0 0) ("1.0@-0" 1.0 0)
- ("#e1@0" 1 0) ("#e1@+0" 1 0) ("#e1@-0" 1 0) ("#e0.5@0.0" 1/2 0)
- ("#e1.0@0" 1 0) ("#e1.0@-0" 1 0)
- ("#i1@0" 1.0 0.0) ("#i1@+0" 1.0 0.0) ("#i1@-0" 1.0 -0.0) ("#i1/2@0" 0.5 0.0)
- ("#i1.0@0" 1.0 0.0) ("#i1.0@-0" 1.0 -0.0)
- ("1@+0.0" 1.0 0.0) ("1.0@-0.0" 1.0 -0.0)
- ("2+3i" 2.0 3.0) ("4-5i" 4.0 -5.0)
- ("1+i" 1.0 1.0) ("1-i" 1.0 -1.0) ("+1i" 0.0 1.0) ("-1i" 0.0 -1.0)
- ("+i" 0.0 1.0) ("-i" 0.0 -1.0)
- ("1.0+.1i" 1.0 0.1) ("1.0-.1i" 1.0 -0.1)
- (".1+.0i" 0.1 0.0) ("1.+.0i" 1.0 0.0) (".1+.1i" 0.1 0.1)
- ("1e1+.1i" 10.0 0.1)
- ("0@+nan.0" 0 0) ("0@+inf.0" 0 0) ("0@-inf.0" 0 0)
- ("0.0@+nan.0" 0.0 0.0) ("0.0@+inf.0" 0.0 0.0) ("0.0@-inf.0" 0.0 0.0)
- ("#i0@+nan.0" 0.0 0.0) ("#i0@+inf.0" 0.0 0.0) ("#i0@-inf.0" 0.0 0.0)
- ("0.0@1" 0.0 0.0) ("0.0@2" -0.0 0.0) ("0.0@4" -0.0 -0.0) ("0.0@5" 0.0 -0.0)
- ))
- #t)
- (pass-if-exception "exponent too big"
- exception:out-of-range
- (string->number "12.13e141414"))
- ;; in guile 1.6.7 and earlier, bad polar forms (where the conversion of
- ;; the angle gave #f) caused a segv
- (pass-if "1@a"
- (eq? #f (string->number "1@a"))))
- ;;;
- ;;; number?
- ;;;
- (with-test-prefix "number?"
- (pass-if (documented? number?))
- (pass-if (number? 0))
- (pass-if (number? 7))
- (pass-if (number? -7))
- (pass-if (number? 1.3))
- (pass-if (number? (+ 1 fixnum-max)))
- (pass-if (number? (- 1 fixnum-min)))
- (pass-if (number? 3+4i))
- (pass-if (not (number? #\a)))
- (pass-if (not (number? "a")))
- (pass-if (not (number? (make-vector 0))))
- (pass-if (not (number? (cons 1 2))))
- (pass-if (not (number? #t)))
- (pass-if (not (number? (lambda () #t))))
- (pass-if (not (number? (current-input-port)))))
- ;;;
- ;;; complex?
- ;;;
- (with-test-prefix "complex?"
- (pass-if (documented? complex?))
- (pass-if (complex? 0))
- (pass-if (complex? 7))
- (pass-if (complex? -7))
- (pass-if (complex? (+ 1 fixnum-max)))
- (pass-if (complex? (- 1 fixnum-min)))
- (pass-if (complex? 1.3))
- (pass-if (complex? 3+4i))
- (pass-if (not (complex? #\a)))
- (pass-if (not (complex? "a")))
- (pass-if (not (complex? (make-vector 0))))
- (pass-if (not (complex? (cons 1 2))))
- (pass-if (not (complex? #t)))
- (pass-if (not (complex? (lambda () #t))))
- (pass-if (not (complex? (current-input-port)))))
- ;;;
- ;;; real?
- ;;;
- (with-test-prefix "real?"
- (pass-if (documented? real?))
- (pass-if (real? 0))
- (pass-if (real? 7))
- (pass-if (real? -7))
- (pass-if (real? (+ 1 fixnum-max)))
- (pass-if (real? (- 1 fixnum-min)))
- (pass-if (real? 1.3))
- (pass-if (real? +inf.0))
- (pass-if (real? -inf.0))
- (pass-if (real? +nan.0))
- (pass-if (not (real? +inf.0-inf.0i)))
- (pass-if (not (real? +nan.0+nan.0i)))
- (pass-if (not (real? 3+4i)))
- (pass-if (not (real? #\a)))
- (pass-if (not (real? "a")))
- (pass-if (not (real? (make-vector 0))))
- (pass-if (not (real? (cons 1 2))))
- (pass-if (not (real? #t)))
- (pass-if (not (real? (lambda () #t))))
- (pass-if (not (real? (current-input-port)))))
- ;;;
- ;;; rational?
- ;;;
- (with-test-prefix "rational?"
- (pass-if (documented? rational?))
- (pass-if (rational? 0))
- (pass-if (rational? 7))
- (pass-if (rational? -7))
- (pass-if (rational? (+ 1 fixnum-max)))
- (pass-if (rational? (- 1 fixnum-min)))
- (pass-if (rational? 1.3))
- (pass-if (not (rational? +inf.0)))
- (pass-if (not (rational? -inf.0)))
- (pass-if (not (rational? +nan.0)))
- (pass-if (not (rational? +inf.0-inf.0i)))
- (pass-if (not (rational? +nan.0+nan.0i)))
- (pass-if (not (rational? 3+4i)))
- (pass-if (not (rational? #\a)))
- (pass-if (not (rational? "a")))
- (pass-if (not (rational? (make-vector 0))))
- (pass-if (not (rational? (cons 1 2))))
- (pass-if (not (rational? #t)))
- (pass-if (not (rational? (lambda () #t))))
- (pass-if (not (rational? (current-input-port)))))
- ;;;
- ;;; integer?
- ;;;
- (with-test-prefix "integer?"
- (pass-if (documented? integer?))
- (pass-if (integer? 0))
- (pass-if (integer? 7))
- (pass-if (integer? -7))
- (pass-if (integer? (+ 1 fixnum-max)))
- (pass-if (integer? (- 1 fixnum-min)))
- (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i)))
- (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0)))
- (pass-if (not (integer? 1.3)))
- (pass-if (not (integer? +inf.0)))
- (pass-if (not (integer? -inf.0)))
- (pass-if (not (integer? +nan.0)))
- (pass-if (not (integer? +inf.0-inf.0i)))
- (pass-if (not (integer? +nan.0+nan.0i)))
- (pass-if (not (integer? 3+4i)))
- (pass-if (not (integer? #\a)))
- (pass-if (not (integer? "a")))
- (pass-if (not (integer? (make-vector 0))))
- (pass-if (not (integer? (cons 1 2))))
- (pass-if (not (integer? #t)))
- (pass-if (not (integer? (lambda () #t))))
- (pass-if (not (integer? (current-input-port)))))
- ;;;
- ;;; exact-integer?
- ;;;
- (with-test-prefix "exact-integer?"
- (pass-if (documented? exact-integer?))
- (pass-if (exact-integer? 0))
- (pass-if (exact-integer? 7))
- (pass-if (exact-integer? -7))
- (pass-if (exact-integer? (+ 1 fixnum-max)))
- (pass-if (exact-integer? (- 1 fixnum-min)))
- (pass-if (and (= 1.0 (round 1.0))
- (not (exact-integer? 1.0))))
- (pass-if (not (exact-integer? 1.3)))
- (pass-if (not (exact-integer? +inf.0)))
- (pass-if (not (exact-integer? -inf.0)))
- (pass-if (not (exact-integer? +nan.0)))
- (pass-if (not (exact-integer? +inf.0-inf.0i)))
- (pass-if (not (exact-integer? +nan.0+nan.0i)))
- (pass-if (not (exact-integer? 3+4i)))
- (pass-if (not (exact-integer? #\a)))
- (pass-if (not (exact-integer? "a")))
- (pass-if (not (exact-integer? (make-vector 0))))
- (pass-if (not (exact-integer? (cons 1 2))))
- (pass-if (not (exact-integer? #t)))
- (pass-if (not (exact-integer? (lambda () #t))))
- (pass-if (not (exact-integer? (current-input-port)))))
- ;;;
- ;;; inexact?
- ;;;
- (with-test-prefix "inexact?"
- (pass-if (documented? inexact?))
- (pass-if (not (inexact? 0)))
- (pass-if (not (inexact? 7)))
- (pass-if (not (inexact? -7)))
- (pass-if (not (inexact? (+ 1 fixnum-max))))
- (pass-if (not (inexact? (- 1 fixnum-min))))
- (pass-if (inexact? 1.3))
- (pass-if (inexact? 3.1+4.2i))
- (pass-if (inexact? +inf.0))
- (pass-if (inexact? -inf.0))
- (pass-if (inexact? +nan.0))
- (pass-if-exception "char"
- exception:wrong-type-arg
- (not (inexact? #\a)))
- (pass-if-exception "string"
- exception:wrong-type-arg
- (not (inexact? "a")))
- (pass-if-exception "vector"
- exception:wrong-type-arg
- (not (inexact? (make-vector 0))))
- (pass-if-exception "cons"
- exception:wrong-type-arg
- (not (inexact? (cons 1 2))))
- (pass-if-exception "bool"
- exception:wrong-type-arg
- (not (inexact? #t)))
- (pass-if-exception "procedure"
- exception:wrong-type-arg
- (not (inexact? (lambda () #t))))
- (pass-if-exception "port"
- exception:wrong-type-arg
- (not (inexact? (current-input-port)))))
- ;;;
- ;;; equal?
- ;;;
- (with-test-prefix "equal?"
- (pass-if (documented? equal?))
- ;; The following test will fail on platforms
- ;; without distinct signed zeroes 0.0 and -0.0.
- (pass-if (not (equal? 0.0 -0.0)))
- (pass-if (equal? 0 0))
- (pass-if (equal? 7 7))
- (pass-if (equal? -7 -7))
- (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max)))
- (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1)))
- (pass-if (equal? 0.0 0.0))
- (pass-if (equal? -0.0 -0.0))
- (pass-if (equal? 0.0+0.0i 0.0+0.0i))
- (pass-if (equal? 0.0-0.0i 0.0-0.0i))
- (pass-if (equal? -0.0+0.0i -0.0+0.0i))
- (pass-if (not (equal? 0 1)))
- (pass-if (not (equal? 0 0.0)))
- (pass-if (not (equal? 1 1.0)))
- (pass-if (not (equal? 0.0 0)))
- (pass-if (not (equal? 1.0 1)))
- (pass-if (not (equal? -1.0 -1)))
- (pass-if (not (equal? 1.0 1.0+0.0i)))
- (pass-if (not (equal? 0.0 0.0+0.0i)))
- (pass-if (not (equal? 0.0+0.0i 0.0-0.0i)))
- (pass-if (not (equal? 0.0+0.0i -0.0+0.0i)))
- (pass-if (not (equal? fixnum-max (+ 1 fixnum-max))))
- (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max)))
- (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max))))
- (pass-if (not (equal? fixnum-min (- fixnum-min 1))))
- (pass-if (not (equal? (- fixnum-min 1) fixnum-min)))
- (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2))))
- (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1))))
- (pass-if (not (equal? (ash 1 256) +inf.0)))
- (pass-if (not (equal? +inf.0 (ash 1 256))))
- (pass-if (not (equal? (ash 1 256) -inf.0)))
- (pass-if (not (equal? -inf.0 (ash 1 256))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (pass-if (not (equal? (ash 1 1024) +inf.0)))
- (pass-if (not (equal? +inf.0 (ash 1 1024))))
- (pass-if (not (equal? (- (ash 1 1024)) -inf.0)))
- (pass-if (not (equal? -inf.0 (- (ash 1 1024)))))
- (pass-if (equal? +nan.0 +nan.0))
- (pass-if (equal? +nan.0 +nan.0))
- (pass-if (not (equal? +nan.0 0.0+nan.0i)))
- (pass-if (not (equal? 0 +nan.0)))
- (pass-if (not (equal? +nan.0 0)))
- (pass-if (not (equal? 1 +nan.0)))
- (pass-if (not (equal? +nan.0 1)))
- (pass-if (not (equal? -1 +nan.0)))
- (pass-if (not (equal? +nan.0 -1)))
- (pass-if (not (equal? (ash 1 256) +nan.0)))
- (pass-if (not (equal? +nan.0 (ash 1 256))))
- (pass-if (not (equal? (- (ash 1 256)) +nan.0)))
- (pass-if (not (equal? +nan.0 (- (ash 1 256)))))
- (pass-if (not (equal? (ash 1 8192) +nan.0)))
- (pass-if (not (equal? +nan.0 (ash 1 8192))))
- (pass-if (not (equal? (- (ash 1 8192)) +nan.0)))
- (pass-if (not (equal? +nan.0 (- (ash 1 8192)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (not (equal? (ash 3 1023) +nan.0)))
- (pass-if (not (equal? +nan.0 (ash 3 1023)))))
- ;;;
- ;;; eqv?
- ;;;
- (with-test-prefix "eqv?"
- (pass-if (documented? eqv?))
- ;; The following test will fail on platforms
- ;; without distinct signed zeroes 0.0 and -0.0.
- (pass-if (not (eqv? 0.0 -0.0)))
- (pass-if (eqv? 0 0))
- (pass-if (eqv? 7 7))
- (pass-if (eqv? -7 -7))
- (pass-if (eqv? (+ 1 fixnum-max) (+ 1 fixnum-max)))
- (pass-if (eqv? (- fixnum-min 1) (- fixnum-min 1)))
- (pass-if (eqv? 0.0 0.0))
- (pass-if (eqv? -0.0 -0.0))
- (pass-if (eqv? 0.0+0.0i 0.0+0.0i))
- (pass-if (eqv? 0.0-0.0i 0.0-0.0i))
- (pass-if (eqv? -0.0+0.0i -0.0+0.0i))
- (pass-if (not (eqv? 0.0 -0.0)))
- (pass-if (not (eqv? 0.0 0.0+0.0i)))
- (pass-if (not (eqv? 0.0+0.0i 0.0-0.0i)))
- (pass-if (not (eqv? 0.0+0.0i -0.0+0.0i)))
- (pass-if (not (eqv? 0 1)))
- (pass-if (not (eqv? 0 0.0)))
- (pass-if (not (eqv? 1 1.0)))
- (pass-if (not (eqv? 0.0 0)))
- (pass-if (not (eqv? 1.0 1)))
- (pass-if (not (eqv? -1.0 -1)))
- (pass-if (not (eqv? 1.0 1.0+0.0i)))
- (pass-if (not (eqv? 0.0 0.0+0.0i)))
- (pass-if (not (eqv? fixnum-max (+ 1 fixnum-max))))
- (pass-if (not (eqv? (+ 1 fixnum-max) fixnum-max)))
- (pass-if (not (eqv? (+ 1 fixnum-max) (+ 2 fixnum-max))))
- (pass-if (not (eqv? fixnum-min (- fixnum-min 1))))
- (pass-if (not (eqv? (- fixnum-min 1) fixnum-min)))
- (pass-if (not (eqv? (- fixnum-min 1) (- fixnum-min 2))))
- (pass-if (not (eqv? (+ fixnum-max 1) (- fixnum-min 1))))
- (pass-if (not (eqv? (ash 1 256) +inf.0)))
- (pass-if (not (eqv? +inf.0 (ash 1 256))))
- (pass-if (not (eqv? (ash 1 256) -inf.0)))
- (pass-if (not (eqv? -inf.0 (ash 1 256))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (pass-if (not (eqv? (ash 1 1024) +inf.0)))
- (pass-if (not (eqv? +inf.0 (ash 1 1024))))
- (pass-if (not (eqv? (- (ash 1 1024)) -inf.0)))
- (pass-if (not (eqv? -inf.0 (- (ash 1 1024)))))
- (pass-if (eqv? +nan.0 +nan.0))
- (pass-if (not (eqv? +nan.0 0.0+nan.0i)))
- (pass-if (not (eqv? 0 +nan.0)))
- (pass-if (not (eqv? +nan.0 0)))
- (pass-if (not (eqv? 1 +nan.0)))
- (pass-if (not (eqv? +nan.0 1)))
- (pass-if (not (eqv? -1 +nan.0)))
- (pass-if (not (eqv? +nan.0 -1)))
- (pass-if (not (eqv? (ash 1 256) +nan.0)))
- (pass-if (not (eqv? +nan.0 (ash 1 256))))
- (pass-if (not (eqv? (- (ash 1 256)) +nan.0)))
- (pass-if (not (eqv? +nan.0 (- (ash 1 256)))))
- (pass-if (not (eqv? (ash 1 8192) +nan.0)))
- (pass-if (not (eqv? +nan.0 (ash 1 8192))))
- (pass-if (not (eqv? (- (ash 1 8192)) +nan.0)))
- (pass-if (not (eqv? +nan.0 (- (ash 1 8192)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (not (eqv? (ash 3 1023) +nan.0)))
- (pass-if (not (eqv? +nan.0 (ash 3 1023)))))
- ;;;
- ;;; =
- ;;;
- (with-test-prefix "="
- (pass-if (documented? =))
- (pass-if (= 7 7))
- (pass-if (= -7 -7))
- (pass-if (= 1.0 1))
- (pass-if (= 1 1.0))
- (pass-if (= -1 -1.0))
- (pass-if (= 0.0 0.0))
- (pass-if (= 0.0 -0.0))
- (pass-if (= 1 1.0+0.0i))
- (pass-if (= 0 0))
- (pass-if (= 0 0.0))
- (pass-if (= 0 -0.0))
- (pass-if (= 0 0.0+0.0i))
- (pass-if (= 0 0.0-0.0i))
- (pass-if (= 0 0.0+0.0i))
- (pass-if (= 0 -0.0-0.0i))
- (pass-if (= 0 0))
- (pass-if (= 0.0 0))
- (pass-if (= -0.0 0))
- (pass-if (= 0.0+0.0i 0))
- (pass-if (= 0.0-0.0i 0))
- (pass-if (= 0.0+0.0i 0))
- (pass-if (= -0.0-0.0i 0))
- (pass-if (= 0.0+0.0i 0.0-0.0i))
- (pass-if (= 0.0+0.0i -0.0+0.0i))
- (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max)))
- (pass-if (= (- fixnum-min 1) (- fixnum-min 1)))
- (pass-if (not (= 0 1)))
- (pass-if (not (= fixnum-max (+ 1 fixnum-max))))
- (pass-if (not (= (+ 1 fixnum-max) fixnum-max)))
- (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max))))
- (pass-if (not (= fixnum-min (- fixnum-min 1))))
- (pass-if (not (= (- fixnum-min 1) fixnum-min)))
- (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2))))
- (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1))))
- (pass-if (not (= (ash 1 256) +inf.0)))
- (pass-if (not (= +inf.0 (ash 1 256))))
- (pass-if (not (= (ash 1 256) -inf.0)))
- (pass-if (not (= -inf.0 (ash 1 256))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (pass-if (not (= (ash 1 1024) +inf.0)))
- (pass-if (not (= +inf.0 (ash 1 1024))))
- (pass-if (not (= (- (ash 1 1024)) -inf.0)))
- (pass-if (not (= -inf.0 (- (ash 1 1024)))))
- (pass-if (not (= +nan.0 +nan.0)))
- (pass-if (not (= 0 +nan.0)))
- (pass-if (not (= +nan.0 0)))
- (pass-if (not (= 1 +nan.0)))
- (pass-if (not (= +nan.0 1)))
- (pass-if (not (= -1 +nan.0)))
- (pass-if (not (= +nan.0 -1)))
- (pass-if (not (= (ash 1 256) +nan.0)))
- (pass-if (not (= +nan.0 (ash 1 256))))
- (pass-if (not (= (- (ash 1 256)) +nan.0)))
- (pass-if (not (= +nan.0 (- (ash 1 256)))))
- (pass-if (not (= (ash 1 8192) +nan.0)))
- (pass-if (not (= +nan.0 (ash 1 8192))))
- (pass-if (not (= (- (ash 1 8192)) +nan.0)))
- (pass-if (not (= +nan.0 (- (ash 1 8192)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (not (= (ash 3 1023) +nan.0)))
- (pass-if (not (= +nan.0 (ash 3 1023))))
- (pass-if (= 1/2 0.5))
- (pass-if (not (= 1/3 0.333333333333333333333333333333333)))
- (pass-if (not (= 2/3 0.5)))
- (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000))))))
- (pass-if (= 1/2 0.5+0i))
- (pass-if (not (= 0.333333333333333333333333333333333 1/3)))
- (pass-if (not (= 2/3 0.5+0i)))
- (pass-if (not (= 1/2 0+0.5i)))
- (pass-if (= 0.5 1/2))
- (pass-if (not (= 0.5 2/3)))
- (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5)))
- (pass-if (= 0.5+0i 1/2))
- (pass-if (not (= 0.5+0i 2/3)))
- (pass-if (not (= 0+0.5i 1/2)))
- ;; prior to guile 1.8, inum/flonum comparisons were done just by
- ;; converting the inum to a double, which on a 64-bit would round making
- ;; say inexact 2^58 appear equal to exact 2^58+1
- (pass-if (= (ash-flo 1.0 58) (ash 1 58)))
- (pass-if (not (= (ash-flo 1.0 58) (1+ (ash 1 58)))))
- (pass-if (not (= (ash-flo 1.0 58) (1- (ash 1 58)))))
- (pass-if (= (ash 1 58) (ash-flo 1.0 58)))
- (pass-if (not (= (1+ (ash 1 58)) (ash-flo 1.0 58))))
- (pass-if (not (= (1- (ash 1 58)) (ash-flo 1.0 58))))
- ;; prior to guile 2.0.10, inum/complex comparisons were done just by
- ;; converting the inum to a double, which on a 64-bit would round making
- ;; say inexact 2^58 appear equal to exact 2^58+1
- (pass-if (= (+ +0.0i (ash-flo 1.0 58)) (ash 1 58)))
- (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1+ (ash 1 58)))))
- (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1- (ash 1 58)))))
- (pass-if (= (ash 1 58) (+ +0.0i (ash-flo 1.0 58))))
- (pass-if (not (= (1+ (ash 1 58)) (+ +0.0i (ash-flo 1.0 58)))))
- (pass-if (not (= (1- (ash 1 58)) (+ +0.0i (ash-flo 1.0 58)))))
- ;; prior to guile 2.0.10, fraction/flonum and fraction/complex
- ;; comparisons mishandled infinities.
- (pass-if (not (= 1/2 +inf.0)))
- (pass-if (not (= 1/2 -inf.0)))
- (pass-if (not (= +inf.0 1/2)))
- (pass-if (not (= -inf.0 1/2)))
- (pass-if (not (= 1/2 +inf.0+0.0i)))
- (pass-if (not (= 1/2 -inf.0+0.0i)))
- (pass-if (not (= +inf.0+0.0i 1/2)))
- (pass-if (not (= -inf.0+0.0i 1/2))))
- ;;;
- ;;; <
- ;;;
- (with-test-prefix "<"
- (pass-if "documented?"
- (documented? <))
- (with-test-prefix "(< 0 n)"
-
- (pass-if "n = 0"
- (not (< 0 0)))
-
- (pass-if "n = 0.0"
- (not (< 0 0.0)))
-
- (pass-if "n = 1"
- (< 0 1))
-
- (pass-if "n = 1.0"
- (< 0 1.0))
-
- (pass-if "n = -1"
- (not (< 0 -1)))
-
- (pass-if "n = -1.0"
- (not (< 0 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< 0 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< 0 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< 0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< 0 (- fixnum-min 1)))))
-
- (with-test-prefix "(< 0.0 n)"
-
- (pass-if "n = 0"
- (not (< 0.0 0)))
-
- (pass-if "n = 0.0"
- (not (< 0.0 0.0)))
-
- (pass-if "n = -0.0"
- (not (< 0.0 -0.0)))
-
- (pass-if "n = 1"
- (< 0.0 1))
-
- (pass-if "n = 1.0"
- (< 0.0 1.0))
-
- (pass-if "n = -1"
- (not (< 0.0 -1)))
-
- (pass-if "n = -1.0"
- (not (< 0.0 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< 0.0 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< 0.0 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< 0.0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< 0.0 (- fixnum-min 1)))))
- (pass-if (not (< -0.0 0.0)))
- (pass-if (not (< -0.0 -0.0)))
-
- (with-test-prefix "(< 1 n)"
-
- (pass-if "n = 0"
- (not (< 1 0)))
-
- (pass-if "n = 0.0"
- (not (< 1 0.0)))
-
- (pass-if "n = 1"
- (not (< 1 1)))
-
- (pass-if "n = 1.0"
- (not (< 1 1.0)))
-
- (pass-if "n = -1"
- (not (< 1 -1)))
-
- (pass-if "n = -1.0"
- (not (< 1 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< 1 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< 1 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< 1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< 1 (- fixnum-min 1)))))
- (with-test-prefix "(< 1.0 n)"
-
- (pass-if "n = 0"
- (not (< 1.0 0)))
-
- (pass-if "n = 0.0"
- (not (< 1.0 0.0)))
-
- (pass-if "n = 1"
- (not (< 1.0 1)))
-
- (pass-if "n = 1.0"
- (not (< 1.0 1.0)))
-
- (pass-if "n = -1"
- (not (< 1.0 -1)))
-
- (pass-if "n = -1.0"
- (not (< 1.0 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< 1.0 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< 1.0 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< 1.0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< 1.0 (- fixnum-min 1)))))
-
- (with-test-prefix "(< -1 n)"
-
- (pass-if "n = 0"
- (< -1 0))
-
- (pass-if "n = 0.0"
- (< -1 0.0))
-
- (pass-if "n = 1"
- (< -1 1))
-
- (pass-if "n = 1.0"
- (< -1 1.0))
-
- (pass-if "n = -1"
- (not (< -1 -1)))
-
- (pass-if "n = -1.0"
- (not (< -1 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< -1 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< -1 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< -1 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< -1 (- fixnum-min 1)))))
- (with-test-prefix "(< -1.0 n)"
-
- (pass-if "n = 0"
- (< -1.0 0))
-
- (pass-if "n = 0.0"
- (< -1.0 0.0))
-
- (pass-if "n = 1"
- (< -1.0 1))
-
- (pass-if "n = 1.0"
- (< -1.0 1.0))
-
- (pass-if "n = -1"
- (not (< -1.0 -1)))
-
- (pass-if "n = -1.0"
- (not (< -1.0 -1.0)))
-
- (pass-if "n = fixnum-max"
- (< -1.0 fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< -1.0 (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< -1.0 fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< -1.0 (- fixnum-min 1)))))
- (with-test-prefix "(< fixnum-max n)"
-
- (pass-if "n = 0"
- (not (< fixnum-max 0)))
-
- (pass-if "n = 0.0"
- (not (< fixnum-max 0.0)))
-
- (pass-if "n = 1"
- (not (< fixnum-max 1)))
-
- (pass-if "n = 1.0"
- (not (< fixnum-max 1.0)))
-
- (pass-if "n = -1"
- (not (< fixnum-max -1)))
-
- (pass-if "n = -1.0"
- (not (< fixnum-max -1.0)))
-
- (pass-if "n = fixnum-max"
- (not (< fixnum-max fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (< fixnum-max (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< fixnum-max fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< fixnum-max (- fixnum-min 1)))))
- (with-test-prefix "(< (+ fixnum-max 1) n)"
-
- (pass-if "n = 0"
- (not (< (+ fixnum-max 1) 0)))
-
- (pass-if "n = 0.0"
- (not (< (+ fixnum-max 1) 0.0)))
-
- (pass-if "n = 1"
- (not (< (+ fixnum-max 1) 1)))
-
- (pass-if "n = 1.0"
- (not (< (+ fixnum-max 1) 1.0)))
-
- (pass-if "n = -1"
- (not (< (+ fixnum-max 1) -1)))
-
- (pass-if "n = -1.0"
- (not (< (+ fixnum-max 1) -1.0)))
-
- (pass-if "n = fixnum-max"
- (not (< (+ fixnum-max 1) fixnum-max)))
- (pass-if "n = fixnum-max + 1"
- (not (< (+ fixnum-max 1) (+ fixnum-max 1))))
- (pass-if "n = fixnum-min"
- (not (< (+ fixnum-max 1) fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< (+ fixnum-max 1) (- fixnum-min 1)))))
- (with-test-prefix "(< fixnum-min n)"
-
- (pass-if "n = 0"
- (< fixnum-min 0))
-
- (pass-if "n = 0.0"
- (< fixnum-min 0.0))
-
- (pass-if "n = 1"
- (< fixnum-min 1))
-
- (pass-if "n = 1.0"
- (< fixnum-min 1.0))
-
- (pass-if "n = -1"
- (< fixnum-min -1))
-
- (pass-if "n = -1.0"
- (< fixnum-min -1.0))
-
- (pass-if "n = fixnum-max"
- (< fixnum-min fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< fixnum-min (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (not (< fixnum-min fixnum-min)))
- (pass-if "n = fixnum-min - 1"
- (not (< fixnum-min (- fixnum-min 1)))))
- (with-test-prefix "(< (- fixnum-min 1) n)"
-
- (pass-if "n = 0"
- (< (- fixnum-min 1) 0))
-
- (pass-if "n = 0.0"
- (< (- fixnum-min 1) 0.0))
-
- (pass-if "n = 1"
- (< (- fixnum-min 1) 1))
-
- (pass-if "n = 1.0"
- (< (- fixnum-min 1) 1.0))
-
- (pass-if "n = -1"
- (< (- fixnum-min 1) -1))
-
- (pass-if "n = -1.0"
- (< (- fixnum-min 1) -1.0))
-
- (pass-if "n = fixnum-max"
- (< (- fixnum-min 1) fixnum-max))
- (pass-if "n = fixnum-max + 1"
- (< (- fixnum-min 1) (+ fixnum-max 1)))
- (pass-if "n = fixnum-min"
- (< (- fixnum-min 1) fixnum-min))
- (pass-if "n = fixnum-min - 1"
- (not (< (- fixnum-min 1) (- fixnum-min 1)))))
- (pass-if (< (ash 1 256) +inf.0))
- (pass-if (not (< +inf.0 (ash 1 256))))
- (pass-if (not (< (ash 1 256) -inf.0)))
- (pass-if (< -inf.0 (ash 1 256)))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (pass-if (< (1- (ash 1 1024)) +inf.0))
- (pass-if (< (ash 1 1024) +inf.0))
- (pass-if (< (1+ (ash 1 1024)) +inf.0))
- (pass-if (not (< +inf.0 (1- (ash 1 1024)))))
- (pass-if (not (< +inf.0 (ash 1 1024))))
- (pass-if (not (< +inf.0 (1+ (ash 1 1024)))))
- (pass-if (< -inf.0 (- (1- (ash 1 1024)))))
- (pass-if (< -inf.0 (- (ash 1 1024))))
- (pass-if (< -inf.0 (- (1+ (ash 1 1024)))))
- (pass-if (not (< (- (1- (ash 1 1024))) -inf.0)))
- (pass-if (not (< (- (ash 1 1024)) -inf.0)))
- (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0)))
- (pass-if (not (< +nan.0 +nan.0)))
- (pass-if (not (< 0 +nan.0)))
- (pass-if (not (< +nan.0 0)))
- (pass-if (not (< 1 +nan.0)))
- (pass-if (not (< +nan.0 1)))
- (pass-if (not (< -1 +nan.0)))
- (pass-if (not (< +nan.0 -1)))
- (pass-if (not (< (ash 1 256) +nan.0)))
- (pass-if (not (< +nan.0 (ash 1 256))))
- (pass-if (not (< (- (ash 1 256)) +nan.0)))
- (pass-if (not (< +nan.0 (- (ash 1 256)))))
-
- (pass-if (not (< (ash 1 8192) +nan.0)))
- (pass-if (not (< +nan.0 (ash 1 8192))))
- (pass-if (not (< (- (ash 1 8192)) +nan.0)))
- (pass-if (not (< +nan.0 (- (ash 1 8192)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (not (< (ash 3 1023) +nan.0)))
- (pass-if (not (< (1+ (ash 3 1023)) +nan.0)))
- (pass-if (not (< (1- (ash 3 1023)) +nan.0)))
- (pass-if (not (< +nan.0 (ash 3 1023))))
- (pass-if (not (< +nan.0 (1+ (ash 3 1023)))))
- (pass-if (not (< +nan.0 (1- (ash 3 1023)))))
- (with-test-prefix "inum/frac"
- (pass-if (< 2 9/4))
- (pass-if (< -2 9/4))
- (pass-if (< -2 7/4))
- (pass-if (< -2 -7/4))
- (pass-if (eq? #f (< 2 7/4)))
- (pass-if (eq? #f (< 2 -7/4)))
- (pass-if (eq? #f (< 2 -9/4)))
- (pass-if (eq? #f (< -2 -9/4))))
- (with-test-prefix "bignum/frac"
- (let ((x (ash 1 2048)))
- (pass-if (< x (* 4/3 x)))
- (pass-if (< (- x) (* 4/3 x)))
- (pass-if (< (- x) (* 2/3 x)))
- (pass-if (< (- x) (* -2/3 x)))
- (pass-if (eq? #f (< x (* 2/3 x))))
- (pass-if (eq? #f (< x (* -2/3 x))))
- (pass-if (eq? #f (< x (* -4/3 x))))
- (pass-if (eq? #f (< (- x) (* -4/3 x))))))
- (with-test-prefix "inum/flonum"
- (pass-if (< 4 4.5))
- (pass-if (< 4.5 5))
- (pass-if (< -5 -4.5))
- (pass-if (< -4.5 4))
- (pass-if (not (< 4.5 4)))
- (pass-if (not (< 5 4.5)))
- (pass-if (not (< -4.5 -5)))
- (pass-if (not (< 4 -4.5)))
- (pass-if (< 4 +inf.0))
- (pass-if (< -4 +inf.0))
- (pass-if (< -inf.0 4))
- (pass-if (< -inf.0 -4))
- (pass-if (not (< +inf.0 4)))
- (pass-if (not (< +inf.0 -4)))
- (pass-if (not (< 4 -inf.0)))
- (pass-if (not (< -4 -inf.0)))
- (pass-if (not (< +nan.0 4)))
- (pass-if (not (< +nan.0 -4)))
- (pass-if (not (< 4 +nan.0)))
- (pass-if (not (< -4 +nan.0)))
- (pass-if (< most-positive-fixnum (expt 2.0 fixnum-bit)))
- (pass-if (not (< (expt 2.0 fixnum-bit) most-positive-fixnum)))
- (pass-if (< (- (expt 2.0 fixnum-bit)) most-negative-fixnum))
- (pass-if (not (< most-negative-fixnum (- (expt 2.0 fixnum-bit)))))
- ;; Prior to guile 2.0.10, we would unconditionally convert the inum
- ;; to a double, which on a 64-bit system could result in a
- ;; significant change in its value, thus corrupting the comparison.
- (pass-if (< most-positive-fixnum (exact->inexact (+ 1 most-positive-fixnum))))
- (pass-if (< (exact->inexact (- (+ 1 most-positive-fixnum))) (- most-positive-fixnum))))
- (with-test-prefix "flonum/frac"
- (pass-if (< 0.75 4/3))
- (pass-if (< -0.75 4/3))
- (pass-if (< -0.75 2/3))
- (pass-if (< -0.75 -2/3))
- (pass-if (eq? #f (< 0.75 2/3)))
- (pass-if (eq? #f (< 0.75 -2/3)))
- (pass-if (eq? #f (< 0.75 -4/3)))
- (pass-if (eq? #f (< -0.75 -4/3)))
- (pass-if (< -inf.0 4/3))
- (pass-if (< -inf.0 -4/3))
- (pass-if (eq? #f (< +inf.0 4/3)))
- (pass-if (eq? #f (< +inf.0 -4/3)))
- (pass-if (eq? #f (< +nan.0 4/3)))
- (pass-if (eq? #f (< +nan.0 -4/3))))
- (with-test-prefix "frac/inum"
- (pass-if (< 7/4 2))
- (pass-if (< -7/4 2))
- (pass-if (< -9/4 2))
- (pass-if (< -9/4 -2))
- (pass-if (eq? #f (< 9/4 2)))
- (pass-if (eq? #f (< 9/4 -2)))
- (pass-if (eq? #f (< 7/4 -2)))
- (pass-if (eq? #f (< -7/4 -2))))
- (with-test-prefix "frac/bignum"
- (let ((x (ash 1 2048)))
- (pass-if (< (* 2/3 x) x))
- (pass-if (< (* -2/3 x) x))
- (pass-if (< (* -4/3 x) x))
- (pass-if (< (* -4/3 x) (- x)))
- (pass-if (eq? #f (< (* 4/3 x) x)))
- (pass-if (eq? #f (< (* 4/3 x) (- x))))
- (pass-if (eq? #f (< (* 2/3 x) (- x))))
- (pass-if (eq? #f (< (* -2/3 x) (- x))))))
- (with-test-prefix "frac/flonum"
- (pass-if (< 2/3 0.75))
- (pass-if (< -2/3 0.75))
- (pass-if (< -4/3 0.75))
- (pass-if (< -4/3 -0.75))
- (pass-if (eq? #f (< 4/3 0.75)))
- (pass-if (eq? #f (< 4/3 -0.75)))
- (pass-if (eq? #f (< 2/3 -0.75)))
- (pass-if (eq? #f (< -2/3 -0.75)))
- (pass-if (< 4/3 +inf.0))
- (pass-if (< -4/3 +inf.0))
- (pass-if (eq? #f (< 4/3 -inf.0)))
- (pass-if (eq? #f (< -4/3 -inf.0)))
- (pass-if (eq? #f (< 4/3 +nan.0)))
- (pass-if (eq? #f (< -4/3 +nan.0))))
- (with-test-prefix "frac/frac"
- (pass-if (< 2/3 6/7))
- (pass-if (< -2/3 6/7))
- (pass-if (< -4/3 6/7))
- (pass-if (< -4/3 -6/7))
- (pass-if (eq? #f (< 4/3 6/7)))
- (pass-if (eq? #f (< 4/3 -6/7)))
- (pass-if (eq? #f (< 2/3 -6/7)))
- (pass-if (eq? #f (< -2/3 -6/7)))))
- ;;;
- ;;; >
- ;;;
- ;; currently not tested -- implementation is trivial
- ;; (> x y) is implemented as (< y x)
- ;; FIXME: tests should probably be added in case we change implementation.
- ;;;
- ;;; <=
- ;;;
- ;; currently not tested -- implementation is trivial
- ;; (<= x y) is implemented as (not (< y x))
- ;; FIXME: tests should probably be added in case we change implementation.
- ;;;
- ;;; >=
- ;;;
- ;; currently not tested -- implementation is trivial
- ;; (>= x y) is implemented as (not (< x y))
- ;; FIXME: tests should probably be added in case we change implementation.
- ;;;
- ;;; zero?
- ;;;
- (with-test-prefix "zero?"
- (pass-if (documented? zero?))
- (pass-if (zero? 0))
- (pass-if (zero? 0.0))
- (pass-if (zero? -0.0))
- (pass-if (zero? 0.0+0.0i))
- (pass-if (zero? 0.0-0.0i))
- (pass-if (zero? 0.0+0.0i))
- (pass-if (zero? -0.0-0.0i))
- (pass-if (not (zero? 7)))
- (pass-if (not (zero? -7)))
- (pass-if (not (zero? 1/7)))
- (pass-if (not (zero? -inf.0)))
- (pass-if (not (zero? +inf.0)))
- (pass-if (not (zero? +nan.0)))
- (pass-if (not (zero? (+ 1 fixnum-max))))
- (pass-if (not (zero? (- 1 fixnum-min))))
- (pass-if (not (zero? 1.3)))
- (pass-if (not (zero? 3.1+4.2i)))
- (pass-if (not (zero? 1.0+0.0i)))
- (pass-if (not (zero? 0.0-1.0i))))
- ;;;
- ;;; positive?
- ;;;
- (with-test-prefix "positive?"
- (pass-if (documented? positive?))
- (pass-if (positive? 1))
- (pass-if (positive? (+ fixnum-max 1)))
- (pass-if (positive? 1.3))
- (pass-if (not (positive? 0)))
- (pass-if (not (positive? -1)))
- (pass-if (not (positive? (- fixnum-min 1))))
- (pass-if (not (positive? -1.3))))
- ;;;
- ;;; negative?
- ;;;
- (with-test-prefix "negative?"
- (pass-if (documented? negative?))
- (pass-if (not (negative? 1)))
- (pass-if (not (negative? (+ fixnum-max 1))))
- (pass-if (not (negative? 1.3)))
- (pass-if (not (negative? 0)))
- (pass-if (negative? -1))
- (pass-if (negative? (- fixnum-min 1)))
- (pass-if (negative? -1.3)))
- ;;;
- ;;; max
- ;;;
- (with-test-prefix "max"
- (pass-if-exception "no args" exception:wrong-num-args
- (max))
- (pass-if-exception "one complex" exception:wrong-type-arg
- (max 1+i))
- (pass-if-exception "inum/complex" exception:wrong-type-arg
- (max 123 1+i))
- (pass-if-exception "big/complex" exception:wrong-type-arg
- (max 9999999999999999999999999999999999999999 1+i))
- (pass-if-exception "real/complex" exception:wrong-type-arg
- (max 123.0 1+i))
- (pass-if-exception "frac/complex" exception:wrong-type-arg
- (max 123/456 1+i))
- (pass-if-exception "complex/inum" exception:wrong-type-arg
- (max 1+i 123))
- (pass-if-exception "complex/big" exception:wrong-type-arg
- (max 1+i 9999999999999999999999999999999999999999))
- (pass-if-exception "complex/real" exception:wrong-type-arg
- (max 1+i 123.0))
- (pass-if-exception "complex/frac" exception:wrong-type-arg
- (max 1+i 123/456))
- (let ((big*2 (* fixnum-max 2))
- (big*3 (* fixnum-max 3))
- (big*4 (* fixnum-max 4))
- (big*5 (* fixnum-max 5)))
- (with-test-prefix "inum / frac"
- (pass-if (eqv? 3 (max 3 5/2)))
- (pass-if (eqv? 5/2 (max 2 5/2))))
- (with-test-prefix "frac / inum"
- (pass-if (eqv? 3 (max 5/2 3)))
- (pass-if (eqv? 5/2 (max 5/2 2))))
- (with-test-prefix "infinities and NaNs"
- ;; +inf.0 beats everything except NaNs
- (pass-if (eqv? +inf.0 (max +inf.0 123 )))
- (pass-if (eqv? +inf.0 (max 123 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 -123.3 )))
- (pass-if (eqv? +inf.0 (max -123.3 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 -7/2 )))
- (pass-if (eqv? +inf.0 (max -7/2 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 -1e20 )))
- (pass-if (eqv? +inf.0 (max -1e20 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 (- big*2))))
- (pass-if (eqv? +inf.0 (max (- big*2) +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
- (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
- ;; NaNs beat everything
- (pass-if (real-nan? (max +nan.0 123 )))
- (pass-if (real-nan? (max 123 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 123.3 )))
- (pass-if (real-nan? (max 123.3 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 -7/2 )))
- (pass-if (real-nan? (max -7/2 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 -1e20 )))
- (pass-if (real-nan? (max -1e20 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 (- big*2))))
- (pass-if (real-nan? (max (- big*2) +nan.0 )))
- (pass-if (real-nan? (max +nan.0 -inf.0 )))
- (pass-if (real-nan? (max -inf.0 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 +nan.0 )))
- (pass-if (real-nan? (max +inf.0 +nan.0 )))
- (pass-if (real-nan? (max +nan.0 +inf.0 )))
- ;; -inf.0 always loses, except against itself
- (pass-if (eqv? -inf.0 (max -inf.0 -inf.0 )))
- (pass-if (eqv? -123.0 (max -inf.0 -123 )))
- (pass-if (eqv? -123.0 (max -123 -inf.0 )))
- (pass-if (eqv? -123.3 (max -inf.0 -123.3 )))
- (pass-if (eqv? -123.3 (max -123.3 -inf.0 )))
- (pass-if (eqv? -3.5 (max -inf.0 -7/2 )))
- (pass-if (eqv? -3.5 (max -7/2 -inf.0 )))
- (pass-if (eqv? -1.0e20 (max -inf.0 -1e20 )))
- (pass-if (eqv? -1.0e20 (max -1e20 -inf.0 )))
- (pass-if (eqv? (exact->inexact (- big*2))
- (max -inf.0 (- big*2))))
- (pass-if (eqv? (exact->inexact (- big*2))
- (max (- big*2) -inf.0 ))))
- (with-test-prefix "signed zeroes"
- (pass-if (eqv? 0.0 (max 0.0 0.0)))
- (pass-if (eqv? 0.0 (max 0.0 -0.0)))
- (pass-if (eqv? 0.0 (max -0.0 0.0)))
- (pass-if (eqv? -0.0 (max -0.0 -0.0)))
- (pass-if (eqv? 0.0 (max -0.0 0 )))
- (pass-if (eqv? 0.0 (max 0.0 0 )))
- (pass-if (eqv? 0.0 (max 0 -0.0)))
- (pass-if (eqv? 0.0 (max 0 0.0)))
- (pass-if (eqv? 0 (min 0 0 ))))
- (with-test-prefix "big / frac"
- (pass-if (eqv? big*2 (max big*2 5/2)))
- (pass-if (eqv? 5/2 (max (- big*2) 5/2))))
- (with-test-prefix "frac / big"
- (pass-if (eqv? big*2 (max 5/2 big*2)))
- (pass-if (eqv? 5/2 (max 5/2 (- big*2)))))
- (with-test-prefix "big / real"
- (pass-if (real-nan? (max big*5 +nan.0)))
- (pass-if (eqv? (exact->inexact big*5) (max big*5 -inf.0)))
- (pass-if (eqv? (exact->inexact big*5) (max big*5 1.0)))
- (pass-if (eqv? +inf.0 (max big*5 +inf.0)))
- (pass-if (eqv? 1.0 (max (- big*5) 1.0))))
- (with-test-prefix "real / big"
- (pass-if (real-nan? (max +nan.0 big*5)))
- (pass-if (eqv? (exact->inexact big*5) (max -inf.0 big*5)))
- (pass-if (eqv? (exact->inexact big*5) (max 1.0 big*5)))
- (pass-if (eqv? +inf.0 (max +inf.0 big*5)))
- (pass-if (eqv? 1.0 (max 1.0 (- big*5)))))
- (with-test-prefix "frac / frac"
- (pass-if (eqv? 2/3 (max 1/2 2/3)))
- (pass-if (eqv? 2/3 (max 2/3 1/2)))
- (pass-if (eqv? -1/2 (max -1/2 -2/3)))
- (pass-if (eqv? -1/2 (max -2/3 -1/2))))
- (with-test-prefix "real / real"
- (pass-if (real-nan? (max 123.0 +nan.0)))
- (pass-if (real-nan? (max +nan.0 123.0)))
- (pass-if (real-nan? (max +nan.0 +nan.0)))
- (pass-if (eqv? 456.0 (max 123.0 456.0)))
- (pass-if (eqv? 456.0 (max 456.0 123.0)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (for-each (lambda (b)
- (pass-if (list b +inf.0)
- (eqv? +inf.0 (max b +inf.0)))
- (pass-if (list +inf.0 b)
- (eqv? +inf.0 (max b +inf.0)))
- (pass-if (list b -inf.0)
- (eqv? (exact->inexact b) (max b -inf.0)))
- (pass-if (list -inf.0 b)
- (eqv? (exact->inexact b) (max b -inf.0))))
- (list (1- (ash 1 1024))
- (ash 1 1024)
- (1+ (ash 1 1024))
- (- (1- (ash 1 1024)))
- (- (ash 1 1024))
- (- (1+ (ash 1 1024)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (real-nan? (max (ash 1 2048) +nan.0)))
- (pass-if (real-nan? (max +nan.0 (ash 1 2048)))))
- ;;;
- ;;; min
- ;;;
- ;; FIXME: unfinished...
- (with-test-prefix "min"
- (pass-if-exception "no args" exception:wrong-num-args
- (min))
- (pass-if-exception "one complex" exception:wrong-type-arg
- (min 1+i))
- (pass-if-exception "inum/complex" exception:wrong-type-arg
- (min 123 1+i))
- (pass-if-exception "big/complex" exception:wrong-type-arg
- (min 9999999999999999999999999999999999999999 1+i))
- (pass-if-exception "real/complex" exception:wrong-type-arg
- (min 123.0 1+i))
- (pass-if-exception "frac/complex" exception:wrong-type-arg
- (min 123/456 1+i))
- (pass-if-exception "complex/inum" exception:wrong-type-arg
- (min 1+i 123))
- (pass-if-exception "complex/big" exception:wrong-type-arg
- (min 1+i 9999999999999999999999999999999999999999))
- (pass-if-exception "complex/real" exception:wrong-type-arg
- (min 1+i 123.0))
- (pass-if-exception "complex/frac" exception:wrong-type-arg
- (min 1+i 123/456))
- (let ((big*2 (* fixnum-max 2))
- (big*3 (* fixnum-max 3))
- (big*4 (* fixnum-max 4))
- (big*5 (* fixnum-max 5)))
- (pass-if (documented? min))
- (pass-if (eqv? 1 (min 7 3 1 5)))
- (pass-if (eqv? 1 (min 1 7 3 5)))
- (pass-if (eqv? 1 (min 7 3 5 1)))
- (pass-if (eqv? -7 (min 2 3 4 -2 5 -7 1 -1 4 2)))
- (pass-if (eqv? -7 (min -7 2 3 4 -2 5 1 -1 4 2)))
- (pass-if (eqv? -7 (min 2 3 4 -2 5 1 -1 4 2 -7)))
- (pass-if (eqv? big*2 (min big*3 big*5 big*2 big*4)))
- (pass-if (eqv? big*2 (min big*2 big*3 big*5 big*4)))
- (pass-if (eqv? big*2 (min big*3 big*5 big*4 big*2)))
- (pass-if
- (eqv? (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max))))
- (pass-if
- (eqv? (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max))))
- (pass-if
- (eqv? (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1))))
- (with-test-prefix "inum / frac"
- (pass-if (eqv? 5/2 (min 3 5/2)))
- (pass-if (eqv? 2 (min 2 5/2))))
- (with-test-prefix "frac / inum"
- (pass-if (eqv? 5/2 (min 5/2 3)))
- (pass-if (eqv? 2 (min 5/2 2))))
- (with-test-prefix "infinities and NaNs"
- ;; -inf.0 beats everything except NaNs
- (pass-if (eqv? -inf.0 (min -inf.0 123 )))
- (pass-if (eqv? -inf.0 (min 123 -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 -123.3 )))
- (pass-if (eqv? -inf.0 (min -123.3 -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 -7/2 )))
- (pass-if (eqv? -inf.0 (min -7/2 -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 -1e20 )))
- (pass-if (eqv? -inf.0 (min -1e20 -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 (- big*2))))
- (pass-if (eqv? -inf.0 (min (- big*2) -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 +inf.0 )))
- (pass-if (eqv? -inf.0 (min +inf.0 -inf.0 )))
- (pass-if (eqv? -inf.0 (min -inf.0 -inf.0 )))
- ;; NaNs beat everything
- (pass-if (real-nan? (min +nan.0 123 )))
- (pass-if (real-nan? (min 123 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 123.3 )))
- (pass-if (real-nan? (min 123.3 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 -7/2 )))
- (pass-if (real-nan? (min -7/2 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 -1e20 )))
- (pass-if (real-nan? (min -1e20 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 (- big*2))))
- (pass-if (real-nan? (min (- big*2) +nan.0 )))
- (pass-if (real-nan? (min +nan.0 +inf.0 )))
- (pass-if (real-nan? (min +inf.0 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 +nan.0 )))
- (pass-if (real-nan? (min -inf.0 +nan.0 )))
- (pass-if (real-nan? (min +nan.0 -inf.0 )))
- ;; +inf.0 always loses, except against itself
- (pass-if (eqv? +inf.0 (min +inf.0 +inf.0 )))
- (pass-if (eqv? -123.0 (min +inf.0 -123 )))
- (pass-if (eqv? -123.0 (min -123 +inf.0 )))
- (pass-if (eqv? -123.3 (min +inf.0 -123.3 )))
- (pass-if (eqv? -123.3 (min -123.3 +inf.0 )))
- (pass-if (eqv? -3.5 (min +inf.0 -7/2 )))
- (pass-if (eqv? -3.5 (min -7/2 +inf.0 )))
- (pass-if (eqv? -1.0e20 (min +inf.0 -1e20 )))
- (pass-if (eqv? -1.0e20 (min -1e20 +inf.0 )))
- (pass-if (eqv? (exact->inexact (- big*2))
- (min +inf.0 (- big*2))))
- (pass-if (eqv? (exact->inexact (- big*2))
- (min (- big*2) +inf.0 ))))
- (with-test-prefix "signed zeroes"
- (pass-if (eqv? 0.0 (min 0.0 0.0)))
- (pass-if (eqv? -0.0 (min 0.0 -0.0)))
- (pass-if (eqv? -0.0 (min -0.0 0.0)))
- (pass-if (eqv? -0.0 (min -0.0 -0.0)))
- (pass-if (eqv? -0.0 (min -0.0 0 )))
- (pass-if (eqv? 0.0 (min 0.0 0 )))
- (pass-if (eqv? -0.0 (min 0 -0.0)))
- (pass-if (eqv? 0.0 (min 0 0.0)))
- (pass-if (eqv? 0 (min 0 0 ))))
- (with-test-prefix "big / frac"
- (pass-if (eqv? 5/2 (min big*2 5/2)))
- (pass-if (eqv? (- big*2) (min (- big*2) 5/2))))
- (with-test-prefix "frac / big"
- (pass-if (eqv? 5/2 (min 5/2 big*2)))
- (pass-if (eqv? (- big*2) (min 5/2 (- big*2)))))
- (with-test-prefix "big / real"
- (pass-if (real-nan? (min big*5 +nan.0)))
- (pass-if (eqv? (exact->inexact big*5) (min big*5 +inf.0)))
- (pass-if (eqv? -inf.0 (min big*5 -inf.0)))
- (pass-if (eqv? 1.0 (min big*5 1.0)))
- (pass-if (eqv? (exact->inexact (- big*5)) (min (- big*5) 1.0))))
- (with-test-prefix "real / big"
- (pass-if (real-nan? (min +nan.0 big*5)))
- (pass-if (eqv? (exact->inexact big*5) (min +inf.0 big*5)))
- (pass-if (eqv? -inf.0 (min -inf.0 big*5)))
- (pass-if (eqv? 1.0 (min 1.0 big*5)))
- (pass-if (eqv? (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
- (with-test-prefix "frac / frac"
- (pass-if (eqv? 1/2 (min 1/2 2/3)))
- (pass-if (eqv? 1/2 (min 2/3 1/2)))
- (pass-if (eqv? -2/3 (min -1/2 -2/3)))
- (pass-if (eqv? -2/3 (min -2/3 -1/2))))
- (with-test-prefix "real / real"
- (pass-if (real-nan? (min 123.0 +nan.0)))
- (pass-if (real-nan? (min +nan.0 123.0)))
- (pass-if (real-nan? (min +nan.0 +nan.0)))
- (pass-if (eqv? 123.0 (min 123.0 456.0)))
- (pass-if (eqv? 123.0 (min 456.0 123.0)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
- ;; sure we've avoided that
- (for-each (lambda (b)
- (pass-if (list b +inf.0)
- (eqv? (exact->inexact b) (min b +inf.0)))
- (pass-if (list +inf.0 b)
- (eqv? (exact->inexact b) (min b +inf.0)))
- (pass-if (list b -inf.0)
- (eqv? -inf.0 (min b -inf.0)))
- (pass-if (list -inf.0 b)
- (eqv? -inf.0 (min b -inf.0))))
- (list (1- (ash 1 1024))
- (ash 1 1024)
- (1+ (ash 1 1024))
- (- (1- (ash 1 1024)))
- (- (ash 1 1024))
- (- (1+ (ash 1 1024)))))
- ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
- ;; sure we've avoided that
- (pass-if (real-nan? (min (- (ash 1 2048)) (- +nan.0))))
- (pass-if (real-nan? (min (- +nan.0) (- (ash 1 2048))))))
- ;;;
- ;;; +
- ;;;
- (with-test-prefix/c&e "+"
- (pass-if "documented?"
- (documented? +))
- (pass-if "simple"
- (and (eqv? 7 (+ 3 4))
- (eqv? 3 (+ 3))
- (eqv? 0 (+))))
- (pass-if "exactness propagation"
- (and (eqv? 8 (+ 3 5))
- (eqv? 8.0 (+ 3 5.0))
- (eqv? 8.0 (+ 3.0 5))
- (eqv? 8.0 (+ 3.0 5.0))
- (eqv? 5/6 (+ 1/2 1/3))
- (eqv? 5.5 (+ 1/2 5.0))
- (eqv? 3.25 (+ 3.0 1/4))))
- (pass-if "signed zeroes"
- (and (eqv? 0.0 (+ 0.0))
- (eqv? -0.0 (+ -0.0))
- (eqv? 0.0 (+ 0.0 0.0))
- (eqv? 0.0 (+ 0.0 -0.0))
- (eqv? 0.0 (+ -0.0 0.0))
- (eqv? -0.0 (+ -0.0 -0.0))))
- (pass-if "NaNs"
- (and (real-nan? (+ +nan.0 +nan.0))
- (real-nan? (+ 0 +nan.0))
- (real-nan? (+ +nan.0 0))
- (real-nan? (+ 1 +nan.0))
- (real-nan? (+ +nan.0 1))
- (real-nan? (+ -1 +nan.0))
- (real-nan? (+ +nan.0 -1))
- (real-nan? (+ -7/2 +nan.0))
- (real-nan? (+ +nan.0 -7/2))
- (real-nan? (+ 1e20 +nan.0))
- (real-nan? (+ +nan.0 1e20))
- (real-nan? (+ +inf.0 +nan.0))
- (real-nan? (+ +nan.0 +inf.0))
- (real-nan? (+ -inf.0 +nan.0))
- (real-nan? (+ +nan.0 -inf.0))
- (real-nan? (+ (* fixnum-max 2) +nan.0))
- (real-nan? (+ +nan.0 (* fixnum-max 2)))))
- (pass-if "infinities"
- (and (eqv? +inf.0 (+ +inf.0 +inf.0))
- (eqv? -inf.0 (+ -inf.0 -inf.0))
- (real-nan? (+ +inf.0 -inf.0))
- (real-nan? (+ -inf.0 +inf.0))))
- ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
- (pass-if "fixnum + fixnum = bignum (32-bit)"
- (eqv? 536870912 (+ 536870910 2)))
- ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
- (pass-if "fixnum + fixnum = bignum (64-bit)"
- (eqv? 2305843009213693952 (+ 2305843009213693950 2)))
- (pass-if "bignum + fixnum = fixnum"
- (eqv? 0 (+ (1+ most-positive-fixnum) most-negative-fixnum))))
- ;;;
- ;;; -
- ;;;
- (with-test-prefix/c&e "-"
- (pass-if "double-negation of fixnum-min: ="
- (= fixnum-min (- (- fixnum-min))))
- (pass-if "double-negation of fixnum-min: eqv?"
- (eqv? fixnum-min (- (- fixnum-min))))
- (pass-if "double-negation of fixnum-min: equal?"
- (equal? fixnum-min (- (- fixnum-min))))
- (pass-if "binary double-negation of fixnum-min: ="
- (= fixnum-min (- 0 (- 0 fixnum-min))))
- (pass-if "binary double-negation of fixnum-min: eqv?"
- (eqv? fixnum-min (- 0 (- 0 fixnum-min))))
- (pass-if "binary double-negation of fixnum-min: equal?"
- (equal? fixnum-min (- 0 (- 0 fixnum-min))))
- (pass-if "signed zeroes"
- (and (eqv? +0.0 (- -0.0))
- (eqv? -0.0 (- +0.0))
- (eqv? 0.0 (- 0.0 0.0))
- (eqv? 0.0 (- 0.0 -0.0))
- (eqv? 0.0 (- -0.0 -0.0))
- (eqv? -0.0 (- -0.0 0.0))))
- (pass-if "exactness propagation"
- (and (eqv? 3 (- 8 5))
- (eqv? 3.0 (- 8 5.0))
- (eqv? 3.0 (- 8.0 5))
- (eqv? 3.0 (- 8.0 5.0))
- (eqv? -1/6 (- 1/3 1/2))
- (eqv? -4.5 (- 1/2 5.0))
- (eqv? 2.75 (- 3.0 1/4))))
- (pass-if "infinities"
- (and (eqv? +inf.0 (- +inf.0 -inf.0))
- (eqv? -inf.0 (- -inf.0 +inf.0))
- (real-nan? (- +inf.0 +inf.0))
- (real-nan? (- -inf.0 -inf.0))))
- (pass-if "NaNs"
- (and (real-nan? (- +nan.0 +nan.0))
- (real-nan? (- 0 +nan.0))
- (real-nan? (- +nan.0 0))
- (real-nan? (- 1 +nan.0))
- (real-nan? (- +nan.0 1))
- (real-nan? (- -1 +nan.0))
- (real-nan? (- +nan.0 -1))
- (real-nan? (- -7/2 +nan.0))
- (real-nan? (- +nan.0 -7/2))
- (real-nan? (- 1e20 +nan.0))
- (real-nan? (- +nan.0 1e20))
- (real-nan? (- +inf.0 +nan.0))
- (real-nan? (- +nan.0 +inf.0))
- (real-nan? (- -inf.0 +nan.0))
- (real-nan? (- +nan.0 -inf.0))
- (real-nan? (- (* fixnum-max 2) +nan.0))
- (real-nan? (- +nan.0 (* fixnum-max 2)))))
- (pass-if "(eqv? fixnum-min (- (- fixnum-min)))"
- (eqv? fixnum-min (- (- fixnum-min))))
- (pass-if "(eqv? fixnum-min (- 0 (- 0 fixnum-min)))"
- (eqv? fixnum-min (- 0 (- 0 fixnum-min))))
- (pass-if "(eqv? fixnum-num (apply - (list (apply - (list fixnum-min)))))"
- (eqv? fixnum-min (apply - (list (apply - (list fixnum-min))))))
- (pass-if "-inum - +bignum"
- (= #x-100000000000000000000000000000001
- (- -1 #x100000000000000000000000000000000)))
-
- (pass-if "big - inum"
- (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
- (- #x100000000000000000000000000000000 1)))
-
- (pass-if "big - -inum"
- (= #x100000000000000000000000000000001
- (- #x100000000000000000000000000000000 -1)))
- ;; The mininum fixnum on a 32-bit architecture: -2^29.
- (pass-if "fixnum - fixnum = bignum (32-bit)"
- (eqv? -536870912 (- -536870910 2)))
- ;; The minimum fixnum on a 64-bit architecture: -2^61.
- (pass-if "fixnum - fixnum = bignum (64-bit)"
- (eqv? -2305843009213693952 (- -2305843009213693950 2)))
- (pass-if "bignum - fixnum = fixnum"
- (eqv? most-positive-fixnum (- (1+ most-positive-fixnum) 1))))
- ;;;
- ;;; *
- ;;;
- (with-test-prefix "*"
- (with-test-prefix "double-negation of fixnum-min"
- (pass-if (= fixnum-min (* -1 (* -1 fixnum-min))))
- (pass-if (eqv? fixnum-min (* -1 (* -1 fixnum-min))))
- (pass-if (equal? fixnum-min (* -1 (* -1 fixnum-min))))
- (pass-if (= fixnum-min (* (* fixnum-min -1) -1)))
- (pass-if (eqv? fixnum-min (* (* fixnum-min -1) -1)))
- (pass-if (equal? fixnum-min (* (* fixnum-min -1) -1))))
- (with-test-prefix "signed fixnum overflow"
- (pass-if (eqv? (* 65536 65536) 4294967296))
- (pass-if (eqv? (* -65536 65536) -4294967296))
- (pass-if (eqv? (* 65536 -65536) -4294967296))
- (pass-if (eqv? (* -65536 -65536) 4294967296))
- (pass-if (eqv? (* 4294967296 4294967296) 18446744073709551616))
- (pass-if (eqv? (* -4294967296 4294967296) -18446744073709551616))
- (pass-if (eqv? (* 4294967296 -4294967296) -18446744073709551616))
- (pass-if (eqv? (* -4294967296 -4294967296) 18446744073709551616)))
- (with-test-prefix "signed zeroes"
- (pass-if (eqv? +0.0 (* +0.0 +0.0)))
- (pass-if (eqv? -0.0 (* -0.0 +0.0)))
- (pass-if (eqv? +0.0 (* -0.0 -0.0)))
- (pass-if (eqv? -0.0 (* +0.0 -0.0)))
- (pass-if (eqv? +0.0+0.0i (* +i +0.0)))
- (pass-if (eqv? +0.0-0.0i (* -i +0.0)))
- (pass-if (eqv? -0.0-0.0i (* +i -0.0)))
- (pass-if (eqv? -0.0+0.0i (* -i -0.0))))
- (with-test-prefix "exactness propagation"
- (pass-if (eqv? -0.0 (* 0 -1.0 )))
- (pass-if (eqv? 0.0 (* 0 1.0 )))
- (pass-if (eqv? -0.0 (* -1.0 0 )))
- (pass-if (eqv? 0.0 (* 1.0 0 )))
- (pass-if (eqv? 0 (* 0 1/2 )))
- (pass-if (eqv? 0 (* 1/2 0 )))
- (pass-if (eqv? 0.0+0.0i (* 0 1+i )))
- (pass-if (eqv? 0.0+0.0i (* 1+i 0 )))
- (pass-if (eqv? -1.0 (* 1 -1.0 )))
- (pass-if (eqv? 1.0 (* 1 1.0 )))
- (pass-if (eqv? -1.0 (* -1.0 1 )))
- (pass-if (eqv? 1.0 (* 1.0 1 )))
- (pass-if (eqv? 1/2 (* 1 1/2 )))
- (pass-if (eqv? 1/2 (* 1/2 1 )))
- (pass-if (eqv? 1+i (* 1 1+i )))
- (pass-if (eqv? 1+i (* 1+i 1 ))))
- (with-test-prefix "propagation of NaNs"
- (pass-if (real-nan? (* +nan.0 +nan.0)))
- (pass-if (real-nan? (* +nan.0 1 )))
- (pass-if (real-nan? (* +nan.0 -1 )))
- (pass-if (real-nan? (* +nan.0 -7/2 )))
- (pass-if (real-nan? (* +nan.0 1e20 )))
- (pass-if (real-nan? (* 1 +nan.0)))
- (pass-if (real-nan? (* -1 +nan.0)))
- (pass-if (real-nan? (* -7/2 +nan.0)))
- (pass-if (real-nan? (* 1e20 +nan.0)))
- (pass-if (real-nan? (* +inf.0 +nan.0)))
- (pass-if (real-nan? (* +nan.0 +inf.0)))
- (pass-if (real-nan? (* -inf.0 +nan.0)))
- (pass-if (real-nan? (* +nan.0 -inf.0)))
- (pass-if (real-nan? (* (* fixnum-max 2) +nan.0)))
- (pass-if (real-nan? (* +nan.0 (* fixnum-max 2))))
- (pass-if (real-nan? (* 0 +nan.0 )))
- (pass-if (real-nan? (* +nan.0 0 )))
- (pass-if (almost-real-nan? (* 0 +nan.0+i)))
- (pass-if (almost-real-nan? (* +nan.0+i 0 )))
- (pass-if (imaginary-nan? (* 0 +nan.0i )))
- (pass-if (imaginary-nan? (* +nan.0i 0 )))
- (pass-if (imaginary-nan? (* 0 1+nan.0i )))
- (pass-if (imaginary-nan? (* 1+nan.0i 0 )))
- (pass-if (complex-nan? (* 0 +nan.0+nan.0i )))
- (pass-if (complex-nan? (* +nan.0+nan.0i 0 ))))
- (with-test-prefix "infinities"
- (pass-if (eqv? +inf.0 (* +inf.0 5 )))
- (pass-if (eqv? -inf.0 (* +inf.0 -5 )))
- (pass-if (eqv? +inf.0 (* +inf.0 73.1)))
- (pass-if (eqv? -inf.0 (* +inf.0 -9.2)))
- (pass-if (eqv? +inf.0 (* +inf.0 5/2)))
- (pass-if (eqv? -inf.0 (* +inf.0 -5/2)))
- (pass-if (eqv? -inf.0 (* -5 +inf.0)))
- (pass-if (eqv? +inf.0 (* 73.1 +inf.0)))
- (pass-if (eqv? -inf.0 (* -9.2 +inf.0)))
- (pass-if (eqv? +inf.0 (* 5/2 +inf.0)))
- (pass-if (eqv? -inf.0 (* -5/2 +inf.0)))
- (pass-if (eqv? -inf.0 (* -inf.0 5 )))
- (pass-if (eqv? +inf.0 (* -inf.0 -5 )))
- (pass-if (eqv? -inf.0 (* -inf.0 73.1)))
- (pass-if (eqv? +inf.0 (* -inf.0 -9.2)))
- (pass-if (eqv? -inf.0 (* -inf.0 5/2)))
- (pass-if (eqv? +inf.0 (* -inf.0 -5/2)))
- (pass-if (eqv? +inf.0 (* -5 -inf.0)))
- (pass-if (eqv? -inf.0 (* 73.1 -inf.0)))
- (pass-if (eqv? +inf.0 (* -9.2 -inf.0)))
- (pass-if (eqv? -inf.0 (* 5/2 -inf.0)))
- (pass-if (eqv? +inf.0 (* -5/2 -inf.0)))
- (pass-if (real-nan? (* 0.0 +inf.0)))
- (pass-if (real-nan? (* -0.0 +inf.0)))
- (pass-if (real-nan? (* +inf.0 0.0)))
- (pass-if (real-nan? (* +inf.0 -0.0)))
- (pass-if (real-nan? (* 0.0 -inf.0)))
- (pass-if (real-nan? (* -0.0 -inf.0)))
- (pass-if (real-nan? (* -inf.0 0.0)))
- (pass-if (real-nan? (* -inf.0 -0.0)))
- (pass-if (real-nan? (* 0 +inf.0 )))
- (pass-if (real-nan? (* +inf.0 0 )))
- (pass-if (real-nan? (* 0 -inf.0 )))
- (pass-if (real-nan? (* -inf.0 0 )))
- (pass-if (almost-real-nan? (* 0 +inf.0+i)))
- (pass-if (almost-real-nan? (* +inf.0+i 0 )))
- (pass-if (almost-real-nan? (* 0 -inf.0+i)))
- (pass-if (almost-real-nan? (* -inf.0+i 0 )))
- (pass-if (imaginary-nan? (* 0 +inf.0i )))
- (pass-if (imaginary-nan? (* +inf.0i 0 )))
- (pass-if (imaginary-nan? (* 0 1+inf.0i )))
- (pass-if (imaginary-nan? (* 1+inf.0i 0 )))
- (pass-if (imaginary-nan? (* 0 -inf.0i )))
- (pass-if (imaginary-nan? (* -inf.0i 0 )))
- (pass-if (imaginary-nan? (* 0 1-inf.0i )))
- (pass-if (imaginary-nan? (* 1-inf.0i 0 )))
- (pass-if (complex-nan? (* 0 +inf.0+inf.0i )))
- (pass-if (complex-nan? (* +inf.0+inf.0i 0 )))
- (pass-if (complex-nan? (* 0 +inf.0-inf.0i )))
- (pass-if (complex-nan? (* -inf.0+inf.0i 0 ))))
- (with-test-prefix "inum * bignum"
- (pass-if "0 * 2^256 = 0"
- (eqv? 0 (* 0 (ash 1 256)))))
- (with-test-prefix "inum * flonum"
- (pass-if "0 * 1.0 = 0.0"
- (eqv? 0.0 (* 0 1.0))))
- (with-test-prefix "inum * complex"
- (pass-if "0 * 1+1i = 0.0+0.0i"
- (eqv? 0.0+0.0i (* 0 1+1i))))
- (with-test-prefix "inum * frac"
- (pass-if "0 * 2/3 = 0"
- (eqv? 0 (* 0 2/3))))
- (with-test-prefix "bignum * inum"
- (pass-if "2^256 * 0 = 0"
- (eqv? 0 (* (ash 1 256) 0))))
- (with-test-prefix "flonum * inum"
- (pass-if "1.0 * 0 = 0.0"
- (eqv? 0.0 (* 1.0 0))))
- (with-test-prefix "complex * inum"
- (pass-if "1+1i * 0 = 0.0+0.0i"
- (eqv? 0.0+0.0i (* 1+1i 0))))
- (pass-if "complex * bignum"
- (let ((big (ash 1 90)))
- (= (make-rectangular big big)
- (* 1+1i big))))
- (with-test-prefix "frac * inum"
- (pass-if "2/3 * 0 = 0"
- (eqv? 0 (* 2/3 0)))))
- ;;;
- ;;; /
- ;;;
- (with-test-prefix "/"
- (with-test-prefix "double-negation of fixnum-min"
- (pass-if (= fixnum-min (/ (/ fixnum-min -1) -1)))
- (pass-if (eqv? fixnum-min (/ (/ fixnum-min -1) -1)))
- (pass-if (equal? fixnum-min (/ (/ fixnum-min -1) -1))))
- (pass-if "documented?"
- (documented? /))
- (with-test-prefix "division by zero"
- (pass-if-exception "(/ 0)"
- exception:numerical-overflow
- (/ 0))
- (pass-if "(/ 0.0)"
- (= +inf.0 (/ 0.0)))
- (pass-if-exception "(/ 1 0)"
- exception:numerical-overflow
- (/ 1 0))
- (pass-if "(/ 1 0.0)"
- (= +inf.0 (/ 1 0.0)))
- (pass-if-exception "(/ bignum 0)"
- exception:numerical-overflow
- (/ (+ fixnum-max 1) 0))
- (pass-if "(/ bignum 0.0)"
- (= +inf.0 (/ (+ fixnum-max 1) 0.0)))
- (pass-if-exception "(/ 1.0 0)"
- exception:numerical-overflow
- (/ 1.0 0))
- (pass-if "(/ 1.0 0.0)"
- (= +inf.0 (/ 1.0 0.0)))
- (pass-if-exception "(/ +i 0)"
- exception:numerical-overflow
- (/ +i 0))
- (pass-if "(/ +i 0.0)"
- (= +inf.0 (imag-part (/ +i 0.0)))))
- (with-test-prefix "1/complex"
- (pass-if "0+1i"
- (eqv? 0-1i (/ 0+1i)))
- ;; in guile 1.6 through 1.6.7 this incorrectly resulted in nans
- (pass-if "0-1i"
- (eqv? 0+1i (/ 0-1i)))
- (pass-if "1+1i"
- (eqv? 0.5-0.5i (/ 1+1i)))
- (pass-if "1-1i"
- (eqv? 0.5+0.5i (/ 1-1i)))
- (pass-if "-1+1i"
- (eqv? -0.5-0.5i (/ -1+1i)))
- (pass-if "-1-1i"
- (eqv? -0.5+0.5i (/ -1-1i)))
- (pass-if "(/ 3+4i)"
- (= (/ 3+4i) 0.12-0.16i))
- (pass-if "(/ 4+3i)"
- (= (/ 4+3i) 0.16-0.12i))
- (pass-if "(/ 1e200+1e200i)"
- (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i)))
- (with-test-prefix "inum/complex"
- (pass-if "(/ 25 3+4i)"
- (= (/ 25 3+4i) 3.0-4.0i))
- (pass-if "(/ 25 4+3i)"
- (= (/ 25 4+3i) 4.0-3.0i)))
- (with-test-prefix "complex/complex"
- (pass-if "(/ 25+125i 3+4i)"
- (= (/ 25+125i 3+4i) 23.0+11.0i))
- (pass-if "(/ 25+125i 4+3i)"
- (= (/ 25+125i 4+3i) 19.0+17.0i))))
- ;;;
- ;;; floor
- ;;;
- (with-test-prefix "floor"
- (pass-if (= 1 (floor 1.75)))
- (pass-if (= 1 (floor 1.5)))
- (pass-if (= 1 (floor 1.25)))
- (pass-if (= 0 (floor 0.75)))
- (pass-if (= 0 (floor 0.5)))
- (pass-if (= 0 (floor 0.0)))
- (pass-if (= -1 (floor -0.5)))
- (pass-if (= -2 (floor -1.25)))
- (pass-if (= -2 (floor -1.5)))
- (with-test-prefix "inum"
- (pass-if "0"
- (and (= 0 (floor 0))
- (exact? (floor 0))))
- (pass-if "1"
- (and (= 1 (floor 1))
- (exact? (floor 1))))
- (pass-if "-1"
- (and (= -1 (floor -1))
- (exact? (floor -1)))))
- (with-test-prefix "bignum"
- (let ((x (1+ most-positive-fixnum)))
- (pass-if "(1+ most-positive-fixnum)"
- (and (= x (floor x))
- (exact? (floor x)))))
- (let ((x (1- most-negative-fixnum)))
- (pass-if "(1- most-negative-fixnum)"
- (and (= x (floor x))
- (exact? (floor x))))))
- (with-test-prefix "frac"
- (define (=exact x y)
- (and (= x y)
- (exact? y)))
- (pass-if (=exact -3 (floor -7/3)))
- (pass-if (=exact -2 (floor -5/3)))
- (pass-if (=exact -2 (floor -4/3)))
- (pass-if (=exact -1 (floor -2/3)))
- (pass-if (=exact -1 (floor -1/3)))
- (pass-if (=exact 0 (floor 1/3)))
- (pass-if (=exact 0 (floor 2/3)))
- (pass-if (=exact 1 (floor 4/3)))
- (pass-if (=exact 1 (floor 5/3)))
- (pass-if (=exact 2 (floor 7/3)))
- (pass-if (=exact -3 (floor -17/6)))
- (pass-if (=exact -3 (floor -16/6)))
- (pass-if (=exact -3 (floor -15/6)))
- (pass-if (=exact -3 (floor -14/6)))
- (pass-if (=exact -3 (floor -13/6)))
- (pass-if (=exact -2 (floor -11/6)))
- (pass-if (=exact -2 (floor -10/6)))
- (pass-if (=exact -2 (floor -9/6)))
- (pass-if (=exact -2 (floor -8/6)))
- (pass-if (=exact -2 (floor -7/6)))
- (pass-if (=exact -1 (floor -5/6)))
- (pass-if (=exact -1 (floor -4/6)))
- (pass-if (=exact -1 (floor -3/6)))
- (pass-if (=exact -1 (floor -2/6)))
- (pass-if (=exact -1 (floor -1/6)))
- (pass-if (=exact 0 (floor 1/6)))
- (pass-if (=exact 0 (floor 2/6)))
- (pass-if (=exact 0 (floor 3/6)))
- (pass-if (=exact 0 (floor 4/6)))
- (pass-if (=exact 0 (floor 5/6)))
- (pass-if (=exact 1 (floor 7/6)))
- (pass-if (=exact 1 (floor 8/6)))
- (pass-if (=exact 1 (floor 9/6)))
- (pass-if (=exact 1 (floor 10/6)))
- (pass-if (=exact 1 (floor 11/6)))
- (pass-if (=exact 2 (floor 13/6)))
- (pass-if (=exact 2 (floor 14/6)))
- (pass-if (=exact 2 (floor 15/6)))
- (pass-if (=exact 2 (floor 16/6)))
- (pass-if (=exact 2 (floor 17/6))))
- (with-test-prefix "real"
- (pass-if "0.0"
- (and (= 0.0 (floor 0.0))
- (inexact? (floor 0.0))))
- (pass-if "1.0"
- (and (= 1.0 (floor 1.0))
- (inexact? (floor 1.0))))
- (pass-if "-1.0"
- (and (= -1.0 (floor -1.0))
- (inexact? (floor -1.0))))
- (pass-if "-3.1"
- (and (= -4.0 (floor -3.1))
- (inexact? (floor -3.1))))
- (pass-if "3.1"
- (and (= 3.0 (floor 3.1))
- (inexact? (floor 3.1))))
- (pass-if "3.9"
- (and (= 3.0 (floor 3.9))
- (inexact? (floor 3.9))))
- (pass-if "-3.9"
- (and (= -4.0 (floor -3.9))
- (inexact? (floor -3.9))))
- (pass-if "1.5"
- (and (= 1.0 (floor 1.5))
- (inexact? (floor 1.5))))
- (pass-if "2.5"
- (and (= 2.0 (floor 2.5))
- (inexact? (floor 2.5))))
- (pass-if "3.5"
- (and (= 3.0 (floor 3.5))
- (inexact? (floor 3.5))))
- (pass-if "-1.5"
- (and (= -2.0 (floor -1.5))
- (inexact? (floor -1.5))))
- (pass-if "-2.5"
- (and (= -3.0 (floor -2.5))
- (inexact? (floor -2.5))))
- (pass-if "-3.5"
- (and (= -4.0 (floor -3.5))
- (inexact? (floor -3.5))))))
- ;;;
- ;;; ceiling
- ;;;
- (with-test-prefix "ceiling"
- (pass-if (= 2 (ceiling 1.75)))
- (pass-if (= 2 (ceiling 1.5)))
- (pass-if (= 2 (ceiling 1.25)))
- (pass-if (= 1 (ceiling 0.75)))
- (pass-if (= 1 (ceiling 0.5)))
- (pass-if (= 0 (ceiling 0.0)))
- (pass-if (= 0 (ceiling -0.5)))
- (pass-if (= -1 (ceiling -1.25)))
- (pass-if (= -1 (ceiling -1.5)))
- (with-test-prefix "inum"
- (pass-if "0"
- (and (= 0 (ceiling 0))
- (exact? (ceiling 0))))
- (pass-if "1"
- (and (= 1 (ceiling 1))
- (exact? (ceiling 1))))
- (pass-if "-1"
- (and (= -1 (ceiling -1))
- (exact? (ceiling -1)))))
- (with-test-prefix "bignum"
- (let ((x (1+ most-positive-fixnum)))
- (pass-if "(1+ most-positive-fixnum)"
- (and (= x (ceiling x))
- (exact? (ceiling x)))))
- (let ((x (1- most-negative-fixnum)))
- (pass-if "(1- most-negative-fixnum)"
- (and (= x (ceiling x))
- (exact? (ceiling x))))))
- (with-test-prefix "frac"
- (define (=exact x y)
- (and (= x y)
- (exact? y)))
- (pass-if (=exact -2 (ceiling -7/3)))
- (pass-if (=exact -1 (ceiling -5/3)))
- (pass-if (=exact -1 (ceiling -4/3)))
- (pass-if (=exact 0 (ceiling -2/3)))
- (pass-if (=exact 0 (ceiling -1/3)))
- (pass-if (=exact 1 (ceiling 1/3)))
- (pass-if (=exact 1 (ceiling 2/3)))
- (pass-if (=exact 2 (ceiling 4/3)))
- (pass-if (=exact 2 (ceiling 5/3)))
- (pass-if (=exact 3 (ceiling 7/3)))
- (pass-if (=exact -2 (ceiling -17/6)))
- (pass-if (=exact -2 (ceiling -16/6)))
- (pass-if (=exact -2 (ceiling -15/6)))
- (pass-if (=exact -2 (ceiling -14/6)))
- (pass-if (=exact -2 (ceiling -13/6)))
- (pass-if (=exact -1 (ceiling -11/6)))
- (pass-if (=exact -1 (ceiling -10/6)))
- (pass-if (=exact -1 (ceiling -9/6)))
- (pass-if (=exact -1 (ceiling -8/6)))
- (pass-if (=exact -1 (ceiling -7/6)))
- (pass-if (=exact 0 (ceiling -5/6)))
- (pass-if (=exact 0 (ceiling -4/6)))
- (pass-if (=exact 0 (ceiling -3/6)))
- (pass-if (=exact 0 (ceiling -2/6)))
- (pass-if (=exact 0 (ceiling -1/6)))
- (pass-if (=exact 1 (ceiling 1/6)))
- (pass-if (=exact 1 (ceiling 2/6)))
- (pass-if (=exact 1 (ceiling 3/6)))
- (pass-if (=exact 1 (ceiling 4/6)))
- (pass-if (=exact 1 (ceiling 5/6)))
- (pass-if (=exact 2 (ceiling 7/6)))
- (pass-if (=exact 2 (ceiling 8/6)))
- (pass-if (=exact 2 (ceiling 9/6)))
- (pass-if (=exact 2 (ceiling 10/6)))
- (pass-if (=exact 2 (ceiling 11/6)))
- (pass-if (=exact 3 (ceiling 13/6)))
- (pass-if (=exact 3 (ceiling 14/6)))
- (pass-if (=exact 3 (ceiling 15/6)))
- (pass-if (=exact 3 (ceiling 16/6)))
- (pass-if (=exact 3 (ceiling 17/6))))
- (with-test-prefix "real"
- (pass-if "0.0"
- (and (= 0.0 (ceiling 0.0))
- (inexact? (ceiling 0.0))))
- (pass-if "1.0"
- (and (= 1.0 (ceiling 1.0))
- (inexact? (ceiling 1.0))))
- (pass-if "-1.0"
- (and (= -1.0 (ceiling -1.0))
- (inexact? (ceiling -1.0))))
- (pass-if "-3.1"
- (and (= -3.0 (ceiling -3.1))
- (inexact? (ceiling -3.1))))
- (pass-if "3.1"
- (and (= 4.0 (ceiling 3.1))
- (inexact? (ceiling 3.1))))
- (pass-if "3.9"
- (and (= 4.0 (ceiling 3.9))
- (inexact? (ceiling 3.9))))
- (pass-if "-3.9"
- (and (= -3.0 (ceiling -3.9))
- (inexact? (ceiling -3.9))))
- (pass-if "1.5"
- (and (= 2.0 (ceiling 1.5))
- (inexact? (ceiling 1.5))))
- (pass-if "2.5"
- (and (= 3.0 (ceiling 2.5))
- (inexact? (ceiling 2.5))))
- (pass-if "3.5"
- (and (= 4.0 (ceiling 3.5))
- (inexact? (ceiling 3.5))))
- (pass-if "-1.5"
- (and (= -1.0 (ceiling -1.5))
- (inexact? (ceiling -1.5))))
- (pass-if "-2.5"
- (and (= -2.0 (ceiling -2.5))
- (inexact? (ceiling -2.5))))
- (pass-if "-3.5"
- (and (= -3.0 (ceiling -3.5))
- (inexact? (ceiling -3.5))))))
- ;;;
- ;;; truncate
- ;;;
- (with-test-prefix "truncate"
- (pass-if (= 1 (truncate 1.75)))
- (pass-if (= 1 (truncate 1.5)))
- (pass-if (= 1 (truncate 1.25)))
- (pass-if (= 0 (truncate 0.75)))
- (pass-if (= 0 (truncate 0.5)))
- (pass-if (= 0 (truncate 0.0)))
- (pass-if (= 0 (truncate -0.5)))
- (pass-if (= -1 (truncate -1.25)))
- (pass-if (= -1 (truncate -1.5)))
- (with-test-prefix "inum"
- (pass-if "0"
- (and (= 0 (truncate 0))
- (exact? (truncate 0))))
- (pass-if "1"
- (and (= 1 (truncate 1))
- (exact? (truncate 1))))
- (pass-if "-1"
- (and (= -1 (truncate -1))
- (exact? (truncate -1)))))
- (with-test-prefix "bignum"
- (let ((x (1+ most-positive-fixnum)))
- (pass-if "(1+ most-positive-fixnum)"
- (and (= x (truncate x))
- (exact? (truncate x)))))
- (let ((x (1- most-negative-fixnum)))
- (pass-if "(1- most-negative-fixnum)"
- (and (= x (truncate x))
- (exact? (truncate x))))))
- (with-test-prefix "frac"
- (define (=exact x y)
- (and (= x y)
- (exact? y)))
- (pass-if (=exact -2 (truncate -7/3)))
- (pass-if (=exact -1 (truncate -5/3)))
- (pass-if (=exact -1 (truncate -4/3)))
- (pass-if (=exact 0 (truncate -2/3)))
- (pass-if (=exact 0 (truncate -1/3)))
- (pass-if (=exact 0 (truncate 1/3)))
- (pass-if (=exact 0 (truncate 2/3)))
- (pass-if (=exact 1 (truncate 4/3)))
- (pass-if (=exact 1 (truncate 5/3)))
- (pass-if (=exact 2 (truncate 7/3)))
- (pass-if (=exact -2 (truncate -17/6)))
- (pass-if (=exact -2 (truncate -16/6)))
- (pass-if (=exact -2 (truncate -15/6)))
- (pass-if (=exact -2 (truncate -14/6)))
- (pass-if (=exact -2 (truncate -13/6)))
- (pass-if (=exact -1 (truncate -11/6)))
- (pass-if (=exact -1 (truncate -10/6)))
- (pass-if (=exact -1 (truncate -9/6)))
- (pass-if (=exact -1 (truncate -8/6)))
- (pass-if (=exact -1 (truncate -7/6)))
- (pass-if (=exact 0 (truncate -5/6)))
- (pass-if (=exact 0 (truncate -4/6)))
- (pass-if (=exact 0 (truncate -3/6)))
- (pass-if (=exact 0 (truncate -2/6)))
- (pass-if (=exact 0 (truncate -1/6)))
- (pass-if (=exact 0 (truncate 1/6)))
- (pass-if (=exact 0 (truncate 2/6)))
- (pass-if (=exact 0 (truncate 3/6)))
- (pass-if (=exact 0 (truncate 4/6)))
- (pass-if (=exact 0 (truncate 5/6)))
- (pass-if (=exact 1 (truncate 7/6)))
- (pass-if (=exact 1 (truncate 8/6)))
- (pass-if (=exact 1 (truncate 9/6)))
- (pass-if (=exact 1 (truncate 10/6)))
- (pass-if (=exact 1 (truncate 11/6)))
- (pass-if (=exact 2 (truncate 13/6)))
- (pass-if (=exact 2 (truncate 14/6)))
- (pass-if (=exact 2 (truncate 15/6)))
- (pass-if (=exact 2 (truncate 16/6)))
- (pass-if (=exact 2 (truncate 17/6))))
- (with-test-prefix "real"
- (pass-if "0.0"
- (and (= 0.0 (truncate 0.0))
- (inexact? (truncate 0.0))))
- (pass-if "1.0"
- (and (= 1.0 (truncate 1.0))
- (inexact? (truncate 1.0))))
- (pass-if "-1.0"
- (and (= -1.0 (truncate -1.0))
- (inexact? (truncate -1.0))))
- (pass-if "-3.1"
- (and (= -3.0 (truncate -3.1))
- (inexact? (truncate -3.1))))
- (pass-if "3.1"
- (and (= 3.0 (truncate 3.1))
- (inexact? (truncate 3.1))))
- (pass-if "3.9"
- (and (= 3.0 (truncate 3.9))
- (inexact? (truncate 3.9))))
- (pass-if "-3.9"
- (and (= -3.0 (truncate -3.9))
- (inexact? (truncate -3.9))))
- (pass-if "1.5"
- (and (= 1.0 (truncate 1.5))
- (inexact? (truncate 1.5))))
- (pass-if "2.5"
- (and (= 2.0 (truncate 2.5))
- (inexact? (truncate 2.5))))
- (pass-if "3.5"
- (and (= 3.0 (truncate 3.5))
- (inexact? (truncate 3.5))))
- (pass-if "-1.5"
- (and (= -1.0 (truncate -1.5))
- (inexact? (truncate -1.5))))
- (pass-if "-2.5"
- (and (= -2.0 (truncate -2.5))
- (inexact? (truncate -2.5))))
- (pass-if "-3.5"
- (and (= -3.0 (truncate -3.5))
- (inexact? (truncate -3.5))))))
- ;;;
- ;;; round
- ;;;
- (with-test-prefix "round"
- (pass-if (= 2 (round 1.75)))
- (pass-if (= 2 (round 1.5)))
- (pass-if (= 1 (round 1.25)))
- (pass-if (= 1 (round 0.75)))
- (pass-if (= 0 (round 0.5)))
- (pass-if (= 0 (round 0.0)))
- (pass-if (= 0 (round -0.5)))
- (pass-if (= -1 (round -1.25)))
- (pass-if (= -2 (round -1.5)))
- (with-test-prefix "inum"
- (pass-if "0"
- (and (= 0 (round 0))
- (exact? (round 0))))
- (pass-if "1"
- (and (= 1 (round 1))
- (exact? (round 1))))
- (pass-if "-1"
- (and (= -1 (round -1))
- (exact? (round -1)))))
- (with-test-prefix "bignum"
- (let ((x (1+ most-positive-fixnum)))
- (pass-if "(1+ most-positive-fixnum)"
- (and (= x (round x))
- (exact? (round x)))))
- (let ((x (1- most-negative-fixnum)))
- (pass-if "(1- most-negative-fixnum)"
- (and (= x (round x))
- (exact? (round x))))))
- (with-test-prefix "frac"
- (define (=exact x y)
- (and (= x y)
- (exact? y)))
- (pass-if (=exact -2 (round -7/3)))
- (pass-if (=exact -2 (round -5/3)))
- (pass-if (=exact -1 (round -4/3)))
- (pass-if (=exact -1 (round -2/3)))
- (pass-if (=exact 0 (round -1/3)))
- (pass-if (=exact 0 (round 1/3)))
- (pass-if (=exact 1 (round 2/3)))
- (pass-if (=exact 1 (round 4/3)))
- (pass-if (=exact 2 (round 5/3)))
- (pass-if (=exact 2 (round 7/3)))
- (pass-if (=exact -3 (round -17/6)))
- (pass-if (=exact -3 (round -16/6)))
- (pass-if (=exact -2 (round -15/6)))
- (pass-if (=exact -2 (round -14/6)))
- (pass-if (=exact -2 (round -13/6)))
- (pass-if (=exact -2 (round -11/6)))
- (pass-if (=exact -2 (round -10/6)))
- (pass-if (=exact -2 (round -9/6)))
- (pass-if (=exact -1 (round -8/6)))
- (pass-if (=exact -1 (round -7/6)))
- (pass-if (=exact -1 (round -5/6)))
- (pass-if (=exact -1 (round -4/6)))
- (pass-if (=exact 0 (round -3/6)))
- (pass-if (=exact 0 (round -2/6)))
- (pass-if (=exact 0 (round -1/6)))
- (pass-if (=exact 0 (round 1/6)))
- (pass-if (=exact 0 (round 2/6)))
- (pass-if (=exact 0 (round 3/6)))
- (pass-if (=exact 1 (round 4/6)))
- (pass-if (=exact 1 (round 5/6)))
- (pass-if (=exact 1 (round 7/6)))
- (pass-if (=exact 1 (round 8/6)))
- (pass-if (=exact 2 (round 9/6)))
- (pass-if (=exact 2 (round 10/6)))
- (pass-if (=exact 2 (round 11/6)))
- (pass-if (=exact 2 (round 13/6)))
- (pass-if (=exact 2 (round 14/6)))
- (pass-if (=exact 2 (round 15/6)))
- (pass-if (=exact 3 (round 16/6)))
- (pass-if (=exact 3 (round 17/6))))
- (with-test-prefix "real"
- (pass-if "0.0"
- (and (= 0.0 (round 0.0))
- (inexact? (round 0.0))))
- (pass-if "1.0"
- (and (= 1.0 (round 1.0))
- (inexact? (round 1.0))))
- (pass-if "-1.0"
- (and (= -1.0 (round -1.0))
- (inexact? (round -1.0))))
- (pass-if "-3.1"
- (and (= -3.0 (round -3.1))
- (inexact? (round -3.1))))
- (pass-if "3.1"
- (and (= 3.0 (round 3.1))
- (inexact? (round 3.1))))
- (pass-if "3.9"
- (and (= 4.0 (round 3.9))
- (inexact? (round 3.9))))
- (pass-if "-3.9"
- (and (= -4.0 (round -3.9))
- (inexact? (round -3.9))))
- (pass-if "1.5"
- (and (= 2.0 (round 1.5))
- (inexact? (round 1.5))))
- (pass-if "2.5"
- (and (= 2.0 (round 2.5))
- (inexact? (round 2.5))))
- (pass-if "3.5"
- (and (= 4.0 (round 3.5))
- (inexact? (round 3.5))))
- (pass-if "-1.5"
- (and (= -2.0 (round -1.5))
- (inexact? (round -1.5))))
- (pass-if "-2.5"
- (and (= -2.0 (round -2.5))
- (inexact? (round -2.5))))
- (pass-if "-3.5"
- (and (= -4.0 (round -3.5))
- (inexact? (round -3.5))))
- ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
- ;; float with mantissa all ones) came out as 2^53 from `round' (except
- ;; on i386 and m68k systems using the coprocessor and optimizing, where
- ;; extra precision hid the problem)
- (pass-if "2^53-1"
- (let ((x (exact->inexact (1- (ash 1 53)))))
- (and (= x (round x))
- (inexact? (round x)))))
- (pass-if "-(2^53-1)"
- (let ((x (exact->inexact (- (1- (ash 1 53))))))
- (and (= x (round x))
- (inexact? (round x)))))))
- ;;;
- ;;; exact->inexact
- ;;;
- (with-test-prefix "exact->inexact"
- ;; Test "(exact->inexact n)", expect "want".
- (define (test name n want)
- (with-test-prefix name
- (pass-if-equal "pos" want (exact->inexact n))
- (pass-if-equal "neg" (- want) (exact->inexact (- n)))))
- ;; Test "(exact->inexact n)", expect "want".
- ;; "i" is a index, for diagnostic purposes.
- (define (try-i i n want)
- (test (list i n want) n want))
-
- (with-test-prefix "2^i, no round"
- (do ((i 0 (1+ i))
- (n 1 (* 2 n))
- (want 1.0 (* 2.0 want)))
- ((> i 100))
- (try-i i n want)))
-
- (with-test-prefix "2^i+1, no round"
- (do ((i 1 (1+ i))
- (n 3 (1- (* 2 n)))
- (want 3.0 (- (* 2.0 want) 1.0)))
- ((>= i dbl-mant-dig))
- (try-i i n want)))
-
- (with-test-prefix "(2^i+1)*2^100, no round"
- (do ((i 1 (1+ i))
- (n 3 (1- (* 2 n)))
- (want 3.0 (- (* 2.0 want) 1.0)))
- ((>= i dbl-mant-dig))
- (try-i i (ash n 100) (ash-flo want 100))))
-
- ;; bit pattern: 1111....11100.00
- ;; <-mantdig-><-i->
- ;;
- (with-test-prefix "mantdig ones then zeros, no rounding"
- (do ((i 0 (1+ i))
- (n (- (ash 1 dbl-mant-dig) 1) (* 2 n))
- (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want)))
- ((> i 100))
- (try-i i n want)))
-
- ;; bit pattern: 1111....111011..1
- ;; <-mantdig-> <-i->
- ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when
- ;; i >= 11 (that's when the total is 65 or more bits).
- ;;
- (with-test-prefix "mantdig ones then 011..11, round down"
- (do ((i 0 (1+ i))
- (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n)))
- (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want)))
- ((> i 100))
- (try-i i n want)))
-
- ;; bit pattern: 1111....111100..001
- ;; <-mantdig-> <--i->
- ;;
- (with-test-prefix "mantdig ones then 100..001, round up"
- (do ((i 0 (1+ i))
- (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
- (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want)))
- ((> i 100))
- (try-i i n want)))
-
- ;; bit pattern: 1000....000100..001
- ;; <-mantdig-> <--i->
- ;;
- (with-test-prefix "2^mantdig then 100..001, round up"
- (do ((i 0 (1+ i))
- (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
- (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
- ((> i 100))
- (try-i i n want)))
- (pass-if "frac big/big"
- (let ((big (ash 1 256)))
- (= 1.0 (exact->inexact (/ (1+ big) big)))))
- ;; In guile 1.8.0 this failed, giving back "nan" because it tried to
- ;; convert the num and den to doubles, resulting in infs.
- (pass-if "frac big/big, exceeding double"
- (let ((big (ash 1 4096)))
- (= 1.0 (exact->inexact (/ (1+ big) big)))))
- ;; In guile 2.0.9, 'exact->inexact' guaranteed proper rounding when
- ;; applied to non-negative fractions, but on 64-bit systems would
- ;; sometimes double-round when applied to negative fractions,
- ;; specifically when the numerator was a fixnum not exactly
- ;; representable as a double.
- (with-test-prefix "frac inum/inum, numerator not exactly representable as a double"
- (let ((n (+ 1 (expt 2 dbl-mant-dig))))
- (for-each (lambda (d)
- (test (/ n d)
- (/ n d)
- (exact->inexact (/ n d))))
- '(3 5 6 7 9 11 13 17 19 23 0.0 -0.0 +nan.0 +inf.0 -inf.0))))
- (test "round up to odd"
- ;; =====================================================
- ;; 11111111111111111111111111111111111111111111111111000101 ->
- ;; 11111111111111111111111111111111111111111111111111001000
- (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000101)
- (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000))
- (test "round down to odd"
- ;; =====================================================
- ;; 11111111111111111111111111111111111111111111111111001011 ->
- ;; 11111111111111111111111111111111111111111111111111001000
- (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b001011)
- (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000))
- (test "round tie up to even"
- ;; =====================================================
- ;; 11111111111111111111111111111111111111111111111111011100 ->
- ;; 11111111111111111111111111111111111111111111111111100000
- (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b011100)
- (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b100000))
- (test "round tie down to even"
- ;; =====================================================
- ;; 11111111111111111111111111111111111111111111111111000100 ->
- ;; 11111111111111111111111111111111111111111111111111000000
- (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000100)
- (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b000000))
- (test "round tie up to next power of two"
- ;; =====================================================
- ;; 11111111111111111111111111111111111111111111111111111100 ->
- ;; 100000000000000000000000000000000000000000000000000000000
- (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b111100)
- (expt 2.0 (+ dbl-mant-dig 3)))
- (test "miniscule value rounds to zero of appropriate sign"
- (expt 17 (- dbl-min-exp dbl-mant-dig))
- 0.0)
- (test "smallest inexact"
- (expt 2 (- dbl-min-exp dbl-mant-dig))
- (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
- (test "1/2 smallest inexact rounds down to zero"
- (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- 0.0)
- (test "just over 1/2 smallest inexact rounds up"
- (+ (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (expt 7 (- dbl-min-exp dbl-mant-dig)))
- (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
- (test "3/2 smallest inexact rounds up to twice smallest inexact"
- (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
- (test "just under 3/2 smallest inexact rounds down"
- (- (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (expt 11 (- dbl-min-exp dbl-mant-dig)))
- (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
- (test "5/2 smallest inexact rounds down to twice smallest inexact"
- (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
- (test "just over 5/2 smallest inexact rounds up"
- (+ (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (expt 13 (- dbl-min-exp dbl-mant-dig)))
- (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
- (test "one plus dbl-epsilon"
- (+ 1 dbl-epsilon-exact)
- (+ 1.0 dbl-epsilon))
- (test "one plus 1/2 dbl-epsilon rounds down to 1.0"
- (+ 1 (* 1/2 dbl-epsilon-exact))
- 1.0)
- (test "just over one plus 1/2 dbl-epsilon rounds up"
- (+ 1
- (* 1/2 dbl-epsilon-exact)
- (expt 13 (- dbl-min-exp dbl-mant-dig)))
- (+ 1.0 dbl-epsilon))
- (test "one plus 3/2 dbl-epsilon rounds up"
- (+ 1 (* 3/2 dbl-epsilon-exact))
- (+ 1.0 (* 2.0 dbl-epsilon)))
- (test "just under one plus 3/2 dbl-epsilon rounds down"
- (+ 1
- (* 3/2 dbl-epsilon-exact)
- (- (expt 17 (- dbl-min-exp dbl-mant-dig))))
- (+ 1.0 dbl-epsilon))
- (test "one plus 5/2 dbl-epsilon rounds down"
- (+ 1 (* 5/2 dbl-epsilon-exact))
- (+ 1.0 (* 2.0 dbl-epsilon)))
- (test "just over one plus 5/2 dbl-epsilon rounds up"
- (+ 1
- (* 5/2 dbl-epsilon-exact)
- (expt 13 (- dbl-min-exp dbl-mant-dig)))
- (+ 1.0 (* 3.0 dbl-epsilon)))
-
- (test "largest finite inexact"
- (* (- (expt 2 dbl-mant-dig) 1)
- (expt 2 (- dbl-max-exp dbl-mant-dig)))
- (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
-
- (test "largest finite inexact plus 1/2 epsilon rounds up to infinity"
- (* (+ (expt 2 dbl-mant-dig) -1 1/2)
- (expt 2 (- dbl-max-exp dbl-mant-dig)))
- (inf))
-
- (test "largest finite inexact plus just under 1/2 epsilon rounds down"
- (* (+ (expt 2 dbl-mant-dig) -1 1/2
- (- (expt 13 (- dbl-min-exp dbl-mant-dig))))
- (expt 2 (- dbl-max-exp dbl-mant-dig)))
- (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
-
- (test "1/2 largest finite inexact"
- (* (- (expt 2 dbl-mant-dig) 1)
- (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
- (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig 1))))
-
- (test "1/2 largest finite inexact plus 1/2 epsilon rounds up to next power of two"
- (* (+ (expt 2 dbl-mant-dig) -1 1/2)
- (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
- (expt 2.0 (- dbl-max-exp 1)))
-
- (test "1/2 largest finite inexact plus just over 1/2 epsilon rounds up to next power of two"
- (* (+ (expt 2 dbl-mant-dig) -1 1/2
- (expt 13 (- dbl-min-exp dbl-mant-dig)))
- (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
- (expt 2.0 (- dbl-max-exp 1)))
-
- (test "1/2 largest finite inexact plus just under 1/2 epsilon rounds down"
- (* (+ (expt 2 dbl-mant-dig) -1 1/2
- (- (expt 13 (- dbl-min-exp dbl-mant-dig))))
- (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
- (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig 1))))
- )
- ;;;
- ;;; expt
- ;;;
- (with-test-prefix "expt"
- (pass-if (documented? expt))
- ;;
- ;; expt no longer requires its first argument to be a scheme number,
- ;; for the sake of extensibility, and expt calls integer-expt for
- ;; integer powers. To raise to a positive power, all that is required
- ;; is that it can be multiplied using `*'. For negative powers we
- ;; must also be able to find the reciprocal. If we try to raise #t to
- ;; any power other than 0 or 1 it may throw an exception, depending on
- ;; whether * has been defined for #t. However, when raising to the 0
- ;; or 1 power, the first argument is not manipulated at all.
- ;;
- ;; (pass-if-exception "non-numeric base" exception:wrong-type-arg
- ;; (expt #t 0))
- ;;
- (pass-if (eqv? 1 (expt 0 0)))
- (pass-if (eqv? 1 (expt 0.0 0)))
- (pass-if (eqv? 1.0 (expt 0 0.0)))
- (pass-if (eqv? 1.0 (expt 0.0 0.0)))
- (pass-if (real-nan? (expt 0 -1)))
- (pass-if (real-nan? (expt 0 -1.0)))
- (pass-if (real-nan? (expt 0.0 -1)))
- (pass-if (real-nan? (expt 0.0 -1.0)))
- (pass-if (eqv? 0 (expt 0 3)))
- (pass-if (= 0 (expt 0 4.0)))
- (pass-if (eqv? 0.0 (expt 0.0 5)))
- (pass-if (eqv? 0.0 (expt 0.0 6.0)))
- (pass-if (eqv? -2742638075.5 (expt -2742638075.5 1)))
- (pass-if (eqv? (* -2742638075.5 -2742638075.5)
- (expt -2742638075.5 2)))
- (pass-if (eqv? 4.0 (expt -2.0 2.0)))
- (pass-if (eqv? -1/8 (expt -2 -3)))
- (pass-if (eqv? -0.125 (expt -2.0 -3)))
- (pass-if (eqv? -0.125 (expt -2 -3.0)))
- (pass-if (eqv? -0.125 (expt -2.0 -3.0)))
- (pass-if (eqv? 0.25 (expt 2.0 -2.0)))
- (pass-if (eqv? 32/243 (expt 2/3 5)))
- (pass-if (eqv? 243/32 (expt 2/3 -5)))
- (pass-if (eqv? 32 (expt 1/2 -5)))
- (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (expt +12398i 2.0)))
- (pass-if (eqv-loosely? +i (expt -1 0.5)))
- (pass-if (eqv-loosely? +i (expt -1 1/2)))
- (pass-if (eqv-loosely? 1.0+1.7320508075688i (expt -8 1/3)))
- (pass-if (eqv? +inf.0 (expt 2 +inf.0)))
- (pass-if (eqv? +inf.0 (expt 2.0 +inf.0)))
- (pass-if (eqv? 0.0 (expt 2 -inf.0)))
- (pass-if (eqv? 0.0 (expt 2.0 -inf.0))))
- ;;;
- ;;; sin
- ;;;
- (with-test-prefix "sin"
- (pass-if (eqv? 0 (sin 0)))
- (pass-if (eqv? 0.0 (sin 0.0)))
- (pass-if (eqv-loosely? 1.0 (sin 1.57)))
- (pass-if (eqv-loosely? +1.175i (sin +i)))
- (pass-if (real-nan? (sin +nan.0)))
- (pass-if (real-nan? (sin +inf.0)))
- (pass-if (real-nan? (sin -inf.0))))
- ;;;
- ;;; cos
- ;;;
- (with-test-prefix "cos"
- (pass-if (eqv? 1 (cos 0)))
- (pass-if (eqv? 1.0 (cos 0.0)))
- (pass-if (eqv-loosely? 0.0 (cos 1.57)))
- (pass-if (eqv-loosely? 1.543 (cos +i)))
- (pass-if (real-nan? (cos +nan.0)))
- (pass-if (real-nan? (cos +inf.0)))
- (pass-if (real-nan? (cos -inf.0))))
- ;;;
- ;;; tan
- ;;;
- (with-test-prefix "tan"
- (pass-if (eqv? 0 (tan 0)))
- (pass-if (eqv? 0.0 (tan 0.0)))
- (pass-if (eqv-loosely? 1.0 (tan 0.785)))
- (pass-if (eqv-loosely? +0.76i (tan +i)))
- (pass-if (real-nan? (tan +nan.0)))
- (pass-if (real-nan? (tan +inf.0)))
- (pass-if (real-nan? (tan -inf.0))))
- ;;;
- ;;; asin
- ;;;
- (with-test-prefix "asin"
- (pass-if (complex-nan? (asin +nan.0)))
- (pass-if (eqv? 0 (asin 0)))
- (pass-if (eqv? 0.0 (asin 0.0))))
- ;;;
- ;;; acos
- ;;;
- (with-test-prefix "acos"
- (pass-if (complex-nan? (acos +nan.0)))
- (pass-if (eqv? 0 (acos 1)))
- (pass-if (eqv? 0.0 (acos 1.0))))
- ;;;
- ;;; atan
- ;;;
- ;;; FIXME: add tests for two-argument atan
- ;;;
- (with-test-prefix "atan"
- (pass-if (real-nan? (atan +nan.0)))
- (pass-if (eqv? 0 (atan 0)))
- (pass-if (eqv? 0.0 (atan 0.0)))
- (pass-if (eqv-loosely? 1.57 (atan +inf.0)))
- (pass-if (eqv-loosely? -1.57 (atan -inf.0)))
- (pass-if (eqv-loosely? -1.42+0.5i (atan -0.5+2.0i))))
- ;;;
- ;;; sinh
- ;;;
- (with-test-prefix "sinh"
- (pass-if (= 0 (sinh 0)))
- (pass-if (= 0.0 (sinh 0.0))))
- ;;;
- ;;; cosh
- ;;;
- (with-test-prefix "cosh"
- (pass-if (= 1 (cosh 0)))
- (pass-if (= 1.0 (cosh 0.0))))
- ;;;
- ;;; tanh
- ;;;
- (with-test-prefix "tanh"
- (pass-if (= 0 (tanh 0)))
- (pass-if (= 0.0 (tanh 0.0))))
- ;;;
- ;;; asinh
- ;;;
- (with-test-prefix "asinh"
- (pass-if (= 0 (asinh 0)))
- (pass-if (= 0.0 (asinh 0.0))))
- ;;;
- ;;; acosh
- ;;;
- (with-test-prefix "acosh"
- (pass-if (= 0 (acosh 1)))
- (pass-if (= 0.0 (acosh 1.0))))
- ;;;
- ;;; atanh
- ;;;
- (with-test-prefix "atanh"
- (pass-if (= 0 (atanh 0)))
- (pass-if (= 0.0 (atanh 0.0))))
- ;;;
- ;;; make-rectangular
- ;;;
-
- (with-test-prefix "make-rectangular"
- (pass-if (real? (make-rectangular 5.0 0 )))
- (pass-if (not (real? (make-rectangular 5.0 0.0))))
- (pass-if (not (real? (make-rectangular 5.0 -0.0)))))
- ;;;
- ;;; make-polar
- ;;;
- (with-test-prefix "make-polar"
- (define pi 3.14159265358979323846)
- (define (almost= x y)
- (> 0.01 (magnitude (- x y))))
-
- (pass-if (real? (make-polar 0 1.0)))
- (pass-if (real? (make-polar 5.0 0 )))
- (pass-if (not (real? (make-polar 5.0 0.0))))
- (pass-if (not (real? (make-polar 5.0 -0.0))))
- (pass-if (eqv? 0 (make-polar 0 0)))
- (pass-if (eqv? 0 (make-polar 0 123.456)))
- (pass-if (eqv? 1 (make-polar 1 0)))
- (pass-if (eqv? -1 (make-polar -1 0)))
-
- (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
- (pass-if (almost= -1 (make-polar 1 (* 1.0 pi))))
- (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
- (pass-if (almost= 1 (make-polar 1 (* 2.0 pi)))))
- ;;;
- ;;; real-part
- ;;;
- (with-test-prefix "real-part"
- (pass-if (documented? real-part))
- (pass-if (eqv? 5.0 (real-part 5.0)))
- (pass-if (eqv? 0.0 (real-part +5.0i)))
- (pass-if (eqv? 5 (real-part 5)))
- (pass-if (eqv? 1/5 (real-part 1/5)))
- (pass-if (eqv? (1+ fixnum-max) (real-part (1+ fixnum-max)))))
- ;;;
- ;;; imag-part
- ;;;
- (with-test-prefix "imag-part"
- (pass-if (documented? imag-part))
- (pass-if (eqv? 0 (imag-part 5.0)))
- (pass-if (eqv? 5.0 (imag-part +5.0i)))
- (pass-if (eqv? 0 (imag-part 5)))
- (pass-if (eqv? 0 (imag-part 1/5)))
- (pass-if (eqv? 0 (imag-part (1+ fixnum-max)))))
- ;;;
- ;;; magnitude
- ;;;
- (with-test-prefix "magnitude"
- (pass-if (documented? magnitude))
- (pass-if (= 0 (magnitude 0)))
- (pass-if (= 1 (magnitude 1)))
- (pass-if (= 1 (magnitude -1)))
- (pass-if (= 1 (magnitude 0+i)))
- (pass-if (= 1 (magnitude 0-i)))
- (pass-if (= 5 (magnitude 3+4i)))
- (pass-if (= 5 (magnitude 3-4i)))
- (pass-if (= 5 (magnitude -3+4i)))
- (pass-if (= 5 (magnitude -3-4i))))
- ;;;
- ;;; angle
- ;;;
- (with-test-prefix "angle"
- (define pi 3.14159265358979323846)
- (define (almost= x y)
- (> 0.000001 (magnitude (- x y))))
-
- (pass-if (documented? angle))
- (pass-if "inum +ve" (= 0 (angle 1)))
- (pass-if "inum -ve" (almost= pi (angle -1)))
- (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max))))
- (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min))))
- (pass-if "flonum +ve" (= 0 (angle 1.5)))
- (pass-if "flonum -ve" (almost= pi (angle -1.5)))
- (pass-if "signed zero +ve" (= 0 (angle 0.0)))
- (pass-if "signed zero -ve" (almost= pi (angle -0.0))))
- ;;;
- ;;; inexact->exact
- ;;;
- (with-test-prefix "inexact->exact"
- ;; Test "(inexact->exact f)", expect "want".
- (define (test name f want)
- (with-test-prefix name
- (pass-if-equal "pos" want (inexact->exact f))
- (pass-if-equal "neg" (- want) (inexact->exact (- f)))))
- (pass-if (documented? inexact->exact))
- (pass-if-exception "+inf" exception:out-of-range
- (inexact->exact +inf.0))
-
- (pass-if-exception "-inf" exception:out-of-range
- (inexact->exact -inf.0))
-
- (pass-if-exception "nan" exception:out-of-range
- (inexact->exact +nan.0))
- (test "0.0" 0.0 0)
- (test "small even integer" 72.0 72)
- (test "small odd integer" 73.0 73)
- (test "largest inexact odd integer"
- (- (expt 2.0 dbl-mant-dig) 1)
- (- (expt 2 dbl-mant-dig) 1))
- (test "largest inexact odd integer - 1"
- (- (expt 2.0 dbl-mant-dig) 2)
- (- (expt 2 dbl-mant-dig) 2))
- (test "largest inexact odd integer + 3"
- (+ (expt 2.0 dbl-mant-dig) 2)
- (+ (expt 2 dbl-mant-dig) 2))
- (test "largest inexact odd integer * 2^48"
- (* (expt 2.0 48) (- (expt 2.0 dbl-mant-dig) 1))
- (* (expt 2 48) (- (expt 2 dbl-mant-dig) 1)))
- (test "largest inexact odd integer / 2^48"
- (* (expt 0.5 48) (- (expt 2.0 dbl-mant-dig) 1))
- (* (expt 1/2 48) (- (expt 2 dbl-mant-dig) 1)))
- (test "largest finite inexact"
- (* (- (expt 2.0 dbl-mant-dig) 1)
- (expt 2.0 (- dbl-max-exp dbl-mant-dig)))
- (* (- (expt 2 dbl-mant-dig) 1)
- (expt 2 (- dbl-max-exp dbl-mant-dig))))
- (test "smallest inexact"
- (expt 2.0 (- dbl-min-exp dbl-mant-dig))
- (expt 2 (- dbl-min-exp dbl-mant-dig)))
- (test "smallest inexact * 2"
- (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
- (* 2 (expt 2 (- dbl-min-exp dbl-mant-dig))))
- (test "smallest inexact * 3"
- (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
- (* 3 (expt 2 (- dbl-min-exp dbl-mant-dig))))
- (with-test-prefix "2.0**i to exact"
- (do ((i 0 (1+ i))
- (n 1 (* 2 n))
- (f 1.0 (* 2.0 f)))
- ((> i 100))
- (test (list i n) f n))))
- ;;;
- ;;; integer-expt
- ;;;
- (with-test-prefix "integer-expt"
- (pass-if (documented? integer-expt))
- (pass-if-exception "2^+inf" exception:wrong-type-arg
- (integer-expt 2 +inf.0))
- (pass-if-exception "2^-inf" exception:wrong-type-arg
- (integer-expt 2 -inf.0))
- (pass-if-exception "2^nan" exception:wrong-type-arg
- (integer-expt 2 +nan.0))
- (pass-if (eqv? 1 (integer-expt 0 0)))
- (pass-if (eqv? 1 (integer-expt 0.0 0)))
- (pass-if (real-nan? (integer-expt 0 -1)))
- (pass-if (real-nan? (integer-expt 0.0 -1)))
- (pass-if (eqv? 0 (integer-expt 0 3)))
- (pass-if (eqv? 0.0 (integer-expt 0.0 5)))
- (pass-if (eqv? -2742638075.5 (integer-expt -2742638075.5 1)))
- (pass-if (eqv? (* -2742638075.5 -2742638075.5)
- (integer-expt -2742638075.5 2)))
- (pass-if (eqv? 4.0 (integer-expt -2.0 2)))
- (pass-if (eqv? -1/8 (integer-expt -2 -3)))
- (pass-if (eqv? -0.125 (integer-expt -2.0 -3)))
- (pass-if (eqv? 0.25 (integer-expt 2.0 -2)))
- (pass-if (eqv? 32/243 (integer-expt 2/3 5)))
- (pass-if (eqv? 243/32 (integer-expt 2/3 -5)))
- (pass-if (eqv? 32 (integer-expt 1/2 -5)))
- (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (integer-expt +12398.0i 2))))
- ;;;
- ;;; integer-length
- ;;;
- (with-test-prefix "integer-length"
- (pass-if (documented? integer-length))
-
- (with-test-prefix "-2^i, ...11100..00"
- (do ((n -1 (ash n 1))
- (i 0 (1+ i)))
- ((> i 256))
- (pass-if (list n "expect" i)
- (= i (integer-length n)))))
-
- (with-test-prefix "-2^i+1 ...11100..01"
- (do ((n -3 (logxor 3 (ash n 1)))
- (i 2 (1+ i)))
- ((> i 256))
- (pass-if n
- (= i (integer-length n)))))
-
- (with-test-prefix "-2^i-1 ...111011..11"
- (do ((n -2 (1+ (ash n 1)))
- (i 1 (1+ i)))
- ((> i 256))
- (pass-if n
- (= i (integer-length n))))))
- ;;;
- ;;; log
- ;;;
- (with-test-prefix "log"
- (pass-if (documented? log))
- (pass-if-exception "no args" exception:wrong-num-args
- (log))
- (pass-if-exception "two args" exception:wrong-num-args
- (log 123 456))
- (pass-if-exception "(log 0)" exception:numerical-overflow
- (log 0))
- (pass-if (test-eqv? -inf.0 (log 0.0)))
- (pass-if (test-eqv? +inf.0 (log +inf.0)))
- (pass-if (test-eqv? -inf.0+3.14159265358979i (log -0.0)))
- (pass-if (test-eqv? +inf.0+3.14159265358979i (log -inf.0)))
- (pass-if (test-eqv? 0.0 (log 1 )))
- (pass-if (test-eqv? 0.0 (log 1.0)))
- (pass-if (test-eqv? 1.0 (log const-e)))
- (pass-if (test-eqv? 2.0 (log const-e^2)))
- (pass-if (test-eqv? -1.0 (log const-1/e)))
- (pass-if (test-eqv? -1.0+3.14159265358979i (log (- const-1/e))))
- (pass-if (test-eqv? 2.30258509299405 (log 10)))
- (pass-if (test-eqv? 2.30258509299405+3.14159265358979i (log -10)))
- (pass-if (test-eqv? 1.0+0.0i (log (+ const-e +0.0i))))
- (pass-if (test-eqv? 1.0-0.0i (log (+ const-e -0.0i))))
- (pass-if (eqv-loosely? 230258.509299405 (log (expt 10 100000))))
- (pass-if (eqv-loosely? -230258.509299405 (log (expt 10 -100000))))
- (pass-if (eqv-loosely? 230257.410687116 (log (/ (expt 10 100000) 3))))
- (pass-if (eqv-loosely? 230258.509299405+3.14159265358979i
- (log (- (expt 10 100000)))))
- (pass-if (eqv-loosely? -230258.509299405+3.14159265358979i
- (log (- (expt 10 -100000)))))
- (pass-if (eqv-loosely? 230257.410687116+3.14159265358979i
- (log (- (/ (expt 10 100000) 3)))))
- (pass-if (test-eqv? 3.05493636349961e-151
- (log (/ (1+ (expt 2 500)) (expt 2 500)))))
- (pass-if (eqv-loosely? 1.0+1.57079i (log 0+2.71828i)))
- (pass-if (eqv-loosely? 1.0-1.57079i (log 0-2.71828i)))
- (pass-if (eqv-loosely? 0.0+3.14159i (log -1.0)))
- (pass-if (eqv-loosely? 1.0+3.14159i (log -2.71828)))
- (pass-if (eqv-loosely? 2.0+3.14159i (log (* -2.71828 2.71828)))))
- ;;;
- ;;; log10
- ;;;
- (with-test-prefix "log10"
- (pass-if (documented? log10))
- (pass-if-exception "no args" exception:wrong-num-args
- (log10))
- (pass-if-exception "two args" exception:wrong-num-args
- (log10 123 456))
- (pass-if-exception "(log10 0)" exception:numerical-overflow
- (log10 0))
- (pass-if (test-eqv? -inf.0 (log10 0.0)))
- (pass-if (test-eqv? +inf.0 (log10 +inf.0)))
- (pass-if (test-eqv? -inf.0+1.36437635384184i (log10 -0.0)))
- (pass-if (test-eqv? +inf.0+1.36437635384184i (log10 -inf.0)))
- (pass-if (test-eqv? 0.0 (log10 1 )))
- (pass-if (test-eqv? 0.0 (log10 1.0)))
- (pass-if (test-eqv? 1.0 (log10 10 )))
- (pass-if (test-eqv? 1.0 (log10 10.0)))
- (pass-if (test-eqv? 2.0 (log10 100.0)))
- (pass-if (test-eqv? -1.0 (log10 0.1)))
- (pass-if (test-eqv? -1.0+1.36437635384184i (log10 -0.1)))
- (pass-if (test-eqv? 1.0+1.36437635384184i (log10 -10 )))
- (pass-if (test-eqv? 1.0+0.0i (log10 10.0+0.0i)))
- (pass-if (test-eqv? 1.0-0.0i (log10 10.0-0.0i)))
- (pass-if (eqv-loosely? 100000.0 (log10 (expt 10 100000))))
- (pass-if (eqv-loosely? -100000.0 (log10 (expt 10 -100000))))
- (pass-if (eqv-loosely? 99999.5228787453 (log10 (/ (expt 10 100000) 3))))
- (pass-if (eqv-loosely? 100000.0+1.36437635384184i
- (log10 (- (expt 10 100000)))))
- (pass-if (eqv-loosely? -100000.0+1.36437635384184i
- (log10 (- (expt 10 -100000)))))
- (pass-if (eqv-loosely? 99999.5228787453+1.36437635384184i
- (log10 (- (/ (expt 10 100000) 3)))))
- (pass-if (test-eqv? 1.32674200523347e-151
- (log10 (/ (1+ (expt 2 500)) (expt 2 500)))))
- (pass-if (eqv-loosely? 1.0+0.68218i (log10 0+10.0i)))
- (pass-if (eqv-loosely? 1.0-0.68218i (log10 0-10.0i)))
- (pass-if (eqv-loosely? 0.0+1.36437i (log10 -1)))
- (pass-if (eqv-loosely? 1.0+1.36437i (log10 -10)))
- (pass-if (eqv-loosely? 2.0+1.36437i (log10 -100))))
- ;;;
- ;;; logbit?
- ;;;
- (with-test-prefix "logbit?"
- (pass-if (documented? logbit?))
- (pass-if (eq? #f (logbit? 0 0)))
- (pass-if (eq? #f (logbit? 1 0)))
- (pass-if (eq? #f (logbit? 31 0)))
- (pass-if (eq? #f (logbit? 32 0)))
- (pass-if (eq? #f (logbit? 33 0)))
- (pass-if (eq? #f (logbit? 63 0)))
- (pass-if (eq? #f (logbit? 64 0)))
- (pass-if (eq? #f (logbit? 65 0)))
- ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
- ;; around and return #t where it ought to be #f
- (pass-if (eq? #t (logbit? 0 1)))
- (pass-if (eq? #f (logbit? 1 1)))
- (pass-if (eq? #f (logbit? 31 1)))
- (pass-if (eq? #f (logbit? 32 1)))
- (pass-if (eq? #f (logbit? 33 1)))
- (pass-if (eq? #f (logbit? 63 1)))
- (pass-if (eq? #f (logbit? 64 1)))
- (pass-if (eq? #f (logbit? 65 1)))
- (pass-if (eq? #f (logbit? 128 1)))
- (pass-if (eq? #t (logbit? 0 -1)))
- (pass-if (eq? #t (logbit? 1 -1)))
- (pass-if (eq? #t (logbit? 31 -1)))
- (pass-if (eq? #t (logbit? 32 -1)))
- (pass-if (eq? #t (logbit? 33 -1)))
- (pass-if (eq? #t (logbit? 63 -1)))
- (pass-if (eq? #t (logbit? 64 -1)))
- (pass-if (eq? #t (logbit? 65 -1))))
- ;;;
- ;;; logcount
- ;;;
- (with-test-prefix "logcount"
- (pass-if (documented? logcount))
-
- (with-test-prefix "-2^i, meaning ...11100..00"
- (do ((n -1 (ash n 1))
- (i 0 (1+ i)))
- ((> i 256))
- (pass-if n
- (= i (logcount n)))))
- (with-test-prefix "2^i"
- (do ((n 1 (ash n 1))
- (i 0 (1+ i)))
- ((> i 256))
- (pass-if n
- (= 1 (logcount n)))))
- (with-test-prefix "2^i-1"
- (do ((n 0 (1+ (ash n 1)))
- (i 0 (1+ i)))
- ((> i 256))
- (pass-if n
- (= i (logcount n))))))
- ;;;
- ;;; logior
- ;;;
- (with-test-prefix "logior"
- (pass-if (documented? logior))
- (pass-if (eqv? -1 (logior (ash -1 1) 1)))
- ;; check that bignum or bignum+inum args will reduce to an inum
- (let ()
- (define (test x y)
- (pass-if (list x y '=> -1)
- (eqv? -1 (logior x y)))
- (pass-if (list y x '=> -1)
- (eqv? -1 (logior y x))))
- (test (ash -1 8) #xFF)
- (test (ash -1 28) #x0FFFFFFF)
- (test (ash -1 29) #x1FFFFFFF)
- (test (ash -1 30) #x3FFFFFFF)
- (test (ash -1 31) #x7FFFFFFF)
- (test (ash -1 32) #xFFFFFFFF)
- (test (ash -1 33) #x1FFFFFFFF)
- (test (ash -1 60) #x0FFFFFFFFFFFFFFF)
- (test (ash -1 61) #x1FFFFFFFFFFFFFFF)
- (test (ash -1 62) #x3FFFFFFFFFFFFFFF)
- (test (ash -1 63) #x7FFFFFFFFFFFFFFF)
- (test (ash -1 64) #xFFFFFFFFFFFFFFFF)
- (test (ash -1 65) #x1FFFFFFFFFFFFFFFF)
- (test (ash -1 128) #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
- ;;;
- ;;; lognot
- ;;;
- (with-test-prefix "lognot"
- (pass-if (documented? lognot))
- (pass-if (= -1 (lognot 0)))
- (pass-if (= 0 (lognot -1)))
- (pass-if (= -2 (lognot 1)))
- (pass-if (= 1 (lognot -2)))
-
- (pass-if (= #x-100000000000000000000000000000000
- (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
- (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
- (lognot #x-100000000000000000000000000000000))))
- ;;;
- ;;; exact-integer-sqrt
- ;;;
- (with-test-prefix "exact-integer-sqrt"
- (define (non-negative-exact-integer? k)
- (and (integer? k) (exact? k) (>= k 0)))
- (define (test k)
- (pass-if k (let-values (((s r) (exact-integer-sqrt k)))
- (and (non-negative-exact-integer? s)
- (non-negative-exact-integer? r)
- (= k (+ r (* s s)))
- (< k (* (1+ s) (1+ s)))))))
- (define (test-wrong-type-arg k)
- (pass-if-exception k exception:wrong-type-arg
- (let-values (((s r) (exact-integer-sqrt k)))
- #t)))
- (pass-if (documented? exact-integer-sqrt))
- (pass-if-exception "no args" exception:wrong-num-args
- (exact-integer-sqrt))
- (pass-if-exception "two args" exception:wrong-num-args
- (exact-integer-sqrt 123 456))
- (test 0)
- (test 1)
- (test 9)
- (test 10)
- (test fixnum-max)
- (test (1+ fixnum-max))
- (test (* fixnum-max fixnum-max))
- (test (+ 1 (* fixnum-max fixnum-max)))
- (test (expt 10 100))
- (test (+ 3 (expt 10 100)))
- (test-wrong-type-arg -1)
- (test-wrong-type-arg 1/9)
- (test-wrong-type-arg fixnum-min)
- (test-wrong-type-arg (1- fixnum-min))
- (test-wrong-type-arg 1.0)
- (test-wrong-type-arg 1.5)
- (test-wrong-type-arg "foo")
- (test-wrong-type-arg 'foo))
- ;;;
- ;;; sqrt
- ;;;
- (with-test-prefix "sqrt"
- (pass-if (documented? sqrt))
- (pass-if-exception "no args" exception:wrong-num-args
- (sqrt))
- (pass-if-exception "two args" exception:wrong-num-args
- (sqrt 123 456))
- (pass-if (eqv? 0 (sqrt 0)))
- (pass-if (eqv? 1 (sqrt 1)))
- (pass-if (eqv? 2 (sqrt 4)))
- (pass-if (eqv? 3 (sqrt 9)))
- (pass-if (eqv? 4 (sqrt 16)))
- (pass-if (eqv? fixnum-max (sqrt (expt fixnum-max 2))))
- (pass-if (eqv? (+ 1 fixnum-max) (sqrt (expt (+ 1 fixnum-max) 2))))
- (pass-if (eqv? (expt 10 400) (sqrt (expt 10 800))))
- (pass-if (eqv? (/ (expt 10 1000)
- (expt 13 1000))
- (sqrt (/ (expt 10 2000)
- (expt 13 2000)))))
- (with-test-prefix "exact sqrt"
- (define (test root)
- (pass-if (list root 'exact)
- (eqv? root (sqrt (expt root 2))))
- (pass-if (list root '*2)
- (let ((r (sqrt (* 2 (expt root 2)))))
- (and (inexact? r)
- (eqv-loosely? (* (sqrt 2) root) r))))
- (pass-if (list root '-1)
- (let ((r (sqrt (- (expt root 2) 1))))
- (and (inexact? r)
- (eqv-loosely? root r))))
- (pass-if (list root '+1)
- (let ((r (sqrt (+ (expt root 2) 1))))
- (and (inexact? r)
- (eqv-loosely? root r))))
- (pass-if (list root 'negative)
- (eqv-loosely? (* +i root) (sqrt (- (expt root 2))))))
- (test (exact-integer-sqrt (+ -1 (expt 2 (+ 2 dbl-mant-dig)))))
- (test (exact-integer-sqrt (+ -1 (expt 2 (+ 1 dbl-mant-dig)))))
- (test (exact-integer-sqrt (+ -1 (expt 2 (+ 0 dbl-mant-dig)))))
- (test (exact-integer-sqrt (+ -1 (expt 2 (+ -1 dbl-mant-dig)))))
- (test (exact-integer-sqrt (+ -1 (expt 2 (+ -2 dbl-mant-dig)))))
- ;; largest finite inexact
- (test (* (- (expt 2 dbl-mant-dig) 1)
- (expt 2 (- dbl-max-exp dbl-mant-dig)))))
- (pass-if-equal "smallest inexact"
- (expt 2.0 (- dbl-min-exp dbl-mant-dig))
- (sqrt (/ (+ -1 (expt 2 (* 2 (- dbl-mant-dig dbl-min-exp)))))))
- (with-test-prefix "extreme ratios"
- (define-syntax-rule (test want x)
- (pass-if 'x
- (let ((got (sqrt x)))
- (and (inexact? got)
- (test-eqv? 1.0 (/ want got))))))
- (test 1.511139943175573e176 (/ (expt 3 2001) (expt 2 2001)))
- (test 2.1370746022826034e176 (/ (expt 3 2001) (expt 2 2000)))
- (test 8.724570529756128e175 (/ (expt 3 2000) (expt 2 2001)))
- (test 6.6175207962444435e-177 (/ (expt 2 2001) (expt 3 2001)))
- (test 1.1461882239239027e-176 (/ (expt 2 2001) (expt 3 2000)))
- (test 4.679293829667447e-177 (/ (expt 2 2000) (expt 3 2001))))
- (pass-if (eqv? (/ (expt 2 1000)
- (expt 3 1000))
- (sqrt (/ (expt 2 2000)
- (expt 3 2000)))))
- (pass-if (eqv? (/ (expt 3 1000)
- (expt 2 1000))
- (sqrt (/ (expt 3 2000)
- (expt 2 2000)))))
- (pass-if (eqv? +4i (sqrt -16)))
- (pass-if (eqv-loosely? +1.0e150i (sqrt #e-1e300)))
- (pass-if (eqv-loosely? +0.7071i (sqrt -1/2)))
- (pass-if (eqv? 0.0 (sqrt 0.0)))
- (pass-if (eqv? 1.0 (sqrt 1.0)))
- (pass-if (eqv-loosely? 2.0 (sqrt 4.0)))
- (pass-if (eqv-loosely? 31.62 (sqrt 1000.0)))
- (pass-if (eqv? +1.0i (sqrt -1.0)))
- (pass-if (eqv-loosely? +2.0i (sqrt -4.0)))
- (pass-if (eqv-loosely? +31.62i (sqrt -1000.0)))
- (pass-if "+i swings back to 45deg angle"
- (eqv-loosely? +0.7071+0.7071i (sqrt +1.0i)))
- ;; Note: glibc 2.3 csqrt() had a bug affecting this test case, so if it
- ;; fails check whether that's the cause (there's a configure test to
- ;; reject it, but when cross-compiling we assume the C library is ok).
- (pass-if "-100i swings back to 45deg down"
- (eqv-loosely? +7.071-7.071i (sqrt -100.0i))))
- ;;;
- ;;; Tests for number-theoretic division operators:
- ;;;
- ;;; euclidean/
- ;;; euclidean-quotient
- ;;; euclidean-remainder
- ;;; floor/
- ;;; floor-quotient
- ;;; floor-remainder
- ;;; ceiling/
- ;;; ceiling-quotient
- ;;; ceiling-remainder
- ;;; truncate/
- ;;; truncate-quotient
- ;;; truncate-remainder
- ;;; centered/
- ;;; centered-quotient
- ;;; centered-remainder
- ;;; round/
- ;;; round-quotient
- ;;; round-remainder
- ;;;
- (with-test-prefix "Number-theoretic division"
- ;; Tests that (lo <1 x <2 hi),
- ;; but allowing for imprecision
- ;; if x is inexact.
- (define (test-within-range? lo <1 x <2 hi)
- (if (exact? x)
- (and (<1 lo x) (<2 x hi))
- (let ((lo (- lo test-epsilon))
- (hi (+ hi test-epsilon)))
- (<= lo x hi))))
- (define (valid-euclidean-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (test-within-range? 0 <= r < (abs y)))
- (test-eqv? q (/ x y)))))
- (define (valid-floor-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (if (> y 0)
- (test-within-range? 0 <= r < y)
- (test-within-range? y < r <= 0)))
- (test-eqv? q (/ x y)))))
- (define (valid-ceiling-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (if (> y 0)
- (test-within-range? (- y) < r <= 0)
- (test-within-range? 0 <= r < (- y))))
- (test-eqv? q (/ x y)))))
- (define (valid-truncate-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (if (> x 0)
- (test-within-range? 0 <= r < (abs y))
- (test-within-range? (- (abs y)) < r <= 0)))
- (test-eqv? q (/ x y)))))
- (define (valid-centered-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (test-within-range?
- (* -1/2 (abs y)) <= r < (* +1/2 (abs y))))
- (test-eqv? q (/ x y)))))
- (define (valid-round-answer? x y q r)
- (and (eq? (exact? q)
- (exact? r)
- (and (exact? x) (exact? y)))
- (test-eqv? r (- x (* q y)))
- (if (and (finite? x) (finite? y))
- (and (integer? q)
- (let ((ay/2 (/ (abs y) 2)))
- (if (even? q)
- (test-within-range? (- ay/2) <= r <= ay/2)
- (test-within-range? (- ay/2) < r < ay/2))))
- (test-eqv? q (/ x y)))))
- (define (for lsts f) (apply for-each f lsts))
- (define big (expt 10 (1+ (inexact->exact (ceiling (log10 fixnum-max))))))
- (define (run-division-tests quo+rem quo rem valid-answer?)
- (define (test n d)
- (run-test (list n d) #t
- (lambda ()
- (let-values (((q r) (quo+rem n d)))
- (and (test-eqv? q (quo n d))
- (test-eqv? r (rem n d))
- (valid-answer? n d q r))))))
- (define (test+/- n d)
- (test n d )
- (test n (- d))
- (cond ((not (zero? n))
- (test (- n) d )
- (test (- n) (- d)))))
- (define (test-for-exception n d exception)
- (let ((name (list n d)))
- (pass-if-exception name exception (quo+rem n d))
- (pass-if-exception name exception (quo n d))
- (pass-if-exception name exception (rem n d))))
- (run-test "documented?" #t
- (lambda ()
- (and (documented? quo+rem)
- (documented? quo)
- (documented? rem))))
- (with-test-prefix "inum / inum"
- (with-test-prefix "fixnum-min / -1"
- (test fixnum-min -1))
- (for '((1 2 5 10)) ;; denominators
- (lambda (d)
- (for '((0 1 2 5 10)) ;; multiples
- (lambda (m)
- (for '((-2 -1 0 1 2 3 4 5 7 10
- 12 15 16 19 20)) ;; offsets
- (lambda (b)
- (test+/- (+ b (* m d))
- d))))))))
- (with-test-prefix "inum / big"
- (with-test-prefix "fixnum-min / -fixnum-min"
- (test fixnum-min (- fixnum-min)))
- (with-test-prefix "fixnum-max / (2*fixnum-max)"
- (test+/- fixnum-max (* 2 fixnum-max)))
- (for `((0 1 2 10 ,(1- fixnum-max) ,fixnum-max))
- (lambda (n)
- (test n (1+ fixnum-max))
- (test (- n) (1+ fixnum-max))
- (test n (1- fixnum-min))
- (test (- n) (1- fixnum-min)))))
- (with-test-prefix "big / inum"
- (with-test-prefix "-fixnum-min / fixnum-min"
- (test (- fixnum-min) fixnum-min))
- (for '((1 4 5 10)) ;; denominators
- (lambda (d)
- (for `((1 2 5 ,@(if (even? d)
- '(1/2 3/2 5/2)
- '()))) ;; multiples
- (lambda (m)
- (for '((-2 -1 0 1 2)) ;; offsets
- (lambda (b)
- (test+/- (+ b (* m d big))
- d))))))))
- (with-test-prefix "big / big"
- (for `((,big ,(1+ big))) ;; denominators
- (lambda (d)
- (for `((1 2 5 ,@(if (even? d)
- '(1/2 3/2 5/2)
- '()))) ;; multiples
- (lambda (m)
- (for '((-2 -1 0 1 2)) ;; offsets
- (lambda (b)
- (test+/- (+ b (* m d))
- d))))))))
- (with-test-prefix "inexact"
- (for '((0.5 1.5 2.25 5.75)) ;; denominators
- (lambda (d)
- (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples
- (lambda (m)
- (for '((-2 -1 0 1 2)) ;; offsets
- (lambda (b)
- (test+/- (+ b (* m d))
- d))))))))
- (with-test-prefix "fractions"
- (for '((1/10 16/3 10/7)) ;; denominators
- (lambda (d)
- (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples
- (lambda (m)
- (for '((-2/9 -1/11 0 1/3 2/3)) ;; offsets
- (lambda (b)
- (test+/- (+ b (* m d))
- d))))))))
- (with-test-prefix "mixed types"
- (for `((10 ,big 12.0 10/7 +inf.0 -inf.0 +nan.0)) ;; denominators
- (lambda (d)
- (for `((25 ,(* 3/2 big) 130.0 15/7
- 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators
- (lambda (n)
- (test+/- n d))))))
- (with-test-prefix "divide by zero"
- (for `((0 0.0 -0.0)) ;; denominators
- (lambda (d)
- (for `((15 ,(* 3/2 big) 18.0 33/7
- 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators
- (lambda (n)
- (test-for-exception
- n d exception:numerical-overflow)))))))
- (with-test-prefix "euclidean/"
- (run-division-tests euclidean/
- euclidean-quotient
- euclidean-remainder
- valid-euclidean-answer?))
- (with-test-prefix "floor/"
- (run-division-tests floor/
- floor-quotient
- floor-remainder
- valid-floor-answer?))
- (with-test-prefix "ceiling/"
- (run-division-tests ceiling/
- ceiling-quotient
- ceiling-remainder
- valid-ceiling-answer?))
- (with-test-prefix "truncate/"
- (run-division-tests truncate/
- truncate-quotient
- truncate-remainder
- valid-truncate-answer?))
- (with-test-prefix "centered/"
- (run-division-tests centered/
- centered-quotient
- centered-remainder
- valid-centered-answer?))
- (with-test-prefix "round/"
- (run-division-tests round/
- round-quotient
- round-remainder
- valid-round-answer?)))
- ;;;
- ;;; ash
- ;;; round-ash
- ;;;
- (let ()
- (define (test-ash-variant name ash-variant round-variant rounded?)
- (with-test-prefix name
- (define (test n count)
- (pass-if (list n count)
- (eqv? (ash-variant n count)
- (round-variant (* n (expt 2 count))))))
- (pass-if "documented?"
- (documented? ash-variant))
- (for-each (lambda (n)
- (for-each (lambda (count) (test n count))
- `(-1000
- ,(* 2 (- fixnum-bit))
- ,(- -3 fixnum-bit)
- ,(- -2 fixnum-bit)
- ,(- -1 fixnum-bit)
- ,(- fixnum-bit)
- ,(- (- fixnum-bit 1))
- -3 -2 -1 0 1 2 3
- ,(- fixnum-bit 1)
- ,fixnum-bit
- ,(+ fixnum-bit 1)
- ,(+ fixnum-bit 2)
- ,(+ fixnum-bit 3)
- ,(* 2 fixnum-bit)
- 1000)))
- (list 0 1 3 23 -1 -3 -23
- fixnum-max
- (1+ fixnum-max)
- (1- fixnum-max)
- (* fixnum-max 4)
- (quotient fixnum-max 4)
- fixnum-min
- (1+ fixnum-min)
- (1- fixnum-min)
- (* fixnum-min 4)
- (quotient fixnum-min 4)))
- (do ((count -2 (1- count))
- (vals '(1 3 5 7 9 11)
- (map (lambda (n) (* 2 n)) vals)))
- ((> (car vals) (* 2 fixnum-max)) 'done)
- (for-each (lambda (n)
- (test n count)
- (test (- n) count))
- vals))
- ;; Test rounding
- (for-each (lambda (base)
- (for-each (lambda (offset) (test (+ base offset) -3))
- '(#b11001 #b11100 #b11101 #b10001 #b10100 #b10101)))
- (list 0 64 -64 (* 64 fixnum-max) (* 64 fixnum-min)))
- ;; Right shift by LONG_MIN, typically (ash -1 63) and (ash -1 31)
- ;; depending on the word size, where negating the shift count
- ;; overflows. See <https://bugs.gnu.org/21901>.
- (pass-if-equal "Right shift of positive integer by (ash -1 63) bits"
- 0
- (ash-variant 123 (ash -1 63)))
- (pass-if-equal "Right shift of negative integer by (ash -1 63) bits"
- (if rounded? 0 -1)
- (ash-variant -123 (ash -1 63)))
- (pass-if-equal "Right shift of positive integer by (ash -1 31) bits"
- 0
- (ash-variant 123 (ash -1 31)))
- (pass-if-equal "Right shift of negative integer by (ash -1 31) bits"
- (if rounded? 0 -1)
- (ash-variant -123 (ash -1 31)))
- ;; Huge shift counts
- (pass-if-equal "Huge left shift of 0"
- 0
- (ash-variant 0 (expt 2 1000)))
- (pass-if-equal "Huge right shift of 0"
- 0
- (ash-variant 0 (- (expt 2 1000))))
- (pass-if-equal "Huge right shift of positive integer"
- 0
- (ash-variant 123 (- (expt 2 1000))))
- (pass-if-equal "Huge right shift of negative integer"
- (if rounded? 0 -1)
- (ash-variant -123 (- (expt 2 1000))))
- (pass-if-equal "Huge right shift of -1"
- (if rounded? 0 -1)
- (ash-variant -1 (- (expt 2 1000))))
- (pass-if-exception "Huge left shift of non-zero => numerical overflow"
- exception:numerical-overflow
- (ash-variant 123 (expt 2 1000)))))
- (test-ash-variant 'ash ash floor #f)
- (test-ash-variant 'round-ash round-ash round #t))
- ;;;
- ;;; regressions
- ;;;
- (with-test-prefix/c&e "bug in unboxing f64 in 2.1.6"
- (pass-if "= real and complex"
- (= 1.0 (make-rectangular 1.0 0.0))))
|