numbers.test 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481
  1. ;;;; numbers.test --- tests guile's numbers -*- scheme -*-
  2. ;;;; Copyright (C) 2000, 2001, 2003-2006, 2009-2013,
  3. ;;;; 2015, 2018 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite test-numbers)
  19. #:use-module (test-suite lib)
  20. #:use-module (ice-9 documentation)
  21. #:use-module (srfi srfi-1) ; list library
  22. #:use-module (srfi srfi-11)) ; let-values
  23. ;;;
  24. ;;; miscellaneous
  25. ;;;
  26. (define exception:numerical-overflow
  27. (cons 'numerical-overflow "^Numerical overflow"))
  28. (define (documented? object)
  29. (not (not (object-documentation object))))
  30. (define fixnum-bit
  31. (do ((i 0 (+ 1 i))
  32. (n 1 (* 2 n)))
  33. ((> n most-positive-fixnum)
  34. (+ 1 i))))
  35. (define fixnum-min most-negative-fixnum)
  36. (define fixnum-max most-positive-fixnum)
  37. ;; Divine the number of bits in the mantissa of a flonum.
  38. ;; We look for when 2.0^i+1.0 gets rounded, ie. the difference between that
  39. ;; value and 2.0^k is not 1.0.
  40. ;; Of course this assumes flonums have a fixed precision mantissa, but
  41. ;; that's the case now and probably into the forseeable future.
  42. ;; On an IEEE system, which means pretty much everywhere, the value here is
  43. ;; the usual 53.
  44. ;;
  45. (define dbl-mant-dig
  46. (do ((prec 0 (+ prec 1))
  47. (eps 1.0 (/ eps 2.0)))
  48. ((begin (when (> prec 1000000)
  49. (error "Unable to determine dbl-mant-dig"))
  50. (= 1.0 (+ 1.0 eps)))
  51. prec)))
  52. (define dbl-epsilon
  53. (expt 0.5 (- dbl-mant-dig 1)))
  54. (define dbl-epsilon-exact
  55. (expt 1/2 (- dbl-mant-dig 1)))
  56. (define dbl-min-exp
  57. (do ((x 1.0 (/ x 2.0))
  58. (y (+ 1.0 dbl-epsilon) (/ y 2.0))
  59. (e 2 (- e 1)))
  60. ((begin (when (< e -100000000)
  61. (error "Unable to determine dbl-min-exp"))
  62. (= x y))
  63. e)))
  64. (define dbl-max-exp
  65. (do ((x 1.0 (* x 2.0))
  66. (e 0 (+ e 1)))
  67. ((begin (when (> e 100000000)
  68. (error "Unable to determine dbl-max-exp"))
  69. (inf? x))
  70. e)))
  71. ;; like ash, but working on a flonum
  72. (define (ash-flo x n)
  73. (while (> n 0)
  74. (set! x (* 2.0 x))
  75. (set! n (1- n)))
  76. (while (< n 0)
  77. (set! x (* 0.5 x))
  78. (set! n (1+ n)))
  79. x)
  80. ;; `quotient' but rounded towards -infinity, like `modulo' or `ash' do
  81. ;; note only positive D supported (that's all that's currently required)
  82. (define-public (quotient-floor n d)
  83. (if (negative? n)
  84. (quotient (- n d -1) d) ;; neg/pos
  85. (quotient n d))) ;; pos/pos
  86. ;; return true of X is in the range LO to HI, inclusive
  87. (define (within-range? lo hi x)
  88. (and (>= x (min lo hi))
  89. (<= x (max lo hi))))
  90. ;; return true if GOT is within +/- 0.01 of GOT
  91. ;; for a complex number both real and imaginary parts must be in that range
  92. (define (eqv-loosely? want got)
  93. (and (within-range? (- (real-part want) 0.01)
  94. (+ (real-part want) 0.01)
  95. (real-part got))
  96. (within-range? (- (imag-part want) 0.01)
  97. (+ (imag-part want) 0.01)
  98. (imag-part got))))
  99. ;; return true if OBJ is negative infinity
  100. (define (negative-infinity? obj)
  101. (and (real? obj)
  102. (negative? obj)
  103. (inf? obj)))
  104. ;;
  105. ;; Tolerance used by test-eqv? for inexact numbers.
  106. ;;
  107. (define test-epsilon 1e-10)
  108. ;;
  109. ;; Like eqv?, except that inexact finite numbers need only be within
  110. ;; test-epsilon (1e-10) to be considered equal. For non-real complex
  111. ;; numbers, each component is tested according to these rules. The
  112. ;; intent is that the known-correct value will be the first parameter.
  113. ;;
  114. (define (test-eqv? x y)
  115. (cond ((real? x)
  116. (and (real? y) (test-real-eqv? x y)))
  117. ((complex? x)
  118. (and (not (real? y))
  119. (test-real-eqv? (real-part x) (real-part y))
  120. (test-real-eqv? (imag-part x) (imag-part y))))
  121. (else (eqv? x y))))
  122. ;; Auxiliary predicate used by test-eqv?
  123. (define (test-real-eqv? x y)
  124. (cond ((or (exact? x) (nan? x) (inf? x))
  125. (eqv? x y))
  126. (else (and (inexact? y) (> test-epsilon (abs (- x y)))))))
  127. ;; return true if OBJ is a real NaN
  128. (define (real-nan? obj)
  129. (and (real? obj)
  130. (nan? obj)))
  131. ;; return true if OBJ is a non-real complex number
  132. ;; whose real part is a nan, and whose imaginary
  133. ;; part is an inexact zero.
  134. (define (almost-real-nan? obj)
  135. (and (not (real? obj))
  136. (nan? (real-part obj))
  137. (zero? (imag-part obj))))
  138. ;; return true if both the real and imaginary
  139. ;; parts of OBJ are NaNs
  140. (define (complex-nan? obj)
  141. (and (nan? (real-part obj))
  142. (nan? (imag-part obj))))
  143. ;; return true if the real part of OBJ is zero
  144. ;; and the imaginary part is a NaN.
  145. (define (imaginary-nan? obj)
  146. (and (zero? (real-part obj))
  147. (nan? (imag-part obj))))
  148. ;; return true if OBJ is a non-real complex zero
  149. (define (complex-zero? obj)
  150. (and (zero? obj)
  151. (complex? obj)
  152. (not (real? obj))))
  153. (define const-e 2.7182818284590452354)
  154. (define const-e^2 7.3890560989306502274)
  155. (define const-1/e 0.3678794411714423215)
  156. ;;;
  157. ;;; 1+
  158. ;;;
  159. (with-test-prefix/c&e "1+"
  160. (pass-if "documented?"
  161. (documented? 1+))
  162. (pass-if "0" (eqv? 1 (1+ 0)))
  163. (pass-if "-1" (eqv? 0 (1+ -1)))
  164. (pass-if "100" (eqv? 101 (1+ 100)))
  165. (pass-if "-100" (eqv? -99 (1+ -100)))
  166. ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
  167. (pass-if "1+ fixnum = bignum (32-bit)"
  168. (eqv? 536870912 (1+ 536870911)))
  169. ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
  170. (pass-if "1+ fixnum = bignum (64-bit)"
  171. (eqv? 2305843009213693952 (1+ 2305843009213693951))))
  172. ;;;
  173. ;;; 1-
  174. ;;;
  175. (with-test-prefix/c&e "1-"
  176. (pass-if "documented?"
  177. (documented? 1-))
  178. (pass-if "0" (eqv? -1 (1- 0)))
  179. (pass-if "1" (eqv? 0 (1- 1)))
  180. (pass-if "100" (eqv? 99 (1- 100)))
  181. (pass-if "-100" (eqv? -101 (1- -100)))
  182. ;; The minimum fixnum on a 32-bit architecture: -2^29.
  183. (pass-if "1- fixnum = bignum (32-bit)"
  184. (eqv? -536870913 (1- -536870912)))
  185. ;; The minimum fixnum on a 64-bit architecture: -2^61.
  186. (pass-if "1- fixnum = bignum (64-bit)"
  187. (eqv? -2305843009213693953 (1- -2305843009213693952))))
  188. ;;;
  189. ;;; exact?
  190. ;;;
  191. (with-test-prefix "exact?"
  192. (pass-if "documented?"
  193. (documented? exact?))
  194. (with-test-prefix "integers"
  195. (pass-if "0"
  196. (exact? 0))
  197. (pass-if "fixnum-max"
  198. (exact? fixnum-max))
  199. (pass-if "fixnum-max + 1"
  200. (exact? (+ fixnum-max 1)))
  201. (pass-if "fixnum-min"
  202. (exact? fixnum-min))
  203. (pass-if "fixnum-min - 1"
  204. (exact? (- fixnum-min 1))))
  205. (with-test-prefix "reals"
  206. ;; (FIXME: need better examples.)
  207. (pass-if "sqrt (fixnum-max^2 - 1)"
  208. (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1)))))
  209. (pass-if "sqrt ((fixnum-max+1)^2 - 1)"
  210. (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))
  211. (pass-if (not (exact? +inf.0)))
  212. (pass-if (not (exact? -inf.0)))
  213. (pass-if (not (exact? +nan.0)))))
  214. ;;;
  215. ;;; exp
  216. ;;;
  217. (with-test-prefix "exp"
  218. (pass-if (documented? exp))
  219. (pass-if-exception "no args" exception:wrong-num-args
  220. (exp))
  221. (pass-if-exception "two args" exception:wrong-num-args
  222. (exp 123 456))
  223. (pass-if (eqv? 0.0 (exp -inf.0)))
  224. (pass-if (eqv-loosely? 1.0 (exp 0)))
  225. (pass-if (eqv-loosely? 1.0 (exp 0.0)))
  226. (pass-if (eqv-loosely? const-e (exp 1.0)))
  227. (pass-if (eqv-loosely? const-e^2 (exp 2.0)))
  228. (pass-if (eqv-loosely? const-1/e (exp -1)))
  229. (pass-if "exp(pi*i) = -1"
  230. (eqv-loosely? -1.0 (exp 0+3.14159i)))
  231. (pass-if "exp(-pi*i) = -1"
  232. (eqv-loosely? -1.0 (exp 0-3.14159i)))
  233. (pass-if "exp(2*pi*i) = +1"
  234. (eqv-loosely? 1.0 (exp 0+6.28318i)))
  235. (pass-if "exp(2-pi*i) = -e^2"
  236. (eqv-loosely? (- const-e^2) (exp 2.0-3.14159i))))
  237. ;;;
  238. ;;; odd?
  239. ;;;
  240. (with-test-prefix "odd?"
  241. (pass-if (documented? odd?))
  242. (pass-if (odd? 1))
  243. (pass-if (odd? -1))
  244. (pass-if (not (odd? 0)))
  245. (pass-if (not (odd? 2)))
  246. (pass-if (not (odd? -2)))
  247. (pass-if (odd? (+ (* 2 fixnum-max) 1)))
  248. (pass-if (not (odd? (* 2 fixnum-max))))
  249. (pass-if (odd? (- (* 2 fixnum-min) 1)))
  250. (pass-if (not (odd? (* 2 fixnum-min)))))
  251. ;;;
  252. ;;; even?
  253. ;;;
  254. (with-test-prefix "even?"
  255. (pass-if (documented? even?))
  256. (pass-if (even? 2))
  257. (pass-if (even? -2))
  258. (pass-if (even? 0))
  259. (pass-if (not (even? 1)))
  260. (pass-if (not (even? -1)))
  261. (pass-if (not (even? (+ (* 2 fixnum-max) 1))))
  262. (pass-if (even? (* 2 fixnum-max)))
  263. (pass-if (not (even? (- (* 2 fixnum-min) 1))))
  264. (pass-if (even? (* 2 fixnum-min))))
  265. ;;;
  266. ;;; finite?
  267. ;;;
  268. (with-test-prefix "finite?"
  269. (pass-if (documented? finite?))
  270. (pass-if (not (finite? (inf))))
  271. (pass-if (not (finite? +inf.0)))
  272. (pass-if (not (finite? -inf.0)))
  273. (pass-if-exception
  274. "complex numbers not in domain of finite?"
  275. exception:wrong-type-arg
  276. (finite? +inf.0+1i))
  277. (pass-if-exception
  278. "complex numbers not in domain of finite? (2)"
  279. exception:wrong-type-arg
  280. (finite? +1+inf.0i))
  281. (pass-if-exception
  282. "complex numbers not in domain of finite? (3)"
  283. exception:wrong-type-arg
  284. (finite? +1+1i))
  285. (pass-if (finite? 3+0i))
  286. (pass-if (not (finite? (nan))))
  287. (pass-if (not (finite? +nan.0)))
  288. (pass-if (finite? 0))
  289. (pass-if (finite? 0.0))
  290. (pass-if (finite? -0.0))
  291. (pass-if (finite? 42.0))
  292. (pass-if (finite? 1/2))
  293. (pass-if (finite? (+ fixnum-max 1)))
  294. (pass-if (finite? (- fixnum-min 1))))
  295. ;;;
  296. ;;; inf? and inf
  297. ;;;
  298. (with-test-prefix "inf?"
  299. (pass-if (documented? inf?))
  300. (pass-if (inf? (inf)))
  301. ;; FIXME: what are the expected behaviors?
  302. ;; (pass-if (inf? (/ 1.0 0.0))
  303. ;; (pass-if (inf? (/ 1 0.0))
  304. (pass-if-exception
  305. "complex numbers not in domain of inf?"
  306. exception:wrong-type-arg
  307. (inf? +1+inf.0i))
  308. (pass-if (inf? +inf.0+0i))
  309. (pass-if (not (inf? 0)))
  310. (pass-if (not (inf? 42.0)))
  311. (pass-if (not (inf? (+ fixnum-max 1))))
  312. (pass-if (not (inf? (- fixnum-min 1)))))
  313. ;;;
  314. ;;; nan? and nan
  315. ;;;
  316. (with-test-prefix "nan?"
  317. (pass-if (documented? nan?))
  318. (pass-if (nan? (nan)))
  319. ;; FIXME: other ways we should be able to generate NaN?
  320. (pass-if (not (nan? 0)))
  321. (pass-if (not (nan? 42.0)))
  322. (pass-if (not (nan? (+ fixnum-max 1))))
  323. (pass-if (not (nan? (- fixnum-min 1)))))
  324. ;;;
  325. ;;; abs
  326. ;;;
  327. (with-test-prefix "abs"
  328. (pass-if (documented? abs))
  329. (pass-if (eqv? 0 (abs 0)))
  330. (pass-if (eqv? 1 (abs 1)))
  331. (pass-if (eqv? 1 (abs -1)))
  332. (with-test-prefix "double-negation of fixnum-min"
  333. (pass-if (eqv? fixnum-min (- (abs fixnum-min)))))
  334. (pass-if (eqv? (+ fixnum-max 1) (abs (+ fixnum-max 1))))
  335. (pass-if (eqv? (+ (- fixnum-min) 1) (abs (- fixnum-min 1))))
  336. (pass-if (eqv? 0.0 (abs 0.0)))
  337. (pass-if (eqv? 0.0 (abs -0.0)))
  338. (pass-if (eqv? 1.0 (abs 1.0)))
  339. (pass-if (eqv? 1.0 (abs -1.0)))
  340. (pass-if (real-nan? (abs +nan.0)))
  341. (pass-if (eqv? +inf.0 (abs +inf.0)))
  342. (pass-if (eqv? +inf.0 (abs -inf.0))))
  343. ;;;
  344. ;;; quotient
  345. ;;;
  346. (with-test-prefix "quotient"
  347. (pass-if (documented? quotient))
  348. (with-test-prefix "0 / n"
  349. (pass-if "n = 1"
  350. (eqv? 0 (quotient 0 1)))
  351. (pass-if "n = -1"
  352. (eqv? 0 (quotient 0 -1)))
  353. (pass-if "n = 2"
  354. (eqv? 0 (quotient 0 2)))
  355. (pass-if "n = fixnum-max"
  356. (eqv? 0 (quotient 0 fixnum-max)))
  357. (pass-if "n = fixnum-max + 1"
  358. (eqv? 0 (quotient 0 (+ fixnum-max 1))))
  359. (pass-if "n = fixnum-min"
  360. (eqv? 0 (quotient 0 fixnum-min)))
  361. (pass-if "n = fixnum-min - 1"
  362. (eqv? 0 (quotient 0 (- fixnum-min 1)))))
  363. (with-test-prefix "1 / n"
  364. (pass-if "n = 1"
  365. (eqv? 1 (quotient 1 1)))
  366. (pass-if "n = -1"
  367. (eqv? -1 (quotient 1 -1)))
  368. (pass-if "n = 2"
  369. (eqv? 0 (quotient 1 2)))
  370. (pass-if "n = fixnum-max"
  371. (eqv? 0 (quotient 1 fixnum-max)))
  372. (pass-if "n = fixnum-max + 1"
  373. (eqv? 0 (quotient 1 (+ fixnum-max 1))))
  374. (pass-if "n = fixnum-min"
  375. (eqv? 0 (quotient 1 fixnum-min)))
  376. (pass-if "n = fixnum-min - 1"
  377. (eqv? 0 (quotient 1 (- fixnum-min 1)))))
  378. (with-test-prefix "-1 / n"
  379. (pass-if "n = 1"
  380. (eqv? -1 (quotient -1 1)))
  381. (pass-if "n = -1"
  382. (eqv? 1 (quotient -1 -1)))
  383. (pass-if "n = 2"
  384. (eqv? 0 (quotient -1 2)))
  385. (pass-if "n = fixnum-max"
  386. (eqv? 0 (quotient -1 fixnum-max)))
  387. (pass-if "n = fixnum-max + 1"
  388. (eqv? 0 (quotient -1 (+ fixnum-max 1))))
  389. (pass-if "n = fixnum-min"
  390. (eqv? 0 (quotient -1 fixnum-min)))
  391. (pass-if "n = fixnum-min - 1"
  392. (eqv? 0 (quotient -1 (- fixnum-min 1)))))
  393. (with-test-prefix "fixnum-max / n"
  394. (pass-if "n = 1"
  395. (eqv? fixnum-max (quotient fixnum-max 1)))
  396. (pass-if "n = -1"
  397. (eqv? (- fixnum-max) (quotient fixnum-max -1)))
  398. (pass-if "n = 2"
  399. (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1)))
  400. (pass-if "n = fixnum-max"
  401. (eqv? 1 (quotient fixnum-max fixnum-max)))
  402. (pass-if "n = fixnum-max + 1"
  403. (eqv? 0 (quotient fixnum-max (+ fixnum-max 1))))
  404. (pass-if "n = fixnum-min"
  405. (eqv? 0 (quotient fixnum-max fixnum-min)))
  406. (pass-if "n = fixnum-min - 1"
  407. (eqv? 0 (quotient fixnum-max (- fixnum-min 1)))))
  408. (with-test-prefix "(fixnum-max + 1) / n"
  409. (pass-if "n = 1"
  410. (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1)))
  411. (pass-if "n = -1"
  412. (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1)))
  413. (pass-if "n = 2"
  414. (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2)))
  415. (pass-if "n = fixnum-max"
  416. (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max)))
  417. (pass-if "n = fixnum-max + 1"
  418. (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1))))
  419. (pass-if "n = fixnum-min"
  420. (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min)))
  421. (pass-if "n = fixnum-min - 1"
  422. (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1)))))
  423. (with-test-prefix "fixnum-min / n"
  424. (pass-if "n = 1"
  425. (eqv? fixnum-min (quotient fixnum-min 1)))
  426. (pass-if "n = -1"
  427. (eqv? (- fixnum-min) (quotient fixnum-min -1)))
  428. (pass-if "n = 2"
  429. (eqv? fixnum-min (* (quotient fixnum-min 2) 2)))
  430. (pass-if "n = fixnum-max"
  431. (eqv? -1 (quotient fixnum-min fixnum-max)))
  432. (pass-if "n = fixnum-max + 1"
  433. (eqv? -1 (quotient fixnum-min (+ fixnum-max 1))))
  434. (pass-if "n = fixnum-min"
  435. (eqv? 1 (quotient fixnum-min fixnum-min)))
  436. (pass-if "n = fixnum-min - 1"
  437. (eqv? 0 (quotient fixnum-min (- fixnum-min 1))))
  438. (pass-if "n = - fixnum-min - 1"
  439. (eqv? -1 (quotient fixnum-min (1- (- fixnum-min)))))
  440. ;; special case, normally inum/big is zero
  441. (pass-if "n = - fixnum-min"
  442. (eqv? -1 (quotient fixnum-min (- fixnum-min))))
  443. (pass-if "n = - fixnum-min + 1"
  444. (eqv? 0 (quotient fixnum-min (1+ (- fixnum-min))))))
  445. (with-test-prefix "(fixnum-min - 1) / n"
  446. (pass-if "n = 1"
  447. (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1)))
  448. (pass-if "n = -1"
  449. (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1)))
  450. (pass-if "n = 2"
  451. (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2)))
  452. (pass-if "n = fixnum-max"
  453. (eqv? -1 (quotient (- fixnum-min 1) fixnum-max)))
  454. (pass-if "n = fixnum-max + 1"
  455. (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1))))
  456. (pass-if "n = fixnum-min"
  457. (eqv? 1 (quotient (- fixnum-min 1) fixnum-min)))
  458. (pass-if "n = fixnum-min - 1"
  459. (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1)))))
  460. ;; Inexact integers
  461. (pass-if (eqv? 5.0 (quotient 35.0 7.0)))
  462. (pass-if (eqv? 5.0 (quotient 35 7.0)))
  463. (pass-if (eqv? 5.0 (quotient 35.0 7 )))
  464. ;; Positive dividend and divisor
  465. (pass-if "35 / 7"
  466. (eqv? 5 (quotient 35 7)))
  467. ;; Negative dividend, positive divisor
  468. (pass-if "-35 / 7"
  469. (eqv? -5 (quotient -35 7)))
  470. ;; Positive dividend, negative divisor
  471. (pass-if "35 / -7"
  472. (eqv? -5 (quotient 35 -7)))
  473. ;; Negative dividend and divisor
  474. (pass-if "-35 / -7"
  475. (eqv? 5 (quotient -35 -7)))
  476. ;; Are numerical overflows detected correctly?
  477. (with-test-prefix "division by zero"
  478. (pass-if-exception "(quotient 1 0)"
  479. exception:numerical-overflow
  480. (quotient 1 0))
  481. (pass-if-exception "(quotient bignum 0)"
  482. exception:numerical-overflow
  483. (quotient (+ fixnum-max 1) 0)))
  484. ;; Are wrong type arguments detected correctly?
  485. )
  486. ;;;
  487. ;;; remainder
  488. ;;;
  489. (with-test-prefix "remainder"
  490. (pass-if (documented? remainder))
  491. (with-test-prefix "0 / n"
  492. (pass-if "n = 1"
  493. (eqv? 0 (remainder 0 1)))
  494. (pass-if "n = -1"
  495. (eqv? 0 (remainder 0 -1)))
  496. (pass-if "n = fixnum-max"
  497. (eqv? 0 (remainder 0 fixnum-max)))
  498. (pass-if "n = fixnum-max + 1"
  499. (eqv? 0 (remainder 0 (+ fixnum-max 1))))
  500. (pass-if "n = fixnum-min"
  501. (eqv? 0 (remainder 0 fixnum-min)))
  502. (pass-if "n = fixnum-min - 1"
  503. (eqv? 0 (remainder 0 (- fixnum-min 1)))))
  504. (with-test-prefix "1 / n"
  505. (pass-if "n = 1"
  506. (eqv? 0 (remainder 1 1)))
  507. (pass-if "n = -1"
  508. (eqv? 0 (remainder 1 -1)))
  509. (pass-if "n = fixnum-max"
  510. (eqv? 1 (remainder 1 fixnum-max)))
  511. (pass-if "n = fixnum-max + 1"
  512. (eqv? 1 (remainder 1 (+ fixnum-max 1))))
  513. (pass-if "n = fixnum-min"
  514. (eqv? 1 (remainder 1 fixnum-min)))
  515. (pass-if "n = fixnum-min - 1"
  516. (eqv? 1 (remainder 1 (- fixnum-min 1)))))
  517. (with-test-prefix "-1 / n"
  518. (pass-if "n = 1"
  519. (eqv? 0 (remainder -1 1)))
  520. (pass-if "n = -1"
  521. (eqv? 0 (remainder -1 -1)))
  522. (pass-if "n = fixnum-max"
  523. (eqv? -1 (remainder -1 fixnum-max)))
  524. (pass-if "n = fixnum-max + 1"
  525. (eqv? -1 (remainder -1 (+ fixnum-max 1))))
  526. (pass-if "n = fixnum-min"
  527. (eqv? -1 (remainder -1 fixnum-min)))
  528. (pass-if "n = fixnum-min - 1"
  529. (eqv? -1 (remainder -1 (- fixnum-min 1)))))
  530. (with-test-prefix "fixnum-max / n"
  531. (pass-if "n = 1"
  532. (eqv? 0 (remainder fixnum-max 1)))
  533. (pass-if "n = -1"
  534. (eqv? 0 (remainder fixnum-max -1)))
  535. (pass-if "n = fixnum-max"
  536. (eqv? 0 (remainder fixnum-max fixnum-max)))
  537. (pass-if "n = fixnum-max + 1"
  538. (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1))))
  539. (pass-if "n = fixnum-min"
  540. (eqv? fixnum-max (remainder fixnum-max fixnum-min)))
  541. (pass-if "n = fixnum-min - 1"
  542. (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1)))))
  543. (with-test-prefix "(fixnum-max + 1) / n"
  544. (pass-if "n = 1"
  545. (eqv? 0 (remainder (+ fixnum-max 1) 1)))
  546. (pass-if "n = -1"
  547. (eqv? 0 (remainder (+ fixnum-max 1) -1)))
  548. (pass-if "n = fixnum-max"
  549. (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max)))
  550. (pass-if "n = fixnum-max + 1"
  551. (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1))))
  552. (pass-if "n = fixnum-min"
  553. (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min)))
  554. (pass-if "n = fixnum-min - 1"
  555. (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1)))))
  556. (with-test-prefix "fixnum-min / n"
  557. (pass-if "n = 1"
  558. (eqv? 0 (remainder fixnum-min 1)))
  559. (pass-if "n = -1"
  560. (eqv? 0 (remainder fixnum-min -1)))
  561. (pass-if "n = fixnum-max"
  562. (eqv? -1 (remainder fixnum-min fixnum-max)))
  563. (pass-if "n = fixnum-max + 1"
  564. (eqv? 0 (remainder fixnum-min (+ fixnum-max 1))))
  565. (pass-if "n = fixnum-min"
  566. (eqv? 0 (remainder fixnum-min fixnum-min)))
  567. (pass-if "n = fixnum-min - 1"
  568. (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1))))
  569. (pass-if "n = - fixnum-min - 1"
  570. (eqv? -1 (remainder fixnum-min (1- (- fixnum-min)))))
  571. ;; special case, normally inum%big is the inum
  572. (pass-if "n = - fixnum-min"
  573. (eqv? 0 (remainder fixnum-min (- fixnum-min))))
  574. (pass-if "n = - fixnum-min + 1"
  575. (eqv? fixnum-min (remainder fixnum-min (1+ (- fixnum-min))))))
  576. (with-test-prefix "(fixnum-min - 1) / n"
  577. (pass-if "n = 1"
  578. (eqv? 0 (remainder (- fixnum-min 1) 1)))
  579. (pass-if "n = -1"
  580. (eqv? 0 (remainder (- fixnum-min 1) -1)))
  581. (pass-if "n = fixnum-max"
  582. (eqv? -2 (remainder (- fixnum-min 1) fixnum-max)))
  583. (pass-if "n = fixnum-max + 1"
  584. (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1))))
  585. (pass-if "n = fixnum-min"
  586. (eqv? -1 (remainder (- fixnum-min 1) fixnum-min)))
  587. (pass-if "n = fixnum-min - 1"
  588. (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1)))))
  589. ;; Inexact integers
  590. (pass-if (eqv? 2.0 (remainder 37.0 7.0)))
  591. (pass-if (eqv? 2.0 (remainder 37 7.0)))
  592. (pass-if (eqv? 2.0 (remainder 37.0 7 )))
  593. ;; Positive dividend and divisor
  594. (pass-if "35 / 7"
  595. (eqv? 0 (remainder 35 7)))
  596. ;; Negative dividend, positive divisor
  597. (pass-if "-35 / 7"
  598. (eqv? 0 (remainder -35 7)))
  599. ;; Positive dividend, negative divisor
  600. (pass-if "35 / -7"
  601. (eqv? 0 (remainder 35 -7)))
  602. ;; Negative dividend and divisor
  603. (pass-if "-35 / -7"
  604. (eqv? 0 (remainder -35 -7)))
  605. ;; Are numerical overflows detected correctly?
  606. (with-test-prefix "division by zero"
  607. (pass-if-exception "(remainder 1 0)"
  608. exception:numerical-overflow
  609. (remainder 1 0))
  610. (pass-if-exception "(remainder bignum 0)"
  611. exception:numerical-overflow
  612. (remainder (+ fixnum-max 1) 0)))
  613. ;; Are wrong type arguments detected correctly?
  614. )
  615. ;;;
  616. ;;; modulo
  617. ;;;
  618. (with-test-prefix "modulo"
  619. (pass-if (documented? modulo))
  620. (with-test-prefix "0 % n"
  621. (pass-if "n = 1"
  622. (eqv? 0 (modulo 0 1)))
  623. (pass-if "n = -1"
  624. (eqv? 0 (modulo 0 -1)))
  625. (pass-if "n = fixnum-max"
  626. (eqv? 0 (modulo 0 fixnum-max)))
  627. (pass-if "n = fixnum-max + 1"
  628. (eqv? 0 (modulo 0 (+ fixnum-max 1))))
  629. (pass-if "n = fixnum-min"
  630. (eqv? 0 (modulo 0 fixnum-min)))
  631. (pass-if "n = fixnum-min - 1"
  632. (eqv? 0 (modulo 0 (- fixnum-min 1)))))
  633. (with-test-prefix "1 % n"
  634. (pass-if "n = 1"
  635. (eqv? 0 (modulo 1 1)))
  636. (pass-if "n = -1"
  637. (eqv? 0 (modulo 1 -1)))
  638. (pass-if "n = fixnum-max"
  639. (eqv? 1 (modulo 1 fixnum-max)))
  640. (pass-if "n = fixnum-max + 1"
  641. (eqv? 1 (modulo 1 (+ fixnum-max 1))))
  642. (pass-if "n = fixnum-min"
  643. (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min)))
  644. (pass-if "n = fixnum-min - 1"
  645. (eqv? fixnum-min (modulo 1 (- fixnum-min 1)))))
  646. (with-test-prefix "-1 % n"
  647. (pass-if "n = 1"
  648. (eqv? 0 (modulo -1 1)))
  649. (pass-if "n = -1"
  650. (eqv? 0 (modulo -1 -1)))
  651. (pass-if "n = fixnum-max"
  652. (eqv? (- fixnum-max 1) (modulo -1 fixnum-max)))
  653. (pass-if "n = fixnum-max + 1"
  654. (eqv? fixnum-max (modulo -1 (+ fixnum-max 1))))
  655. (pass-if "n = fixnum-min"
  656. (eqv? -1 (modulo -1 fixnum-min)))
  657. (pass-if "n = fixnum-min - 1"
  658. (eqv? -1 (modulo -1 (- fixnum-min 1)))))
  659. (with-test-prefix "fixnum-max % n"
  660. (pass-if "n = 1"
  661. (eqv? 0 (modulo fixnum-max 1)))
  662. (pass-if "n = -1"
  663. (eqv? 0 (modulo fixnum-max -1)))
  664. (pass-if "n = fixnum-max"
  665. (eqv? 0 (modulo fixnum-max fixnum-max)))
  666. (pass-if "n = fixnum-max + 1"
  667. (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1))))
  668. (pass-if "n = fixnum-min"
  669. (eqv? -1 (modulo fixnum-max fixnum-min)))
  670. (pass-if "n = fixnum-min - 1"
  671. (eqv? -2 (modulo fixnum-max (- fixnum-min 1)))))
  672. (with-test-prefix "(fixnum-max + 1) % n"
  673. (pass-if "n = 1"
  674. (eqv? 0 (modulo (+ fixnum-max 1) 1)))
  675. (pass-if "n = -1"
  676. (eqv? 0 (modulo (+ fixnum-max 1) -1)))
  677. (pass-if "n = fixnum-max"
  678. (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max)))
  679. (pass-if "n = fixnum-max + 1"
  680. (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1))))
  681. (pass-if "n = fixnum-min"
  682. (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min)))
  683. (pass-if "n = fixnum-min - 1"
  684. (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1)))))
  685. (with-test-prefix "fixnum-min % n"
  686. (pass-if "n = 1"
  687. (eqv? 0 (modulo fixnum-min 1)))
  688. (pass-if "n = -1"
  689. (eqv? 0 (modulo fixnum-min -1)))
  690. (pass-if "n = fixnum-max"
  691. (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max)))
  692. (pass-if "n = fixnum-max + 1"
  693. (eqv? 0 (modulo fixnum-min (+ fixnum-max 1))))
  694. (pass-if "n = fixnum-min"
  695. (eqv? 0 (modulo fixnum-min fixnum-min)))
  696. (pass-if "n = fixnum-min - 1"
  697. (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1)))))
  698. (with-test-prefix "(fixnum-min - 1) % n"
  699. (pass-if "n = 1"
  700. (eqv? 0 (modulo (- fixnum-min 1) 1)))
  701. (pass-if "n = -1"
  702. (eqv? 0 (modulo (- fixnum-min 1) -1)))
  703. (pass-if "n = fixnum-max"
  704. (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max)))
  705. (pass-if "n = fixnum-max + 1"
  706. (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1))))
  707. (pass-if "n = fixnum-min"
  708. (eqv? -1 (modulo (- fixnum-min 1) fixnum-min)))
  709. (pass-if "n = fixnum-min - 1"
  710. (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1)))))
  711. ;; Inexact integers
  712. (pass-if (eqv? 1.0 (modulo 13.0 4.0)))
  713. (pass-if (eqv? 1.0 (modulo 13 4.0)))
  714. (pass-if (eqv? 1.0 (modulo 13.0 4 )))
  715. ;; Positive dividend and divisor
  716. (pass-if "13 % 4"
  717. (eqv? 1 (modulo 13 4)))
  718. (pass-if "2177452800 % 86400"
  719. (eqv? 0 (modulo 2177452800 86400)))
  720. ;; Negative dividend, positive divisor
  721. (pass-if "-13 % 4"
  722. (eqv? 3 (modulo -13 4)))
  723. (pass-if "-2177452800 % 86400"
  724. (eqv? 0 (modulo -2177452800 86400)))
  725. ;; Positive dividend, negative divisor
  726. (pass-if "13 % -4"
  727. (eqv? -3 (modulo 13 -4)))
  728. (pass-if "2177452800 % -86400"
  729. (eqv? 0 (modulo 2177452800 -86400)))
  730. ;; Negative dividend and divisor
  731. (pass-if "-13 % -4"
  732. (eqv? -1 (modulo -13 -4)))
  733. (pass-if "-2177452800 % -86400"
  734. (eqv? 0 (modulo -2177452800 -86400)))
  735. ;; Are numerical overflows detected correctly?
  736. (with-test-prefix "division by zero"
  737. (pass-if-exception "(modulo 1 0)"
  738. exception:numerical-overflow
  739. (modulo 1 0))
  740. (pass-if-exception "(modulo bignum 0)"
  741. exception:numerical-overflow
  742. (modulo (+ fixnum-max 1) 0)))
  743. ;; Are wrong type arguments detected correctly?
  744. )
  745. ;;;
  746. ;;; modulo-expt
  747. ;;;
  748. (with-test-prefix "modulo-expt"
  749. (pass-if (= 1 (modulo-expt 17 23 47)))
  750. (pass-if (= 1 (modulo-expt 17 -23 47)))
  751. (pass-if (= 17 (modulo-expt 17 -22 47)))
  752. (pass-if (= 36 (modulo-expt 17 22 47)))
  753. (pass-if (= 183658794479969134816674175082294846241553725240 (modulo-expt 111122223333444455556666 111122223333444455556666 1153478690012629968439432872520758982731022934717)))
  754. (pass-if-exception
  755. "Proper exception with 0 modulus"
  756. exception:numerical-overflow
  757. (modulo-expt 17 23 0))
  758. (pass-if-exception
  759. "Proper exception when result not invertible"
  760. exception:numerical-overflow
  761. (modulo-expt 10 -1 48))
  762. (pass-if-exception
  763. "Proper exception with wrong type argument"
  764. exception:wrong-type-arg
  765. (modulo-expt "Sam" 23 10))
  766. (pass-if-exception
  767. "Proper exception with wrong type argument"
  768. exception:wrong-type-arg
  769. (modulo-expt 17 9.9 10))
  770. (pass-if-exception
  771. "Proper exception with wrong type argument"
  772. exception:wrong-type-arg
  773. (modulo-expt 17 23 'Ethel)))
  774. ;;;
  775. ;;; numerator
  776. ;;;
  777. (with-test-prefix "numerator"
  778. (pass-if-equal "0" 0 (numerator 0))
  779. (pass-if-equal "1" 1 (numerator 1))
  780. (pass-if-equal "2" 2 (numerator 2))
  781. (pass-if-equal "-1" -1 (numerator -1))
  782. (pass-if-equal "-2" -2 (numerator -2))
  783. (pass-if-equal "0.0" 0.0 (numerator 0.0))
  784. (pass-if-equal "1.0" 1.0 (numerator 1.0))
  785. (pass-if-equal "2.0" 2.0 (numerator 2.0))
  786. (pass-if-equal "-0.0" -0.0 (numerator -0.0))
  787. (pass-if-equal "-1.0" -1.0 (numerator -1.0))
  788. (pass-if-equal "-2.0" -2.0 (numerator -2.0))
  789. (pass-if-equal "0.5" 1.0 (numerator 0.5))
  790. (pass-if-equal "0.25" 1.0 (numerator 0.25))
  791. (pass-if-equal "0.75" 3.0 (numerator 0.75))
  792. (pass-if-equal "+inf.0" +inf.0 (numerator +inf.0))
  793. (pass-if-equal "-inf.0" -inf.0 (numerator -inf.0)))
  794. ;;;
  795. ;;; denominator
  796. ;;;
  797. (with-test-prefix "denominator"
  798. (pass-if-equal "0" 1 (denominator 0))
  799. (pass-if-equal "1" 1 (denominator 1))
  800. (pass-if-equal "2" 1 (denominator 2))
  801. (pass-if-equal "-1" 1 (denominator -1))
  802. (pass-if-equal "-2" 1 (denominator -2))
  803. (pass-if-equal "0.0" 1.0 (denominator 0.0))
  804. (pass-if-equal "1.0" 1.0 (denominator 1.0))
  805. (pass-if-equal "2.0" 1.0 (denominator 2.0))
  806. (pass-if-equal "-0.0" 1.0 (denominator -0.0))
  807. (pass-if-equal "-1.0" 1.0 (denominator -1.0))
  808. (pass-if-equal "-2.0" 1.0 (denominator -2.0))
  809. (pass-if-equal "0.5" 2.0 (denominator 0.5))
  810. (pass-if-equal "0.25" 4.0 (denominator 0.25))
  811. (pass-if-equal "0.75" 4.0 (denominator 0.75))
  812. (pass-if-equal "+inf.0" 1.0 (denominator +inf.0))
  813. (pass-if-equal "-inf.0" 1.0 (denominator -inf.0)))
  814. ;;;
  815. ;;; gcd
  816. ;;;
  817. (with-test-prefix "gcd"
  818. (pass-if "documented?"
  819. (documented? gcd))
  820. (with-test-prefix "(n)"
  821. (pass-if "n = -2"
  822. (eqv? 2 (gcd -2))))
  823. (with-test-prefix "(0 n)"
  824. (pass-if "n = 0"
  825. (eqv? 0 (gcd 0 0)))
  826. (pass-if "n = 1"
  827. (eqv? 1 (gcd 0 1)))
  828. (pass-if "n = -1"
  829. (eqv? 1 (gcd 0 -1)))
  830. (pass-if "n = fixnum-max"
  831. (eqv? fixnum-max (gcd 0 fixnum-max)))
  832. (pass-if "n = fixnum-max + 1"
  833. (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1))))
  834. (pass-if "n = fixnum-min"
  835. (eqv? (- fixnum-min) (gcd 0 fixnum-min)))
  836. (pass-if "n = fixnum-min - 1"
  837. (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1)))))
  838. (with-test-prefix "(n 0)"
  839. (pass-if "n = 2^128 * fixnum-max"
  840. (eqv? (ash fixnum-max 128) (gcd (ash fixnum-max 128) 0))))
  841. (with-test-prefix "(1 n)"
  842. (pass-if "n = 0"
  843. (eqv? 1 (gcd 1 0)))
  844. (pass-if "n = 1"
  845. (eqv? 1 (gcd 1 1)))
  846. (pass-if "n = -1"
  847. (eqv? 1 (gcd 1 -1)))
  848. (pass-if "n = fixnum-max"
  849. (eqv? 1 (gcd 1 fixnum-max)))
  850. (pass-if "n = fixnum-max + 1"
  851. (eqv? 1 (gcd 1 (+ fixnum-max 1))))
  852. (pass-if "n = fixnum-min"
  853. (eqv? 1 (gcd 1 fixnum-min)))
  854. (pass-if "n = fixnum-min - 1"
  855. (eqv? 1 (gcd 1 (- fixnum-min 1)))))
  856. (with-test-prefix "(-1 n)"
  857. (pass-if "n = 0"
  858. (eqv? 1 (gcd -1 0)))
  859. (pass-if "n = 1"
  860. (eqv? 1 (gcd -1 1)))
  861. (pass-if "n = -1"
  862. (eqv? 1 (gcd -1 -1)))
  863. (pass-if "n = fixnum-max"
  864. (eqv? 1 (gcd -1 fixnum-max)))
  865. (pass-if "n = fixnum-max + 1"
  866. (eqv? 1 (gcd -1 (+ fixnum-max 1))))
  867. (pass-if "n = fixnum-min"
  868. (eqv? 1 (gcd -1 fixnum-min)))
  869. (pass-if "n = fixnum-min - 1"
  870. (eqv? 1 (gcd -1 (- fixnum-min 1)))))
  871. (with-test-prefix "(fixnum-max n)"
  872. (pass-if "n = 0"
  873. (eqv? fixnum-max (gcd fixnum-max 0)))
  874. (pass-if "n = 1"
  875. (eqv? 1 (gcd fixnum-max 1)))
  876. (pass-if "n = -1"
  877. (eqv? 1 (gcd fixnum-max -1)))
  878. (pass-if "n = fixnum-max"
  879. (eqv? fixnum-max (gcd fixnum-max fixnum-max)))
  880. (pass-if "n = fixnum-max + 1"
  881. (eqv? 1 (gcd fixnum-max (+ fixnum-max 1))))
  882. (pass-if "n = fixnum-min"
  883. (eqv? 1 (gcd fixnum-max fixnum-min)))
  884. (pass-if "n = fixnum-min - 1"
  885. (eqv? 1 (gcd fixnum-max (- fixnum-min 1)))))
  886. (with-test-prefix "((+ fixnum-max 1) n)"
  887. (pass-if "n = 0"
  888. (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0)))
  889. (pass-if "n = 1"
  890. (eqv? 1 (gcd (+ fixnum-max 1) 1)))
  891. (pass-if "n = -1"
  892. (eqv? 1 (gcd (+ fixnum-max 1) -1)))
  893. (pass-if "n = fixnum-max"
  894. (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max)))
  895. (pass-if "n = fixnum-max + 1"
  896. (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1))))
  897. (pass-if "n = fixnum-min"
  898. (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min)))
  899. (pass-if "n = fixnum-min - 1"
  900. (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1)))))
  901. (with-test-prefix "(fixnum-min n)"
  902. (pass-if "n = 0"
  903. (eqv? (- fixnum-min) (gcd fixnum-min 0)))
  904. (pass-if "n = 1"
  905. (eqv? 1 (gcd fixnum-min 1)))
  906. (pass-if "n = -1"
  907. (eqv? 1 (gcd fixnum-min -1)))
  908. (pass-if "n = fixnum-max"
  909. (eqv? 1 (gcd fixnum-min fixnum-max)))
  910. (pass-if "n = fixnum-max + 1"
  911. (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1))))
  912. (pass-if "n = fixnum-min"
  913. (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min)))
  914. (pass-if "n = fixnum-min - 1"
  915. (eqv? 1 (gcd fixnum-min (- fixnum-min 1)))))
  916. (with-test-prefix "((- fixnum-min 1) n)"
  917. (pass-if "n = 0"
  918. (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0)))
  919. (pass-if "n = 1"
  920. (eqv? 1 (gcd (- fixnum-min 1) 1)))
  921. (pass-if "n = -1"
  922. (eqv? 1 (gcd (- fixnum-min 1) -1)))
  923. (pass-if "n = fixnum-max"
  924. (eqv? 1 (gcd (- fixnum-min 1) fixnum-max)))
  925. (pass-if "n = fixnum-max + 1"
  926. (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1))))
  927. (pass-if "n = fixnum-min"
  928. (eqv? 1 (gcd (- fixnum-min 1) fixnum-min)))
  929. (pass-if "n = fixnum-min - 1"
  930. (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1)))))
  931. (with-test-prefix "flonum arguments"
  932. (pass-if-equal "flonum"
  933. 15.0
  934. (gcd -15.0))
  935. (pass-if-equal "flonum/flonum"
  936. 3.0
  937. (gcd 6.0 -15.0))
  938. (pass-if-equal "flonum/fixnum"
  939. 3.0
  940. (gcd 6.0 -15))
  941. (pass-if-equal "fixnum/flonum"
  942. 3.0
  943. (gcd -6 15.0))
  944. (pass-if-equal "flonum/bignum"
  945. 2.0
  946. (gcd -6.0 (expt 2 fixnum-bit)))
  947. (pass-if-equal "bignum/flonum"
  948. 3.0
  949. (gcd (- (expt 3 fixnum-bit)) 6.0)))
  950. ;; Are wrong type arguments detected correctly?
  951. )
  952. ;;;
  953. ;;; lcm
  954. ;;;
  955. (with-test-prefix "lcm"
  956. ;; FIXME: more tests?
  957. ;; (some of these are already in r4rs.test)
  958. (pass-if (documented? lcm))
  959. (pass-if-equal 1 (lcm))
  960. (pass-if-equal 15 (lcm -15))
  961. (pass-if-equal 288 (lcm 32 -36))
  962. (with-test-prefix "flonum arguments"
  963. (pass-if-equal "flonum"
  964. 15.0
  965. (lcm -15.0))
  966. (pass-if-equal "flonum/flonum"
  967. 30.0
  968. (lcm 6.0 -15.0))
  969. (pass-if-equal "flonum/fixnum"
  970. 30.0
  971. (lcm 6.0 -15))
  972. (pass-if-equal "fixnum/flonum"
  973. 30.0
  974. (lcm -6 15.0))
  975. (pass-if "flonum/bignum"
  976. (let ((want (* 3.0 (expt 2 fixnum-bit)))
  977. (got (lcm -6.0 (expt 2 fixnum-bit))))
  978. (and (inexact? got)
  979. (test-eqv? 1.0 (/ want got)))))
  980. (pass-if "bignum/flonum"
  981. (let ((want (* 2.0 (expt 3 fixnum-bit)))
  982. (got (lcm (- (expt 3 fixnum-bit)) 6.0)))
  983. (and (inexact? got)
  984. (test-eqv? 1.0 (/ want got))))))
  985. (let ((big-n 115792089237316195423570985008687907853269984665640564039457584007913129639936) ; 2 ^ 256
  986. (lcm-of-big-n-and-11 1273712981610478149659280835095566986385969831322046204434033424087044426039296))
  987. (pass-if (= lcm-of-big-n-and-11 (lcm big-n 11)))
  988. (pass-if (= lcm-of-big-n-and-11 (lcm 11 big-n 11)))))
  989. ;;;
  990. ;;; rationalize
  991. ;;;
  992. (with-test-prefix "rationalize"
  993. (pass-if (documented? rationalize))
  994. (pass-if (eqv? 2 (rationalize 4 2 )))
  995. (pass-if (eqv? -2 (rationalize -4 2 )))
  996. (pass-if (eqv? 2.0 (rationalize 4 2.0)))
  997. (pass-if (eqv? -2.0 (rationalize -4.0 2 )))
  998. (pass-if (eqv? 0 (rationalize 4 8 )))
  999. (pass-if (eqv? 0 (rationalize -4 8 )))
  1000. (pass-if (eqv? 0.0 (rationalize 4 8.0)))
  1001. (pass-if (eqv? 0.0 (rationalize -4.0 8 )))
  1002. (pass-if (eqv? 0.0 (rationalize 3 +inf.0)))
  1003. (pass-if (eqv? 0.0 (rationalize -3 +inf.0)))
  1004. (pass-if (real-nan? (rationalize +inf.0 +inf.0)))
  1005. (pass-if (real-nan? (rationalize +nan.0 +inf.0)))
  1006. (pass-if (real-nan? (rationalize +nan.0 4)))
  1007. (pass-if (eqv? +inf.0 (rationalize +inf.0 3)))
  1008. (pass-if (eqv? 3/10 (rationalize 3/10 0)))
  1009. (pass-if (eqv? -3/10 (rationalize -3/10 0)))
  1010. (pass-if (eqv? 1/3 (rationalize 3/10 1/10)))
  1011. (pass-if (eqv? -1/3 (rationalize -3/10 1/10)))
  1012. (pass-if (eqv? 1/3 (rationalize 3/10 -1/10)))
  1013. (pass-if (eqv? -1/3 (rationalize -3/10 -1/10)))
  1014. ;; Prior to Guile 2.0.10, rationalize used a faulty algorithm that
  1015. ;; incorrectly returned 2/3 and -2/3 in the following cases.
  1016. (pass-if (eqv? 1/2 (rationalize #e+0.67 1/4)))
  1017. (pass-if (eqv? -1/2 (rationalize #e-0.67 1/4)))
  1018. (pass-if (eqv? 1 (rationalize #e+0.67 1/3)))
  1019. (pass-if (eqv? -1 (rationalize #e-0.67 1/3)))
  1020. (pass-if (eqv? 1/2 (rationalize #e+0.66 1/3)))
  1021. (pass-if (eqv? -1/2 (rationalize #e-0.66 1/3)))
  1022. (pass-if (eqv? 1 (rationalize #e+0.67 2/3)))
  1023. (pass-if (eqv? -1 (rationalize #e-0.67 2/3)))
  1024. (pass-if (eqv? 0 (rationalize #e+0.66 2/3)))
  1025. (pass-if (eqv? 0 (rationalize #e-0.66 2/3)))
  1026. ;; Prior to Guile 2.0.10, rationalize used a faulty algorithm that
  1027. ;; incorrectly computed the following approximations of PI.
  1028. (with-test-prefix "pi"
  1029. (define *pi* #e3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679)
  1030. (pass-if (eqv? 16/5 (rationalize *pi* 1/10)))
  1031. (pass-if (eqv? 201/64 (rationalize *pi* 1/1000)))
  1032. (pass-if (eqv? 75948/24175 (rationalize *pi* (expt 10 -7))))
  1033. (pass-if (eqv? 100798/32085 (rationalize *pi* (expt 10 -8))))
  1034. (pass-if (eqv? 58466453/18610450 (rationalize *pi* (expt 10 -14))))
  1035. (pass-if (eqv? 2307954651196778721982809475299879198775111361078/734644782339796933783743757007944508986600750685
  1036. (rationalize *pi* (expt 10 -95)))))
  1037. (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 1/10)))
  1038. (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 1/10)))
  1039. (pass-if (test-eqv? (/ 1.0 3) (rationalize 0.3 -1/10)))
  1040. (pass-if (test-eqv? (/ -1.0 3) (rationalize -0.3 -1/10))))
  1041. ;;;
  1042. ;;; number->string
  1043. ;;;
  1044. (with-test-prefix "number->string"
  1045. (let ((num->str->num
  1046. (lambda (n radix)
  1047. (string->number (number->string n radix) radix))))
  1048. (define (test num)
  1049. (pass-if-equal (list num 'pos)
  1050. num
  1051. (num->str->num num 10))
  1052. (pass-if-equal (list num 'neg)
  1053. (- num)
  1054. (num->str->num (- num) 10)))
  1055. (pass-if (documented? number->string))
  1056. (pass-if (string=? (number->string 0) "0"))
  1057. (pass-if (string=? (number->string 171) "171"))
  1058. (pass-if (= (+ fixnum-max 1) (num->str->num (+ fixnum-max 1) 10)))
  1059. (pass-if (= (- fixnum-min 1) (num->str->num (- fixnum-min 1) 10)))
  1060. (test (inf))
  1061. (test 1.3)
  1062. (test (acos -1)) ; pi
  1063. (test (exp 1)) ; e
  1064. (test (/ 3.0))
  1065. (test (/ 7.0))
  1066. (test 2.2250738585072011e-308)
  1067. (test 2.2250738585072012e-308)
  1068. ;; Largest finite inexact
  1069. (test (* (- (expt 2.0 dbl-mant-dig) 1)
  1070. (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
  1071. (pass-if (string=? "0.0" (number->string 0.0)))
  1072. (pass-if (or (eqv? 0.0 -0.0)
  1073. (string=? "-0.0" (number->string -0.0))))
  1074. (pass-if (string=? (number->string 35.25 36) "z.9"))
  1075. (pass-if (string=? (number->string 0.25 2) "0.01"))
  1076. (pass-if (string=? (number->string 255.0625 16) "ff.1"))
  1077. (pass-if (string=? (number->string (/ 1 3) 3) "1/10"))
  1078. (pass-if (string=? (number->string 10) "10"))
  1079. (pass-if (string=? (number->string 10 11) "a"))
  1080. (pass-if (string=? (number->string 36 36) "10"))
  1081. (pass-if (= (num->str->num 36 36) 36))
  1082. (pass-if (= (string->number "z" 36) 35))
  1083. (pass-if (= (string->number "Z" 36) 35))
  1084. (pass-if (not (string->number "Z" 35)))
  1085. (pass-if (string=? (number->string 35 36) "z"))
  1086. (pass-if (= (num->str->num 35 36) 35))
  1087. (pass-if (string=? (number->string 12342342340000.0) "1.234234234e13"))
  1088. (pass-if (string=? (number->string 1234234234000.0) "1234234234000.0"))
  1089. (pass-if (string=? (number->string 1240000.0) "1240000.0"))
  1090. (with-test-prefix "powers of radix"
  1091. (for-each
  1092. (lambda (radix)
  1093. (for-each (lambda (k)
  1094. (let ((val (exact->inexact (expt radix k)))
  1095. (str (if (<= -3 k 6)
  1096. (assoc-ref '((-3 . "0.001")
  1097. (-2 . "0.01")
  1098. (-1 . "0.1")
  1099. ( 0 . "1.0")
  1100. ( 1 . "10.0")
  1101. ( 2 . "100.0")
  1102. ( 3 . "1000.0")
  1103. ( 4 . "10000.0")
  1104. ( 5 . "100000.0")
  1105. ( 6 . "1000000.0"))
  1106. k)
  1107. (string-append "1.0e"
  1108. (number->string k radix)))))
  1109. (pass-if-equal (list radix k 'pos)
  1110. str
  1111. (number->string val radix))
  1112. (pass-if-equal (list radix k 'neg)
  1113. (string-append "-" str)
  1114. (number->string (- val) radix))))
  1115. (iota 41 -20)))
  1116. (iota 35 2)))
  1117. (with-test-prefix "multiples of smallest inexact"
  1118. (for-each (lambda (k)
  1119. (let ((val (* k (expt 2.0 (- dbl-min-exp dbl-mant-dig)))))
  1120. (test val)))
  1121. (iota 40 1)))
  1122. (with-test-prefix "one plus multiples of epsilon"
  1123. (for-each (lambda (k)
  1124. (let ((val (+ 1.0 (* k dbl-epsilon))))
  1125. (test val)))
  1126. (iota 40 1)))
  1127. (with-test-prefix "one minus multiples of 1/2 epsilon"
  1128. (for-each (lambda (k)
  1129. (let ((val (- 1.0 (* k 1/2 dbl-epsilon))))
  1130. (test val)))
  1131. (iota 40 1)))
  1132. ;; Before Guile 2.0.1, even in the presence of a #e forced exactness
  1133. ;; specifier, negative exponents were applied inexactly and then
  1134. ;; later coerced to exact, yielding an incorrect fraction.
  1135. (pass-if (eqv? (string->number "#e1e-10") 1/10000000000))
  1136. (pass-if (string=? (number->string 11.33333333333333333 12)
  1137. "b.4"))
  1138. (pass-if (string=? (number->string 1.324e44 16)
  1139. "5.efe0a14fafdf8e24"))))
  1140. ;;;
  1141. ;;; string->number
  1142. ;;;
  1143. (with-test-prefix "string->number"
  1144. (pass-if "documented?"
  1145. (documented? string->number))
  1146. (pass-if "non number strings"
  1147. (for-each (lambda (x) (if (string->number x) (throw 'fail)))
  1148. '("" "q" "1q" "6+7iq" "8+9q" "10+11" "13+" "18@19q" "20@q" "23@"
  1149. "+25iq" "26i" "-q" "-iq" "i" "5#.0" "8/" "10#11" ".#" "."
  1150. "#o.2" "3.4q" "15.16e17q" "18.19e+q" ".q" ".17#18" "10q" "#b2"
  1151. "#b3" "#b4" "#b5" "#b6" "#b7" "#b8" "#b9" "#ba" "#bb" "#bc"
  1152. "#bd" "#be" "#bf" "#q" "#b#b1" "#o#o1" "#d#d1" "#x#x1" "#e#e1"
  1153. "#i#i1" "12@12+0i" "3/0" "0/0" "4+3/0i" "4/0-3i" "2+0/0i"
  1154. "nan.0" "inf.0" "#e+nan.0" "#e+inf.0" "#e-inf.0"
  1155. "3@inf.0" "4@nan.0"))
  1156. #t)
  1157. (pass-if "valid number strings"
  1158. (for-each (lambda (couple)
  1159. (apply
  1160. (lambda (x y)
  1161. (let ((xx (string->number x)))
  1162. (if (or (eq? xx #f) (not (eqv? xx y)))
  1163. (begin
  1164. (pk x y)
  1165. (throw 'fail)))))
  1166. couple))
  1167. `(;; Radix:
  1168. ("#b0" 0) ("#B0" 0) ("#b1" 1) ("#B1" 1) ("#o0" 0) ("#O0" 0)
  1169. ("#o1" 1) ("#O1" 1) ("#o2" 2) ("#O2" 2) ("#o3" 3) ("#O3" 3)
  1170. ("#o4" 4) ("#O4" 4) ("#o5" 5) ("#O5" 5) ("#o6" 6) ("#O6" 6)
  1171. ("#o7" 7) ("#O7" 7) ("#d0" 0) ("#D0" 0) ("#d1" 1) ("#D1" 1)
  1172. ("#d2" 2) ("#D2" 2) ("#d3" 3) ("#D3" 3) ("#d4" 4) ("#D4" 4)
  1173. ("#d5" 5) ("#D5" 5) ("#d6" 6) ("#D6" 6) ("#d7" 7) ("#D7" 7)
  1174. ("#d8" 8) ("#D8" 8) ("#d9" 9) ("#D9" 9)
  1175. ("#xa" 10) ("#Xa" 10) ("#xb" 11) ("#Xb" 11)
  1176. ("#xc" 12) ("#Xc" 12) ("#xd" 13) ("#Xd" 13)
  1177. ("#xe" 14) ("#Xe" 14) ("#xf" 15) ("#Xf" 15)
  1178. ("#b1010" 10)
  1179. ("#o12345670" 2739128)
  1180. ("#d1234567890" 1234567890)
  1181. ("#x1234567890abcdef" 1311768467294899695)
  1182. ;; Exactness:
  1183. ("#e1" 1) ("#e1.2" 12/10)
  1184. ("#i1.1" 1.1) ("#i1" 1.0)
  1185. ;; Integers:
  1186. ("1" ,(1+ 0)) ("23" ,(+ 9 9 5)) ("-1" ,(- 0 1))
  1187. ("-45" ,(- 0 45)) ("2#" 20.0) ("2##" 200.0) ("12##" 1200.0)
  1188. ("#b#i100" 4.0)
  1189. ;; Fractions:
  1190. ("1/1" 1) ("1/2" 1/2) ("-1/2" -1/2) ("1#/1" 10.0)
  1191. ("10/1#" 1.0) ("1#/1#" 1.0) ("#e9/10" 9/10) ("#e10/1#" 1)
  1192. ("#i6/8" 0.75) ("#i1/1" 1.0)
  1193. ;; Infinities and NaNs:
  1194. ("+inf.0" ,(inf)) ("-inf.0" ,(- (inf)))
  1195. ("+Inf.0" ,(inf)) ("-Inf.0" ,(- (inf)))
  1196. ("+InF.0" ,(inf)) ("-InF.0" ,(- (inf)))
  1197. ("+INF.0" ,(inf)) ("-INF.0" ,(- (inf)))
  1198. ("#i+InF.0" ,(inf)) ("#i-InF.0" ,(- (inf)))
  1199. ("+nan.0" ,(nan)) ("-nan.0" ,(nan))
  1200. ("#i+nan.0" ,(nan)) ("#i-nan.0" ,(nan))
  1201. ;; Decimal numbers:
  1202. ;; * <uinteger 10> <suffix>
  1203. ("1e2" 100.0) ("1E2" 100.0) ("1s2" 100.0) ("1S2" 100.0)
  1204. ("1f2" 100.0) ("1F2" 100.0) ("1d2" 100.0) ("1D2" 100.0)
  1205. ("1l2" 100.0) ("1L2" 100.0) ("1e+2" 100.0) ("1e-2" 0.01)
  1206. ;; * . <digit 10>+ #* <suffix>
  1207. (".1" .1) (".0123456789" 123456789e-10) (".16#" 0.16)
  1208. (".0123456789e10" 123456789.0) (".16#e3" 160.0) ("#d.3" 0.3)
  1209. ;; * <digit 10>+ . <digit 10>* #* <suffix>
  1210. ("3." ,(exact->inexact 3)) ("3.e0" ,(exact->inexact 3))
  1211. ("3.1" ,(exact->inexact 31/10)) ("3.1e0" 3.1) ("3.1#" 3.1)
  1212. ("3.1#e0" 3.1)
  1213. ;; * <digit 10>+ #+ . #* <suffix>
  1214. ("3#." 30.0) ("3#.e0" 30.0) ("3#.#" 30.0) ("3#.#e0" 30.0)
  1215. ))
  1216. #t)
  1217. (pass-if "valid complex number strings"
  1218. (for-each (lambda (triple)
  1219. (apply
  1220. (lambda (str re im)
  1221. (let ((z (string->number str)))
  1222. (if (or (eq? z #f)
  1223. (not (and (eqv? (real-part z) re)
  1224. (eqv? (imag-part z) im))))
  1225. (begin
  1226. (pk str re im)
  1227. (throw 'fail)))))
  1228. triple))
  1229. `(("1@0" 1 0) ("1@+0" 1 0) ("1@-0" 1 0) ("1/2@0" 1/2 0)
  1230. ("1.0@0" 1.0 0) ("1.0@-0" 1.0 0)
  1231. ("#e1@0" 1 0) ("#e1@+0" 1 0) ("#e1@-0" 1 0) ("#e0.5@0.0" 1/2 0)
  1232. ("#e1.0@0" 1 0) ("#e1.0@-0" 1 0)
  1233. ("#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)
  1234. ("#i1.0@0" 1.0 0.0) ("#i1.0@-0" 1.0 -0.0)
  1235. ("1@+0.0" 1.0 0.0) ("1.0@-0.0" 1.0 -0.0)
  1236. ("2+3i" 2.0 3.0) ("4-5i" 4.0 -5.0)
  1237. ("1+i" 1.0 1.0) ("1-i" 1.0 -1.0) ("+1i" 0.0 1.0) ("-1i" 0.0 -1.0)
  1238. ("+i" 0.0 1.0) ("-i" 0.0 -1.0)
  1239. ("1.0+.1i" 1.0 0.1) ("1.0-.1i" 1.0 -0.1)
  1240. (".1+.0i" 0.1 0.0) ("1.+.0i" 1.0 0.0) (".1+.1i" 0.1 0.1)
  1241. ("1e1+.1i" 10.0 0.1)
  1242. ("0@+nan.0" 0 0) ("0@+inf.0" 0 0) ("0@-inf.0" 0 0)
  1243. ("0.0@+nan.0" 0.0 0.0) ("0.0@+inf.0" 0.0 0.0) ("0.0@-inf.0" 0.0 0.0)
  1244. ("#i0@+nan.0" 0.0 0.0) ("#i0@+inf.0" 0.0 0.0) ("#i0@-inf.0" 0.0 0.0)
  1245. ("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)
  1246. ))
  1247. #t)
  1248. (pass-if-exception "exponent too big"
  1249. exception:out-of-range
  1250. (string->number "12.13e141414"))
  1251. ;; in guile 1.6.7 and earlier, bad polar forms (where the conversion of
  1252. ;; the angle gave #f) caused a segv
  1253. (pass-if "1@a"
  1254. (eq? #f (string->number "1@a"))))
  1255. ;;;
  1256. ;;; number?
  1257. ;;;
  1258. (with-test-prefix "number?"
  1259. (pass-if (documented? number?))
  1260. (pass-if (number? 0))
  1261. (pass-if (number? 7))
  1262. (pass-if (number? -7))
  1263. (pass-if (number? 1.3))
  1264. (pass-if (number? (+ 1 fixnum-max)))
  1265. (pass-if (number? (- 1 fixnum-min)))
  1266. (pass-if (number? 3+4i))
  1267. (pass-if (not (number? #\a)))
  1268. (pass-if (not (number? "a")))
  1269. (pass-if (not (number? (make-vector 0))))
  1270. (pass-if (not (number? (cons 1 2))))
  1271. (pass-if (not (number? #t)))
  1272. (pass-if (not (number? (lambda () #t))))
  1273. (pass-if (not (number? (current-input-port)))))
  1274. ;;;
  1275. ;;; complex?
  1276. ;;;
  1277. (with-test-prefix "complex?"
  1278. (pass-if (documented? complex?))
  1279. (pass-if (complex? 0))
  1280. (pass-if (complex? 7))
  1281. (pass-if (complex? -7))
  1282. (pass-if (complex? (+ 1 fixnum-max)))
  1283. (pass-if (complex? (- 1 fixnum-min)))
  1284. (pass-if (complex? 1.3))
  1285. (pass-if (complex? 3+4i))
  1286. (pass-if (not (complex? #\a)))
  1287. (pass-if (not (complex? "a")))
  1288. (pass-if (not (complex? (make-vector 0))))
  1289. (pass-if (not (complex? (cons 1 2))))
  1290. (pass-if (not (complex? #t)))
  1291. (pass-if (not (complex? (lambda () #t))))
  1292. (pass-if (not (complex? (current-input-port)))))
  1293. ;;;
  1294. ;;; real?
  1295. ;;;
  1296. (with-test-prefix "real?"
  1297. (pass-if (documented? real?))
  1298. (pass-if (real? 0))
  1299. (pass-if (real? 7))
  1300. (pass-if (real? -7))
  1301. (pass-if (real? (+ 1 fixnum-max)))
  1302. (pass-if (real? (- 1 fixnum-min)))
  1303. (pass-if (real? 1.3))
  1304. (pass-if (real? +inf.0))
  1305. (pass-if (real? -inf.0))
  1306. (pass-if (real? +nan.0))
  1307. (pass-if (not (real? +inf.0-inf.0i)))
  1308. (pass-if (not (real? +nan.0+nan.0i)))
  1309. (pass-if (not (real? 3+4i)))
  1310. (pass-if (not (real? #\a)))
  1311. (pass-if (not (real? "a")))
  1312. (pass-if (not (real? (make-vector 0))))
  1313. (pass-if (not (real? (cons 1 2))))
  1314. (pass-if (not (real? #t)))
  1315. (pass-if (not (real? (lambda () #t))))
  1316. (pass-if (not (real? (current-input-port)))))
  1317. ;;;
  1318. ;;; rational?
  1319. ;;;
  1320. (with-test-prefix "rational?"
  1321. (pass-if (documented? rational?))
  1322. (pass-if (rational? 0))
  1323. (pass-if (rational? 7))
  1324. (pass-if (rational? -7))
  1325. (pass-if (rational? (+ 1 fixnum-max)))
  1326. (pass-if (rational? (- 1 fixnum-min)))
  1327. (pass-if (rational? 1.3))
  1328. (pass-if (not (rational? +inf.0)))
  1329. (pass-if (not (rational? -inf.0)))
  1330. (pass-if (not (rational? +nan.0)))
  1331. (pass-if (not (rational? +inf.0-inf.0i)))
  1332. (pass-if (not (rational? +nan.0+nan.0i)))
  1333. (pass-if (not (rational? 3+4i)))
  1334. (pass-if (not (rational? #\a)))
  1335. (pass-if (not (rational? "a")))
  1336. (pass-if (not (rational? (make-vector 0))))
  1337. (pass-if (not (rational? (cons 1 2))))
  1338. (pass-if (not (rational? #t)))
  1339. (pass-if (not (rational? (lambda () #t))))
  1340. (pass-if (not (rational? (current-input-port)))))
  1341. ;;;
  1342. ;;; integer?
  1343. ;;;
  1344. (with-test-prefix "integer?"
  1345. (pass-if (documented? integer?))
  1346. (pass-if (integer? 0))
  1347. (pass-if (integer? 7))
  1348. (pass-if (integer? -7))
  1349. (pass-if (integer? (+ 1 fixnum-max)))
  1350. (pass-if (integer? (- 1 fixnum-min)))
  1351. (pass-if (and (= 3+0i (round 3+0i)) (integer? 3+0i)))
  1352. (pass-if (and (= 1.0 (round 1.0)) (integer? 1.0)))
  1353. (pass-if (not (integer? 1.3)))
  1354. (pass-if (not (integer? +inf.0)))
  1355. (pass-if (not (integer? -inf.0)))
  1356. (pass-if (not (integer? +nan.0)))
  1357. (pass-if (not (integer? +inf.0-inf.0i)))
  1358. (pass-if (not (integer? +nan.0+nan.0i)))
  1359. (pass-if (not (integer? 3+4i)))
  1360. (pass-if (not (integer? #\a)))
  1361. (pass-if (not (integer? "a")))
  1362. (pass-if (not (integer? (make-vector 0))))
  1363. (pass-if (not (integer? (cons 1 2))))
  1364. (pass-if (not (integer? #t)))
  1365. (pass-if (not (integer? (lambda () #t))))
  1366. (pass-if (not (integer? (current-input-port)))))
  1367. ;;;
  1368. ;;; exact-integer?
  1369. ;;;
  1370. (with-test-prefix "exact-integer?"
  1371. (pass-if (documented? exact-integer?))
  1372. (pass-if (exact-integer? 0))
  1373. (pass-if (exact-integer? 7))
  1374. (pass-if (exact-integer? -7))
  1375. (pass-if (exact-integer? (+ 1 fixnum-max)))
  1376. (pass-if (exact-integer? (- 1 fixnum-min)))
  1377. (pass-if (and (= 1.0 (round 1.0))
  1378. (not (exact-integer? 1.0))))
  1379. (pass-if (not (exact-integer? 1.3)))
  1380. (pass-if (not (exact-integer? +inf.0)))
  1381. (pass-if (not (exact-integer? -inf.0)))
  1382. (pass-if (not (exact-integer? +nan.0)))
  1383. (pass-if (not (exact-integer? +inf.0-inf.0i)))
  1384. (pass-if (not (exact-integer? +nan.0+nan.0i)))
  1385. (pass-if (not (exact-integer? 3+4i)))
  1386. (pass-if (not (exact-integer? #\a)))
  1387. (pass-if (not (exact-integer? "a")))
  1388. (pass-if (not (exact-integer? (make-vector 0))))
  1389. (pass-if (not (exact-integer? (cons 1 2))))
  1390. (pass-if (not (exact-integer? #t)))
  1391. (pass-if (not (exact-integer? (lambda () #t))))
  1392. (pass-if (not (exact-integer? (current-input-port)))))
  1393. ;;;
  1394. ;;; inexact?
  1395. ;;;
  1396. (with-test-prefix "inexact?"
  1397. (pass-if (documented? inexact?))
  1398. (pass-if (not (inexact? 0)))
  1399. (pass-if (not (inexact? 7)))
  1400. (pass-if (not (inexact? -7)))
  1401. (pass-if (not (inexact? (+ 1 fixnum-max))))
  1402. (pass-if (not (inexact? (- 1 fixnum-min))))
  1403. (pass-if (inexact? 1.3))
  1404. (pass-if (inexact? 3.1+4.2i))
  1405. (pass-if (inexact? +inf.0))
  1406. (pass-if (inexact? -inf.0))
  1407. (pass-if (inexact? +nan.0))
  1408. (pass-if-exception "char"
  1409. exception:wrong-type-arg
  1410. (not (inexact? #\a)))
  1411. (pass-if-exception "string"
  1412. exception:wrong-type-arg
  1413. (not (inexact? "a")))
  1414. (pass-if-exception "vector"
  1415. exception:wrong-type-arg
  1416. (not (inexact? (make-vector 0))))
  1417. (pass-if-exception "cons"
  1418. exception:wrong-type-arg
  1419. (not (inexact? (cons 1 2))))
  1420. (pass-if-exception "bool"
  1421. exception:wrong-type-arg
  1422. (not (inexact? #t)))
  1423. (pass-if-exception "procedure"
  1424. exception:wrong-type-arg
  1425. (not (inexact? (lambda () #t))))
  1426. (pass-if-exception "port"
  1427. exception:wrong-type-arg
  1428. (not (inexact? (current-input-port)))))
  1429. ;;;
  1430. ;;; equal?
  1431. ;;;
  1432. (with-test-prefix "equal?"
  1433. (pass-if (documented? equal?))
  1434. ;; The following test will fail on platforms
  1435. ;; without distinct signed zeroes 0.0 and -0.0.
  1436. (pass-if (not (equal? 0.0 -0.0)))
  1437. (pass-if (equal? 0 0))
  1438. (pass-if (equal? 7 7))
  1439. (pass-if (equal? -7 -7))
  1440. (pass-if (equal? (+ 1 fixnum-max) (+ 1 fixnum-max)))
  1441. (pass-if (equal? (- fixnum-min 1) (- fixnum-min 1)))
  1442. (pass-if (equal? 0.0 0.0))
  1443. (pass-if (equal? -0.0 -0.0))
  1444. (pass-if (equal? 0.0+0.0i 0.0+0.0i))
  1445. (pass-if (equal? 0.0-0.0i 0.0-0.0i))
  1446. (pass-if (equal? -0.0+0.0i -0.0+0.0i))
  1447. (pass-if (not (equal? 0 1)))
  1448. (pass-if (not (equal? 0 0.0)))
  1449. (pass-if (not (equal? 1 1.0)))
  1450. (pass-if (not (equal? 0.0 0)))
  1451. (pass-if (not (equal? 1.0 1)))
  1452. (pass-if (not (equal? -1.0 -1)))
  1453. (pass-if (not (equal? 1.0 1.0+0.0i)))
  1454. (pass-if (not (equal? 0.0 0.0+0.0i)))
  1455. (pass-if (not (equal? 0.0+0.0i 0.0-0.0i)))
  1456. (pass-if (not (equal? 0.0+0.0i -0.0+0.0i)))
  1457. (pass-if (not (equal? fixnum-max (+ 1 fixnum-max))))
  1458. (pass-if (not (equal? (+ 1 fixnum-max) fixnum-max)))
  1459. (pass-if (not (equal? (+ 1 fixnum-max) (+ 2 fixnum-max))))
  1460. (pass-if (not (equal? fixnum-min (- fixnum-min 1))))
  1461. (pass-if (not (equal? (- fixnum-min 1) fixnum-min)))
  1462. (pass-if (not (equal? (- fixnum-min 1) (- fixnum-min 2))))
  1463. (pass-if (not (equal? (+ fixnum-max 1) (- fixnum-min 1))))
  1464. (pass-if (not (equal? (ash 1 256) +inf.0)))
  1465. (pass-if (not (equal? +inf.0 (ash 1 256))))
  1466. (pass-if (not (equal? (ash 1 256) -inf.0)))
  1467. (pass-if (not (equal? -inf.0 (ash 1 256))))
  1468. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  1469. ;; sure we've avoided that
  1470. (pass-if (not (equal? (ash 1 1024) +inf.0)))
  1471. (pass-if (not (equal? +inf.0 (ash 1 1024))))
  1472. (pass-if (not (equal? (- (ash 1 1024)) -inf.0)))
  1473. (pass-if (not (equal? -inf.0 (- (ash 1 1024)))))
  1474. (pass-if (equal? +nan.0 +nan.0))
  1475. (pass-if (equal? +nan.0 +nan.0))
  1476. (pass-if (not (equal? +nan.0 0.0+nan.0i)))
  1477. (pass-if (not (equal? 0 +nan.0)))
  1478. (pass-if (not (equal? +nan.0 0)))
  1479. (pass-if (not (equal? 1 +nan.0)))
  1480. (pass-if (not (equal? +nan.0 1)))
  1481. (pass-if (not (equal? -1 +nan.0)))
  1482. (pass-if (not (equal? +nan.0 -1)))
  1483. (pass-if (not (equal? (ash 1 256) +nan.0)))
  1484. (pass-if (not (equal? +nan.0 (ash 1 256))))
  1485. (pass-if (not (equal? (- (ash 1 256)) +nan.0)))
  1486. (pass-if (not (equal? +nan.0 (- (ash 1 256)))))
  1487. (pass-if (not (equal? (ash 1 8192) +nan.0)))
  1488. (pass-if (not (equal? +nan.0 (ash 1 8192))))
  1489. (pass-if (not (equal? (- (ash 1 8192)) +nan.0)))
  1490. (pass-if (not (equal? +nan.0 (- (ash 1 8192)))))
  1491. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  1492. ;; sure we've avoided that
  1493. (pass-if (not (equal? (ash 3 1023) +nan.0)))
  1494. (pass-if (not (equal? +nan.0 (ash 3 1023)))))
  1495. ;;;
  1496. ;;; eqv?
  1497. ;;;
  1498. (with-test-prefix "eqv?"
  1499. (pass-if (documented? eqv?))
  1500. ;; The following test will fail on platforms
  1501. ;; without distinct signed zeroes 0.0 and -0.0.
  1502. (pass-if (not (eqv? 0.0 -0.0)))
  1503. (pass-if (eqv? 0 0))
  1504. (pass-if (eqv? 7 7))
  1505. (pass-if (eqv? -7 -7))
  1506. (pass-if (eqv? (+ 1 fixnum-max) (+ 1 fixnum-max)))
  1507. (pass-if (eqv? (- fixnum-min 1) (- fixnum-min 1)))
  1508. (pass-if (eqv? 0.0 0.0))
  1509. (pass-if (eqv? -0.0 -0.0))
  1510. (pass-if (eqv? 0.0+0.0i 0.0+0.0i))
  1511. (pass-if (eqv? 0.0-0.0i 0.0-0.0i))
  1512. (pass-if (eqv? -0.0+0.0i -0.0+0.0i))
  1513. (pass-if (not (eqv? 0.0 -0.0)))
  1514. (pass-if (not (eqv? 0.0 0.0+0.0i)))
  1515. (pass-if (not (eqv? 0.0+0.0i 0.0-0.0i)))
  1516. (pass-if (not (eqv? 0.0+0.0i -0.0+0.0i)))
  1517. (pass-if (not (eqv? 0 1)))
  1518. (pass-if (not (eqv? 0 0.0)))
  1519. (pass-if (not (eqv? 1 1.0)))
  1520. (pass-if (not (eqv? 0.0 0)))
  1521. (pass-if (not (eqv? 1.0 1)))
  1522. (pass-if (not (eqv? -1.0 -1)))
  1523. (pass-if (not (eqv? 1.0 1.0+0.0i)))
  1524. (pass-if (not (eqv? 0.0 0.0+0.0i)))
  1525. (pass-if (not (eqv? fixnum-max (+ 1 fixnum-max))))
  1526. (pass-if (not (eqv? (+ 1 fixnum-max) fixnum-max)))
  1527. (pass-if (not (eqv? (+ 1 fixnum-max) (+ 2 fixnum-max))))
  1528. (pass-if (not (eqv? fixnum-min (- fixnum-min 1))))
  1529. (pass-if (not (eqv? (- fixnum-min 1) fixnum-min)))
  1530. (pass-if (not (eqv? (- fixnum-min 1) (- fixnum-min 2))))
  1531. (pass-if (not (eqv? (+ fixnum-max 1) (- fixnum-min 1))))
  1532. (pass-if (not (eqv? (ash 1 256) +inf.0)))
  1533. (pass-if (not (eqv? +inf.0 (ash 1 256))))
  1534. (pass-if (not (eqv? (ash 1 256) -inf.0)))
  1535. (pass-if (not (eqv? -inf.0 (ash 1 256))))
  1536. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  1537. ;; sure we've avoided that
  1538. (pass-if (not (eqv? (ash 1 1024) +inf.0)))
  1539. (pass-if (not (eqv? +inf.0 (ash 1 1024))))
  1540. (pass-if (not (eqv? (- (ash 1 1024)) -inf.0)))
  1541. (pass-if (not (eqv? -inf.0 (- (ash 1 1024)))))
  1542. (pass-if (eqv? +nan.0 +nan.0))
  1543. (pass-if (not (eqv? +nan.0 0.0+nan.0i)))
  1544. (pass-if (not (eqv? 0 +nan.0)))
  1545. (pass-if (not (eqv? +nan.0 0)))
  1546. (pass-if (not (eqv? 1 +nan.0)))
  1547. (pass-if (not (eqv? +nan.0 1)))
  1548. (pass-if (not (eqv? -1 +nan.0)))
  1549. (pass-if (not (eqv? +nan.0 -1)))
  1550. (pass-if (not (eqv? (ash 1 256) +nan.0)))
  1551. (pass-if (not (eqv? +nan.0 (ash 1 256))))
  1552. (pass-if (not (eqv? (- (ash 1 256)) +nan.0)))
  1553. (pass-if (not (eqv? +nan.0 (- (ash 1 256)))))
  1554. (pass-if (not (eqv? (ash 1 8192) +nan.0)))
  1555. (pass-if (not (eqv? +nan.0 (ash 1 8192))))
  1556. (pass-if (not (eqv? (- (ash 1 8192)) +nan.0)))
  1557. (pass-if (not (eqv? +nan.0 (- (ash 1 8192)))))
  1558. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  1559. ;; sure we've avoided that
  1560. (pass-if (not (eqv? (ash 3 1023) +nan.0)))
  1561. (pass-if (not (eqv? +nan.0 (ash 3 1023)))))
  1562. ;;;
  1563. ;;; =
  1564. ;;;
  1565. (with-test-prefix "="
  1566. (pass-if (documented? =))
  1567. (pass-if (= 7 7))
  1568. (pass-if (= -7 -7))
  1569. (pass-if (= 1.0 1))
  1570. (pass-if (= 1 1.0))
  1571. (pass-if (= -1 -1.0))
  1572. (pass-if (= 0.0 0.0))
  1573. (pass-if (= 0.0 -0.0))
  1574. (pass-if (= 1 1.0+0.0i))
  1575. (pass-if (= 0 0))
  1576. (pass-if (= 0 0.0))
  1577. (pass-if (= 0 -0.0))
  1578. (pass-if (= 0 0.0+0.0i))
  1579. (pass-if (= 0 0.0-0.0i))
  1580. (pass-if (= 0 0.0+0.0i))
  1581. (pass-if (= 0 -0.0-0.0i))
  1582. (pass-if (= 0 0))
  1583. (pass-if (= 0.0 0))
  1584. (pass-if (= -0.0 0))
  1585. (pass-if (= 0.0+0.0i 0))
  1586. (pass-if (= 0.0-0.0i 0))
  1587. (pass-if (= 0.0+0.0i 0))
  1588. (pass-if (= -0.0-0.0i 0))
  1589. (pass-if (= 0.0+0.0i 0.0-0.0i))
  1590. (pass-if (= 0.0+0.0i -0.0+0.0i))
  1591. (pass-if (= (+ 1 fixnum-max) (+ 1 fixnum-max)))
  1592. (pass-if (= (- fixnum-min 1) (- fixnum-min 1)))
  1593. (pass-if (not (= 0 1)))
  1594. (pass-if (not (= fixnum-max (+ 1 fixnum-max))))
  1595. (pass-if (not (= (+ 1 fixnum-max) fixnum-max)))
  1596. (pass-if (not (= (+ 1 fixnum-max) (+ 2 fixnum-max))))
  1597. (pass-if (not (= fixnum-min (- fixnum-min 1))))
  1598. (pass-if (not (= (- fixnum-min 1) fixnum-min)))
  1599. (pass-if (not (= (- fixnum-min 1) (- fixnum-min 2))))
  1600. (pass-if (not (= (+ fixnum-max 1) (- fixnum-min 1))))
  1601. (pass-if (not (= (ash 1 256) +inf.0)))
  1602. (pass-if (not (= +inf.0 (ash 1 256))))
  1603. (pass-if (not (= (ash 1 256) -inf.0)))
  1604. (pass-if (not (= -inf.0 (ash 1 256))))
  1605. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  1606. ;; sure we've avoided that
  1607. (pass-if (not (= (ash 1 1024) +inf.0)))
  1608. (pass-if (not (= +inf.0 (ash 1 1024))))
  1609. (pass-if (not (= (- (ash 1 1024)) -inf.0)))
  1610. (pass-if (not (= -inf.0 (- (ash 1 1024)))))
  1611. (pass-if (not (= +nan.0 +nan.0)))
  1612. (pass-if (not (= 0 +nan.0)))
  1613. (pass-if (not (= +nan.0 0)))
  1614. (pass-if (not (= 1 +nan.0)))
  1615. (pass-if (not (= +nan.0 1)))
  1616. (pass-if (not (= -1 +nan.0)))
  1617. (pass-if (not (= +nan.0 -1)))
  1618. (pass-if (not (= (ash 1 256) +nan.0)))
  1619. (pass-if (not (= +nan.0 (ash 1 256))))
  1620. (pass-if (not (= (- (ash 1 256)) +nan.0)))
  1621. (pass-if (not (= +nan.0 (- (ash 1 256)))))
  1622. (pass-if (not (= (ash 1 8192) +nan.0)))
  1623. (pass-if (not (= +nan.0 (ash 1 8192))))
  1624. (pass-if (not (= (- (ash 1 8192)) +nan.0)))
  1625. (pass-if (not (= +nan.0 (- (ash 1 8192)))))
  1626. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  1627. ;; sure we've avoided that
  1628. (pass-if (not (= (ash 3 1023) +nan.0)))
  1629. (pass-if (not (= +nan.0 (ash 3 1023))))
  1630. (pass-if (= 1/2 0.5))
  1631. (pass-if (not (= 1/3 0.333333333333333333333333333333333)))
  1632. (pass-if (not (= 2/3 0.5)))
  1633. (pass-if (not (= 0.5 (+ 1/2 (/ 1 (ash 1 1000))))))
  1634. (pass-if (= 1/2 0.5+0i))
  1635. (pass-if (not (= 0.333333333333333333333333333333333 1/3)))
  1636. (pass-if (not (= 2/3 0.5+0i)))
  1637. (pass-if (not (= 1/2 0+0.5i)))
  1638. (pass-if (= 0.5 1/2))
  1639. (pass-if (not (= 0.5 2/3)))
  1640. (pass-if (not (= (+ 1/2 (/ 1 (ash 1 1000))) 0.5)))
  1641. (pass-if (= 0.5+0i 1/2))
  1642. (pass-if (not (= 0.5+0i 2/3)))
  1643. (pass-if (not (= 0+0.5i 1/2)))
  1644. ;; prior to guile 1.8, inum/flonum comparisons were done just by
  1645. ;; converting the inum to a double, which on a 64-bit would round making
  1646. ;; say inexact 2^58 appear equal to exact 2^58+1
  1647. (pass-if (= (ash-flo 1.0 58) (ash 1 58)))
  1648. (pass-if (not (= (ash-flo 1.0 58) (1+ (ash 1 58)))))
  1649. (pass-if (not (= (ash-flo 1.0 58) (1- (ash 1 58)))))
  1650. (pass-if (= (ash 1 58) (ash-flo 1.0 58)))
  1651. (pass-if (not (= (1+ (ash 1 58)) (ash-flo 1.0 58))))
  1652. (pass-if (not (= (1- (ash 1 58)) (ash-flo 1.0 58))))
  1653. ;; prior to guile 2.0.10, inum/complex comparisons were done just by
  1654. ;; converting the inum to a double, which on a 64-bit would round making
  1655. ;; say inexact 2^58 appear equal to exact 2^58+1
  1656. (pass-if (= (+ +0.0i (ash-flo 1.0 58)) (ash 1 58)))
  1657. (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1+ (ash 1 58)))))
  1658. (pass-if (not (= (+ +0.0i (ash-flo 1.0 58)) (1- (ash 1 58)))))
  1659. (pass-if (= (ash 1 58) (+ +0.0i (ash-flo 1.0 58))))
  1660. (pass-if (not (= (1+ (ash 1 58)) (+ +0.0i (ash-flo 1.0 58)))))
  1661. (pass-if (not (= (1- (ash 1 58)) (+ +0.0i (ash-flo 1.0 58)))))
  1662. ;; prior to guile 2.0.10, fraction/flonum and fraction/complex
  1663. ;; comparisons mishandled infinities.
  1664. (pass-if (not (= 1/2 +inf.0)))
  1665. (pass-if (not (= 1/2 -inf.0)))
  1666. (pass-if (not (= +inf.0 1/2)))
  1667. (pass-if (not (= -inf.0 1/2)))
  1668. (pass-if (not (= 1/2 +inf.0+0.0i)))
  1669. (pass-if (not (= 1/2 -inf.0+0.0i)))
  1670. (pass-if (not (= +inf.0+0.0i 1/2)))
  1671. (pass-if (not (= -inf.0+0.0i 1/2))))
  1672. ;;;
  1673. ;;; <
  1674. ;;;
  1675. (with-test-prefix "<"
  1676. (pass-if "documented?"
  1677. (documented? <))
  1678. (with-test-prefix "(< 0 n)"
  1679. (pass-if "n = 0"
  1680. (not (< 0 0)))
  1681. (pass-if "n = 0.0"
  1682. (not (< 0 0.0)))
  1683. (pass-if "n = 1"
  1684. (< 0 1))
  1685. (pass-if "n = 1.0"
  1686. (< 0 1.0))
  1687. (pass-if "n = -1"
  1688. (not (< 0 -1)))
  1689. (pass-if "n = -1.0"
  1690. (not (< 0 -1.0)))
  1691. (pass-if "n = fixnum-max"
  1692. (< 0 fixnum-max))
  1693. (pass-if "n = fixnum-max + 1"
  1694. (< 0 (+ fixnum-max 1)))
  1695. (pass-if "n = fixnum-min"
  1696. (not (< 0 fixnum-min)))
  1697. (pass-if "n = fixnum-min - 1"
  1698. (not (< 0 (- fixnum-min 1)))))
  1699. (with-test-prefix "(< 0.0 n)"
  1700. (pass-if "n = 0"
  1701. (not (< 0.0 0)))
  1702. (pass-if "n = 0.0"
  1703. (not (< 0.0 0.0)))
  1704. (pass-if "n = -0.0"
  1705. (not (< 0.0 -0.0)))
  1706. (pass-if "n = 1"
  1707. (< 0.0 1))
  1708. (pass-if "n = 1.0"
  1709. (< 0.0 1.0))
  1710. (pass-if "n = -1"
  1711. (not (< 0.0 -1)))
  1712. (pass-if "n = -1.0"
  1713. (not (< 0.0 -1.0)))
  1714. (pass-if "n = fixnum-max"
  1715. (< 0.0 fixnum-max))
  1716. (pass-if "n = fixnum-max + 1"
  1717. (< 0.0 (+ fixnum-max 1)))
  1718. (pass-if "n = fixnum-min"
  1719. (not (< 0.0 fixnum-min)))
  1720. (pass-if "n = fixnum-min - 1"
  1721. (not (< 0.0 (- fixnum-min 1)))))
  1722. (pass-if (not (< -0.0 0.0)))
  1723. (pass-if (not (< -0.0 -0.0)))
  1724. (with-test-prefix "(< 1 n)"
  1725. (pass-if "n = 0"
  1726. (not (< 1 0)))
  1727. (pass-if "n = 0.0"
  1728. (not (< 1 0.0)))
  1729. (pass-if "n = 1"
  1730. (not (< 1 1)))
  1731. (pass-if "n = 1.0"
  1732. (not (< 1 1.0)))
  1733. (pass-if "n = -1"
  1734. (not (< 1 -1)))
  1735. (pass-if "n = -1.0"
  1736. (not (< 1 -1.0)))
  1737. (pass-if "n = fixnum-max"
  1738. (< 1 fixnum-max))
  1739. (pass-if "n = fixnum-max + 1"
  1740. (< 1 (+ fixnum-max 1)))
  1741. (pass-if "n = fixnum-min"
  1742. (not (< 1 fixnum-min)))
  1743. (pass-if "n = fixnum-min - 1"
  1744. (not (< 1 (- fixnum-min 1)))))
  1745. (with-test-prefix "(< 1.0 n)"
  1746. (pass-if "n = 0"
  1747. (not (< 1.0 0)))
  1748. (pass-if "n = 0.0"
  1749. (not (< 1.0 0.0)))
  1750. (pass-if "n = 1"
  1751. (not (< 1.0 1)))
  1752. (pass-if "n = 1.0"
  1753. (not (< 1.0 1.0)))
  1754. (pass-if "n = -1"
  1755. (not (< 1.0 -1)))
  1756. (pass-if "n = -1.0"
  1757. (not (< 1.0 -1.0)))
  1758. (pass-if "n = fixnum-max"
  1759. (< 1.0 fixnum-max))
  1760. (pass-if "n = fixnum-max + 1"
  1761. (< 1.0 (+ fixnum-max 1)))
  1762. (pass-if "n = fixnum-min"
  1763. (not (< 1.0 fixnum-min)))
  1764. (pass-if "n = fixnum-min - 1"
  1765. (not (< 1.0 (- fixnum-min 1)))))
  1766. (with-test-prefix "(< -1 n)"
  1767. (pass-if "n = 0"
  1768. (< -1 0))
  1769. (pass-if "n = 0.0"
  1770. (< -1 0.0))
  1771. (pass-if "n = 1"
  1772. (< -1 1))
  1773. (pass-if "n = 1.0"
  1774. (< -1 1.0))
  1775. (pass-if "n = -1"
  1776. (not (< -1 -1)))
  1777. (pass-if "n = -1.0"
  1778. (not (< -1 -1.0)))
  1779. (pass-if "n = fixnum-max"
  1780. (< -1 fixnum-max))
  1781. (pass-if "n = fixnum-max + 1"
  1782. (< -1 (+ fixnum-max 1)))
  1783. (pass-if "n = fixnum-min"
  1784. (not (< -1 fixnum-min)))
  1785. (pass-if "n = fixnum-min - 1"
  1786. (not (< -1 (- fixnum-min 1)))))
  1787. (with-test-prefix "(< -1.0 n)"
  1788. (pass-if "n = 0"
  1789. (< -1.0 0))
  1790. (pass-if "n = 0.0"
  1791. (< -1.0 0.0))
  1792. (pass-if "n = 1"
  1793. (< -1.0 1))
  1794. (pass-if "n = 1.0"
  1795. (< -1.0 1.0))
  1796. (pass-if "n = -1"
  1797. (not (< -1.0 -1)))
  1798. (pass-if "n = -1.0"
  1799. (not (< -1.0 -1.0)))
  1800. (pass-if "n = fixnum-max"
  1801. (< -1.0 fixnum-max))
  1802. (pass-if "n = fixnum-max + 1"
  1803. (< -1.0 (+ fixnum-max 1)))
  1804. (pass-if "n = fixnum-min"
  1805. (not (< -1.0 fixnum-min)))
  1806. (pass-if "n = fixnum-min - 1"
  1807. (not (< -1.0 (- fixnum-min 1)))))
  1808. (with-test-prefix "(< fixnum-max n)"
  1809. (pass-if "n = 0"
  1810. (not (< fixnum-max 0)))
  1811. (pass-if "n = 0.0"
  1812. (not (< fixnum-max 0.0)))
  1813. (pass-if "n = 1"
  1814. (not (< fixnum-max 1)))
  1815. (pass-if "n = 1.0"
  1816. (not (< fixnum-max 1.0)))
  1817. (pass-if "n = -1"
  1818. (not (< fixnum-max -1)))
  1819. (pass-if "n = -1.0"
  1820. (not (< fixnum-max -1.0)))
  1821. (pass-if "n = fixnum-max"
  1822. (not (< fixnum-max fixnum-max)))
  1823. (pass-if "n = fixnum-max + 1"
  1824. (< fixnum-max (+ fixnum-max 1)))
  1825. (pass-if "n = fixnum-min"
  1826. (not (< fixnum-max fixnum-min)))
  1827. (pass-if "n = fixnum-min - 1"
  1828. (not (< fixnum-max (- fixnum-min 1)))))
  1829. (with-test-prefix "(< (+ fixnum-max 1) n)"
  1830. (pass-if "n = 0"
  1831. (not (< (+ fixnum-max 1) 0)))
  1832. (pass-if "n = 0.0"
  1833. (not (< (+ fixnum-max 1) 0.0)))
  1834. (pass-if "n = 1"
  1835. (not (< (+ fixnum-max 1) 1)))
  1836. (pass-if "n = 1.0"
  1837. (not (< (+ fixnum-max 1) 1.0)))
  1838. (pass-if "n = -1"
  1839. (not (< (+ fixnum-max 1) -1)))
  1840. (pass-if "n = -1.0"
  1841. (not (< (+ fixnum-max 1) -1.0)))
  1842. (pass-if "n = fixnum-max"
  1843. (not (< (+ fixnum-max 1) fixnum-max)))
  1844. (pass-if "n = fixnum-max + 1"
  1845. (not (< (+ fixnum-max 1) (+ fixnum-max 1))))
  1846. (pass-if "n = fixnum-min"
  1847. (not (< (+ fixnum-max 1) fixnum-min)))
  1848. (pass-if "n = fixnum-min - 1"
  1849. (not (< (+ fixnum-max 1) (- fixnum-min 1)))))
  1850. (with-test-prefix "(< fixnum-min n)"
  1851. (pass-if "n = 0"
  1852. (< fixnum-min 0))
  1853. (pass-if "n = 0.0"
  1854. (< fixnum-min 0.0))
  1855. (pass-if "n = 1"
  1856. (< fixnum-min 1))
  1857. (pass-if "n = 1.0"
  1858. (< fixnum-min 1.0))
  1859. (pass-if "n = -1"
  1860. (< fixnum-min -1))
  1861. (pass-if "n = -1.0"
  1862. (< fixnum-min -1.0))
  1863. (pass-if "n = fixnum-max"
  1864. (< fixnum-min fixnum-max))
  1865. (pass-if "n = fixnum-max + 1"
  1866. (< fixnum-min (+ fixnum-max 1)))
  1867. (pass-if "n = fixnum-min"
  1868. (not (< fixnum-min fixnum-min)))
  1869. (pass-if "n = fixnum-min - 1"
  1870. (not (< fixnum-min (- fixnum-min 1)))))
  1871. (with-test-prefix "(< (- fixnum-min 1) n)"
  1872. (pass-if "n = 0"
  1873. (< (- fixnum-min 1) 0))
  1874. (pass-if "n = 0.0"
  1875. (< (- fixnum-min 1) 0.0))
  1876. (pass-if "n = 1"
  1877. (< (- fixnum-min 1) 1))
  1878. (pass-if "n = 1.0"
  1879. (< (- fixnum-min 1) 1.0))
  1880. (pass-if "n = -1"
  1881. (< (- fixnum-min 1) -1))
  1882. (pass-if "n = -1.0"
  1883. (< (- fixnum-min 1) -1.0))
  1884. (pass-if "n = fixnum-max"
  1885. (< (- fixnum-min 1) fixnum-max))
  1886. (pass-if "n = fixnum-max + 1"
  1887. (< (- fixnum-min 1) (+ fixnum-max 1)))
  1888. (pass-if "n = fixnum-min"
  1889. (< (- fixnum-min 1) fixnum-min))
  1890. (pass-if "n = fixnum-min - 1"
  1891. (not (< (- fixnum-min 1) (- fixnum-min 1)))))
  1892. (pass-if (< (ash 1 256) +inf.0))
  1893. (pass-if (not (< +inf.0 (ash 1 256))))
  1894. (pass-if (not (< (ash 1 256) -inf.0)))
  1895. (pass-if (< -inf.0 (ash 1 256)))
  1896. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  1897. ;; sure we've avoided that
  1898. (pass-if (< (1- (ash 1 1024)) +inf.0))
  1899. (pass-if (< (ash 1 1024) +inf.0))
  1900. (pass-if (< (1+ (ash 1 1024)) +inf.0))
  1901. (pass-if (not (< +inf.0 (1- (ash 1 1024)))))
  1902. (pass-if (not (< +inf.0 (ash 1 1024))))
  1903. (pass-if (not (< +inf.0 (1+ (ash 1 1024)))))
  1904. (pass-if (< -inf.0 (- (1- (ash 1 1024)))))
  1905. (pass-if (< -inf.0 (- (ash 1 1024))))
  1906. (pass-if (< -inf.0 (- (1+ (ash 1 1024)))))
  1907. (pass-if (not (< (- (1- (ash 1 1024))) -inf.0)))
  1908. (pass-if (not (< (- (ash 1 1024)) -inf.0)))
  1909. (pass-if (not (< (- (1+ (ash 1 1024))) -inf.0)))
  1910. (pass-if (not (< +nan.0 +nan.0)))
  1911. (pass-if (not (< 0 +nan.0)))
  1912. (pass-if (not (< +nan.0 0)))
  1913. (pass-if (not (< 1 +nan.0)))
  1914. (pass-if (not (< +nan.0 1)))
  1915. (pass-if (not (< -1 +nan.0)))
  1916. (pass-if (not (< +nan.0 -1)))
  1917. (pass-if (not (< (ash 1 256) +nan.0)))
  1918. (pass-if (not (< +nan.0 (ash 1 256))))
  1919. (pass-if (not (< (- (ash 1 256)) +nan.0)))
  1920. (pass-if (not (< +nan.0 (- (ash 1 256)))))
  1921. (pass-if (not (< (ash 1 8192) +nan.0)))
  1922. (pass-if (not (< +nan.0 (ash 1 8192))))
  1923. (pass-if (not (< (- (ash 1 8192)) +nan.0)))
  1924. (pass-if (not (< +nan.0 (- (ash 1 8192)))))
  1925. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  1926. ;; sure we've avoided that
  1927. (pass-if (not (< (ash 3 1023) +nan.0)))
  1928. (pass-if (not (< (1+ (ash 3 1023)) +nan.0)))
  1929. (pass-if (not (< (1- (ash 3 1023)) +nan.0)))
  1930. (pass-if (not (< +nan.0 (ash 3 1023))))
  1931. (pass-if (not (< +nan.0 (1+ (ash 3 1023)))))
  1932. (pass-if (not (< +nan.0 (1- (ash 3 1023)))))
  1933. (with-test-prefix "inum/frac"
  1934. (pass-if (< 2 9/4))
  1935. (pass-if (< -2 9/4))
  1936. (pass-if (< -2 7/4))
  1937. (pass-if (< -2 -7/4))
  1938. (pass-if (eq? #f (< 2 7/4)))
  1939. (pass-if (eq? #f (< 2 -7/4)))
  1940. (pass-if (eq? #f (< 2 -9/4)))
  1941. (pass-if (eq? #f (< -2 -9/4))))
  1942. (with-test-prefix "bignum/frac"
  1943. (let ((x (ash 1 2048)))
  1944. (pass-if (< x (* 4/3 x)))
  1945. (pass-if (< (- x) (* 4/3 x)))
  1946. (pass-if (< (- x) (* 2/3 x)))
  1947. (pass-if (< (- x) (* -2/3 x)))
  1948. (pass-if (eq? #f (< x (* 2/3 x))))
  1949. (pass-if (eq? #f (< x (* -2/3 x))))
  1950. (pass-if (eq? #f (< x (* -4/3 x))))
  1951. (pass-if (eq? #f (< (- x) (* -4/3 x))))))
  1952. (with-test-prefix "inum/flonum"
  1953. (pass-if (< 4 4.5))
  1954. (pass-if (< 4.5 5))
  1955. (pass-if (< -5 -4.5))
  1956. (pass-if (< -4.5 4))
  1957. (pass-if (not (< 4.5 4)))
  1958. (pass-if (not (< 5 4.5)))
  1959. (pass-if (not (< -4.5 -5)))
  1960. (pass-if (not (< 4 -4.5)))
  1961. (pass-if (< 4 +inf.0))
  1962. (pass-if (< -4 +inf.0))
  1963. (pass-if (< -inf.0 4))
  1964. (pass-if (< -inf.0 -4))
  1965. (pass-if (not (< +inf.0 4)))
  1966. (pass-if (not (< +inf.0 -4)))
  1967. (pass-if (not (< 4 -inf.0)))
  1968. (pass-if (not (< -4 -inf.0)))
  1969. (pass-if (not (< +nan.0 4)))
  1970. (pass-if (not (< +nan.0 -4)))
  1971. (pass-if (not (< 4 +nan.0)))
  1972. (pass-if (not (< -4 +nan.0)))
  1973. (pass-if (< most-positive-fixnum (expt 2.0 fixnum-bit)))
  1974. (pass-if (not (< (expt 2.0 fixnum-bit) most-positive-fixnum)))
  1975. (pass-if (< (- (expt 2.0 fixnum-bit)) most-negative-fixnum))
  1976. (pass-if (not (< most-negative-fixnum (- (expt 2.0 fixnum-bit)))))
  1977. ;; Prior to guile 2.0.10, we would unconditionally convert the inum
  1978. ;; to a double, which on a 64-bit system could result in a
  1979. ;; significant change in its value, thus corrupting the comparison.
  1980. (pass-if (< most-positive-fixnum (exact->inexact (+ 1 most-positive-fixnum))))
  1981. (pass-if (< (exact->inexact (- (+ 1 most-positive-fixnum))) (- most-positive-fixnum))))
  1982. (with-test-prefix "flonum/frac"
  1983. (pass-if (< 0.75 4/3))
  1984. (pass-if (< -0.75 4/3))
  1985. (pass-if (< -0.75 2/3))
  1986. (pass-if (< -0.75 -2/3))
  1987. (pass-if (eq? #f (< 0.75 2/3)))
  1988. (pass-if (eq? #f (< 0.75 -2/3)))
  1989. (pass-if (eq? #f (< 0.75 -4/3)))
  1990. (pass-if (eq? #f (< -0.75 -4/3)))
  1991. (pass-if (< -inf.0 4/3))
  1992. (pass-if (< -inf.0 -4/3))
  1993. (pass-if (eq? #f (< +inf.0 4/3)))
  1994. (pass-if (eq? #f (< +inf.0 -4/3)))
  1995. (pass-if (eq? #f (< +nan.0 4/3)))
  1996. (pass-if (eq? #f (< +nan.0 -4/3))))
  1997. (with-test-prefix "frac/inum"
  1998. (pass-if (< 7/4 2))
  1999. (pass-if (< -7/4 2))
  2000. (pass-if (< -9/4 2))
  2001. (pass-if (< -9/4 -2))
  2002. (pass-if (eq? #f (< 9/4 2)))
  2003. (pass-if (eq? #f (< 9/4 -2)))
  2004. (pass-if (eq? #f (< 7/4 -2)))
  2005. (pass-if (eq? #f (< -7/4 -2))))
  2006. (with-test-prefix "frac/bignum"
  2007. (let ((x (ash 1 2048)))
  2008. (pass-if (< (* 2/3 x) x))
  2009. (pass-if (< (* -2/3 x) x))
  2010. (pass-if (< (* -4/3 x) x))
  2011. (pass-if (< (* -4/3 x) (- x)))
  2012. (pass-if (eq? #f (< (* 4/3 x) x)))
  2013. (pass-if (eq? #f (< (* 4/3 x) (- x))))
  2014. (pass-if (eq? #f (< (* 2/3 x) (- x))))
  2015. (pass-if (eq? #f (< (* -2/3 x) (- x))))))
  2016. (with-test-prefix "frac/flonum"
  2017. (pass-if (< 2/3 0.75))
  2018. (pass-if (< -2/3 0.75))
  2019. (pass-if (< -4/3 0.75))
  2020. (pass-if (< -4/3 -0.75))
  2021. (pass-if (eq? #f (< 4/3 0.75)))
  2022. (pass-if (eq? #f (< 4/3 -0.75)))
  2023. (pass-if (eq? #f (< 2/3 -0.75)))
  2024. (pass-if (eq? #f (< -2/3 -0.75)))
  2025. (pass-if (< 4/3 +inf.0))
  2026. (pass-if (< -4/3 +inf.0))
  2027. (pass-if (eq? #f (< 4/3 -inf.0)))
  2028. (pass-if (eq? #f (< -4/3 -inf.0)))
  2029. (pass-if (eq? #f (< 4/3 +nan.0)))
  2030. (pass-if (eq? #f (< -4/3 +nan.0))))
  2031. (with-test-prefix "frac/frac"
  2032. (pass-if (< 2/3 6/7))
  2033. (pass-if (< -2/3 6/7))
  2034. (pass-if (< -4/3 6/7))
  2035. (pass-if (< -4/3 -6/7))
  2036. (pass-if (eq? #f (< 4/3 6/7)))
  2037. (pass-if (eq? #f (< 4/3 -6/7)))
  2038. (pass-if (eq? #f (< 2/3 -6/7)))
  2039. (pass-if (eq? #f (< -2/3 -6/7)))))
  2040. ;;;
  2041. ;;; >
  2042. ;;;
  2043. ;; currently not tested -- implementation is trivial
  2044. ;; (> x y) is implemented as (< y x)
  2045. ;; FIXME: tests should probably be added in case we change implementation.
  2046. ;;;
  2047. ;;; <=
  2048. ;;;
  2049. ;; currently not tested -- implementation is trivial
  2050. ;; (<= x y) is implemented as (not (< y x))
  2051. ;; FIXME: tests should probably be added in case we change implementation.
  2052. ;;;
  2053. ;;; >=
  2054. ;;;
  2055. ;; currently not tested -- implementation is trivial
  2056. ;; (>= x y) is implemented as (not (< x y))
  2057. ;; FIXME: tests should probably be added in case we change implementation.
  2058. ;;;
  2059. ;;; zero?
  2060. ;;;
  2061. (with-test-prefix "zero?"
  2062. (pass-if (documented? zero?))
  2063. (pass-if (zero? 0))
  2064. (pass-if (zero? 0.0))
  2065. (pass-if (zero? -0.0))
  2066. (pass-if (zero? 0.0+0.0i))
  2067. (pass-if (zero? 0.0-0.0i))
  2068. (pass-if (zero? 0.0+0.0i))
  2069. (pass-if (zero? -0.0-0.0i))
  2070. (pass-if (not (zero? 7)))
  2071. (pass-if (not (zero? -7)))
  2072. (pass-if (not (zero? 1/7)))
  2073. (pass-if (not (zero? -inf.0)))
  2074. (pass-if (not (zero? +inf.0)))
  2075. (pass-if (not (zero? +nan.0)))
  2076. (pass-if (not (zero? (+ 1 fixnum-max))))
  2077. (pass-if (not (zero? (- 1 fixnum-min))))
  2078. (pass-if (not (zero? 1.3)))
  2079. (pass-if (not (zero? 3.1+4.2i)))
  2080. (pass-if (not (zero? 1.0+0.0i)))
  2081. (pass-if (not (zero? 0.0-1.0i))))
  2082. ;;;
  2083. ;;; positive?
  2084. ;;;
  2085. (with-test-prefix "positive?"
  2086. (pass-if (documented? positive?))
  2087. (pass-if (positive? 1))
  2088. (pass-if (positive? (+ fixnum-max 1)))
  2089. (pass-if (positive? 1.3))
  2090. (pass-if (not (positive? 0)))
  2091. (pass-if (not (positive? -1)))
  2092. (pass-if (not (positive? (- fixnum-min 1))))
  2093. (pass-if (not (positive? -1.3))))
  2094. ;;;
  2095. ;;; negative?
  2096. ;;;
  2097. (with-test-prefix "negative?"
  2098. (pass-if (documented? negative?))
  2099. (pass-if (not (negative? 1)))
  2100. (pass-if (not (negative? (+ fixnum-max 1))))
  2101. (pass-if (not (negative? 1.3)))
  2102. (pass-if (not (negative? 0)))
  2103. (pass-if (negative? -1))
  2104. (pass-if (negative? (- fixnum-min 1)))
  2105. (pass-if (negative? -1.3)))
  2106. ;;;
  2107. ;;; max
  2108. ;;;
  2109. (with-test-prefix "max"
  2110. (pass-if-exception "no args" exception:wrong-num-args
  2111. (max))
  2112. (pass-if-exception "one complex" exception:wrong-type-arg
  2113. (max 1+i))
  2114. (pass-if-exception "inum/complex" exception:wrong-type-arg
  2115. (max 123 1+i))
  2116. (pass-if-exception "big/complex" exception:wrong-type-arg
  2117. (max 9999999999999999999999999999999999999999 1+i))
  2118. (pass-if-exception "real/complex" exception:wrong-type-arg
  2119. (max 123.0 1+i))
  2120. (pass-if-exception "frac/complex" exception:wrong-type-arg
  2121. (max 123/456 1+i))
  2122. (pass-if-exception "complex/inum" exception:wrong-type-arg
  2123. (max 1+i 123))
  2124. (pass-if-exception "complex/big" exception:wrong-type-arg
  2125. (max 1+i 9999999999999999999999999999999999999999))
  2126. (pass-if-exception "complex/real" exception:wrong-type-arg
  2127. (max 1+i 123.0))
  2128. (pass-if-exception "complex/frac" exception:wrong-type-arg
  2129. (max 1+i 123/456))
  2130. (let ((big*2 (* fixnum-max 2))
  2131. (big*3 (* fixnum-max 3))
  2132. (big*4 (* fixnum-max 4))
  2133. (big*5 (* fixnum-max 5)))
  2134. (with-test-prefix "inum / frac"
  2135. (pass-if (eqv? 3 (max 3 5/2)))
  2136. (pass-if (eqv? 5/2 (max 2 5/2))))
  2137. (with-test-prefix "frac / inum"
  2138. (pass-if (eqv? 3 (max 5/2 3)))
  2139. (pass-if (eqv? 5/2 (max 5/2 2))))
  2140. (with-test-prefix "infinities and NaNs"
  2141. ;; +inf.0 beats everything except NaNs
  2142. (pass-if (eqv? +inf.0 (max +inf.0 123 )))
  2143. (pass-if (eqv? +inf.0 (max 123 +inf.0 )))
  2144. (pass-if (eqv? +inf.0 (max +inf.0 -123.3 )))
  2145. (pass-if (eqv? +inf.0 (max -123.3 +inf.0 )))
  2146. (pass-if (eqv? +inf.0 (max +inf.0 -7/2 )))
  2147. (pass-if (eqv? +inf.0 (max -7/2 +inf.0 )))
  2148. (pass-if (eqv? +inf.0 (max +inf.0 -1e20 )))
  2149. (pass-if (eqv? +inf.0 (max -1e20 +inf.0 )))
  2150. (pass-if (eqv? +inf.0 (max +inf.0 (- big*2))))
  2151. (pass-if (eqv? +inf.0 (max (- big*2) +inf.0 )))
  2152. (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
  2153. (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
  2154. (pass-if (eqv? +inf.0 (max +inf.0 +inf.0 )))
  2155. ;; NaNs beat everything
  2156. (pass-if (real-nan? (max +nan.0 123 )))
  2157. (pass-if (real-nan? (max 123 +nan.0 )))
  2158. (pass-if (real-nan? (max +nan.0 123.3 )))
  2159. (pass-if (real-nan? (max 123.3 +nan.0 )))
  2160. (pass-if (real-nan? (max +nan.0 -7/2 )))
  2161. (pass-if (real-nan? (max -7/2 +nan.0 )))
  2162. (pass-if (real-nan? (max +nan.0 -1e20 )))
  2163. (pass-if (real-nan? (max -1e20 +nan.0 )))
  2164. (pass-if (real-nan? (max +nan.0 (- big*2))))
  2165. (pass-if (real-nan? (max (- big*2) +nan.0 )))
  2166. (pass-if (real-nan? (max +nan.0 -inf.0 )))
  2167. (pass-if (real-nan? (max -inf.0 +nan.0 )))
  2168. (pass-if (real-nan? (max +nan.0 +nan.0 )))
  2169. (pass-if (real-nan? (max +inf.0 +nan.0 )))
  2170. (pass-if (real-nan? (max +nan.0 +inf.0 )))
  2171. ;; -inf.0 always loses, except against itself
  2172. (pass-if (eqv? -inf.0 (max -inf.0 -inf.0 )))
  2173. (pass-if (eqv? -123.0 (max -inf.0 -123 )))
  2174. (pass-if (eqv? -123.0 (max -123 -inf.0 )))
  2175. (pass-if (eqv? -123.3 (max -inf.0 -123.3 )))
  2176. (pass-if (eqv? -123.3 (max -123.3 -inf.0 )))
  2177. (pass-if (eqv? -3.5 (max -inf.0 -7/2 )))
  2178. (pass-if (eqv? -3.5 (max -7/2 -inf.0 )))
  2179. (pass-if (eqv? -1.0e20 (max -inf.0 -1e20 )))
  2180. (pass-if (eqv? -1.0e20 (max -1e20 -inf.0 )))
  2181. (pass-if (eqv? (exact->inexact (- big*2))
  2182. (max -inf.0 (- big*2))))
  2183. (pass-if (eqv? (exact->inexact (- big*2))
  2184. (max (- big*2) -inf.0 ))))
  2185. (with-test-prefix "signed zeroes"
  2186. (pass-if (eqv? 0.0 (max 0.0 0.0)))
  2187. (pass-if (eqv? 0.0 (max 0.0 -0.0)))
  2188. (pass-if (eqv? 0.0 (max -0.0 0.0)))
  2189. (pass-if (eqv? -0.0 (max -0.0 -0.0)))
  2190. (pass-if (eqv? 0.0 (max -0.0 0 )))
  2191. (pass-if (eqv? 0.0 (max 0.0 0 )))
  2192. (pass-if (eqv? 0.0 (max 0 -0.0)))
  2193. (pass-if (eqv? 0.0 (max 0 0.0)))
  2194. (pass-if (eqv? 0 (min 0 0 ))))
  2195. (with-test-prefix "big / frac"
  2196. (pass-if (eqv? big*2 (max big*2 5/2)))
  2197. (pass-if (eqv? 5/2 (max (- big*2) 5/2))))
  2198. (with-test-prefix "frac / big"
  2199. (pass-if (eqv? big*2 (max 5/2 big*2)))
  2200. (pass-if (eqv? 5/2 (max 5/2 (- big*2)))))
  2201. (with-test-prefix "big / real"
  2202. (pass-if (real-nan? (max big*5 +nan.0)))
  2203. (pass-if (eqv? (exact->inexact big*5) (max big*5 -inf.0)))
  2204. (pass-if (eqv? (exact->inexact big*5) (max big*5 1.0)))
  2205. (pass-if (eqv? +inf.0 (max big*5 +inf.0)))
  2206. (pass-if (eqv? 1.0 (max (- big*5) 1.0))))
  2207. (with-test-prefix "real / big"
  2208. (pass-if (real-nan? (max +nan.0 big*5)))
  2209. (pass-if (eqv? (exact->inexact big*5) (max -inf.0 big*5)))
  2210. (pass-if (eqv? (exact->inexact big*5) (max 1.0 big*5)))
  2211. (pass-if (eqv? +inf.0 (max +inf.0 big*5)))
  2212. (pass-if (eqv? 1.0 (max 1.0 (- big*5)))))
  2213. (with-test-prefix "frac / frac"
  2214. (pass-if (eqv? 2/3 (max 1/2 2/3)))
  2215. (pass-if (eqv? 2/3 (max 2/3 1/2)))
  2216. (pass-if (eqv? -1/2 (max -1/2 -2/3)))
  2217. (pass-if (eqv? -1/2 (max -2/3 -1/2))))
  2218. (with-test-prefix "real / real"
  2219. (pass-if (real-nan? (max 123.0 +nan.0)))
  2220. (pass-if (real-nan? (max +nan.0 123.0)))
  2221. (pass-if (real-nan? (max +nan.0 +nan.0)))
  2222. (pass-if (eqv? 456.0 (max 123.0 456.0)))
  2223. (pass-if (eqv? 456.0 (max 456.0 123.0)))))
  2224. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  2225. ;; sure we've avoided that
  2226. (for-each (lambda (b)
  2227. (pass-if (list b +inf.0)
  2228. (eqv? +inf.0 (max b +inf.0)))
  2229. (pass-if (list +inf.0 b)
  2230. (eqv? +inf.0 (max b +inf.0)))
  2231. (pass-if (list b -inf.0)
  2232. (eqv? (exact->inexact b) (max b -inf.0)))
  2233. (pass-if (list -inf.0 b)
  2234. (eqv? (exact->inexact b) (max b -inf.0))))
  2235. (list (1- (ash 1 1024))
  2236. (ash 1 1024)
  2237. (1+ (ash 1 1024))
  2238. (- (1- (ash 1 1024)))
  2239. (- (ash 1 1024))
  2240. (- (1+ (ash 1 1024)))))
  2241. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  2242. ;; sure we've avoided that
  2243. (pass-if (real-nan? (max (ash 1 2048) +nan.0)))
  2244. (pass-if (real-nan? (max +nan.0 (ash 1 2048)))))
  2245. ;;;
  2246. ;;; min
  2247. ;;;
  2248. ;; FIXME: unfinished...
  2249. (with-test-prefix "min"
  2250. (pass-if-exception "no args" exception:wrong-num-args
  2251. (min))
  2252. (pass-if-exception "one complex" exception:wrong-type-arg
  2253. (min 1+i))
  2254. (pass-if-exception "inum/complex" exception:wrong-type-arg
  2255. (min 123 1+i))
  2256. (pass-if-exception "big/complex" exception:wrong-type-arg
  2257. (min 9999999999999999999999999999999999999999 1+i))
  2258. (pass-if-exception "real/complex" exception:wrong-type-arg
  2259. (min 123.0 1+i))
  2260. (pass-if-exception "frac/complex" exception:wrong-type-arg
  2261. (min 123/456 1+i))
  2262. (pass-if-exception "complex/inum" exception:wrong-type-arg
  2263. (min 1+i 123))
  2264. (pass-if-exception "complex/big" exception:wrong-type-arg
  2265. (min 1+i 9999999999999999999999999999999999999999))
  2266. (pass-if-exception "complex/real" exception:wrong-type-arg
  2267. (min 1+i 123.0))
  2268. (pass-if-exception "complex/frac" exception:wrong-type-arg
  2269. (min 1+i 123/456))
  2270. (let ((big*2 (* fixnum-max 2))
  2271. (big*3 (* fixnum-max 3))
  2272. (big*4 (* fixnum-max 4))
  2273. (big*5 (* fixnum-max 5)))
  2274. (pass-if (documented? min))
  2275. (pass-if (eqv? 1 (min 7 3 1 5)))
  2276. (pass-if (eqv? 1 (min 1 7 3 5)))
  2277. (pass-if (eqv? 1 (min 7 3 5 1)))
  2278. (pass-if (eqv? -7 (min 2 3 4 -2 5 -7 1 -1 4 2)))
  2279. (pass-if (eqv? -7 (min -7 2 3 4 -2 5 1 -1 4 2)))
  2280. (pass-if (eqv? -7 (min 2 3 4 -2 5 1 -1 4 2 -7)))
  2281. (pass-if (eqv? big*2 (min big*3 big*5 big*2 big*4)))
  2282. (pass-if (eqv? big*2 (min big*2 big*3 big*5 big*4)))
  2283. (pass-if (eqv? big*2 (min big*3 big*5 big*4 big*2)))
  2284. (pass-if
  2285. (eqv? (- fixnum-min 1) (min 2 4 (- fixnum-min 1) 3 (* 2 fixnum-max))))
  2286. (pass-if
  2287. (eqv? (- fixnum-min 1) (min (- fixnum-min 1) 2 4 3 (* 2 fixnum-max))))
  2288. (pass-if
  2289. (eqv? (- fixnum-min 1) (min 2 4 3 (* 2 fixnum-max) (- fixnum-min 1))))
  2290. (with-test-prefix "inum / frac"
  2291. (pass-if (eqv? 5/2 (min 3 5/2)))
  2292. (pass-if (eqv? 2 (min 2 5/2))))
  2293. (with-test-prefix "frac / inum"
  2294. (pass-if (eqv? 5/2 (min 5/2 3)))
  2295. (pass-if (eqv? 2 (min 5/2 2))))
  2296. (with-test-prefix "infinities and NaNs"
  2297. ;; -inf.0 beats everything except NaNs
  2298. (pass-if (eqv? -inf.0 (min -inf.0 123 )))
  2299. (pass-if (eqv? -inf.0 (min 123 -inf.0 )))
  2300. (pass-if (eqv? -inf.0 (min -inf.0 -123.3 )))
  2301. (pass-if (eqv? -inf.0 (min -123.3 -inf.0 )))
  2302. (pass-if (eqv? -inf.0 (min -inf.0 -7/2 )))
  2303. (pass-if (eqv? -inf.0 (min -7/2 -inf.0 )))
  2304. (pass-if (eqv? -inf.0 (min -inf.0 -1e20 )))
  2305. (pass-if (eqv? -inf.0 (min -1e20 -inf.0 )))
  2306. (pass-if (eqv? -inf.0 (min -inf.0 (- big*2))))
  2307. (pass-if (eqv? -inf.0 (min (- big*2) -inf.0 )))
  2308. (pass-if (eqv? -inf.0 (min -inf.0 +inf.0 )))
  2309. (pass-if (eqv? -inf.0 (min +inf.0 -inf.0 )))
  2310. (pass-if (eqv? -inf.0 (min -inf.0 -inf.0 )))
  2311. ;; NaNs beat everything
  2312. (pass-if (real-nan? (min +nan.0 123 )))
  2313. (pass-if (real-nan? (min 123 +nan.0 )))
  2314. (pass-if (real-nan? (min +nan.0 123.3 )))
  2315. (pass-if (real-nan? (min 123.3 +nan.0 )))
  2316. (pass-if (real-nan? (min +nan.0 -7/2 )))
  2317. (pass-if (real-nan? (min -7/2 +nan.0 )))
  2318. (pass-if (real-nan? (min +nan.0 -1e20 )))
  2319. (pass-if (real-nan? (min -1e20 +nan.0 )))
  2320. (pass-if (real-nan? (min +nan.0 (- big*2))))
  2321. (pass-if (real-nan? (min (- big*2) +nan.0 )))
  2322. (pass-if (real-nan? (min +nan.0 +inf.0 )))
  2323. (pass-if (real-nan? (min +inf.0 +nan.0 )))
  2324. (pass-if (real-nan? (min +nan.0 +nan.0 )))
  2325. (pass-if (real-nan? (min -inf.0 +nan.0 )))
  2326. (pass-if (real-nan? (min +nan.0 -inf.0 )))
  2327. ;; +inf.0 always loses, except against itself
  2328. (pass-if (eqv? +inf.0 (min +inf.0 +inf.0 )))
  2329. (pass-if (eqv? -123.0 (min +inf.0 -123 )))
  2330. (pass-if (eqv? -123.0 (min -123 +inf.0 )))
  2331. (pass-if (eqv? -123.3 (min +inf.0 -123.3 )))
  2332. (pass-if (eqv? -123.3 (min -123.3 +inf.0 )))
  2333. (pass-if (eqv? -3.5 (min +inf.0 -7/2 )))
  2334. (pass-if (eqv? -3.5 (min -7/2 +inf.0 )))
  2335. (pass-if (eqv? -1.0e20 (min +inf.0 -1e20 )))
  2336. (pass-if (eqv? -1.0e20 (min -1e20 +inf.0 )))
  2337. (pass-if (eqv? (exact->inexact (- big*2))
  2338. (min +inf.0 (- big*2))))
  2339. (pass-if (eqv? (exact->inexact (- big*2))
  2340. (min (- big*2) +inf.0 ))))
  2341. (with-test-prefix "signed zeroes"
  2342. (pass-if (eqv? 0.0 (min 0.0 0.0)))
  2343. (pass-if (eqv? -0.0 (min 0.0 -0.0)))
  2344. (pass-if (eqv? -0.0 (min -0.0 0.0)))
  2345. (pass-if (eqv? -0.0 (min -0.0 -0.0)))
  2346. (pass-if (eqv? -0.0 (min -0.0 0 )))
  2347. (pass-if (eqv? 0.0 (min 0.0 0 )))
  2348. (pass-if (eqv? -0.0 (min 0 -0.0)))
  2349. (pass-if (eqv? 0.0 (min 0 0.0)))
  2350. (pass-if (eqv? 0 (min 0 0 ))))
  2351. (with-test-prefix "big / frac"
  2352. (pass-if (eqv? 5/2 (min big*2 5/2)))
  2353. (pass-if (eqv? (- big*2) (min (- big*2) 5/2))))
  2354. (with-test-prefix "frac / big"
  2355. (pass-if (eqv? 5/2 (min 5/2 big*2)))
  2356. (pass-if (eqv? (- big*2) (min 5/2 (- big*2)))))
  2357. (with-test-prefix "big / real"
  2358. (pass-if (real-nan? (min big*5 +nan.0)))
  2359. (pass-if (eqv? (exact->inexact big*5) (min big*5 +inf.0)))
  2360. (pass-if (eqv? -inf.0 (min big*5 -inf.0)))
  2361. (pass-if (eqv? 1.0 (min big*5 1.0)))
  2362. (pass-if (eqv? (exact->inexact (- big*5)) (min (- big*5) 1.0))))
  2363. (with-test-prefix "real / big"
  2364. (pass-if (real-nan? (min +nan.0 big*5)))
  2365. (pass-if (eqv? (exact->inexact big*5) (min +inf.0 big*5)))
  2366. (pass-if (eqv? -inf.0 (min -inf.0 big*5)))
  2367. (pass-if (eqv? 1.0 (min 1.0 big*5)))
  2368. (pass-if (eqv? (exact->inexact (- big*5)) (min 1.0 (- big*5)))))
  2369. (with-test-prefix "frac / frac"
  2370. (pass-if (eqv? 1/2 (min 1/2 2/3)))
  2371. (pass-if (eqv? 1/2 (min 2/3 1/2)))
  2372. (pass-if (eqv? -2/3 (min -1/2 -2/3)))
  2373. (pass-if (eqv? -2/3 (min -2/3 -1/2))))
  2374. (with-test-prefix "real / real"
  2375. (pass-if (real-nan? (min 123.0 +nan.0)))
  2376. (pass-if (real-nan? (min +nan.0 123.0)))
  2377. (pass-if (real-nan? (min +nan.0 +nan.0)))
  2378. (pass-if (eqv? 123.0 (min 123.0 456.0)))
  2379. (pass-if (eqv? 123.0 (min 456.0 123.0)))))
  2380. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating Inf as 2^1024, make
  2381. ;; sure we've avoided that
  2382. (for-each (lambda (b)
  2383. (pass-if (list b +inf.0)
  2384. (eqv? (exact->inexact b) (min b +inf.0)))
  2385. (pass-if (list +inf.0 b)
  2386. (eqv? (exact->inexact b) (min b +inf.0)))
  2387. (pass-if (list b -inf.0)
  2388. (eqv? -inf.0 (min b -inf.0)))
  2389. (pass-if (list -inf.0 b)
  2390. (eqv? -inf.0 (min b -inf.0))))
  2391. (list (1- (ash 1 1024))
  2392. (ash 1 1024)
  2393. (1+ (ash 1 1024))
  2394. (- (1- (ash 1 1024)))
  2395. (- (ash 1 1024))
  2396. (- (1+ (ash 1 1024)))))
  2397. ;; in gmp prior to 4.2, mpz_cmp_d ended up treating NaN as 3*2^1023, make
  2398. ;; sure we've avoided that
  2399. (pass-if (real-nan? (min (- (ash 1 2048)) (- +nan.0))))
  2400. (pass-if (real-nan? (min (- +nan.0) (- (ash 1 2048))))))
  2401. ;;;
  2402. ;;; +
  2403. ;;;
  2404. (with-test-prefix/c&e "+"
  2405. (pass-if "documented?"
  2406. (documented? +))
  2407. (pass-if "simple"
  2408. (and (eqv? 7 (+ 3 4))
  2409. (eqv? 3 (+ 3))
  2410. (eqv? 0 (+))))
  2411. (pass-if "exactness propagation"
  2412. (and (eqv? 8 (+ 3 5))
  2413. (eqv? 8.0 (+ 3 5.0))
  2414. (eqv? 8.0 (+ 3.0 5))
  2415. (eqv? 8.0 (+ 3.0 5.0))
  2416. (eqv? 5/6 (+ 1/2 1/3))
  2417. (eqv? 5.5 (+ 1/2 5.0))
  2418. (eqv? 3.25 (+ 3.0 1/4))))
  2419. (pass-if "signed zeroes"
  2420. (and (eqv? 0.0 (+ 0.0))
  2421. (eqv? -0.0 (+ -0.0))
  2422. (eqv? 0.0 (+ 0.0 0.0))
  2423. (eqv? 0.0 (+ 0.0 -0.0))
  2424. (eqv? 0.0 (+ -0.0 0.0))
  2425. (eqv? -0.0 (+ -0.0 -0.0))))
  2426. (pass-if "NaNs"
  2427. (and (real-nan? (+ +nan.0 +nan.0))
  2428. (real-nan? (+ 0 +nan.0))
  2429. (real-nan? (+ +nan.0 0))
  2430. (real-nan? (+ 1 +nan.0))
  2431. (real-nan? (+ +nan.0 1))
  2432. (real-nan? (+ -1 +nan.0))
  2433. (real-nan? (+ +nan.0 -1))
  2434. (real-nan? (+ -7/2 +nan.0))
  2435. (real-nan? (+ +nan.0 -7/2))
  2436. (real-nan? (+ 1e20 +nan.0))
  2437. (real-nan? (+ +nan.0 1e20))
  2438. (real-nan? (+ +inf.0 +nan.0))
  2439. (real-nan? (+ +nan.0 +inf.0))
  2440. (real-nan? (+ -inf.0 +nan.0))
  2441. (real-nan? (+ +nan.0 -inf.0))
  2442. (real-nan? (+ (* fixnum-max 2) +nan.0))
  2443. (real-nan? (+ +nan.0 (* fixnum-max 2)))))
  2444. (pass-if "infinities"
  2445. (and (eqv? +inf.0 (+ +inf.0 +inf.0))
  2446. (eqv? -inf.0 (+ -inf.0 -inf.0))
  2447. (real-nan? (+ +inf.0 -inf.0))
  2448. (real-nan? (+ -inf.0 +inf.0))))
  2449. ;; The maximum fixnum on a 32-bit architecture: 2^29 - 1.
  2450. (pass-if "fixnum + fixnum = bignum (32-bit)"
  2451. (eqv? 536870912 (+ 536870910 2)))
  2452. ;; The maximum fixnum on a 64-bit architecture: 2^61 - 1.
  2453. (pass-if "fixnum + fixnum = bignum (64-bit)"
  2454. (eqv? 2305843009213693952 (+ 2305843009213693950 2)))
  2455. (pass-if "bignum + fixnum = fixnum"
  2456. (eqv? 0 (+ (1+ most-positive-fixnum) most-negative-fixnum))))
  2457. ;;;
  2458. ;;; -
  2459. ;;;
  2460. (with-test-prefix/c&e "-"
  2461. (pass-if "double-negation of fixnum-min: ="
  2462. (= fixnum-min (- (- fixnum-min))))
  2463. (pass-if "double-negation of fixnum-min: eqv?"
  2464. (eqv? fixnum-min (- (- fixnum-min))))
  2465. (pass-if "double-negation of fixnum-min: equal?"
  2466. (equal? fixnum-min (- (- fixnum-min))))
  2467. (pass-if "binary double-negation of fixnum-min: ="
  2468. (= fixnum-min (- 0 (- 0 fixnum-min))))
  2469. (pass-if "binary double-negation of fixnum-min: eqv?"
  2470. (eqv? fixnum-min (- 0 (- 0 fixnum-min))))
  2471. (pass-if "binary double-negation of fixnum-min: equal?"
  2472. (equal? fixnum-min (- 0 (- 0 fixnum-min))))
  2473. (pass-if "signed zeroes"
  2474. (and (eqv? +0.0 (- -0.0))
  2475. (eqv? -0.0 (- +0.0))
  2476. (eqv? 0.0 (- 0.0 0.0))
  2477. (eqv? 0.0 (- 0.0 -0.0))
  2478. (eqv? 0.0 (- -0.0 -0.0))
  2479. (eqv? -0.0 (- -0.0 0.0))))
  2480. (pass-if "exactness propagation"
  2481. (and (eqv? 3 (- 8 5))
  2482. (eqv? 3.0 (- 8 5.0))
  2483. (eqv? 3.0 (- 8.0 5))
  2484. (eqv? 3.0 (- 8.0 5.0))
  2485. (eqv? -1/6 (- 1/3 1/2))
  2486. (eqv? -4.5 (- 1/2 5.0))
  2487. (eqv? 2.75 (- 3.0 1/4))))
  2488. (pass-if "infinities"
  2489. (and (eqv? +inf.0 (- +inf.0 -inf.0))
  2490. (eqv? -inf.0 (- -inf.0 +inf.0))
  2491. (real-nan? (- +inf.0 +inf.0))
  2492. (real-nan? (- -inf.0 -inf.0))))
  2493. (pass-if "NaNs"
  2494. (and (real-nan? (- +nan.0 +nan.0))
  2495. (real-nan? (- 0 +nan.0))
  2496. (real-nan? (- +nan.0 0))
  2497. (real-nan? (- 1 +nan.0))
  2498. (real-nan? (- +nan.0 1))
  2499. (real-nan? (- -1 +nan.0))
  2500. (real-nan? (- +nan.0 -1))
  2501. (real-nan? (- -7/2 +nan.0))
  2502. (real-nan? (- +nan.0 -7/2))
  2503. (real-nan? (- 1e20 +nan.0))
  2504. (real-nan? (- +nan.0 1e20))
  2505. (real-nan? (- +inf.0 +nan.0))
  2506. (real-nan? (- +nan.0 +inf.0))
  2507. (real-nan? (- -inf.0 +nan.0))
  2508. (real-nan? (- +nan.0 -inf.0))
  2509. (real-nan? (- (* fixnum-max 2) +nan.0))
  2510. (real-nan? (- +nan.0 (* fixnum-max 2)))))
  2511. (pass-if "(eqv? fixnum-min (- (- fixnum-min)))"
  2512. (eqv? fixnum-min (- (- fixnum-min))))
  2513. (pass-if "(eqv? fixnum-min (- 0 (- 0 fixnum-min)))"
  2514. (eqv? fixnum-min (- 0 (- 0 fixnum-min))))
  2515. (pass-if "(eqv? fixnum-num (apply - (list (apply - (list fixnum-min)))))"
  2516. (eqv? fixnum-min (apply - (list (apply - (list fixnum-min))))))
  2517. (pass-if "-inum - +bignum"
  2518. (= #x-100000000000000000000000000000001
  2519. (- -1 #x100000000000000000000000000000000)))
  2520. (pass-if "big - inum"
  2521. (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  2522. (- #x100000000000000000000000000000000 1)))
  2523. (pass-if "big - -inum"
  2524. (= #x100000000000000000000000000000001
  2525. (- #x100000000000000000000000000000000 -1)))
  2526. ;; The mininum fixnum on a 32-bit architecture: -2^29.
  2527. (pass-if "fixnum - fixnum = bignum (32-bit)"
  2528. (eqv? -536870912 (- -536870910 2)))
  2529. ;; The minimum fixnum on a 64-bit architecture: -2^61.
  2530. (pass-if "fixnum - fixnum = bignum (64-bit)"
  2531. (eqv? -2305843009213693952 (- -2305843009213693950 2)))
  2532. (pass-if "bignum - fixnum = fixnum"
  2533. (eqv? most-positive-fixnum (- (1+ most-positive-fixnum) 1))))
  2534. ;;;
  2535. ;;; *
  2536. ;;;
  2537. (with-test-prefix "*"
  2538. (with-test-prefix "double-negation of fixnum-min"
  2539. (pass-if (= fixnum-min (* -1 (* -1 fixnum-min))))
  2540. (pass-if (eqv? fixnum-min (* -1 (* -1 fixnum-min))))
  2541. (pass-if (equal? fixnum-min (* -1 (* -1 fixnum-min))))
  2542. (pass-if (= fixnum-min (* (* fixnum-min -1) -1)))
  2543. (pass-if (eqv? fixnum-min (* (* fixnum-min -1) -1)))
  2544. (pass-if (equal? fixnum-min (* (* fixnum-min -1) -1))))
  2545. (with-test-prefix "signed fixnum overflow"
  2546. (pass-if (eqv? (* 65536 65536) 4294967296))
  2547. (pass-if (eqv? (* -65536 65536) -4294967296))
  2548. (pass-if (eqv? (* 65536 -65536) -4294967296))
  2549. (pass-if (eqv? (* -65536 -65536) 4294967296))
  2550. (pass-if (eqv? (* 4294967296 4294967296) 18446744073709551616))
  2551. (pass-if (eqv? (* -4294967296 4294967296) -18446744073709551616))
  2552. (pass-if (eqv? (* 4294967296 -4294967296) -18446744073709551616))
  2553. (pass-if (eqv? (* -4294967296 -4294967296) 18446744073709551616)))
  2554. (with-test-prefix "signed zeroes"
  2555. (pass-if (eqv? +0.0 (* +0.0 +0.0)))
  2556. (pass-if (eqv? -0.0 (* -0.0 +0.0)))
  2557. (pass-if (eqv? +0.0 (* -0.0 -0.0)))
  2558. (pass-if (eqv? -0.0 (* +0.0 -0.0)))
  2559. (pass-if (eqv? +0.0+0.0i (* +i +0.0)))
  2560. (pass-if (eqv? +0.0-0.0i (* -i +0.0)))
  2561. (pass-if (eqv? -0.0-0.0i (* +i -0.0)))
  2562. (pass-if (eqv? -0.0+0.0i (* -i -0.0))))
  2563. (with-test-prefix "exactness propagation"
  2564. (pass-if (eqv? -0.0 (* 0 -1.0 )))
  2565. (pass-if (eqv? 0.0 (* 0 1.0 )))
  2566. (pass-if (eqv? -0.0 (* -1.0 0 )))
  2567. (pass-if (eqv? 0.0 (* 1.0 0 )))
  2568. (pass-if (eqv? 0 (* 0 1/2 )))
  2569. (pass-if (eqv? 0 (* 1/2 0 )))
  2570. (pass-if (eqv? 0.0+0.0i (* 0 1+i )))
  2571. (pass-if (eqv? 0.0+0.0i (* 1+i 0 )))
  2572. (pass-if (eqv? -1.0 (* 1 -1.0 )))
  2573. (pass-if (eqv? 1.0 (* 1 1.0 )))
  2574. (pass-if (eqv? -1.0 (* -1.0 1 )))
  2575. (pass-if (eqv? 1.0 (* 1.0 1 )))
  2576. (pass-if (eqv? 1/2 (* 1 1/2 )))
  2577. (pass-if (eqv? 1/2 (* 1/2 1 )))
  2578. (pass-if (eqv? 1+i (* 1 1+i )))
  2579. (pass-if (eqv? 1+i (* 1+i 1 ))))
  2580. (with-test-prefix "propagation of NaNs"
  2581. (pass-if (real-nan? (* +nan.0 +nan.0)))
  2582. (pass-if (real-nan? (* +nan.0 1 )))
  2583. (pass-if (real-nan? (* +nan.0 -1 )))
  2584. (pass-if (real-nan? (* +nan.0 -7/2 )))
  2585. (pass-if (real-nan? (* +nan.0 1e20 )))
  2586. (pass-if (real-nan? (* 1 +nan.0)))
  2587. (pass-if (real-nan? (* -1 +nan.0)))
  2588. (pass-if (real-nan? (* -7/2 +nan.0)))
  2589. (pass-if (real-nan? (* 1e20 +nan.0)))
  2590. (pass-if (real-nan? (* +inf.0 +nan.0)))
  2591. (pass-if (real-nan? (* +nan.0 +inf.0)))
  2592. (pass-if (real-nan? (* -inf.0 +nan.0)))
  2593. (pass-if (real-nan? (* +nan.0 -inf.0)))
  2594. (pass-if (real-nan? (* (* fixnum-max 2) +nan.0)))
  2595. (pass-if (real-nan? (* +nan.0 (* fixnum-max 2))))
  2596. (pass-if (real-nan? (* 0 +nan.0 )))
  2597. (pass-if (real-nan? (* +nan.0 0 )))
  2598. (pass-if (almost-real-nan? (* 0 +nan.0+i)))
  2599. (pass-if (almost-real-nan? (* +nan.0+i 0 )))
  2600. (pass-if (imaginary-nan? (* 0 +nan.0i )))
  2601. (pass-if (imaginary-nan? (* +nan.0i 0 )))
  2602. (pass-if (imaginary-nan? (* 0 1+nan.0i )))
  2603. (pass-if (imaginary-nan? (* 1+nan.0i 0 )))
  2604. (pass-if (complex-nan? (* 0 +nan.0+nan.0i )))
  2605. (pass-if (complex-nan? (* +nan.0+nan.0i 0 ))))
  2606. (with-test-prefix "infinities"
  2607. (pass-if (eqv? +inf.0 (* +inf.0 5 )))
  2608. (pass-if (eqv? -inf.0 (* +inf.0 -5 )))
  2609. (pass-if (eqv? +inf.0 (* +inf.0 73.1)))
  2610. (pass-if (eqv? -inf.0 (* +inf.0 -9.2)))
  2611. (pass-if (eqv? +inf.0 (* +inf.0 5/2)))
  2612. (pass-if (eqv? -inf.0 (* +inf.0 -5/2)))
  2613. (pass-if (eqv? -inf.0 (* -5 +inf.0)))
  2614. (pass-if (eqv? +inf.0 (* 73.1 +inf.0)))
  2615. (pass-if (eqv? -inf.0 (* -9.2 +inf.0)))
  2616. (pass-if (eqv? +inf.0 (* 5/2 +inf.0)))
  2617. (pass-if (eqv? -inf.0 (* -5/2 +inf.0)))
  2618. (pass-if (eqv? -inf.0 (* -inf.0 5 )))
  2619. (pass-if (eqv? +inf.0 (* -inf.0 -5 )))
  2620. (pass-if (eqv? -inf.0 (* -inf.0 73.1)))
  2621. (pass-if (eqv? +inf.0 (* -inf.0 -9.2)))
  2622. (pass-if (eqv? -inf.0 (* -inf.0 5/2)))
  2623. (pass-if (eqv? +inf.0 (* -inf.0 -5/2)))
  2624. (pass-if (eqv? +inf.0 (* -5 -inf.0)))
  2625. (pass-if (eqv? -inf.0 (* 73.1 -inf.0)))
  2626. (pass-if (eqv? +inf.0 (* -9.2 -inf.0)))
  2627. (pass-if (eqv? -inf.0 (* 5/2 -inf.0)))
  2628. (pass-if (eqv? +inf.0 (* -5/2 -inf.0)))
  2629. (pass-if (real-nan? (* 0.0 +inf.0)))
  2630. (pass-if (real-nan? (* -0.0 +inf.0)))
  2631. (pass-if (real-nan? (* +inf.0 0.0)))
  2632. (pass-if (real-nan? (* +inf.0 -0.0)))
  2633. (pass-if (real-nan? (* 0.0 -inf.0)))
  2634. (pass-if (real-nan? (* -0.0 -inf.0)))
  2635. (pass-if (real-nan? (* -inf.0 0.0)))
  2636. (pass-if (real-nan? (* -inf.0 -0.0)))
  2637. (pass-if (real-nan? (* 0 +inf.0 )))
  2638. (pass-if (real-nan? (* +inf.0 0 )))
  2639. (pass-if (real-nan? (* 0 -inf.0 )))
  2640. (pass-if (real-nan? (* -inf.0 0 )))
  2641. (pass-if (almost-real-nan? (* 0 +inf.0+i)))
  2642. (pass-if (almost-real-nan? (* +inf.0+i 0 )))
  2643. (pass-if (almost-real-nan? (* 0 -inf.0+i)))
  2644. (pass-if (almost-real-nan? (* -inf.0+i 0 )))
  2645. (pass-if (imaginary-nan? (* 0 +inf.0i )))
  2646. (pass-if (imaginary-nan? (* +inf.0i 0 )))
  2647. (pass-if (imaginary-nan? (* 0 1+inf.0i )))
  2648. (pass-if (imaginary-nan? (* 1+inf.0i 0 )))
  2649. (pass-if (imaginary-nan? (* 0 -inf.0i )))
  2650. (pass-if (imaginary-nan? (* -inf.0i 0 )))
  2651. (pass-if (imaginary-nan? (* 0 1-inf.0i )))
  2652. (pass-if (imaginary-nan? (* 1-inf.0i 0 )))
  2653. (pass-if (complex-nan? (* 0 +inf.0+inf.0i )))
  2654. (pass-if (complex-nan? (* +inf.0+inf.0i 0 )))
  2655. (pass-if (complex-nan? (* 0 +inf.0-inf.0i )))
  2656. (pass-if (complex-nan? (* -inf.0+inf.0i 0 ))))
  2657. (with-test-prefix "inum * bignum"
  2658. (pass-if "0 * 2^256 = 0"
  2659. (eqv? 0 (* 0 (ash 1 256)))))
  2660. (with-test-prefix "inum * flonum"
  2661. (pass-if "0 * 1.0 = 0.0"
  2662. (eqv? 0.0 (* 0 1.0))))
  2663. (with-test-prefix "inum * complex"
  2664. (pass-if "0 * 1+1i = 0.0+0.0i"
  2665. (eqv? 0.0+0.0i (* 0 1+1i))))
  2666. (with-test-prefix "inum * frac"
  2667. (pass-if "0 * 2/3 = 0"
  2668. (eqv? 0 (* 0 2/3))))
  2669. (with-test-prefix "bignum * inum"
  2670. (pass-if "2^256 * 0 = 0"
  2671. (eqv? 0 (* (ash 1 256) 0))))
  2672. (with-test-prefix "flonum * inum"
  2673. (pass-if "1.0 * 0 = 0.0"
  2674. (eqv? 0.0 (* 1.0 0))))
  2675. (with-test-prefix "complex * inum"
  2676. (pass-if "1+1i * 0 = 0.0+0.0i"
  2677. (eqv? 0.0+0.0i (* 1+1i 0))))
  2678. (pass-if "complex * bignum"
  2679. (let ((big (ash 1 90)))
  2680. (= (make-rectangular big big)
  2681. (* 1+1i big))))
  2682. (with-test-prefix "frac * inum"
  2683. (pass-if "2/3 * 0 = 0"
  2684. (eqv? 0 (* 2/3 0)))))
  2685. ;;;
  2686. ;;; /
  2687. ;;;
  2688. (with-test-prefix "/"
  2689. (with-test-prefix "double-negation of fixnum-min"
  2690. (pass-if (= fixnum-min (/ (/ fixnum-min -1) -1)))
  2691. (pass-if (eqv? fixnum-min (/ (/ fixnum-min -1) -1)))
  2692. (pass-if (equal? fixnum-min (/ (/ fixnum-min -1) -1))))
  2693. (pass-if "documented?"
  2694. (documented? /))
  2695. (with-test-prefix "division by zero"
  2696. (pass-if-exception "(/ 0)"
  2697. exception:numerical-overflow
  2698. (/ 0))
  2699. (pass-if "(/ 0.0)"
  2700. (= +inf.0 (/ 0.0)))
  2701. (pass-if-exception "(/ 1 0)"
  2702. exception:numerical-overflow
  2703. (/ 1 0))
  2704. (pass-if "(/ 1 0.0)"
  2705. (= +inf.0 (/ 1 0.0)))
  2706. (pass-if-exception "(/ bignum 0)"
  2707. exception:numerical-overflow
  2708. (/ (+ fixnum-max 1) 0))
  2709. (pass-if "(/ bignum 0.0)"
  2710. (= +inf.0 (/ (+ fixnum-max 1) 0.0)))
  2711. (pass-if-exception "(/ 1.0 0)"
  2712. exception:numerical-overflow
  2713. (/ 1.0 0))
  2714. (pass-if "(/ 1.0 0.0)"
  2715. (= +inf.0 (/ 1.0 0.0)))
  2716. (pass-if-exception "(/ +i 0)"
  2717. exception:numerical-overflow
  2718. (/ +i 0))
  2719. (pass-if "(/ +i 0.0)"
  2720. (= +inf.0 (imag-part (/ +i 0.0)))))
  2721. (with-test-prefix "1/complex"
  2722. (pass-if "0+1i"
  2723. (eqv? 0-1i (/ 0+1i)))
  2724. ;; in guile 1.6 through 1.6.7 this incorrectly resulted in nans
  2725. (pass-if "0-1i"
  2726. (eqv? 0+1i (/ 0-1i)))
  2727. (pass-if "1+1i"
  2728. (eqv? 0.5-0.5i (/ 1+1i)))
  2729. (pass-if "1-1i"
  2730. (eqv? 0.5+0.5i (/ 1-1i)))
  2731. (pass-if "-1+1i"
  2732. (eqv? -0.5-0.5i (/ -1+1i)))
  2733. (pass-if "-1-1i"
  2734. (eqv? -0.5+0.5i (/ -1-1i)))
  2735. (pass-if "(/ 3+4i)"
  2736. (= (/ 3+4i) 0.12-0.16i))
  2737. (pass-if "(/ 4+3i)"
  2738. (= (/ 4+3i) 0.16-0.12i))
  2739. (pass-if "(/ 1e200+1e200i)"
  2740. (= (/ 1e200+1e200i) 5.0e-201-5.0e-201i)))
  2741. (with-test-prefix "inum/complex"
  2742. (pass-if "(/ 25 3+4i)"
  2743. (= (/ 25 3+4i) 3.0-4.0i))
  2744. (pass-if "(/ 25 4+3i)"
  2745. (= (/ 25 4+3i) 4.0-3.0i)))
  2746. (with-test-prefix "complex/complex"
  2747. (pass-if "(/ 25+125i 3+4i)"
  2748. (= (/ 25+125i 3+4i) 23.0+11.0i))
  2749. (pass-if "(/ 25+125i 4+3i)"
  2750. (= (/ 25+125i 4+3i) 19.0+17.0i))))
  2751. ;;;
  2752. ;;; floor
  2753. ;;;
  2754. (with-test-prefix "floor"
  2755. (pass-if (= 1 (floor 1.75)))
  2756. (pass-if (= 1 (floor 1.5)))
  2757. (pass-if (= 1 (floor 1.25)))
  2758. (pass-if (= 0 (floor 0.75)))
  2759. (pass-if (= 0 (floor 0.5)))
  2760. (pass-if (= 0 (floor 0.0)))
  2761. (pass-if (= -1 (floor -0.5)))
  2762. (pass-if (= -2 (floor -1.25)))
  2763. (pass-if (= -2 (floor -1.5)))
  2764. (with-test-prefix "inum"
  2765. (pass-if "0"
  2766. (and (= 0 (floor 0))
  2767. (exact? (floor 0))))
  2768. (pass-if "1"
  2769. (and (= 1 (floor 1))
  2770. (exact? (floor 1))))
  2771. (pass-if "-1"
  2772. (and (= -1 (floor -1))
  2773. (exact? (floor -1)))))
  2774. (with-test-prefix "bignum"
  2775. (let ((x (1+ most-positive-fixnum)))
  2776. (pass-if "(1+ most-positive-fixnum)"
  2777. (and (= x (floor x))
  2778. (exact? (floor x)))))
  2779. (let ((x (1- most-negative-fixnum)))
  2780. (pass-if "(1- most-negative-fixnum)"
  2781. (and (= x (floor x))
  2782. (exact? (floor x))))))
  2783. (with-test-prefix "frac"
  2784. (define (=exact x y)
  2785. (and (= x y)
  2786. (exact? y)))
  2787. (pass-if (=exact -3 (floor -7/3)))
  2788. (pass-if (=exact -2 (floor -5/3)))
  2789. (pass-if (=exact -2 (floor -4/3)))
  2790. (pass-if (=exact -1 (floor -2/3)))
  2791. (pass-if (=exact -1 (floor -1/3)))
  2792. (pass-if (=exact 0 (floor 1/3)))
  2793. (pass-if (=exact 0 (floor 2/3)))
  2794. (pass-if (=exact 1 (floor 4/3)))
  2795. (pass-if (=exact 1 (floor 5/3)))
  2796. (pass-if (=exact 2 (floor 7/3)))
  2797. (pass-if (=exact -3 (floor -17/6)))
  2798. (pass-if (=exact -3 (floor -16/6)))
  2799. (pass-if (=exact -3 (floor -15/6)))
  2800. (pass-if (=exact -3 (floor -14/6)))
  2801. (pass-if (=exact -3 (floor -13/6)))
  2802. (pass-if (=exact -2 (floor -11/6)))
  2803. (pass-if (=exact -2 (floor -10/6)))
  2804. (pass-if (=exact -2 (floor -9/6)))
  2805. (pass-if (=exact -2 (floor -8/6)))
  2806. (pass-if (=exact -2 (floor -7/6)))
  2807. (pass-if (=exact -1 (floor -5/6)))
  2808. (pass-if (=exact -1 (floor -4/6)))
  2809. (pass-if (=exact -1 (floor -3/6)))
  2810. (pass-if (=exact -1 (floor -2/6)))
  2811. (pass-if (=exact -1 (floor -1/6)))
  2812. (pass-if (=exact 0 (floor 1/6)))
  2813. (pass-if (=exact 0 (floor 2/6)))
  2814. (pass-if (=exact 0 (floor 3/6)))
  2815. (pass-if (=exact 0 (floor 4/6)))
  2816. (pass-if (=exact 0 (floor 5/6)))
  2817. (pass-if (=exact 1 (floor 7/6)))
  2818. (pass-if (=exact 1 (floor 8/6)))
  2819. (pass-if (=exact 1 (floor 9/6)))
  2820. (pass-if (=exact 1 (floor 10/6)))
  2821. (pass-if (=exact 1 (floor 11/6)))
  2822. (pass-if (=exact 2 (floor 13/6)))
  2823. (pass-if (=exact 2 (floor 14/6)))
  2824. (pass-if (=exact 2 (floor 15/6)))
  2825. (pass-if (=exact 2 (floor 16/6)))
  2826. (pass-if (=exact 2 (floor 17/6))))
  2827. (with-test-prefix "real"
  2828. (pass-if "0.0"
  2829. (and (= 0.0 (floor 0.0))
  2830. (inexact? (floor 0.0))))
  2831. (pass-if "1.0"
  2832. (and (= 1.0 (floor 1.0))
  2833. (inexact? (floor 1.0))))
  2834. (pass-if "-1.0"
  2835. (and (= -1.0 (floor -1.0))
  2836. (inexact? (floor -1.0))))
  2837. (pass-if "-3.1"
  2838. (and (= -4.0 (floor -3.1))
  2839. (inexact? (floor -3.1))))
  2840. (pass-if "3.1"
  2841. (and (= 3.0 (floor 3.1))
  2842. (inexact? (floor 3.1))))
  2843. (pass-if "3.9"
  2844. (and (= 3.0 (floor 3.9))
  2845. (inexact? (floor 3.9))))
  2846. (pass-if "-3.9"
  2847. (and (= -4.0 (floor -3.9))
  2848. (inexact? (floor -3.9))))
  2849. (pass-if "1.5"
  2850. (and (= 1.0 (floor 1.5))
  2851. (inexact? (floor 1.5))))
  2852. (pass-if "2.5"
  2853. (and (= 2.0 (floor 2.5))
  2854. (inexact? (floor 2.5))))
  2855. (pass-if "3.5"
  2856. (and (= 3.0 (floor 3.5))
  2857. (inexact? (floor 3.5))))
  2858. (pass-if "-1.5"
  2859. (and (= -2.0 (floor -1.5))
  2860. (inexact? (floor -1.5))))
  2861. (pass-if "-2.5"
  2862. (and (= -3.0 (floor -2.5))
  2863. (inexact? (floor -2.5))))
  2864. (pass-if "-3.5"
  2865. (and (= -4.0 (floor -3.5))
  2866. (inexact? (floor -3.5))))))
  2867. ;;;
  2868. ;;; ceiling
  2869. ;;;
  2870. (with-test-prefix "ceiling"
  2871. (pass-if (= 2 (ceiling 1.75)))
  2872. (pass-if (= 2 (ceiling 1.5)))
  2873. (pass-if (= 2 (ceiling 1.25)))
  2874. (pass-if (= 1 (ceiling 0.75)))
  2875. (pass-if (= 1 (ceiling 0.5)))
  2876. (pass-if (= 0 (ceiling 0.0)))
  2877. (pass-if (= 0 (ceiling -0.5)))
  2878. (pass-if (= -1 (ceiling -1.25)))
  2879. (pass-if (= -1 (ceiling -1.5)))
  2880. (with-test-prefix "inum"
  2881. (pass-if "0"
  2882. (and (= 0 (ceiling 0))
  2883. (exact? (ceiling 0))))
  2884. (pass-if "1"
  2885. (and (= 1 (ceiling 1))
  2886. (exact? (ceiling 1))))
  2887. (pass-if "-1"
  2888. (and (= -1 (ceiling -1))
  2889. (exact? (ceiling -1)))))
  2890. (with-test-prefix "bignum"
  2891. (let ((x (1+ most-positive-fixnum)))
  2892. (pass-if "(1+ most-positive-fixnum)"
  2893. (and (= x (ceiling x))
  2894. (exact? (ceiling x)))))
  2895. (let ((x (1- most-negative-fixnum)))
  2896. (pass-if "(1- most-negative-fixnum)"
  2897. (and (= x (ceiling x))
  2898. (exact? (ceiling x))))))
  2899. (with-test-prefix "frac"
  2900. (define (=exact x y)
  2901. (and (= x y)
  2902. (exact? y)))
  2903. (pass-if (=exact -2 (ceiling -7/3)))
  2904. (pass-if (=exact -1 (ceiling -5/3)))
  2905. (pass-if (=exact -1 (ceiling -4/3)))
  2906. (pass-if (=exact 0 (ceiling -2/3)))
  2907. (pass-if (=exact 0 (ceiling -1/3)))
  2908. (pass-if (=exact 1 (ceiling 1/3)))
  2909. (pass-if (=exact 1 (ceiling 2/3)))
  2910. (pass-if (=exact 2 (ceiling 4/3)))
  2911. (pass-if (=exact 2 (ceiling 5/3)))
  2912. (pass-if (=exact 3 (ceiling 7/3)))
  2913. (pass-if (=exact -2 (ceiling -17/6)))
  2914. (pass-if (=exact -2 (ceiling -16/6)))
  2915. (pass-if (=exact -2 (ceiling -15/6)))
  2916. (pass-if (=exact -2 (ceiling -14/6)))
  2917. (pass-if (=exact -2 (ceiling -13/6)))
  2918. (pass-if (=exact -1 (ceiling -11/6)))
  2919. (pass-if (=exact -1 (ceiling -10/6)))
  2920. (pass-if (=exact -1 (ceiling -9/6)))
  2921. (pass-if (=exact -1 (ceiling -8/6)))
  2922. (pass-if (=exact -1 (ceiling -7/6)))
  2923. (pass-if (=exact 0 (ceiling -5/6)))
  2924. (pass-if (=exact 0 (ceiling -4/6)))
  2925. (pass-if (=exact 0 (ceiling -3/6)))
  2926. (pass-if (=exact 0 (ceiling -2/6)))
  2927. (pass-if (=exact 0 (ceiling -1/6)))
  2928. (pass-if (=exact 1 (ceiling 1/6)))
  2929. (pass-if (=exact 1 (ceiling 2/6)))
  2930. (pass-if (=exact 1 (ceiling 3/6)))
  2931. (pass-if (=exact 1 (ceiling 4/6)))
  2932. (pass-if (=exact 1 (ceiling 5/6)))
  2933. (pass-if (=exact 2 (ceiling 7/6)))
  2934. (pass-if (=exact 2 (ceiling 8/6)))
  2935. (pass-if (=exact 2 (ceiling 9/6)))
  2936. (pass-if (=exact 2 (ceiling 10/6)))
  2937. (pass-if (=exact 2 (ceiling 11/6)))
  2938. (pass-if (=exact 3 (ceiling 13/6)))
  2939. (pass-if (=exact 3 (ceiling 14/6)))
  2940. (pass-if (=exact 3 (ceiling 15/6)))
  2941. (pass-if (=exact 3 (ceiling 16/6)))
  2942. (pass-if (=exact 3 (ceiling 17/6))))
  2943. (with-test-prefix "real"
  2944. (pass-if "0.0"
  2945. (and (= 0.0 (ceiling 0.0))
  2946. (inexact? (ceiling 0.0))))
  2947. (pass-if "1.0"
  2948. (and (= 1.0 (ceiling 1.0))
  2949. (inexact? (ceiling 1.0))))
  2950. (pass-if "-1.0"
  2951. (and (= -1.0 (ceiling -1.0))
  2952. (inexact? (ceiling -1.0))))
  2953. (pass-if "-3.1"
  2954. (and (= -3.0 (ceiling -3.1))
  2955. (inexact? (ceiling -3.1))))
  2956. (pass-if "3.1"
  2957. (and (= 4.0 (ceiling 3.1))
  2958. (inexact? (ceiling 3.1))))
  2959. (pass-if "3.9"
  2960. (and (= 4.0 (ceiling 3.9))
  2961. (inexact? (ceiling 3.9))))
  2962. (pass-if "-3.9"
  2963. (and (= -3.0 (ceiling -3.9))
  2964. (inexact? (ceiling -3.9))))
  2965. (pass-if "1.5"
  2966. (and (= 2.0 (ceiling 1.5))
  2967. (inexact? (ceiling 1.5))))
  2968. (pass-if "2.5"
  2969. (and (= 3.0 (ceiling 2.5))
  2970. (inexact? (ceiling 2.5))))
  2971. (pass-if "3.5"
  2972. (and (= 4.0 (ceiling 3.5))
  2973. (inexact? (ceiling 3.5))))
  2974. (pass-if "-1.5"
  2975. (and (= -1.0 (ceiling -1.5))
  2976. (inexact? (ceiling -1.5))))
  2977. (pass-if "-2.5"
  2978. (and (= -2.0 (ceiling -2.5))
  2979. (inexact? (ceiling -2.5))))
  2980. (pass-if "-3.5"
  2981. (and (= -3.0 (ceiling -3.5))
  2982. (inexact? (ceiling -3.5))))))
  2983. ;;;
  2984. ;;; truncate
  2985. ;;;
  2986. (with-test-prefix "truncate"
  2987. (pass-if (= 1 (truncate 1.75)))
  2988. (pass-if (= 1 (truncate 1.5)))
  2989. (pass-if (= 1 (truncate 1.25)))
  2990. (pass-if (= 0 (truncate 0.75)))
  2991. (pass-if (= 0 (truncate 0.5)))
  2992. (pass-if (= 0 (truncate 0.0)))
  2993. (pass-if (= 0 (truncate -0.5)))
  2994. (pass-if (= -1 (truncate -1.25)))
  2995. (pass-if (= -1 (truncate -1.5)))
  2996. (with-test-prefix "inum"
  2997. (pass-if "0"
  2998. (and (= 0 (truncate 0))
  2999. (exact? (truncate 0))))
  3000. (pass-if "1"
  3001. (and (= 1 (truncate 1))
  3002. (exact? (truncate 1))))
  3003. (pass-if "-1"
  3004. (and (= -1 (truncate -1))
  3005. (exact? (truncate -1)))))
  3006. (with-test-prefix "bignum"
  3007. (let ((x (1+ most-positive-fixnum)))
  3008. (pass-if "(1+ most-positive-fixnum)"
  3009. (and (= x (truncate x))
  3010. (exact? (truncate x)))))
  3011. (let ((x (1- most-negative-fixnum)))
  3012. (pass-if "(1- most-negative-fixnum)"
  3013. (and (= x (truncate x))
  3014. (exact? (truncate x))))))
  3015. (with-test-prefix "frac"
  3016. (define (=exact x y)
  3017. (and (= x y)
  3018. (exact? y)))
  3019. (pass-if (=exact -2 (truncate -7/3)))
  3020. (pass-if (=exact -1 (truncate -5/3)))
  3021. (pass-if (=exact -1 (truncate -4/3)))
  3022. (pass-if (=exact 0 (truncate -2/3)))
  3023. (pass-if (=exact 0 (truncate -1/3)))
  3024. (pass-if (=exact 0 (truncate 1/3)))
  3025. (pass-if (=exact 0 (truncate 2/3)))
  3026. (pass-if (=exact 1 (truncate 4/3)))
  3027. (pass-if (=exact 1 (truncate 5/3)))
  3028. (pass-if (=exact 2 (truncate 7/3)))
  3029. (pass-if (=exact -2 (truncate -17/6)))
  3030. (pass-if (=exact -2 (truncate -16/6)))
  3031. (pass-if (=exact -2 (truncate -15/6)))
  3032. (pass-if (=exact -2 (truncate -14/6)))
  3033. (pass-if (=exact -2 (truncate -13/6)))
  3034. (pass-if (=exact -1 (truncate -11/6)))
  3035. (pass-if (=exact -1 (truncate -10/6)))
  3036. (pass-if (=exact -1 (truncate -9/6)))
  3037. (pass-if (=exact -1 (truncate -8/6)))
  3038. (pass-if (=exact -1 (truncate -7/6)))
  3039. (pass-if (=exact 0 (truncate -5/6)))
  3040. (pass-if (=exact 0 (truncate -4/6)))
  3041. (pass-if (=exact 0 (truncate -3/6)))
  3042. (pass-if (=exact 0 (truncate -2/6)))
  3043. (pass-if (=exact 0 (truncate -1/6)))
  3044. (pass-if (=exact 0 (truncate 1/6)))
  3045. (pass-if (=exact 0 (truncate 2/6)))
  3046. (pass-if (=exact 0 (truncate 3/6)))
  3047. (pass-if (=exact 0 (truncate 4/6)))
  3048. (pass-if (=exact 0 (truncate 5/6)))
  3049. (pass-if (=exact 1 (truncate 7/6)))
  3050. (pass-if (=exact 1 (truncate 8/6)))
  3051. (pass-if (=exact 1 (truncate 9/6)))
  3052. (pass-if (=exact 1 (truncate 10/6)))
  3053. (pass-if (=exact 1 (truncate 11/6)))
  3054. (pass-if (=exact 2 (truncate 13/6)))
  3055. (pass-if (=exact 2 (truncate 14/6)))
  3056. (pass-if (=exact 2 (truncate 15/6)))
  3057. (pass-if (=exact 2 (truncate 16/6)))
  3058. (pass-if (=exact 2 (truncate 17/6))))
  3059. (with-test-prefix "real"
  3060. (pass-if "0.0"
  3061. (and (= 0.0 (truncate 0.0))
  3062. (inexact? (truncate 0.0))))
  3063. (pass-if "1.0"
  3064. (and (= 1.0 (truncate 1.0))
  3065. (inexact? (truncate 1.0))))
  3066. (pass-if "-1.0"
  3067. (and (= -1.0 (truncate -1.0))
  3068. (inexact? (truncate -1.0))))
  3069. (pass-if "-3.1"
  3070. (and (= -3.0 (truncate -3.1))
  3071. (inexact? (truncate -3.1))))
  3072. (pass-if "3.1"
  3073. (and (= 3.0 (truncate 3.1))
  3074. (inexact? (truncate 3.1))))
  3075. (pass-if "3.9"
  3076. (and (= 3.0 (truncate 3.9))
  3077. (inexact? (truncate 3.9))))
  3078. (pass-if "-3.9"
  3079. (and (= -3.0 (truncate -3.9))
  3080. (inexact? (truncate -3.9))))
  3081. (pass-if "1.5"
  3082. (and (= 1.0 (truncate 1.5))
  3083. (inexact? (truncate 1.5))))
  3084. (pass-if "2.5"
  3085. (and (= 2.0 (truncate 2.5))
  3086. (inexact? (truncate 2.5))))
  3087. (pass-if "3.5"
  3088. (and (= 3.0 (truncate 3.5))
  3089. (inexact? (truncate 3.5))))
  3090. (pass-if "-1.5"
  3091. (and (= -1.0 (truncate -1.5))
  3092. (inexact? (truncate -1.5))))
  3093. (pass-if "-2.5"
  3094. (and (= -2.0 (truncate -2.5))
  3095. (inexact? (truncate -2.5))))
  3096. (pass-if "-3.5"
  3097. (and (= -3.0 (truncate -3.5))
  3098. (inexact? (truncate -3.5))))))
  3099. ;;;
  3100. ;;; round
  3101. ;;;
  3102. (with-test-prefix "round"
  3103. (pass-if (= 2 (round 1.75)))
  3104. (pass-if (= 2 (round 1.5)))
  3105. (pass-if (= 1 (round 1.25)))
  3106. (pass-if (= 1 (round 0.75)))
  3107. (pass-if (= 0 (round 0.5)))
  3108. (pass-if (= 0 (round 0.0)))
  3109. (pass-if (= 0 (round -0.5)))
  3110. (pass-if (= -1 (round -1.25)))
  3111. (pass-if (= -2 (round -1.5)))
  3112. (with-test-prefix "inum"
  3113. (pass-if "0"
  3114. (and (= 0 (round 0))
  3115. (exact? (round 0))))
  3116. (pass-if "1"
  3117. (and (= 1 (round 1))
  3118. (exact? (round 1))))
  3119. (pass-if "-1"
  3120. (and (= -1 (round -1))
  3121. (exact? (round -1)))))
  3122. (with-test-prefix "bignum"
  3123. (let ((x (1+ most-positive-fixnum)))
  3124. (pass-if "(1+ most-positive-fixnum)"
  3125. (and (= x (round x))
  3126. (exact? (round x)))))
  3127. (let ((x (1- most-negative-fixnum)))
  3128. (pass-if "(1- most-negative-fixnum)"
  3129. (and (= x (round x))
  3130. (exact? (round x))))))
  3131. (with-test-prefix "frac"
  3132. (define (=exact x y)
  3133. (and (= x y)
  3134. (exact? y)))
  3135. (pass-if (=exact -2 (round -7/3)))
  3136. (pass-if (=exact -2 (round -5/3)))
  3137. (pass-if (=exact -1 (round -4/3)))
  3138. (pass-if (=exact -1 (round -2/3)))
  3139. (pass-if (=exact 0 (round -1/3)))
  3140. (pass-if (=exact 0 (round 1/3)))
  3141. (pass-if (=exact 1 (round 2/3)))
  3142. (pass-if (=exact 1 (round 4/3)))
  3143. (pass-if (=exact 2 (round 5/3)))
  3144. (pass-if (=exact 2 (round 7/3)))
  3145. (pass-if (=exact -3 (round -17/6)))
  3146. (pass-if (=exact -3 (round -16/6)))
  3147. (pass-if (=exact -2 (round -15/6)))
  3148. (pass-if (=exact -2 (round -14/6)))
  3149. (pass-if (=exact -2 (round -13/6)))
  3150. (pass-if (=exact -2 (round -11/6)))
  3151. (pass-if (=exact -2 (round -10/6)))
  3152. (pass-if (=exact -2 (round -9/6)))
  3153. (pass-if (=exact -1 (round -8/6)))
  3154. (pass-if (=exact -1 (round -7/6)))
  3155. (pass-if (=exact -1 (round -5/6)))
  3156. (pass-if (=exact -1 (round -4/6)))
  3157. (pass-if (=exact 0 (round -3/6)))
  3158. (pass-if (=exact 0 (round -2/6)))
  3159. (pass-if (=exact 0 (round -1/6)))
  3160. (pass-if (=exact 0 (round 1/6)))
  3161. (pass-if (=exact 0 (round 2/6)))
  3162. (pass-if (=exact 0 (round 3/6)))
  3163. (pass-if (=exact 1 (round 4/6)))
  3164. (pass-if (=exact 1 (round 5/6)))
  3165. (pass-if (=exact 1 (round 7/6)))
  3166. (pass-if (=exact 1 (round 8/6)))
  3167. (pass-if (=exact 2 (round 9/6)))
  3168. (pass-if (=exact 2 (round 10/6)))
  3169. (pass-if (=exact 2 (round 11/6)))
  3170. (pass-if (=exact 2 (round 13/6)))
  3171. (pass-if (=exact 2 (round 14/6)))
  3172. (pass-if (=exact 2 (round 15/6)))
  3173. (pass-if (=exact 3 (round 16/6)))
  3174. (pass-if (=exact 3 (round 17/6))))
  3175. (with-test-prefix "real"
  3176. (pass-if "0.0"
  3177. (and (= 0.0 (round 0.0))
  3178. (inexact? (round 0.0))))
  3179. (pass-if "1.0"
  3180. (and (= 1.0 (round 1.0))
  3181. (inexact? (round 1.0))))
  3182. (pass-if "-1.0"
  3183. (and (= -1.0 (round -1.0))
  3184. (inexact? (round -1.0))))
  3185. (pass-if "-3.1"
  3186. (and (= -3.0 (round -3.1))
  3187. (inexact? (round -3.1))))
  3188. (pass-if "3.1"
  3189. (and (= 3.0 (round 3.1))
  3190. (inexact? (round 3.1))))
  3191. (pass-if "3.9"
  3192. (and (= 4.0 (round 3.9))
  3193. (inexact? (round 3.9))))
  3194. (pass-if "-3.9"
  3195. (and (= -4.0 (round -3.9))
  3196. (inexact? (round -3.9))))
  3197. (pass-if "1.5"
  3198. (and (= 2.0 (round 1.5))
  3199. (inexact? (round 1.5))))
  3200. (pass-if "2.5"
  3201. (and (= 2.0 (round 2.5))
  3202. (inexact? (round 2.5))))
  3203. (pass-if "3.5"
  3204. (and (= 4.0 (round 3.5))
  3205. (inexact? (round 3.5))))
  3206. (pass-if "-1.5"
  3207. (and (= -2.0 (round -1.5))
  3208. (inexact? (round -1.5))))
  3209. (pass-if "-2.5"
  3210. (and (= -2.0 (round -2.5))
  3211. (inexact? (round -2.5))))
  3212. (pass-if "-3.5"
  3213. (and (= -4.0 (round -3.5))
  3214. (inexact? (round -3.5))))
  3215. ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
  3216. ;; float with mantissa all ones) came out as 2^53 from `round' (except
  3217. ;; on i386 and m68k systems using the coprocessor and optimizing, where
  3218. ;; extra precision hid the problem)
  3219. (pass-if "2^53-1"
  3220. (let ((x (exact->inexact (1- (ash 1 53)))))
  3221. (and (= x (round x))
  3222. (inexact? (round x)))))
  3223. (pass-if "-(2^53-1)"
  3224. (let ((x (exact->inexact (- (1- (ash 1 53))))))
  3225. (and (= x (round x))
  3226. (inexact? (round x)))))))
  3227. ;;;
  3228. ;;; exact->inexact
  3229. ;;;
  3230. (with-test-prefix "exact->inexact"
  3231. ;; Test "(exact->inexact n)", expect "want".
  3232. (define (test name n want)
  3233. (with-test-prefix name
  3234. (pass-if-equal "pos" want (exact->inexact n))
  3235. (pass-if-equal "neg" (- want) (exact->inexact (- n)))))
  3236. ;; Test "(exact->inexact n)", expect "want".
  3237. ;; "i" is a index, for diagnostic purposes.
  3238. (define (try-i i n want)
  3239. (test (list i n want) n want))
  3240. (with-test-prefix "2^i, no round"
  3241. (do ((i 0 (1+ i))
  3242. (n 1 (* 2 n))
  3243. (want 1.0 (* 2.0 want)))
  3244. ((> i 100))
  3245. (try-i i n want)))
  3246. (with-test-prefix "2^i+1, no round"
  3247. (do ((i 1 (1+ i))
  3248. (n 3 (1- (* 2 n)))
  3249. (want 3.0 (- (* 2.0 want) 1.0)))
  3250. ((>= i dbl-mant-dig))
  3251. (try-i i n want)))
  3252. (with-test-prefix "(2^i+1)*2^100, no round"
  3253. (do ((i 1 (1+ i))
  3254. (n 3 (1- (* 2 n)))
  3255. (want 3.0 (- (* 2.0 want) 1.0)))
  3256. ((>= i dbl-mant-dig))
  3257. (try-i i (ash n 100) (ash-flo want 100))))
  3258. ;; bit pattern: 1111....11100.00
  3259. ;; <-mantdig-><-i->
  3260. ;;
  3261. (with-test-prefix "mantdig ones then zeros, no rounding"
  3262. (do ((i 0 (1+ i))
  3263. (n (- (ash 1 dbl-mant-dig) 1) (* 2 n))
  3264. (want (- (ash-flo 1.0 dbl-mant-dig) 1.0) (* 2.0 want)))
  3265. ((> i 100))
  3266. (try-i i n want)))
  3267. ;; bit pattern: 1111....111011..1
  3268. ;; <-mantdig-> <-i->
  3269. ;; This sort of value was incorrectly rounded upwards in Guile 1.6.4 when
  3270. ;; i >= 11 (that's when the total is 65 or more bits).
  3271. ;;
  3272. (with-test-prefix "mantdig ones then 011..11, round down"
  3273. (do ((i 0 (1+ i))
  3274. (n (- (ash 1 (+ 1 dbl-mant-dig)) 2) (+ 1 (* 2 n)))
  3275. (want (- (ash-flo 1.0 (+ 1 dbl-mant-dig)) 2.0) (* 2.0 want)))
  3276. ((> i 100))
  3277. (try-i i n want)))
  3278. ;; bit pattern: 1111....111100..001
  3279. ;; <-mantdig-> <--i->
  3280. ;;
  3281. (with-test-prefix "mantdig ones then 100..001, round up"
  3282. (do ((i 0 (1+ i))
  3283. (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
  3284. (want (ash-flo 1.0 (+ 2 dbl-mant-dig)) (* 2.0 want)))
  3285. ((> i 100))
  3286. (try-i i n want)))
  3287. ;; bit pattern: 1000....000100..001
  3288. ;; <-mantdig-> <--i->
  3289. ;;
  3290. (with-test-prefix "2^mantdig then 100..001, round up"
  3291. (do ((i 0 (1+ i))
  3292. (n (- (ash 1 (+ 2 dbl-mant-dig)) 1) (1- (* 2 n)))
  3293. (want (+ (ash-flo 1.0 (+ 2 dbl-mant-dig)) 4.0) (* 2.0 want)))
  3294. ((> i 100))
  3295. (try-i i n want)))
  3296. (pass-if "frac big/big"
  3297. (let ((big (ash 1 256)))
  3298. (= 1.0 (exact->inexact (/ (1+ big) big)))))
  3299. ;; In guile 1.8.0 this failed, giving back "nan" because it tried to
  3300. ;; convert the num and den to doubles, resulting in infs.
  3301. (pass-if "frac big/big, exceeding double"
  3302. (let ((big (ash 1 4096)))
  3303. (= 1.0 (exact->inexact (/ (1+ big) big)))))
  3304. ;; In guile 2.0.9, 'exact->inexact' guaranteed proper rounding when
  3305. ;; applied to non-negative fractions, but on 64-bit systems would
  3306. ;; sometimes double-round when applied to negative fractions,
  3307. ;; specifically when the numerator was a fixnum not exactly
  3308. ;; representable as a double.
  3309. (with-test-prefix "frac inum/inum, numerator not exactly representable as a double"
  3310. (let ((n (+ 1 (expt 2 dbl-mant-dig))))
  3311. (for-each (lambda (d)
  3312. (test (/ n d)
  3313. (/ n d)
  3314. (exact->inexact (/ n d))))
  3315. '(3 5 6 7 9 11 13 17 19 23 0.0 -0.0 +nan.0 +inf.0 -inf.0))))
  3316. (test "round up to odd"
  3317. ;; =====================================================
  3318. ;; 11111111111111111111111111111111111111111111111111000101 ->
  3319. ;; 11111111111111111111111111111111111111111111111111001000
  3320. (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000101)
  3321. (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000))
  3322. (test "round down to odd"
  3323. ;; =====================================================
  3324. ;; 11111111111111111111111111111111111111111111111111001011 ->
  3325. ;; 11111111111111111111111111111111111111111111111111001000
  3326. (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b001011)
  3327. (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b001000))
  3328. (test "round tie up to even"
  3329. ;; =====================================================
  3330. ;; 11111111111111111111111111111111111111111111111111011100 ->
  3331. ;; 11111111111111111111111111111111111111111111111111100000
  3332. (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b011100)
  3333. (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b100000))
  3334. (test "round tie down to even"
  3335. ;; =====================================================
  3336. ;; 11111111111111111111111111111111111111111111111111000100 ->
  3337. ;; 11111111111111111111111111111111111111111111111111000000
  3338. (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b000100)
  3339. (+ (expt 2.0 (+ dbl-mant-dig 3)) -64 #b000000))
  3340. (test "round tie up to next power of two"
  3341. ;; =====================================================
  3342. ;; 11111111111111111111111111111111111111111111111111111100 ->
  3343. ;; 100000000000000000000000000000000000000000000000000000000
  3344. (+ (expt 2 (+ dbl-mant-dig 3)) -64 #b111100)
  3345. (expt 2.0 (+ dbl-mant-dig 3)))
  3346. (test "miniscule value rounds to zero of appropriate sign"
  3347. (expt 17 (- dbl-min-exp dbl-mant-dig))
  3348. 0.0)
  3349. (test "smallest inexact"
  3350. (expt 2 (- dbl-min-exp dbl-mant-dig))
  3351. (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
  3352. (test "1/2 smallest inexact rounds down to zero"
  3353. (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3354. 0.0)
  3355. (test "just over 1/2 smallest inexact rounds up"
  3356. (+ (* 1/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3357. (expt 7 (- dbl-min-exp dbl-mant-dig)))
  3358. (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
  3359. (test "3/2 smallest inexact rounds up to twice smallest inexact"
  3360. (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3361. (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
  3362. (test "just under 3/2 smallest inexact rounds down"
  3363. (- (* 3/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3364. (expt 11 (- dbl-min-exp dbl-mant-dig)))
  3365. (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
  3366. (test "5/2 smallest inexact rounds down to twice smallest inexact"
  3367. (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3368. (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
  3369. (test "just over 5/2 smallest inexact rounds up"
  3370. (+ (* 5/2 (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3371. (expt 13 (- dbl-min-exp dbl-mant-dig)))
  3372. (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig))))
  3373. (test "one plus dbl-epsilon"
  3374. (+ 1 dbl-epsilon-exact)
  3375. (+ 1.0 dbl-epsilon))
  3376. (test "one plus 1/2 dbl-epsilon rounds down to 1.0"
  3377. (+ 1 (* 1/2 dbl-epsilon-exact))
  3378. 1.0)
  3379. (test "just over one plus 1/2 dbl-epsilon rounds up"
  3380. (+ 1
  3381. (* 1/2 dbl-epsilon-exact)
  3382. (expt 13 (- dbl-min-exp dbl-mant-dig)))
  3383. (+ 1.0 dbl-epsilon))
  3384. (test "one plus 3/2 dbl-epsilon rounds up"
  3385. (+ 1 (* 3/2 dbl-epsilon-exact))
  3386. (+ 1.0 (* 2.0 dbl-epsilon)))
  3387. (test "just under one plus 3/2 dbl-epsilon rounds down"
  3388. (+ 1
  3389. (* 3/2 dbl-epsilon-exact)
  3390. (- (expt 17 (- dbl-min-exp dbl-mant-dig))))
  3391. (+ 1.0 dbl-epsilon))
  3392. (test "one plus 5/2 dbl-epsilon rounds down"
  3393. (+ 1 (* 5/2 dbl-epsilon-exact))
  3394. (+ 1.0 (* 2.0 dbl-epsilon)))
  3395. (test "just over one plus 5/2 dbl-epsilon rounds up"
  3396. (+ 1
  3397. (* 5/2 dbl-epsilon-exact)
  3398. (expt 13 (- dbl-min-exp dbl-mant-dig)))
  3399. (+ 1.0 (* 3.0 dbl-epsilon)))
  3400. (test "largest finite inexact"
  3401. (* (- (expt 2 dbl-mant-dig) 1)
  3402. (expt 2 (- dbl-max-exp dbl-mant-dig)))
  3403. (* (- (expt 2.0 dbl-mant-dig) 1)
  3404. (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
  3405. (test "largest finite inexact plus 1/2 epsilon rounds up to infinity"
  3406. (* (+ (expt 2 dbl-mant-dig) -1 1/2)
  3407. (expt 2 (- dbl-max-exp dbl-mant-dig)))
  3408. (inf))
  3409. (test "largest finite inexact plus just under 1/2 epsilon rounds down"
  3410. (* (+ (expt 2 dbl-mant-dig) -1 1/2
  3411. (- (expt 13 (- dbl-min-exp dbl-mant-dig))))
  3412. (expt 2 (- dbl-max-exp dbl-mant-dig)))
  3413. (* (- (expt 2.0 dbl-mant-dig) 1)
  3414. (expt 2.0 (- dbl-max-exp dbl-mant-dig))))
  3415. (test "1/2 largest finite inexact"
  3416. (* (- (expt 2 dbl-mant-dig) 1)
  3417. (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
  3418. (* (- (expt 2.0 dbl-mant-dig) 1)
  3419. (expt 2.0 (- dbl-max-exp dbl-mant-dig 1))))
  3420. (test "1/2 largest finite inexact plus 1/2 epsilon rounds up to next power of two"
  3421. (* (+ (expt 2 dbl-mant-dig) -1 1/2)
  3422. (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
  3423. (expt 2.0 (- dbl-max-exp 1)))
  3424. (test "1/2 largest finite inexact plus just over 1/2 epsilon rounds up to next power of two"
  3425. (* (+ (expt 2 dbl-mant-dig) -1 1/2
  3426. (expt 13 (- dbl-min-exp dbl-mant-dig)))
  3427. (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
  3428. (expt 2.0 (- dbl-max-exp 1)))
  3429. (test "1/2 largest finite inexact plus just under 1/2 epsilon rounds down"
  3430. (* (+ (expt 2 dbl-mant-dig) -1 1/2
  3431. (- (expt 13 (- dbl-min-exp dbl-mant-dig))))
  3432. (expt 2 (- dbl-max-exp dbl-mant-dig 1)))
  3433. (* (- (expt 2.0 dbl-mant-dig) 1)
  3434. (expt 2.0 (- dbl-max-exp dbl-mant-dig 1))))
  3435. )
  3436. ;;;
  3437. ;;; expt
  3438. ;;;
  3439. (with-test-prefix "expt"
  3440. (pass-if (documented? expt))
  3441. ;;
  3442. ;; expt no longer requires its first argument to be a scheme number,
  3443. ;; for the sake of extensibility, and expt calls integer-expt for
  3444. ;; integer powers. To raise to a positive power, all that is required
  3445. ;; is that it can be multiplied using `*'. For negative powers we
  3446. ;; must also be able to find the reciprocal. If we try to raise #t to
  3447. ;; any power other than 0 or 1 it may throw an exception, depending on
  3448. ;; whether * has been defined for #t. However, when raising to the 0
  3449. ;; or 1 power, the first argument is not manipulated at all.
  3450. ;;
  3451. ;; (pass-if-exception "non-numeric base" exception:wrong-type-arg
  3452. ;; (expt #t 0))
  3453. ;;
  3454. (pass-if (eqv? 1 (expt 0 0)))
  3455. (pass-if (eqv? 1 (expt 0.0 0)))
  3456. (pass-if (eqv? 1.0 (expt 0 0.0)))
  3457. (pass-if (eqv? 1.0 (expt 0.0 0.0)))
  3458. (pass-if (real-nan? (expt 0 -1)))
  3459. (pass-if (real-nan? (expt 0 -1.0)))
  3460. (pass-if (real-nan? (expt 0.0 -1)))
  3461. (pass-if (real-nan? (expt 0.0 -1.0)))
  3462. (pass-if (eqv? 0 (expt 0 3)))
  3463. (pass-if (= 0 (expt 0 4.0)))
  3464. (pass-if (eqv? 0.0 (expt 0.0 5)))
  3465. (pass-if (eqv? 0.0 (expt 0.0 6.0)))
  3466. (pass-if (eqv? -2742638075.5 (expt -2742638075.5 1)))
  3467. (pass-if (eqv? (* -2742638075.5 -2742638075.5)
  3468. (expt -2742638075.5 2)))
  3469. (pass-if (eqv? 4.0 (expt -2.0 2.0)))
  3470. (pass-if (eqv? -1/8 (expt -2 -3)))
  3471. (pass-if (eqv? -0.125 (expt -2.0 -3)))
  3472. (pass-if (eqv? -0.125 (expt -2 -3.0)))
  3473. (pass-if (eqv? -0.125 (expt -2.0 -3.0)))
  3474. (pass-if (eqv? 0.25 (expt 2.0 -2.0)))
  3475. (pass-if (eqv? 32/243 (expt 2/3 5)))
  3476. (pass-if (eqv? 243/32 (expt 2/3 -5)))
  3477. (pass-if (eqv? 32 (expt 1/2 -5)))
  3478. (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (expt +12398i 2.0)))
  3479. (pass-if (eqv-loosely? +i (expt -1 0.5)))
  3480. (pass-if (eqv-loosely? +i (expt -1 1/2)))
  3481. (pass-if (eqv-loosely? 1.0+1.7320508075688i (expt -8 1/3)))
  3482. (pass-if (eqv? +inf.0 (expt 2 +inf.0)))
  3483. (pass-if (eqv? +inf.0 (expt 2.0 +inf.0)))
  3484. (pass-if (eqv? 0.0 (expt 2 -inf.0)))
  3485. (pass-if (eqv? 0.0 (expt 2.0 -inf.0))))
  3486. ;;;
  3487. ;;; sin
  3488. ;;;
  3489. (with-test-prefix "sin"
  3490. (pass-if (eqv? 0 (sin 0)))
  3491. (pass-if (eqv? 0.0 (sin 0.0)))
  3492. (pass-if (eqv-loosely? 1.0 (sin 1.57)))
  3493. (pass-if (eqv-loosely? +1.175i (sin +i)))
  3494. (pass-if (real-nan? (sin +nan.0)))
  3495. (pass-if (real-nan? (sin +inf.0)))
  3496. (pass-if (real-nan? (sin -inf.0))))
  3497. ;;;
  3498. ;;; cos
  3499. ;;;
  3500. (with-test-prefix "cos"
  3501. (pass-if (eqv? 1 (cos 0)))
  3502. (pass-if (eqv? 1.0 (cos 0.0)))
  3503. (pass-if (eqv-loosely? 0.0 (cos 1.57)))
  3504. (pass-if (eqv-loosely? 1.543 (cos +i)))
  3505. (pass-if (real-nan? (cos +nan.0)))
  3506. (pass-if (real-nan? (cos +inf.0)))
  3507. (pass-if (real-nan? (cos -inf.0))))
  3508. ;;;
  3509. ;;; tan
  3510. ;;;
  3511. (with-test-prefix "tan"
  3512. (pass-if (eqv? 0 (tan 0)))
  3513. (pass-if (eqv? 0.0 (tan 0.0)))
  3514. (pass-if (eqv-loosely? 1.0 (tan 0.785)))
  3515. (pass-if (eqv-loosely? +0.76i (tan +i)))
  3516. (pass-if (real-nan? (tan +nan.0)))
  3517. (pass-if (real-nan? (tan +inf.0)))
  3518. (pass-if (real-nan? (tan -inf.0))))
  3519. ;;;
  3520. ;;; asin
  3521. ;;;
  3522. (with-test-prefix "asin"
  3523. (pass-if (complex-nan? (asin +nan.0)))
  3524. (pass-if (eqv? 0 (asin 0)))
  3525. (pass-if (eqv? 0.0 (asin 0.0))))
  3526. ;;;
  3527. ;;; acos
  3528. ;;;
  3529. (with-test-prefix "acos"
  3530. (pass-if (complex-nan? (acos +nan.0)))
  3531. (pass-if (eqv? 0 (acos 1)))
  3532. (pass-if (eqv? 0.0 (acos 1.0))))
  3533. ;;;
  3534. ;;; atan
  3535. ;;;
  3536. ;;; FIXME: add tests for two-argument atan
  3537. ;;;
  3538. (with-test-prefix "atan"
  3539. (pass-if (real-nan? (atan +nan.0)))
  3540. (pass-if (eqv? 0 (atan 0)))
  3541. (pass-if (eqv? 0.0 (atan 0.0)))
  3542. (pass-if (eqv-loosely? 1.57 (atan +inf.0)))
  3543. (pass-if (eqv-loosely? -1.57 (atan -inf.0)))
  3544. (pass-if (eqv-loosely? -1.42+0.5i (atan -0.5+2.0i))))
  3545. ;;;
  3546. ;;; sinh
  3547. ;;;
  3548. (with-test-prefix "sinh"
  3549. (pass-if (= 0 (sinh 0)))
  3550. (pass-if (= 0.0 (sinh 0.0))))
  3551. ;;;
  3552. ;;; cosh
  3553. ;;;
  3554. (with-test-prefix "cosh"
  3555. (pass-if (= 1 (cosh 0)))
  3556. (pass-if (= 1.0 (cosh 0.0))))
  3557. ;;;
  3558. ;;; tanh
  3559. ;;;
  3560. (with-test-prefix "tanh"
  3561. (pass-if (= 0 (tanh 0)))
  3562. (pass-if (= 0.0 (tanh 0.0))))
  3563. ;;;
  3564. ;;; asinh
  3565. ;;;
  3566. (with-test-prefix "asinh"
  3567. (pass-if (= 0 (asinh 0)))
  3568. (pass-if (= 0.0 (asinh 0.0))))
  3569. ;;;
  3570. ;;; acosh
  3571. ;;;
  3572. (with-test-prefix "acosh"
  3573. (pass-if (= 0 (acosh 1)))
  3574. (pass-if (= 0.0 (acosh 1.0))))
  3575. ;;;
  3576. ;;; atanh
  3577. ;;;
  3578. (with-test-prefix "atanh"
  3579. (pass-if (= 0 (atanh 0)))
  3580. (pass-if (= 0.0 (atanh 0.0))))
  3581. ;;;
  3582. ;;; make-rectangular
  3583. ;;;
  3584. (with-test-prefix "make-rectangular"
  3585. (pass-if (real? (make-rectangular 5.0 0 )))
  3586. (pass-if (not (real? (make-rectangular 5.0 0.0))))
  3587. (pass-if (not (real? (make-rectangular 5.0 -0.0)))))
  3588. ;;;
  3589. ;;; make-polar
  3590. ;;;
  3591. (with-test-prefix "make-polar"
  3592. (define pi 3.14159265358979323846)
  3593. (define (almost= x y)
  3594. (> 0.01 (magnitude (- x y))))
  3595. (pass-if (real? (make-polar 0 1.0)))
  3596. (pass-if (real? (make-polar 5.0 0 )))
  3597. (pass-if (not (real? (make-polar 5.0 0.0))))
  3598. (pass-if (not (real? (make-polar 5.0 -0.0))))
  3599. (pass-if (eqv? 0 (make-polar 0 0)))
  3600. (pass-if (eqv? 0 (make-polar 0 123.456)))
  3601. (pass-if (eqv? 1 (make-polar 1 0)))
  3602. (pass-if (eqv? -1 (make-polar -1 0)))
  3603. (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
  3604. (pass-if (almost= -1 (make-polar 1 (* 1.0 pi))))
  3605. (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
  3606. (pass-if (almost= 1 (make-polar 1 (* 2.0 pi)))))
  3607. ;;;
  3608. ;;; real-part
  3609. ;;;
  3610. (with-test-prefix "real-part"
  3611. (pass-if (documented? real-part))
  3612. (pass-if (eqv? 5.0 (real-part 5.0)))
  3613. (pass-if (eqv? 0.0 (real-part +5.0i)))
  3614. (pass-if (eqv? 5 (real-part 5)))
  3615. (pass-if (eqv? 1/5 (real-part 1/5)))
  3616. (pass-if (eqv? (1+ fixnum-max) (real-part (1+ fixnum-max)))))
  3617. ;;;
  3618. ;;; imag-part
  3619. ;;;
  3620. (with-test-prefix "imag-part"
  3621. (pass-if (documented? imag-part))
  3622. (pass-if (eqv? 0 (imag-part 5.0)))
  3623. (pass-if (eqv? 5.0 (imag-part +5.0i)))
  3624. (pass-if (eqv? 0 (imag-part 5)))
  3625. (pass-if (eqv? 0 (imag-part 1/5)))
  3626. (pass-if (eqv? 0 (imag-part (1+ fixnum-max)))))
  3627. ;;;
  3628. ;;; magnitude
  3629. ;;;
  3630. (with-test-prefix "magnitude"
  3631. (pass-if (documented? magnitude))
  3632. (pass-if (= 0 (magnitude 0)))
  3633. (pass-if (= 1 (magnitude 1)))
  3634. (pass-if (= 1 (magnitude -1)))
  3635. (pass-if (= 1 (magnitude 0+i)))
  3636. (pass-if (= 1 (magnitude 0-i)))
  3637. (pass-if (= 5 (magnitude 3+4i)))
  3638. (pass-if (= 5 (magnitude 3-4i)))
  3639. (pass-if (= 5 (magnitude -3+4i)))
  3640. (pass-if (= 5 (magnitude -3-4i))))
  3641. ;;;
  3642. ;;; angle
  3643. ;;;
  3644. (with-test-prefix "angle"
  3645. (define pi 3.14159265358979323846)
  3646. (define (almost= x y)
  3647. (> 0.000001 (magnitude (- x y))))
  3648. (pass-if (documented? angle))
  3649. (pass-if "inum +ve" (= 0 (angle 1)))
  3650. (pass-if "inum -ve" (almost= pi (angle -1)))
  3651. (pass-if "bignum +ve" (= 0 (angle (1+ fixnum-max))))
  3652. (pass-if "bignum -ve" (almost= pi (angle (1- fixnum-min))))
  3653. (pass-if "flonum +ve" (= 0 (angle 1.5)))
  3654. (pass-if "flonum -ve" (almost= pi (angle -1.5)))
  3655. (pass-if "signed zero +ve" (= 0 (angle 0.0)))
  3656. (pass-if "signed zero -ve" (almost= pi (angle -0.0))))
  3657. ;;;
  3658. ;;; inexact->exact
  3659. ;;;
  3660. (with-test-prefix "inexact->exact"
  3661. ;; Test "(inexact->exact f)", expect "want".
  3662. (define (test name f want)
  3663. (with-test-prefix name
  3664. (pass-if-equal "pos" want (inexact->exact f))
  3665. (pass-if-equal "neg" (- want) (inexact->exact (- f)))))
  3666. (pass-if (documented? inexact->exact))
  3667. (pass-if-exception "+inf" exception:out-of-range
  3668. (inexact->exact +inf.0))
  3669. (pass-if-exception "-inf" exception:out-of-range
  3670. (inexact->exact -inf.0))
  3671. (pass-if-exception "nan" exception:out-of-range
  3672. (inexact->exact +nan.0))
  3673. (test "0.0" 0.0 0)
  3674. (test "small even integer" 72.0 72)
  3675. (test "small odd integer" 73.0 73)
  3676. (test "largest inexact odd integer"
  3677. (- (expt 2.0 dbl-mant-dig) 1)
  3678. (- (expt 2 dbl-mant-dig) 1))
  3679. (test "largest inexact odd integer - 1"
  3680. (- (expt 2.0 dbl-mant-dig) 2)
  3681. (- (expt 2 dbl-mant-dig) 2))
  3682. (test "largest inexact odd integer + 3"
  3683. (+ (expt 2.0 dbl-mant-dig) 2)
  3684. (+ (expt 2 dbl-mant-dig) 2))
  3685. (test "largest inexact odd integer * 2^48"
  3686. (* (expt 2.0 48) (- (expt 2.0 dbl-mant-dig) 1))
  3687. (* (expt 2 48) (- (expt 2 dbl-mant-dig) 1)))
  3688. (test "largest inexact odd integer / 2^48"
  3689. (* (expt 0.5 48) (- (expt 2.0 dbl-mant-dig) 1))
  3690. (* (expt 1/2 48) (- (expt 2 dbl-mant-dig) 1)))
  3691. (test "largest finite inexact"
  3692. (* (- (expt 2.0 dbl-mant-dig) 1)
  3693. (expt 2.0 (- dbl-max-exp dbl-mant-dig)))
  3694. (* (- (expt 2 dbl-mant-dig) 1)
  3695. (expt 2 (- dbl-max-exp dbl-mant-dig))))
  3696. (test "smallest inexact"
  3697. (expt 2.0 (- dbl-min-exp dbl-mant-dig))
  3698. (expt 2 (- dbl-min-exp dbl-mant-dig)))
  3699. (test "smallest inexact * 2"
  3700. (* 2.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
  3701. (* 2 (expt 2 (- dbl-min-exp dbl-mant-dig))))
  3702. (test "smallest inexact * 3"
  3703. (* 3.0 (expt 2.0 (- dbl-min-exp dbl-mant-dig)))
  3704. (* 3 (expt 2 (- dbl-min-exp dbl-mant-dig))))
  3705. (with-test-prefix "2.0**i to exact"
  3706. (do ((i 0 (1+ i))
  3707. (n 1 (* 2 n))
  3708. (f 1.0 (* 2.0 f)))
  3709. ((> i 100))
  3710. (test (list i n) f n))))
  3711. ;;;
  3712. ;;; integer-expt
  3713. ;;;
  3714. (with-test-prefix "integer-expt"
  3715. (pass-if (documented? integer-expt))
  3716. (pass-if-exception "2^+inf" exception:wrong-type-arg
  3717. (integer-expt 2 +inf.0))
  3718. (pass-if-exception "2^-inf" exception:wrong-type-arg
  3719. (integer-expt 2 -inf.0))
  3720. (pass-if-exception "2^nan" exception:wrong-type-arg
  3721. (integer-expt 2 +nan.0))
  3722. (pass-if (eqv? 1 (integer-expt 0 0)))
  3723. (pass-if (eqv? 1 (integer-expt 0.0 0)))
  3724. (pass-if (real-nan? (integer-expt 0 -1)))
  3725. (pass-if (real-nan? (integer-expt 0.0 -1)))
  3726. (pass-if (eqv? 0 (integer-expt 0 3)))
  3727. (pass-if (eqv? 0.0 (integer-expt 0.0 5)))
  3728. (pass-if (eqv? -2742638075.5 (integer-expt -2742638075.5 1)))
  3729. (pass-if (eqv? (* -2742638075.5 -2742638075.5)
  3730. (integer-expt -2742638075.5 2)))
  3731. (pass-if (eqv? 4.0 (integer-expt -2.0 2)))
  3732. (pass-if (eqv? -1/8 (integer-expt -2 -3)))
  3733. (pass-if (eqv? -0.125 (integer-expt -2.0 -3)))
  3734. (pass-if (eqv? 0.25 (integer-expt 2.0 -2)))
  3735. (pass-if (eqv? 32/243 (integer-expt 2/3 5)))
  3736. (pass-if (eqv? 243/32 (integer-expt 2/3 -5)))
  3737. (pass-if (eqv? 32 (integer-expt 1/2 -5)))
  3738. (pass-if (test-eqv? (* -1.0+0.0i 12398 12398) (integer-expt +12398.0i 2))))
  3739. ;;;
  3740. ;;; integer-length
  3741. ;;;
  3742. (with-test-prefix "integer-length"
  3743. (pass-if (documented? integer-length))
  3744. (with-test-prefix "-2^i, ...11100..00"
  3745. (do ((n -1 (ash n 1))
  3746. (i 0 (1+ i)))
  3747. ((> i 256))
  3748. (pass-if (list n "expect" i)
  3749. (= i (integer-length n)))))
  3750. (with-test-prefix "-2^i+1 ...11100..01"
  3751. (do ((n -3 (logxor 3 (ash n 1)))
  3752. (i 2 (1+ i)))
  3753. ((> i 256))
  3754. (pass-if n
  3755. (= i (integer-length n)))))
  3756. (with-test-prefix "-2^i-1 ...111011..11"
  3757. (do ((n -2 (1+ (ash n 1)))
  3758. (i 1 (1+ i)))
  3759. ((> i 256))
  3760. (pass-if n
  3761. (= i (integer-length n))))))
  3762. ;;;
  3763. ;;; log
  3764. ;;;
  3765. (with-test-prefix "log"
  3766. (pass-if (documented? log))
  3767. (pass-if-exception "no args" exception:wrong-num-args
  3768. (log))
  3769. (pass-if-exception "two args" exception:wrong-num-args
  3770. (log 123 456))
  3771. (pass-if-exception "(log 0)" exception:numerical-overflow
  3772. (log 0))
  3773. (pass-if (test-eqv? -inf.0 (log 0.0)))
  3774. (pass-if (test-eqv? +inf.0 (log +inf.0)))
  3775. (pass-if (test-eqv? -inf.0+3.14159265358979i (log -0.0)))
  3776. (pass-if (test-eqv? +inf.0+3.14159265358979i (log -inf.0)))
  3777. (pass-if (test-eqv? 0.0 (log 1 )))
  3778. (pass-if (test-eqv? 0.0 (log 1.0)))
  3779. (pass-if (test-eqv? 1.0 (log const-e)))
  3780. (pass-if (test-eqv? 2.0 (log const-e^2)))
  3781. (pass-if (test-eqv? -1.0 (log const-1/e)))
  3782. (pass-if (test-eqv? -1.0+3.14159265358979i (log (- const-1/e))))
  3783. (pass-if (test-eqv? 2.30258509299405 (log 10)))
  3784. (pass-if (test-eqv? 2.30258509299405+3.14159265358979i (log -10)))
  3785. (pass-if (test-eqv? 1.0+0.0i (log (+ const-e +0.0i))))
  3786. (pass-if (test-eqv? 1.0-0.0i (log (+ const-e -0.0i))))
  3787. (pass-if (eqv-loosely? 230258.509299405 (log (expt 10 100000))))
  3788. (pass-if (eqv-loosely? -230258.509299405 (log (expt 10 -100000))))
  3789. (pass-if (eqv-loosely? 230257.410687116 (log (/ (expt 10 100000) 3))))
  3790. (pass-if (eqv-loosely? 230258.509299405+3.14159265358979i
  3791. (log (- (expt 10 100000)))))
  3792. (pass-if (eqv-loosely? -230258.509299405+3.14159265358979i
  3793. (log (- (expt 10 -100000)))))
  3794. (pass-if (eqv-loosely? 230257.410687116+3.14159265358979i
  3795. (log (- (/ (expt 10 100000) 3)))))
  3796. (pass-if (test-eqv? 3.05493636349961e-151
  3797. (log (/ (1+ (expt 2 500)) (expt 2 500)))))
  3798. (pass-if (eqv-loosely? 1.0+1.57079i (log 0+2.71828i)))
  3799. (pass-if (eqv-loosely? 1.0-1.57079i (log 0-2.71828i)))
  3800. (pass-if (eqv-loosely? 0.0+3.14159i (log -1.0)))
  3801. (pass-if (eqv-loosely? 1.0+3.14159i (log -2.71828)))
  3802. (pass-if (eqv-loosely? 2.0+3.14159i (log (* -2.71828 2.71828)))))
  3803. ;;;
  3804. ;;; log10
  3805. ;;;
  3806. (with-test-prefix "log10"
  3807. (pass-if (documented? log10))
  3808. (pass-if-exception "no args" exception:wrong-num-args
  3809. (log10))
  3810. (pass-if-exception "two args" exception:wrong-num-args
  3811. (log10 123 456))
  3812. (pass-if-exception "(log10 0)" exception:numerical-overflow
  3813. (log10 0))
  3814. (pass-if (test-eqv? -inf.0 (log10 0.0)))
  3815. (pass-if (test-eqv? +inf.0 (log10 +inf.0)))
  3816. (pass-if (test-eqv? -inf.0+1.36437635384184i (log10 -0.0)))
  3817. (pass-if (test-eqv? +inf.0+1.36437635384184i (log10 -inf.0)))
  3818. (pass-if (test-eqv? 0.0 (log10 1 )))
  3819. (pass-if (test-eqv? 0.0 (log10 1.0)))
  3820. (pass-if (test-eqv? 1.0 (log10 10 )))
  3821. (pass-if (test-eqv? 1.0 (log10 10.0)))
  3822. (pass-if (test-eqv? 2.0 (log10 100.0)))
  3823. (pass-if (test-eqv? -1.0 (log10 0.1)))
  3824. (pass-if (test-eqv? -1.0+1.36437635384184i (log10 -0.1)))
  3825. (pass-if (test-eqv? 1.0+1.36437635384184i (log10 -10 )))
  3826. (pass-if (test-eqv? 1.0+0.0i (log10 10.0+0.0i)))
  3827. (pass-if (test-eqv? 1.0-0.0i (log10 10.0-0.0i)))
  3828. (pass-if (eqv-loosely? 100000.0 (log10 (expt 10 100000))))
  3829. (pass-if (eqv-loosely? -100000.0 (log10 (expt 10 -100000))))
  3830. (pass-if (eqv-loosely? 99999.5228787453 (log10 (/ (expt 10 100000) 3))))
  3831. (pass-if (eqv-loosely? 100000.0+1.36437635384184i
  3832. (log10 (- (expt 10 100000)))))
  3833. (pass-if (eqv-loosely? -100000.0+1.36437635384184i
  3834. (log10 (- (expt 10 -100000)))))
  3835. (pass-if (eqv-loosely? 99999.5228787453+1.36437635384184i
  3836. (log10 (- (/ (expt 10 100000) 3)))))
  3837. (pass-if (test-eqv? 1.32674200523347e-151
  3838. (log10 (/ (1+ (expt 2 500)) (expt 2 500)))))
  3839. (pass-if (eqv-loosely? 1.0+0.68218i (log10 0+10.0i)))
  3840. (pass-if (eqv-loosely? 1.0-0.68218i (log10 0-10.0i)))
  3841. (pass-if (eqv-loosely? 0.0+1.36437i (log10 -1)))
  3842. (pass-if (eqv-loosely? 1.0+1.36437i (log10 -10)))
  3843. (pass-if (eqv-loosely? 2.0+1.36437i (log10 -100))))
  3844. ;;;
  3845. ;;; logbit?
  3846. ;;;
  3847. (with-test-prefix "logbit?"
  3848. (pass-if (documented? logbit?))
  3849. (pass-if (eq? #f (logbit? 0 0)))
  3850. (pass-if (eq? #f (logbit? 1 0)))
  3851. (pass-if (eq? #f (logbit? 31 0)))
  3852. (pass-if (eq? #f (logbit? 32 0)))
  3853. (pass-if (eq? #f (logbit? 33 0)))
  3854. (pass-if (eq? #f (logbit? 63 0)))
  3855. (pass-if (eq? #f (logbit? 64 0)))
  3856. (pass-if (eq? #f (logbit? 65 0)))
  3857. ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
  3858. ;; around and return #t where it ought to be #f
  3859. (pass-if (eq? #t (logbit? 0 1)))
  3860. (pass-if (eq? #f (logbit? 1 1)))
  3861. (pass-if (eq? #f (logbit? 31 1)))
  3862. (pass-if (eq? #f (logbit? 32 1)))
  3863. (pass-if (eq? #f (logbit? 33 1)))
  3864. (pass-if (eq? #f (logbit? 63 1)))
  3865. (pass-if (eq? #f (logbit? 64 1)))
  3866. (pass-if (eq? #f (logbit? 65 1)))
  3867. (pass-if (eq? #f (logbit? 128 1)))
  3868. (pass-if (eq? #t (logbit? 0 -1)))
  3869. (pass-if (eq? #t (logbit? 1 -1)))
  3870. (pass-if (eq? #t (logbit? 31 -1)))
  3871. (pass-if (eq? #t (logbit? 32 -1)))
  3872. (pass-if (eq? #t (logbit? 33 -1)))
  3873. (pass-if (eq? #t (logbit? 63 -1)))
  3874. (pass-if (eq? #t (logbit? 64 -1)))
  3875. (pass-if (eq? #t (logbit? 65 -1))))
  3876. ;;;
  3877. ;;; logcount
  3878. ;;;
  3879. (with-test-prefix "logcount"
  3880. (pass-if (documented? logcount))
  3881. (with-test-prefix "-2^i, meaning ...11100..00"
  3882. (do ((n -1 (ash n 1))
  3883. (i 0 (1+ i)))
  3884. ((> i 256))
  3885. (pass-if n
  3886. (= i (logcount n)))))
  3887. (with-test-prefix "2^i"
  3888. (do ((n 1 (ash n 1))
  3889. (i 0 (1+ i)))
  3890. ((> i 256))
  3891. (pass-if n
  3892. (= 1 (logcount n)))))
  3893. (with-test-prefix "2^i-1"
  3894. (do ((n 0 (1+ (ash n 1)))
  3895. (i 0 (1+ i)))
  3896. ((> i 256))
  3897. (pass-if n
  3898. (= i (logcount n))))))
  3899. ;;;
  3900. ;;; logior
  3901. ;;;
  3902. (with-test-prefix "logior"
  3903. (pass-if (documented? logior))
  3904. (pass-if (eqv? -1 (logior (ash -1 1) 1)))
  3905. ;; check that bignum or bignum+inum args will reduce to an inum
  3906. (let ()
  3907. (define (test x y)
  3908. (pass-if (list x y '=> -1)
  3909. (eqv? -1 (logior x y)))
  3910. (pass-if (list y x '=> -1)
  3911. (eqv? -1 (logior y x))))
  3912. (test (ash -1 8) #xFF)
  3913. (test (ash -1 28) #x0FFFFFFF)
  3914. (test (ash -1 29) #x1FFFFFFF)
  3915. (test (ash -1 30) #x3FFFFFFF)
  3916. (test (ash -1 31) #x7FFFFFFF)
  3917. (test (ash -1 32) #xFFFFFFFF)
  3918. (test (ash -1 33) #x1FFFFFFFF)
  3919. (test (ash -1 60) #x0FFFFFFFFFFFFFFF)
  3920. (test (ash -1 61) #x1FFFFFFFFFFFFFFF)
  3921. (test (ash -1 62) #x3FFFFFFFFFFFFFFF)
  3922. (test (ash -1 63) #x7FFFFFFFFFFFFFFF)
  3923. (test (ash -1 64) #xFFFFFFFFFFFFFFFF)
  3924. (test (ash -1 65) #x1FFFFFFFFFFFFFFFF)
  3925. (test (ash -1 128) #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
  3926. ;;;
  3927. ;;; lognot
  3928. ;;;
  3929. (with-test-prefix "lognot"
  3930. (pass-if (documented? lognot))
  3931. (pass-if (= -1 (lognot 0)))
  3932. (pass-if (= 0 (lognot -1)))
  3933. (pass-if (= -2 (lognot 1)))
  3934. (pass-if (= 1 (lognot -2)))
  3935. (pass-if (= #x-100000000000000000000000000000000
  3936. (lognot #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)))
  3937. (pass-if (= #xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  3938. (lognot #x-100000000000000000000000000000000))))
  3939. ;;;
  3940. ;;; exact-integer-sqrt
  3941. ;;;
  3942. (with-test-prefix "exact-integer-sqrt"
  3943. (define (non-negative-exact-integer? k)
  3944. (and (integer? k) (exact? k) (>= k 0)))
  3945. (define (test k)
  3946. (pass-if k (let-values (((s r) (exact-integer-sqrt k)))
  3947. (and (non-negative-exact-integer? s)
  3948. (non-negative-exact-integer? r)
  3949. (= k (+ r (* s s)))
  3950. (< k (* (1+ s) (1+ s)))))))
  3951. (define (test-wrong-type-arg k)
  3952. (pass-if-exception k exception:wrong-type-arg
  3953. (let-values (((s r) (exact-integer-sqrt k)))
  3954. #t)))
  3955. (pass-if (documented? exact-integer-sqrt))
  3956. (pass-if-exception "no args" exception:wrong-num-args
  3957. (exact-integer-sqrt))
  3958. (pass-if-exception "two args" exception:wrong-num-args
  3959. (exact-integer-sqrt 123 456))
  3960. (test 0)
  3961. (test 1)
  3962. (test 9)
  3963. (test 10)
  3964. (test fixnum-max)
  3965. (test (1+ fixnum-max))
  3966. (test (* fixnum-max fixnum-max))
  3967. (test (+ 1 (* fixnum-max fixnum-max)))
  3968. (test (expt 10 100))
  3969. (test (+ 3 (expt 10 100)))
  3970. (test-wrong-type-arg -1)
  3971. (test-wrong-type-arg 1/9)
  3972. (test-wrong-type-arg fixnum-min)
  3973. (test-wrong-type-arg (1- fixnum-min))
  3974. (test-wrong-type-arg 1.0)
  3975. (test-wrong-type-arg 1.5)
  3976. (test-wrong-type-arg "foo")
  3977. (test-wrong-type-arg 'foo))
  3978. ;;;
  3979. ;;; sqrt
  3980. ;;;
  3981. (with-test-prefix "sqrt"
  3982. (pass-if (documented? sqrt))
  3983. (pass-if-exception "no args" exception:wrong-num-args
  3984. (sqrt))
  3985. (pass-if-exception "two args" exception:wrong-num-args
  3986. (sqrt 123 456))
  3987. (pass-if (eqv? 0 (sqrt 0)))
  3988. (pass-if (eqv? 1 (sqrt 1)))
  3989. (pass-if (eqv? 2 (sqrt 4)))
  3990. (pass-if (eqv? 3 (sqrt 9)))
  3991. (pass-if (eqv? 4 (sqrt 16)))
  3992. (pass-if (eqv? fixnum-max (sqrt (expt fixnum-max 2))))
  3993. (pass-if (eqv? (+ 1 fixnum-max) (sqrt (expt (+ 1 fixnum-max) 2))))
  3994. (pass-if (eqv? (expt 10 400) (sqrt (expt 10 800))))
  3995. (pass-if (eqv? (/ (expt 10 1000)
  3996. (expt 13 1000))
  3997. (sqrt (/ (expt 10 2000)
  3998. (expt 13 2000)))))
  3999. (with-test-prefix "exact sqrt"
  4000. (define (test root)
  4001. (pass-if (list root 'exact)
  4002. (eqv? root (sqrt (expt root 2))))
  4003. (pass-if (list root '*2)
  4004. (let ((r (sqrt (* 2 (expt root 2)))))
  4005. (and (inexact? r)
  4006. (eqv-loosely? (* (sqrt 2) root) r))))
  4007. (pass-if (list root '-1)
  4008. (let ((r (sqrt (- (expt root 2) 1))))
  4009. (and (inexact? r)
  4010. (eqv-loosely? root r))))
  4011. (pass-if (list root '+1)
  4012. (let ((r (sqrt (+ (expt root 2) 1))))
  4013. (and (inexact? r)
  4014. (eqv-loosely? root r))))
  4015. (pass-if (list root 'negative)
  4016. (eqv-loosely? (* +i root) (sqrt (- (expt root 2))))))
  4017. (test (exact-integer-sqrt (+ -1 (expt 2 (+ 2 dbl-mant-dig)))))
  4018. (test (exact-integer-sqrt (+ -1 (expt 2 (+ 1 dbl-mant-dig)))))
  4019. (test (exact-integer-sqrt (+ -1 (expt 2 (+ 0 dbl-mant-dig)))))
  4020. (test (exact-integer-sqrt (+ -1 (expt 2 (+ -1 dbl-mant-dig)))))
  4021. (test (exact-integer-sqrt (+ -1 (expt 2 (+ -2 dbl-mant-dig)))))
  4022. ;; largest finite inexact
  4023. (test (* (- (expt 2 dbl-mant-dig) 1)
  4024. (expt 2 (- dbl-max-exp dbl-mant-dig)))))
  4025. (pass-if-equal "smallest inexact"
  4026. (expt 2.0 (- dbl-min-exp dbl-mant-dig))
  4027. (sqrt (/ (+ -1 (expt 2 (* 2 (- dbl-mant-dig dbl-min-exp)))))))
  4028. (with-test-prefix "extreme ratios"
  4029. (define-syntax-rule (test want x)
  4030. (pass-if 'x
  4031. (let ((got (sqrt x)))
  4032. (and (inexact? got)
  4033. (test-eqv? 1.0 (/ want got))))))
  4034. (test 1.511139943175573e176 (/ (expt 3 2001) (expt 2 2001)))
  4035. (test 2.1370746022826034e176 (/ (expt 3 2001) (expt 2 2000)))
  4036. (test 8.724570529756128e175 (/ (expt 3 2000) (expt 2 2001)))
  4037. (test 6.6175207962444435e-177 (/ (expt 2 2001) (expt 3 2001)))
  4038. (test 1.1461882239239027e-176 (/ (expt 2 2001) (expt 3 2000)))
  4039. (test 4.679293829667447e-177 (/ (expt 2 2000) (expt 3 2001))))
  4040. (pass-if (eqv? (/ (expt 2 1000)
  4041. (expt 3 1000))
  4042. (sqrt (/ (expt 2 2000)
  4043. (expt 3 2000)))))
  4044. (pass-if (eqv? (/ (expt 3 1000)
  4045. (expt 2 1000))
  4046. (sqrt (/ (expt 3 2000)
  4047. (expt 2 2000)))))
  4048. (pass-if (eqv? +4i (sqrt -16)))
  4049. (pass-if (eqv-loosely? +1.0e150i (sqrt #e-1e300)))
  4050. (pass-if (eqv-loosely? +0.7071i (sqrt -1/2)))
  4051. (pass-if (eqv? 0.0 (sqrt 0.0)))
  4052. (pass-if (eqv? 1.0 (sqrt 1.0)))
  4053. (pass-if (eqv-loosely? 2.0 (sqrt 4.0)))
  4054. (pass-if (eqv-loosely? 31.62 (sqrt 1000.0)))
  4055. (pass-if (eqv? +1.0i (sqrt -1.0)))
  4056. (pass-if (eqv-loosely? +2.0i (sqrt -4.0)))
  4057. (pass-if (eqv-loosely? +31.62i (sqrt -1000.0)))
  4058. (pass-if "+i swings back to 45deg angle"
  4059. (eqv-loosely? +0.7071+0.7071i (sqrt +1.0i)))
  4060. ;; Note: glibc 2.3 csqrt() had a bug affecting this test case, so if it
  4061. ;; fails check whether that's the cause (there's a configure test to
  4062. ;; reject it, but when cross-compiling we assume the C library is ok).
  4063. (pass-if "-100i swings back to 45deg down"
  4064. (eqv-loosely? +7.071-7.071i (sqrt -100.0i))))
  4065. ;;;
  4066. ;;; Tests for number-theoretic division operators:
  4067. ;;;
  4068. ;;; euclidean/
  4069. ;;; euclidean-quotient
  4070. ;;; euclidean-remainder
  4071. ;;; floor/
  4072. ;;; floor-quotient
  4073. ;;; floor-remainder
  4074. ;;; ceiling/
  4075. ;;; ceiling-quotient
  4076. ;;; ceiling-remainder
  4077. ;;; truncate/
  4078. ;;; truncate-quotient
  4079. ;;; truncate-remainder
  4080. ;;; centered/
  4081. ;;; centered-quotient
  4082. ;;; centered-remainder
  4083. ;;; round/
  4084. ;;; round-quotient
  4085. ;;; round-remainder
  4086. ;;;
  4087. (with-test-prefix "Number-theoretic division"
  4088. ;; Tests that (lo <1 x <2 hi),
  4089. ;; but allowing for imprecision
  4090. ;; if x is inexact.
  4091. (define (test-within-range? lo <1 x <2 hi)
  4092. (if (exact? x)
  4093. (and (<1 lo x) (<2 x hi))
  4094. (let ((lo (- lo test-epsilon))
  4095. (hi (+ hi test-epsilon)))
  4096. (<= lo x hi))))
  4097. (define (valid-euclidean-answer? x y q r)
  4098. (and (eq? (exact? q)
  4099. (exact? r)
  4100. (and (exact? x) (exact? y)))
  4101. (test-eqv? r (- x (* q y)))
  4102. (if (and (finite? x) (finite? y))
  4103. (and (integer? q)
  4104. (test-within-range? 0 <= r < (abs y)))
  4105. (test-eqv? q (/ x y)))))
  4106. (define (valid-floor-answer? x y q r)
  4107. (and (eq? (exact? q)
  4108. (exact? r)
  4109. (and (exact? x) (exact? y)))
  4110. (test-eqv? r (- x (* q y)))
  4111. (if (and (finite? x) (finite? y))
  4112. (and (integer? q)
  4113. (if (> y 0)
  4114. (test-within-range? 0 <= r < y)
  4115. (test-within-range? y < r <= 0)))
  4116. (test-eqv? q (/ x y)))))
  4117. (define (valid-ceiling-answer? x y q r)
  4118. (and (eq? (exact? q)
  4119. (exact? r)
  4120. (and (exact? x) (exact? y)))
  4121. (test-eqv? r (- x (* q y)))
  4122. (if (and (finite? x) (finite? y))
  4123. (and (integer? q)
  4124. (if (> y 0)
  4125. (test-within-range? (- y) < r <= 0)
  4126. (test-within-range? 0 <= r < (- y))))
  4127. (test-eqv? q (/ x y)))))
  4128. (define (valid-truncate-answer? x y q r)
  4129. (and (eq? (exact? q)
  4130. (exact? r)
  4131. (and (exact? x) (exact? y)))
  4132. (test-eqv? r (- x (* q y)))
  4133. (if (and (finite? x) (finite? y))
  4134. (and (integer? q)
  4135. (if (> x 0)
  4136. (test-within-range? 0 <= r < (abs y))
  4137. (test-within-range? (- (abs y)) < r <= 0)))
  4138. (test-eqv? q (/ x y)))))
  4139. (define (valid-centered-answer? x y q r)
  4140. (and (eq? (exact? q)
  4141. (exact? r)
  4142. (and (exact? x) (exact? y)))
  4143. (test-eqv? r (- x (* q y)))
  4144. (if (and (finite? x) (finite? y))
  4145. (and (integer? q)
  4146. (test-within-range?
  4147. (* -1/2 (abs y)) <= r < (* +1/2 (abs y))))
  4148. (test-eqv? q (/ x y)))))
  4149. (define (valid-round-answer? x y q r)
  4150. (and (eq? (exact? q)
  4151. (exact? r)
  4152. (and (exact? x) (exact? y)))
  4153. (test-eqv? r (- x (* q y)))
  4154. (if (and (finite? x) (finite? y))
  4155. (and (integer? q)
  4156. (let ((ay/2 (/ (abs y) 2)))
  4157. (if (even? q)
  4158. (test-within-range? (- ay/2) <= r <= ay/2)
  4159. (test-within-range? (- ay/2) < r < ay/2))))
  4160. (test-eqv? q (/ x y)))))
  4161. (define (for lsts f) (apply for-each f lsts))
  4162. (define big (expt 10 (1+ (inexact->exact (ceiling (log10 fixnum-max))))))
  4163. (define (run-division-tests quo+rem quo rem valid-answer?)
  4164. (define (test n d)
  4165. (run-test (list n d) #t
  4166. (lambda ()
  4167. (let-values (((q r) (quo+rem n d)))
  4168. (and (test-eqv? q (quo n d))
  4169. (test-eqv? r (rem n d))
  4170. (valid-answer? n d q r))))))
  4171. (define (test+/- n d)
  4172. (test n d )
  4173. (test n (- d))
  4174. (cond ((not (zero? n))
  4175. (test (- n) d )
  4176. (test (- n) (- d)))))
  4177. (define (test-for-exception n d exception)
  4178. (let ((name (list n d)))
  4179. (pass-if-exception name exception (quo+rem n d))
  4180. (pass-if-exception name exception (quo n d))
  4181. (pass-if-exception name exception (rem n d))))
  4182. (run-test "documented?" #t
  4183. (lambda ()
  4184. (and (documented? quo+rem)
  4185. (documented? quo)
  4186. (documented? rem))))
  4187. (with-test-prefix "inum / inum"
  4188. (with-test-prefix "fixnum-min / -1"
  4189. (test fixnum-min -1))
  4190. (for '((1 2 5 10)) ;; denominators
  4191. (lambda (d)
  4192. (for '((0 1 2 5 10)) ;; multiples
  4193. (lambda (m)
  4194. (for '((-2 -1 0 1 2 3 4 5 7 10
  4195. 12 15 16 19 20)) ;; offsets
  4196. (lambda (b)
  4197. (test+/- (+ b (* m d))
  4198. d))))))))
  4199. (with-test-prefix "inum / big"
  4200. (with-test-prefix "fixnum-min / -fixnum-min"
  4201. (test fixnum-min (- fixnum-min)))
  4202. (with-test-prefix "fixnum-max / (2*fixnum-max)"
  4203. (test+/- fixnum-max (* 2 fixnum-max)))
  4204. (for `((0 1 2 10 ,(1- fixnum-max) ,fixnum-max))
  4205. (lambda (n)
  4206. (test n (1+ fixnum-max))
  4207. (test (- n) (1+ fixnum-max))
  4208. (test n (1- fixnum-min))
  4209. (test (- n) (1- fixnum-min)))))
  4210. (with-test-prefix "big / inum"
  4211. (with-test-prefix "-fixnum-min / fixnum-min"
  4212. (test (- fixnum-min) fixnum-min))
  4213. (for '((1 4 5 10)) ;; denominators
  4214. (lambda (d)
  4215. (for `((1 2 5 ,@(if (even? d)
  4216. '(1/2 3/2 5/2)
  4217. '()))) ;; multiples
  4218. (lambda (m)
  4219. (for '((-2 -1 0 1 2)) ;; offsets
  4220. (lambda (b)
  4221. (test+/- (+ b (* m d big))
  4222. d))))))))
  4223. (with-test-prefix "big / big"
  4224. (for `((,big ,(1+ big))) ;; denominators
  4225. (lambda (d)
  4226. (for `((1 2 5 ,@(if (even? d)
  4227. '(1/2 3/2 5/2)
  4228. '()))) ;; multiples
  4229. (lambda (m)
  4230. (for '((-2 -1 0 1 2)) ;; offsets
  4231. (lambda (b)
  4232. (test+/- (+ b (* m d))
  4233. d))))))))
  4234. (with-test-prefix "inexact"
  4235. (for '((0.5 1.5 2.25 5.75)) ;; denominators
  4236. (lambda (d)
  4237. (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples
  4238. (lambda (m)
  4239. (for '((-2 -1 0 1 2)) ;; offsets
  4240. (lambda (b)
  4241. (test+/- (+ b (* m d))
  4242. d))))))))
  4243. (with-test-prefix "fractions"
  4244. (for '((1/10 16/3 10/7)) ;; denominators
  4245. (lambda (d)
  4246. (for '((0 1 2 5 1/2 3/2 5/2)) ;; multiples
  4247. (lambda (m)
  4248. (for '((-2/9 -1/11 0 1/3 2/3)) ;; offsets
  4249. (lambda (b)
  4250. (test+/- (+ b (* m d))
  4251. d))))))))
  4252. (with-test-prefix "mixed types"
  4253. (for `((10 ,big 12.0 10/7 +inf.0 -inf.0 +nan.0)) ;; denominators
  4254. (lambda (d)
  4255. (for `((25 ,(* 3/2 big) 130.0 15/7
  4256. 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators
  4257. (lambda (n)
  4258. (test+/- n d))))))
  4259. (with-test-prefix "divide by zero"
  4260. (for `((0 0.0 -0.0)) ;; denominators
  4261. (lambda (d)
  4262. (for `((15 ,(* 3/2 big) 18.0 33/7
  4263. 0 0.0 -0.0 +inf.0 -inf.0 +nan.0)) ;; numerators
  4264. (lambda (n)
  4265. (test-for-exception
  4266. n d exception:numerical-overflow)))))))
  4267. (with-test-prefix "euclidean/"
  4268. (run-division-tests euclidean/
  4269. euclidean-quotient
  4270. euclidean-remainder
  4271. valid-euclidean-answer?))
  4272. (with-test-prefix "floor/"
  4273. (run-division-tests floor/
  4274. floor-quotient
  4275. floor-remainder
  4276. valid-floor-answer?))
  4277. (with-test-prefix "ceiling/"
  4278. (run-division-tests ceiling/
  4279. ceiling-quotient
  4280. ceiling-remainder
  4281. valid-ceiling-answer?))
  4282. (with-test-prefix "truncate/"
  4283. (run-division-tests truncate/
  4284. truncate-quotient
  4285. truncate-remainder
  4286. valid-truncate-answer?))
  4287. (with-test-prefix "centered/"
  4288. (run-division-tests centered/
  4289. centered-quotient
  4290. centered-remainder
  4291. valid-centered-answer?))
  4292. (with-test-prefix "round/"
  4293. (run-division-tests round/
  4294. round-quotient
  4295. round-remainder
  4296. valid-round-answer?)))
  4297. ;;;
  4298. ;;; ash
  4299. ;;; round-ash
  4300. ;;;
  4301. (let ()
  4302. (define (test-ash-variant name ash-variant round-variant rounded?)
  4303. (with-test-prefix name
  4304. (define (test n count)
  4305. (pass-if (list n count)
  4306. (eqv? (ash-variant n count)
  4307. (round-variant (* n (expt 2 count))))))
  4308. (pass-if "documented?"
  4309. (documented? ash-variant))
  4310. (for-each (lambda (n)
  4311. (for-each (lambda (count) (test n count))
  4312. `(-1000
  4313. ,(* 2 (- fixnum-bit))
  4314. ,(- -3 fixnum-bit)
  4315. ,(- -2 fixnum-bit)
  4316. ,(- -1 fixnum-bit)
  4317. ,(- fixnum-bit)
  4318. ,(- (- fixnum-bit 1))
  4319. -3 -2 -1 0 1 2 3
  4320. ,(- fixnum-bit 1)
  4321. ,fixnum-bit
  4322. ,(+ fixnum-bit 1)
  4323. ,(+ fixnum-bit 2)
  4324. ,(+ fixnum-bit 3)
  4325. ,(* 2 fixnum-bit)
  4326. 1000)))
  4327. (list 0 1 3 23 -1 -3 -23
  4328. fixnum-max
  4329. (1+ fixnum-max)
  4330. (1- fixnum-max)
  4331. (* fixnum-max 4)
  4332. (quotient fixnum-max 4)
  4333. fixnum-min
  4334. (1+ fixnum-min)
  4335. (1- fixnum-min)
  4336. (* fixnum-min 4)
  4337. (quotient fixnum-min 4)))
  4338. (do ((count -2 (1- count))
  4339. (vals '(1 3 5 7 9 11)
  4340. (map (lambda (n) (* 2 n)) vals)))
  4341. ((> (car vals) (* 2 fixnum-max)) 'done)
  4342. (for-each (lambda (n)
  4343. (test n count)
  4344. (test (- n) count))
  4345. vals))
  4346. ;; Test rounding
  4347. (for-each (lambda (base)
  4348. (for-each (lambda (offset) (test (+ base offset) -3))
  4349. '(#b11001 #b11100 #b11101 #b10001 #b10100 #b10101)))
  4350. (list 0 64 -64 (* 64 fixnum-max) (* 64 fixnum-min)))
  4351. ;; Right shift by LONG_MIN, typically (ash -1 63) and (ash -1 31)
  4352. ;; depending on the word size, where negating the shift count
  4353. ;; overflows. See <https://bugs.gnu.org/21901>.
  4354. (pass-if-equal "Right shift of positive integer by (ash -1 63) bits"
  4355. 0
  4356. (ash-variant 123 (ash -1 63)))
  4357. (pass-if-equal "Right shift of negative integer by (ash -1 63) bits"
  4358. (if rounded? 0 -1)
  4359. (ash-variant -123 (ash -1 63)))
  4360. (pass-if-equal "Right shift of positive integer by (ash -1 31) bits"
  4361. 0
  4362. (ash-variant 123 (ash -1 31)))
  4363. (pass-if-equal "Right shift of negative integer by (ash -1 31) bits"
  4364. (if rounded? 0 -1)
  4365. (ash-variant -123 (ash -1 31)))
  4366. ;; Huge shift counts
  4367. (pass-if-equal "Huge left shift of 0"
  4368. 0
  4369. (ash-variant 0 (expt 2 1000)))
  4370. (pass-if-equal "Huge right shift of 0"
  4371. 0
  4372. (ash-variant 0 (- (expt 2 1000))))
  4373. (pass-if-equal "Huge right shift of positive integer"
  4374. 0
  4375. (ash-variant 123 (- (expt 2 1000))))
  4376. (pass-if-equal "Huge right shift of negative integer"
  4377. (if rounded? 0 -1)
  4378. (ash-variant -123 (- (expt 2 1000))))
  4379. (pass-if-equal "Huge right shift of -1"
  4380. (if rounded? 0 -1)
  4381. (ash-variant -1 (- (expt 2 1000))))
  4382. (pass-if-exception "Huge left shift of non-zero => numerical overflow"
  4383. exception:numerical-overflow
  4384. (ash-variant 123 (expt 2 1000)))))
  4385. (test-ash-variant 'ash ash floor #f)
  4386. (test-ash-variant 'round-ash round-ash round #t))
  4387. ;;;
  4388. ;;; regressions
  4389. ;;;
  4390. (with-test-prefix/c&e "bug in unboxing f64 in 2.1.6"
  4391. (pass-if "= real and complex"
  4392. (= 1.0 (make-rectangular 1.0 0.0))))