math.pp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {-------------------------------------------------------------------------
  12. Using functions from AMath/DAMath libraries, which are covered by the
  13. following license:
  14. (C) Copyright 2009-2013 Wolfgang Ehrhardt
  15. This software is provided 'as-is', without any express or implied warranty.
  16. In no event will the authors be held liable for any damages arising from
  17. the use of this software.
  18. Permission is granted to anyone to use this software for any purpose,
  19. including commercial applications, and to alter it and redistribute it
  20. freely, subject to the following restrictions:
  21. 1. The origin of this software must not be misrepresented; you must not
  22. claim that you wrote the original software. If you use this software in
  23. a product, an acknowledgment in the product documentation would be
  24. appreciated but is not required.
  25. 2. Altered source versions must be plainly marked as such, and must not be
  26. misrepresented as being the original software.
  27. 3. This notice may not be removed or altered from any source distribution.
  28. ----------------------------------------------------------------------------}
  29. {
  30. This unit is an equivalent to the Delphi Math unit
  31. (with some improvements)
  32. What's to do:
  33. o some statistical functions
  34. o optimizations
  35. }
  36. {$MODE objfpc}
  37. {$inline on }
  38. {$GOTO on}
  39. {$IFNDEF FPC_DOTTEDUNITS}
  40. unit Math;
  41. {$ENDIF FPC_DOTTEDUNITS}
  42. interface
  43. {$ifndef FPUNONE}
  44. {$IFDEF FPC_DOTTEDUNITS}
  45. uses
  46. System.SysUtils, System.Types;
  47. {$ELSE FPC_DOTTEDUNITS}
  48. uses
  49. sysutils, types;
  50. {$ENDIF FPC_DOTTEDUNITS}
  51. {$IFDEF FPDOC_MATH}
  52. Type
  53. Float = MaxFloatType;
  54. Const
  55. MinFloat = 0;
  56. MaxFloat = 0;
  57. {$ENDIF}
  58. { Ranges of the IEEE floating point types, including denormals }
  59. {$ifdef FPC_HAS_TYPE_SINGLE}
  60. const
  61. { values according to
  62. https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Single-precision_examples
  63. }
  64. MinSingle = 1.1754943508e-38;
  65. MaxSingle = 3.4028234664e+38;
  66. {$endif FPC_HAS_TYPE_SINGLE}
  67. {$ifdef FPC_HAS_TYPE_DOUBLE}
  68. const
  69. { values according to
  70. https://en.wikipedia.org/wiki/Double-precision_floating-point_format#Double-precision_examples
  71. }
  72. MinDouble = 2.2250738585072014e-308;
  73. MaxDouble = 1.7976931348623157e+308;
  74. {$endif FPC_HAS_TYPE_DOUBLE}
  75. {$ifdef FPC_HAS_TYPE_EXTENDED}
  76. const
  77. MinExtended = 3.4e-4932;
  78. MaxExtended = 1.1e+4932;
  79. {$endif FPC_HAS_TYPE_EXTENDED}
  80. {$ifdef FPC_HAS_TYPE_COMP}
  81. const
  82. MinComp = -9.223372036854775807e+18;
  83. MaxComp = 9.223372036854775807e+18;
  84. {$endif FPC_HAS_TYPE_COMP}
  85. { the original delphi functions use extended as argument, }
  86. { but I would prefer double, because 8 bytes is a very }
  87. { natural size for the processor }
  88. { WARNING : changing float type will }
  89. { break all assembler code PM }
  90. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  91. type
  92. Float = Float128;
  93. const
  94. MinFloat = MinFloat128;
  95. MaxFloat = MaxFloat128;
  96. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  97. type
  98. Float = extended;
  99. const
  100. MinFloat = MinExtended;
  101. MaxFloat = MaxExtended;
  102. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  103. type
  104. Float = double;
  105. const
  106. MinFloat = MinDouble;
  107. MaxFloat = MaxDouble;
  108. {$elseif defined(FPC_HAS_TYPE_SINGLE)}
  109. type
  110. Float = single;
  111. const
  112. MinFloat = MinSingle;
  113. MaxFloat = MaxSingle;
  114. {$else}
  115. {$fatal At least one floating point type must be supported}
  116. {$endif}
  117. type
  118. PFloat = ^Float;
  119. PInteger = ObjPas.PInteger;
  120. TPaymentTime = (ptEndOfPeriod,ptStartOfPeriod);
  121. EInvalidArgument = class(ematherror);
  122. {$IFDEF FPC_DOTTEDUNITS}
  123. TValueRelationship = System.Types.TValueRelationship;
  124. {$ELSE FPC_DOTTEDUNITS}
  125. TValueRelationship = types.TValueRelationship;
  126. {$ENDIF FPC_DOTTEDUNITS}
  127. const
  128. {$IFDEF FPC_DOTTEDUNITS}
  129. EqualsValue = System.Types.EqualsValue;
  130. LessThanValue = System.Types.LessThanValue;
  131. GreaterThanValue = System.Types.GreaterThanValue;
  132. {$ELSE FPC_DOTTEDUNITS}
  133. EqualsValue = types.EqualsValue;
  134. LessThanValue = types.LessThanValue;
  135. GreaterThanValue = types.GreaterThanValue;
  136. {$ENDIF FPC_DOTTEDUNITS}
  137. {$push}
  138. {$R-}
  139. {$Q-}
  140. NaN = 0.0/0.0;
  141. Infinity = 1.0/0.0;
  142. NegInfinity = -1.0/0.0;
  143. {$pop}
  144. {$IFDEF FPDOC_MATH}
  145. // This must be after the above defines.
  146. {$DEFINE FPC_HAS_TYPE_SINGLE}
  147. {$DEFINE FPC_HAS_TYPE_DOUBLE}
  148. {$DEFINE FPC_HAS_TYPE_EXTENDED}
  149. {$DEFINE FPC_HAS_TYPE_COMP}
  150. {$ENDIF}
  151. { Min/max determination }
  152. function MinIntValue(const Data: array of Integer): Integer;
  153. function MaxIntValue(const Data: array of Integer): Integer;
  154. { Extra, not present in Delphi, but used frequently }
  155. function Min(a, b: Integer): Integer;inline; overload;
  156. function Max(a, b: Integer): Integer;inline; overload;
  157. { this causes more trouble than it solves
  158. function Min(a, b: Cardinal): Cardinal; overload;
  159. function Max(a, b: Cardinal): Cardinal; overload;
  160. }
  161. function Min(a, b: Int64): Int64;inline; overload;
  162. function Max(a, b: Int64): Int64;inline; overload;
  163. function Min(a, b: QWord): QWord;inline; overload;
  164. function Max(a, b: QWord): QWord;inline; overload;
  165. {$ifdef FPC_HAS_TYPE_SINGLE}
  166. function Min(a, b: Single): Single;inline; overload;
  167. function Max(a, b: Single): Single;inline; overload;
  168. {$endif FPC_HAS_TYPE_SINGLE}
  169. {$ifdef FPC_HAS_TYPE_DOUBLE}
  170. function Min(a, b: Double): Double;inline; overload;
  171. function Max(a, b: Double): Double;inline; overload;
  172. {$endif FPC_HAS_TYPE_DOUBLE}
  173. {$ifdef FPC_HAS_TYPE_EXTENDED}
  174. function Min(a, b: Extended): Extended;inline; overload;
  175. function Max(a, b: Extended): Extended;inline; overload;
  176. {$endif FPC_HAS_TYPE_EXTENDED}
  177. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline; overload;
  178. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline; overload;
  179. {$ifdef FPC_HAS_TYPE_DOUBLE}
  180. function InRange(const AValue, AMin, AMax: Double): Boolean;inline; overload;
  181. {$endif FPC_HAS_TYPE_DOUBLE}
  182. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline; overload;
  183. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline; overload;
  184. {$ifdef FPC_HAS_TYPE_DOUBLE}
  185. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline; overload;
  186. {$endif FPC_HAS_TYPE_DOUBLE}
  187. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  188. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  189. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  190. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  191. { Floating point modulo}
  192. {$ifdef FPC_HAS_TYPE_SINGLE}
  193. function FMod(const a, b: Single): Single;inline;overload;
  194. {$endif FPC_HAS_TYPE_SINGLE}
  195. {$ifdef FPC_HAS_TYPE_DOUBLE}
  196. function FMod(const a, b: Double): Double;inline;overload;
  197. {$endif FPC_HAS_TYPE_DOUBLE}
  198. {$ifdef FPC_HAS_TYPE_EXTENDED}
  199. function FMod(const a, b: Extended): Extended;inline;overload;
  200. {$endif FPC_HAS_TYPE_EXTENDED}
  201. operator mod(const a,b:float) c:float;inline;
  202. // Sign functions
  203. Type
  204. TValueSign = -1..1;
  205. const
  206. NegativeValue = Low(TValueSign);
  207. ZeroValue = 0;
  208. PositiveValue = High(TValueSign);
  209. function Sign(const AValue: Integer): TValueSign;inline; overload;
  210. function Sign(const AValue: Int64): TValueSign;inline; overload;
  211. {$ifdef FPC_HAS_TYPE_SINGLE}
  212. function Sign(const AValue: Single): TValueSign;inline; overload;
  213. {$endif}
  214. function Sign(const AValue: Double): TValueSign;inline; overload;
  215. {$ifdef FPC_HAS_TYPE_EXTENDED}
  216. function Sign(const AValue: Extended): TValueSign;inline; overload;
  217. {$endif}
  218. function IsZero(const A: Single; Epsilon: Single): Boolean; overload;
  219. function IsZero(const A: Single): Boolean;inline; overload;
  220. {$ifdef FPC_HAS_TYPE_DOUBLE}
  221. function IsZero(const A: Double; Epsilon: Double): Boolean; overload;
  222. function IsZero(const A: Double): Boolean;inline; overload;
  223. {$endif FPC_HAS_TYPE_DOUBLE}
  224. {$ifdef FPC_HAS_TYPE_EXTENDED}
  225. function IsZero(const A: Extended; Epsilon: Extended): Boolean; overload;
  226. function IsZero(const A: Extended): Boolean;inline; overload;
  227. {$endif FPC_HAS_TYPE_EXTENDED}
  228. function IsNan(const d : Single): Boolean; overload;
  229. {$ifdef FPC_HAS_TYPE_DOUBLE}
  230. function IsNan(const d : Double): Boolean; overload;
  231. {$endif FPC_HAS_TYPE_DOUBLE}
  232. {$ifdef FPC_HAS_TYPE_EXTENDED}
  233. function IsNan(const d : Extended): Boolean; overload;
  234. {$endif FPC_HAS_TYPE_EXTENDED}
  235. function IsInfinite(const d : Single): Boolean; overload;
  236. {$ifdef FPC_HAS_TYPE_DOUBLE}
  237. function IsInfinite(const d : Double): Boolean; overload;
  238. {$endif FPC_HAS_TYPE_DOUBLE}
  239. {$ifdef FPC_HAS_TYPE_EXTENDED}
  240. function IsInfinite(const d : Extended): Boolean; overload;
  241. {$endif FPC_HAS_TYPE_EXTENDED}
  242. {$ifdef FPC_HAS_TYPE_EXTENDED}
  243. function SameValue(const A, B: Extended): Boolean;inline; overload;
  244. {$endif}
  245. {$ifdef FPC_HAS_TYPE_DOUBLE}
  246. function SameValue(const A, B: Double): Boolean;inline; overload;
  247. {$endif}
  248. function SameValue(const A, B: Single): Boolean;inline; overload;
  249. {$ifdef FPC_HAS_TYPE_EXTENDED}
  250. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean; overload;
  251. {$endif}
  252. {$ifdef FPC_HAS_TYPE_DOUBLE}
  253. function SameValue(const A, B: Double; Epsilon: Double): Boolean; overload;
  254. {$endif}
  255. function SameValue(const A, B: Single; Epsilon: Single): Boolean; overload;
  256. type
  257. TRoundToRange = -37..37;
  258. {$ifdef FPC_HAS_TYPE_DOUBLE}
  259. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  260. {$endif}
  261. {$ifdef FPC_HAS_TYPE_EXTENDED}
  262. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  263. {$endif}
  264. {$ifdef FPC_HAS_TYPE_SINGLE}
  265. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  266. {$endif}
  267. {$ifdef FPC_HAS_TYPE_SINGLE}
  268. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  269. {$endif}
  270. {$ifdef FPC_HAS_TYPE_DOUBLE}
  271. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  272. {$endif}
  273. {$ifdef FPC_HAS_TYPE_EXTENDED}
  274. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  275. {$endif}
  276. { angle conversion }
  277. function DegToRad(deg : float) : float;inline;
  278. function RadToDeg(rad : float) : float;inline;
  279. function GradToRad(grad : float) : float;inline;
  280. function RadToGrad(rad : float) : float;inline;
  281. function DegToGrad(deg : float) : float;inline;
  282. function GradToDeg(grad : float) : float;inline;
  283. {$ifdef FPC_HAS_TYPE_SINGLE}
  284. function CycleToDeg(const Cycles: Single): Single;
  285. {$ENDIF}
  286. {$ifdef FPC_HAS_TYPE_DOUBLE}
  287. function CycleToDeg(const Cycles: Double): Double;
  288. {$ENDIF}
  289. {$ifdef FPC_HAS_TYPE_EXTENDED}
  290. function CycleToDeg(const Cycles: Extended): Extended;
  291. {$ENDIF}
  292. {$ifdef FPC_HAS_TYPE_SINGLE}
  293. function DegToCycle(const Degrees: Single): Single;
  294. {$ENDIF}
  295. {$ifdef FPC_HAS_TYPE_DOUBLE}
  296. function DegToCycle(const Degrees: Double): Double;
  297. {$ENDIF}
  298. {$ifdef FPC_HAS_TYPE_EXTENDED}
  299. function DegToCycle(const Degrees: Extended): Extended;
  300. {$ENDIF}
  301. {$ifdef FPC_HAS_TYPE_SINGLE}
  302. function CycleToGrad(const Cycles: Single): Single;
  303. {$ENDIF}
  304. {$ifdef FPC_HAS_TYPE_DOUBLE}
  305. function CycleToGrad(const Cycles: Double): Double;
  306. {$ENDIF}
  307. {$ifdef FPC_HAS_TYPE_EXTENDED}
  308. function CycleToGrad(const Cycles: Extended): Extended;
  309. {$ENDIF}
  310. {$ifdef FPC_HAS_TYPE_SINGLE}
  311. function GradToCycle(const Grads: Single): Single;
  312. {$ENDIF}
  313. {$ifdef FPC_HAS_TYPE_DOUBLE}
  314. function GradToCycle(const Grads: Double): Double;
  315. {$ENDIF}
  316. {$ifdef FPC_HAS_TYPE_EXTENDED}
  317. function GradToCycle(const Grads: Extended): Extended;
  318. {$ENDIF}
  319. {$ifdef FPC_HAS_TYPE_SINGLE}
  320. function CycleToRad(const Cycles: Single): Single;
  321. {$ENDIF}
  322. {$ifdef FPC_HAS_TYPE_DOUBLE}
  323. function CycleToRad(const Cycles: Double): Double;
  324. {$ENDIF}
  325. {$ifdef FPC_HAS_TYPE_EXTENDED}
  326. function CycleToRad(const Cycles: Extended): Extended;
  327. {$ENDIF}
  328. {$ifdef FPC_HAS_TYPE_SINGLE}
  329. function RadToCycle(const Rads: Single): Single;
  330. {$ENDIF}
  331. {$ifdef FPC_HAS_TYPE_DOUBLE}
  332. function RadToCycle(const Rads: Double): Double;
  333. {$ENDIF}
  334. {$ifdef FPC_HAS_TYPE_EXTENDED}
  335. function RadToCycle(const Rads: Extended): Extended;
  336. {$ENDIF}
  337. {$ifdef FPC_HAS_TYPE_SINGLE}
  338. Function DegNormalize(deg : single) : single; inline;
  339. {$ENDIF}
  340. {$ifdef FPC_HAS_TYPE_DOUBLE}
  341. Function DegNormalize(deg : double) : double; inline;
  342. {$ENDIF}
  343. {$ifdef FPC_HAS_TYPE_EXTENDED}
  344. Function DegNormalize(deg : extended) : extended; inline;
  345. {$ENDIF}
  346. { trigoniometric functions }
  347. function Tan(x : float) : float;
  348. function Cotan(x : float) : float;
  349. function Cot(x : float) : float; inline;
  350. {$ifdef FPC_HAS_TYPE_SINGLE}
  351. procedure SinCos(theta : single;out sinus,cosinus : single);
  352. {$endif}
  353. {$ifdef FPC_HAS_TYPE_DOUBLE}
  354. procedure SinCos(theta : double;out sinus,cosinus : double);
  355. {$endif}
  356. {$ifdef FPC_HAS_TYPE_EXTENDED}
  357. procedure SinCos(theta : extended;out sinus,cosinus : extended);
  358. {$endif}
  359. function Secant(x : float) : float; inline;
  360. function Cosecant(x : float) : float; inline;
  361. function Sec(x : float) : float; inline;
  362. function Csc(x : float) : float; inline;
  363. { inverse functions }
  364. {$ifdef FPC_HAS_TYPE_SINGLE}
  365. function ArcCos(x : Single) : Single;
  366. {$ENDIF}
  367. {$ifdef FPC_HAS_TYPE_DOUBLE}
  368. function ArcCos(x : Double) : Double;
  369. {$ENDIF}
  370. {$ifdef FPC_HAS_TYPE_EXTENDED}
  371. function ArcCos(x : Extended) : Extended;
  372. {$ENDIF}
  373. {$ifdef FPC_HAS_TYPE_SINGLE}
  374. function ArcSin(x : Single) : Single;
  375. {$ENDIF}
  376. {$ifdef FPC_HAS_TYPE_DOUBLE}
  377. function ArcSin(x : Double) : Double;
  378. {$ENDIF}
  379. {$ifdef FPC_HAS_TYPE_EXTENDED}
  380. function ArcSin(x : Extended) : Extended;
  381. {$ENDIF}
  382. { calculates arctan(y/x) and returns an angle in the correct quadrant }
  383. function ArcTan2(y,x : float) : float;
  384. { hyperbolic functions }
  385. {$ifdef FPC_HAS_TYPE_SINGLE}
  386. function cosh(x : Single) : Single;
  387. {$ENDIF}
  388. {$ifdef FPC_HAS_TYPE_DOUBLE}
  389. function cosh(x : Double) : Double;
  390. {$ENDIF}
  391. {$ifdef FPC_HAS_TYPE_EXTENDED}
  392. function cosh(x : Extended) : Extended;
  393. {$ENDIF}
  394. {$ifdef FPC_HAS_TYPE_SINGLE}
  395. function sinh(x : Single) : Single;
  396. {$ENDIF}
  397. {$ifdef FPC_HAS_TYPE_DOUBLE}
  398. function sinh(x : Double) : Double;
  399. {$ENDIF}
  400. {$ifdef FPC_HAS_TYPE_EXTENDED}
  401. function sinh(x : Extended) : Extended;
  402. {$ENDIF}
  403. {$ifdef FPC_HAS_TYPE_SINGLE}
  404. function tanh(x : Single) : Single;
  405. {$ENDIF}
  406. {$ifdef FPC_HAS_TYPE_DOUBLE}
  407. function tanh(x : Double) : Double;
  408. {$ENDIF}
  409. {$ifdef FPC_HAS_TYPE_EXTENDED}
  410. function tanh(x : Extended) : Extended;
  411. {$ENDIF}
  412. {$ifdef FPC_HAS_TYPE_SINGLE}
  413. function SecH(const X: Single): Single;
  414. {$ENDIF}
  415. {$ifdef FPC_HAS_TYPE_DOUBLE}
  416. function SecH(const X: Double): Double;
  417. {$ENDIF}
  418. {$ifdef FPC_HAS_TYPE_EXTENDED}
  419. function SecH(const X: Extended): Extended;
  420. {$ENDIF}
  421. {$ifdef FPC_HAS_TYPE_SINGLE}
  422. function CscH(const X: Single): Single;
  423. {$ENDIF}
  424. {$ifdef FPC_HAS_TYPE_DOUBLE}
  425. function CscH(const X: Double): Double;
  426. {$ENDIF}
  427. {$ifdef FPC_HAS_TYPE_EXTENDED}
  428. function CscH(const X: Extended): Extended;
  429. {$ENDIF}
  430. {$ifdef FPC_HAS_TYPE_SINGLE}
  431. function CotH(const X: Single): Single;
  432. {$ENDIF}
  433. {$ifdef FPC_HAS_TYPE_DOUBLE}
  434. function CotH(const X: Double): Double;
  435. {$ENDIF}
  436. {$ifdef FPC_HAS_TYPE_EXTENDED}
  437. function CotH(const X: Extended): Extended;
  438. {$ENDIF}
  439. { area functions }
  440. { delphi names: }
  441. function ArcCosH(x : float) : float;inline;
  442. function ArcSinH(x : float) : float;inline;
  443. function ArcTanH(x : float) : float;inline;
  444. { IMHO the function should be called as follows (FK) }
  445. function ArCosH(x : float) : float;
  446. function ArSinH(x : float) : float;
  447. function ArTanH(x : float) : float;
  448. {$ifdef FPC_HAS_TYPE_SINGLE}
  449. function ArcSec(X: Single): Single;
  450. {$ENDIF}
  451. {$ifdef FPC_HAS_TYPE_DOUBLE}
  452. function ArcSec(X: Double): Double;
  453. {$ENDIF}
  454. {$ifdef FPC_HAS_TYPE_EXTENDED}
  455. function ArcSec(X: Extended): Extended;
  456. {$ENDIF}
  457. {$ifdef FPC_HAS_TYPE_SINGLE}
  458. function ArcCsc(X: Single): Single;
  459. {$ENDIF}
  460. {$ifdef FPC_HAS_TYPE_DOUBLE}
  461. function ArcCsc(X: Double): Double;
  462. {$ENDIF}
  463. {$ifdef FPC_HAS_TYPE_EXTENDED}
  464. function ArcCsc(X: Extended): Extended;
  465. {$ENDIF}
  466. {$ifdef FPC_HAS_TYPE_SINGLE}
  467. function ArcCot(X: Single): Single;
  468. {$ENDIF}
  469. {$ifdef FPC_HAS_TYPE_DOUBLE}
  470. function ArcCot(X: Double): Double;
  471. {$ENDIF}
  472. {$ifdef FPC_HAS_TYPE_EXTENDED}
  473. function ArcCot(X: Extended): Extended;
  474. {$ENDIF}
  475. {$ifdef FPC_HAS_TYPE_SINGLE}
  476. function ArcSecH(X : Single): Single;
  477. {$ENDIF}
  478. {$ifdef FPC_HAS_TYPE_DOUBLE}
  479. function ArcSecH(X : Double): Double;
  480. {$ENDIF}
  481. {$ifdef FPC_HAS_TYPE_EXTENDED}
  482. function ArcSecH(X : Extended): Extended;
  483. {$ENDIF}
  484. {$ifdef FPC_HAS_TYPE_SINGLE}
  485. function ArcCscH(X: Single): Single;
  486. {$ENDIF}
  487. {$ifdef FPC_HAS_TYPE_DOUBLE}
  488. function ArcCscH(X: Double): Double;
  489. {$ENDIF}
  490. {$ifdef FPC_HAS_TYPE_EXTENDED}
  491. function ArcCscH(X: Extended): Extended;
  492. {$ENDIF}
  493. {$ifdef FPC_HAS_TYPE_SINGLE}
  494. function ArcCotH(X: Single): Single;
  495. {$ENDIF}
  496. {$ifdef FPC_HAS_TYPE_DOUBLE}
  497. function ArcCotH(X: Double): Double;
  498. {$ENDIF}
  499. {$ifdef FPC_HAS_TYPE_EXTENDED}
  500. function ArcCotH(X: Extended): Extended;
  501. {$ENDIF}
  502. { triangle functions }
  503. { returns the length of the hypotenuse of a right triangle }
  504. { if x and y are the other sides }
  505. function Hypot(x,y : float) : float;
  506. { logarithm functions }
  507. function Log10(x : float) : float;
  508. function Log2(x : float) : float;
  509. function LogN(n,x : float) : float;
  510. { returns natural logarithm of x+1, accurate for x values near zero }
  511. function LnXP1(x : float) : float;
  512. { exponential functions }
  513. function Power(base,exponent : float) : float;
  514. { base^exponent }
  515. function IntPower(base : float;exponent : longint) : float;
  516. operator ** (base,exponent : float) e: float; inline;
  517. operator ** (base,exponent : int64) res: int64;
  518. { number converting }
  519. { rounds x towards positive infinity }
  520. function Ceil(x : float) : Integer;
  521. function Ceil64(x: float): Int64;
  522. { rounds x towards negative infinity }
  523. function Floor(x : float) : Integer;
  524. function Floor64(x: float): Int64;
  525. { misc. functions }
  526. {$ifdef FPC_HAS_TYPE_SINGLE}
  527. { splits x into mantissa and exponent (to base 2) }
  528. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  529. { returns x*(2^p) }
  530. function Ldexp(X: single; p: Integer) : single;
  531. {$endif}
  532. {$ifdef FPC_HAS_TYPE_DOUBLE}
  533. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  534. function Ldexp(X: double; p: Integer) : double;
  535. {$endif}
  536. {$ifdef FPC_HAS_TYPE_EXTENDED}
  537. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  538. function Ldexp(X: extended; p: Integer) : extended;
  539. {$endif}
  540. { statistical functions }
  541. {$ifdef FPC_HAS_TYPE_SINGLE}
  542. function Mean(const data : array of Single) : float;
  543. function Sum(const data : array of Single) : float;inline;
  544. function Mean(const data : PSingle; Const N : longint) : float;
  545. function Sum(const data : PSingle; Const N : Longint) : float;
  546. {$endif FPC_HAS_TYPE_SINGLE}
  547. {$ifdef FPC_HAS_TYPE_DOUBLE}
  548. function Mean(const data : array of double) : float;inline;
  549. function Sum(const data : array of double) : float;inline;
  550. function Mean(const data : PDouble; Const N : longint) : float;
  551. function Sum(const data : PDouble; Const N : Longint) : float;
  552. {$endif FPC_HAS_TYPE_DOUBLE}
  553. {$ifdef FPC_HAS_TYPE_EXTENDED}
  554. function Mean(const data : array of Extended) : float;
  555. function Sum(const data : array of Extended) : float;inline;
  556. function Mean(const data : PExtended; Const N : longint) : float;
  557. function Sum(const data : PExtended; Const N : Longint) : float;
  558. {$endif FPC_HAS_TYPE_EXTENDED}
  559. function SumInt(const data : PInt64;Const N : longint) : Int64;
  560. function SumInt(const data : array of Int64) : Int64;inline;
  561. function Mean(const data : PInt64; const N : Longint):Float;
  562. function Mean(const data: array of Int64):Float;
  563. function SumInt(const data : PInteger; Const N : longint) : Int64;
  564. function SumInt(const data : array of Integer) : Int64;inline;
  565. function Mean(const data : PInteger; const N : Longint):Float;
  566. function Mean(const data: array of Integer):Float;
  567. {$ifdef FPC_HAS_TYPE_SINGLE}
  568. function SumOfSquares(const data : array of Single) : float;inline;
  569. function SumOfSquares(const data : PSingle; Const N : Integer) : float;
  570. { calculates the sum and the sum of squares of data }
  571. procedure SumsAndSquares(const data : array of Single;
  572. var sum,sumofsquares : float);inline;
  573. procedure SumsAndSquares(const data : PSingle; Const N : Integer;
  574. var sum,sumofsquares : float);
  575. {$endif FPC_HAS_TYPE_SINGLE}
  576. {$ifdef FPC_HAS_TYPE_DOUBLE}
  577. function SumOfSquares(const data : array of double) : float;inline;
  578. function SumOfSquares(const data : PDouble; Const N : Integer) : float;
  579. { calculates the sum and the sum of squares of data }
  580. procedure SumsAndSquares(const data : array of Double;
  581. var sum,sumofsquares : float);inline;
  582. procedure SumsAndSquares(const data : PDouble; Const N : Integer;
  583. var sum,sumofsquares : float);
  584. {$endif FPC_HAS_TYPE_DOUBLE}
  585. {$ifdef FPC_HAS_TYPE_EXTENDED}
  586. function SumOfSquares(const data : array of Extended) : float;inline;
  587. function SumOfSquares(const data : PExtended; Const N : Integer) : float;
  588. { calculates the sum and the sum of squares of data }
  589. procedure SumsAndSquares(const data : array of Extended;
  590. var sum,sumofsquares : float);inline;
  591. procedure SumsAndSquares(const data : PExtended; Const N : Integer;
  592. var sum,sumofsquares : float);
  593. {$endif FPC_HAS_TYPE_EXTENDED}
  594. {$ifdef FPC_HAS_TYPE_SINGLE}
  595. function MinValue(const data : array of Single) : Single;inline;
  596. function MinValue(const data : PSingle; Const N : Integer) : Single;
  597. function MaxValue(const data : array of Single) : Single;inline;
  598. function MaxValue(const data : PSingle; Const N : Integer) : Single;
  599. {$endif FPC_HAS_TYPE_SINGLE}
  600. {$ifdef FPC_HAS_TYPE_DOUBLE}
  601. function MinValue(const data : array of Double) : Double;inline;
  602. function MinValue(const data : PDouble; Const N : Integer) : Double;
  603. function MaxValue(const data : array of Double) : Double;inline;
  604. function MaxValue(const data : PDouble; Const N : Integer) : Double;
  605. {$endif FPC_HAS_TYPE_DOUBLE}
  606. {$ifdef FPC_HAS_TYPE_EXTENDED}
  607. function MinValue(const data : array of Extended) : Extended;inline;
  608. function MinValue(const data : PExtended; Const N : Integer) : Extended;
  609. function MaxValue(const data : array of Extended) : Extended;inline;
  610. function MaxValue(const data : PExtended; Const N : Integer) : Extended;
  611. {$endif FPC_HAS_TYPE_EXTENDED}
  612. function MinValue(const data : array of integer) : Integer;inline;
  613. function MinValue(const Data : PInteger; Const N : Integer): Integer;
  614. function MaxValue(const data : array of integer) : Integer;inline;
  615. function MaxValue(const data : PInteger; Const N : Integer) : Integer;
  616. { returns random values with gaussian distribution }
  617. function RandG(mean,stddev : float) : float;
  618. function RandomRange(const aFrom, aTo: Integer): Integer;
  619. function RandomRange(const aFrom, aTo: Int64): Int64;
  620. {$ifdef FPC_HAS_TYPE_SINGLE}
  621. { calculates the standard deviation }
  622. function StdDev(const data : array of Single) : float;inline;
  623. function StdDev(const data : PSingle; Const N : Integer) : float;
  624. { calculates the mean and stddev }
  625. procedure MeanAndStdDev(const data : array of Single;
  626. var mean,stddev : float);inline;
  627. procedure MeanAndStdDev(const data : PSingle;
  628. Const N : Longint;var mean,stddev : float);
  629. function Variance(const data : array of Single) : float;inline;
  630. function TotalVariance(const data : array of Single) : float;inline;
  631. function Variance(const data : PSingle; Const N : Integer) : float;
  632. function TotalVariance(const data : PSingle; Const N : Integer) : float;
  633. { Population (aka uncorrected) variance and standard deviation }
  634. function PopnStdDev(const data : array of Single) : float;inline;
  635. function PopnStdDev(const data : PSingle; Const N : Integer) : float;
  636. function PopnVariance(const data : PSingle; Const N : Integer) : float;
  637. function PopnVariance(const data : array of Single) : float;inline;
  638. procedure MomentSkewKurtosis(const data : array of Single;
  639. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  640. procedure MomentSkewKurtosis(const data : PSingle; Const N : Integer;
  641. out m1,m2,m3,m4,skew,kurtosis : float);
  642. { geometrical function }
  643. { returns the euclidean L2 norm }
  644. function Norm(const data : array of Single) : float;inline;
  645. function Norm(const data : PSingle; Const N : Integer) : float;
  646. {$endif FPC_HAS_TYPE_SINGLE}
  647. {$ifdef FPC_HAS_TYPE_DOUBLE}
  648. { calculates the standard deviation }
  649. function StdDev(const data : array of Double) : float;inline;
  650. function StdDev(const data : PDouble; Const N : Integer) : float;
  651. { calculates the mean and stddev }
  652. procedure MeanAndStdDev(const data : array of Double;
  653. var mean,stddev : float);inline;
  654. procedure MeanAndStdDev(const data : PDouble;
  655. Const N : Longint;var mean,stddev : float);
  656. function Variance(const data : array of Double) : float;inline;
  657. function TotalVariance(const data : array of Double) : float;inline;
  658. function Variance(const data : PDouble; Const N : Integer) : float;
  659. function TotalVariance(const data : PDouble; Const N : Integer) : float;
  660. { Population (aka uncorrected) variance and standard deviation }
  661. function PopnStdDev(const data : array of Double) : float;inline;
  662. function PopnStdDev(const data : PDouble; Const N : Integer) : float;
  663. function PopnVariance(const data : PDouble; Const N : Integer) : float;
  664. function PopnVariance(const data : array of Double) : float;inline;
  665. procedure MomentSkewKurtosis(const data : array of Double;
  666. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  667. procedure MomentSkewKurtosis(const data : PDouble; Const N : Integer;
  668. out m1,m2,m3,m4,skew,kurtosis : float);
  669. { geometrical function }
  670. { returns the euclidean L2 norm }
  671. function Norm(const data : array of double) : float;inline;
  672. function Norm(const data : PDouble; Const N : Integer) : float;
  673. {$endif FPC_HAS_TYPE_DOUBLE}
  674. {$ifdef FPC_HAS_TYPE_EXTENDED}
  675. { calculates the standard deviation }
  676. function StdDev(const data : array of Extended) : float;inline;
  677. function StdDev(const data : PExtended; Const N : Integer) : float;
  678. { calculates the mean and stddev }
  679. procedure MeanAndStdDev(const data : array of Extended;
  680. var mean,stddev : float);inline;
  681. procedure MeanAndStdDev(const data : PExtended;
  682. Const N : Longint;var mean,stddev : float);
  683. function Variance(const data : array of Extended) : float;inline;
  684. function TotalVariance(const data : array of Extended) : float;inline;
  685. function Variance(const data : PExtended; Const N : Integer) : float;
  686. function TotalVariance(const data : PExtended; Const N : Integer) : float;
  687. { Population (aka uncorrected) variance and standard deviation }
  688. function PopnStdDev(const data : array of Extended) : float;inline;
  689. function PopnStdDev(const data : PExtended; Const N : Integer) : float;
  690. function PopnVariance(const data : PExtended; Const N : Integer) : float;
  691. function PopnVariance(const data : array of Extended) : float;inline;
  692. procedure MomentSkewKurtosis(const data : array of Extended;
  693. out m1,m2,m3,m4,skew,kurtosis : float);inline;
  694. procedure MomentSkewKurtosis(const data : PExtended; Const N : Integer;
  695. out m1,m2,m3,m4,skew,kurtosis : float);
  696. { geometrical function }
  697. { returns the euclidean L2 norm }
  698. function Norm(const data : array of Extended) : float;inline;
  699. function Norm(const data : PExtended; Const N : Integer) : float;
  700. {$endif FPC_HAS_TYPE_EXTENDED}
  701. { Financial functions }
  702. function FutureValue(ARate: Float; NPeriods: Integer;
  703. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  704. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  705. APaymentTime: TPaymentTime): Float;
  706. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  707. APaymentTime: TPaymentTime): Float;
  708. function Payment(ARate: Float; NPeriods: Integer;
  709. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  710. function PresentValue(ARate: Float; NPeriods: Integer;
  711. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  712. { Misc functions }
  713. function IfThen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer; inline; overload;
  714. function IfThen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64; inline; overload;
  715. function IfThen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double; inline; overload;
  716. function CompareValue ( const A, B : Integer) : TValueRelationship; inline;
  717. function CompareValue ( const A, B : Int64) : TValueRelationship; inline;
  718. function CompareValue ( const A, B : QWord) : TValueRelationship; inline;
  719. {$ifdef FPC_HAS_TYPE_SINGLE}
  720. function CompareValue ( const A, B : Single; delta : Single = 0.0 ) : TValueRelationship; inline;
  721. {$endif}
  722. {$ifdef FPC_HAS_TYPE_DOUBLE}
  723. function CompareValue ( const A, B : Double; delta : Double = 0.0) : TValueRelationship; inline;
  724. {$endif}
  725. {$ifdef FPC_HAS_TYPE_EXTENDED}
  726. function CompareValue ( const A, B : Extended; delta : Extended = 0.0 ) : TValueRelationship; inline;
  727. {$endif}
  728. function RandomFrom(const AValues: array of Double): Double; overload;
  729. function RandomFrom(const AValues: array of Integer): Integer; overload;
  730. function RandomFrom(const AValues: array of Int64): Int64; overload;
  731. {$if FPC_FULLVERSION >=30101}
  732. generic function RandomFrom<T>(const AValues:array of T):T;
  733. {$endif}
  734. { cpu specific stuff }
  735. type
  736. TFPURoundingMode = system.TFPURoundingMode;
  737. TFPUPrecisionMode = system.TFPUPrecisionMode;
  738. TFPUException = system.TFPUException;
  739. TFPUExceptionMask = system.TFPUExceptionMask;
  740. function GetRoundMode: TFPURoundingMode;
  741. function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;
  742. function GetPrecisionMode: TFPUPrecisionMode;
  743. function SetPrecisionMode(const Precision: TFPUPrecisionMode): TFPUPrecisionMode;
  744. function GetExceptionMask: TFPUExceptionMask;
  745. function SetExceptionMask(const Mask: TFPUExceptionMask): TFPUExceptionMask;
  746. procedure ClearExceptions(RaisePending: Boolean =true);
  747. implementation
  748. function copysign(x,y: float): float; forward; { returns abs(x)*sign(y) }
  749. { include cpu specific stuff }
  750. {$i mathu.inc}
  751. ResourceString
  752. SMathError = 'Math Error : %s';
  753. SInvalidArgument = 'Invalid argument';
  754. Procedure DoMathError(Const S : String);
  755. begin
  756. Raise EMathError.CreateFmt(SMathError,[S]);
  757. end;
  758. Procedure InvalidArgument;
  759. begin
  760. Raise EInvalidArgument.Create(SInvalidArgument);
  761. end;
  762. function Sign(const AValue: Integer): TValueSign;inline;
  763. begin
  764. result:=TValueSign(
  765. SarLongint(AValue,sizeof(AValue)*8-1) or { gives -1 for negative values, 0 otherwise }
  766. (longint(-AValue) shr (sizeof(AValue)*8-1)) { gives 1 for positive values, 0 otherwise }
  767. );
  768. end;
  769. function Sign(const AValue: Int64): TValueSign;inline;
  770. begin
  771. {$ifdef cpu64}
  772. result:=TValueSign(
  773. SarInt64(AValue,sizeof(AValue)*8-1) or
  774. (-AValue shr (sizeof(AValue)*8-1))
  775. );
  776. {$else cpu64}
  777. If Avalue<0 then
  778. Result:=NegativeValue
  779. else If Avalue>0 then
  780. Result:=PositiveValue
  781. else
  782. Result:=ZeroValue;
  783. {$endif}
  784. end;
  785. {$ifdef FPC_HAS_TYPE_SINGLE}
  786. function Sign(const AValue: Single): TValueSign;inline;
  787. begin
  788. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  789. end;
  790. {$endif}
  791. function Sign(const AValue: Double): TValueSign;inline;
  792. begin
  793. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  794. end;
  795. {$ifdef FPC_HAS_TYPE_EXTENDED}
  796. function Sign(const AValue: Extended): TValueSign;inline;
  797. begin
  798. Result:=ord(AValue>0.0)-ord(AValue<0.0);
  799. end;
  800. {$endif}
  801. function degtorad(deg : float) : float;inline;
  802. begin
  803. degtorad:=deg*(pi/180.0);
  804. end;
  805. function radtodeg(rad : float) : float;inline;
  806. begin
  807. radtodeg:=rad*(180.0/pi);
  808. end;
  809. function gradtorad(grad : float) : float;inline;
  810. begin
  811. gradtorad:=grad*(pi/200.0);
  812. end;
  813. function radtograd(rad : float) : float;inline;
  814. begin
  815. radtograd:=rad*(200.0/pi);
  816. end;
  817. function degtograd(deg : float) : float;inline;
  818. begin
  819. degtograd:=deg*(200.0/180.0);
  820. end;
  821. function gradtodeg(grad : float) : float;inline;
  822. begin
  823. gradtodeg:=grad*(180.0/200.0);
  824. end;
  825. {$ifdef FPC_HAS_TYPE_SINGLE}
  826. function CycleToDeg(const Cycles: Single): Single;
  827. begin
  828. CycleToDeg:=Cycles*360.0;
  829. end;
  830. {$ENDIF}
  831. {$ifdef FPC_HAS_TYPE_DOUBLE}
  832. function CycleToDeg(const Cycles: Double): Double;
  833. begin
  834. CycleToDeg:=Cycles*360.0;
  835. end;
  836. {$ENDIF}
  837. {$ifdef FPC_HAS_TYPE_EXTENDED}
  838. function CycleToDeg(const Cycles: Extended): Extended;
  839. begin
  840. CycleToDeg:=Cycles*360.0;
  841. end;
  842. {$ENDIF}
  843. {$ifdef FPC_HAS_TYPE_SINGLE}
  844. function DegToCycle(const Degrees: Single): Single;
  845. begin
  846. DegToCycle:=Degrees*(1/360.0);
  847. end;
  848. {$ENDIF}
  849. {$ifdef FPC_HAS_TYPE_DOUBLE}
  850. function DegToCycle(const Degrees: Double): Double;
  851. begin
  852. DegToCycle:=Degrees*(1/360.0);
  853. end;
  854. {$ENDIF}
  855. {$ifdef FPC_HAS_TYPE_EXTENDED}
  856. function DegToCycle(const Degrees: Extended): Extended;
  857. begin
  858. DegToCycle:=Degrees*(1/360.0);
  859. end;
  860. {$ENDIF}
  861. {$ifdef FPC_HAS_TYPE_SINGLE}
  862. function CycleToGrad(const Cycles: Single): Single;
  863. begin
  864. CycleToGrad:=Cycles*400.0;
  865. end;
  866. {$ENDIF}
  867. {$ifdef FPC_HAS_TYPE_DOUBLE}
  868. function CycleToGrad(const Cycles: Double): Double;
  869. begin
  870. CycleToGrad:=Cycles*400.0;
  871. end;
  872. {$ENDIF}
  873. {$ifdef FPC_HAS_TYPE_EXTENDED}
  874. function CycleToGrad(const Cycles: Extended): Extended;
  875. begin
  876. CycleToGrad:=Cycles*400.0;
  877. end;
  878. {$ENDIF}
  879. {$ifdef FPC_HAS_TYPE_SINGLE}
  880. function GradToCycle(const Grads: Single): Single;
  881. begin
  882. GradToCycle:=Grads*(1/400.0);
  883. end;
  884. {$ENDIF}
  885. {$ifdef FPC_HAS_TYPE_DOUBLE}
  886. function GradToCycle(const Grads: Double): Double;
  887. begin
  888. GradToCycle:=Grads*(1/400.0);
  889. end;
  890. {$ENDIF}
  891. {$ifdef FPC_HAS_TYPE_EXTENDED}
  892. function GradToCycle(const Grads: Extended): Extended;
  893. begin
  894. GradToCycle:=Grads*(1/400.0);
  895. end;
  896. {$ENDIF}
  897. {$ifdef FPC_HAS_TYPE_SINGLE}
  898. function CycleToRad(const Cycles: Single): Single;
  899. begin
  900. CycleToRad:=Cycles*2*pi;
  901. end;
  902. {$ENDIF}
  903. {$ifdef FPC_HAS_TYPE_DOUBLE}
  904. function CycleToRad(const Cycles: Double): Double;
  905. begin
  906. CycleToRad:=Cycles*2*pi;
  907. end;
  908. {$ENDIF}
  909. {$ifdef FPC_HAS_TYPE_EXTENDED}
  910. function CycleToRad(const Cycles: Extended): Extended;
  911. begin
  912. CycleToRad:=Cycles*2*pi;
  913. end;
  914. {$ENDIF}
  915. {$ifdef FPC_HAS_TYPE_SINGLE}
  916. function RadToCycle(const Rads: Single): Single;
  917. begin
  918. RadToCycle:=Rads*(1/(2*pi));
  919. end;
  920. {$ENDIF}
  921. {$ifdef FPC_HAS_TYPE_DOUBLE}
  922. function RadToCycle(const Rads: Double): Double;
  923. begin
  924. RadToCycle:=Rads*(1/(2*pi));
  925. end;
  926. {$ENDIF}
  927. {$ifdef FPC_HAS_TYPE_EXTENDED}
  928. function RadToCycle(const Rads: Extended): Extended;
  929. begin
  930. RadToCycle:=Rads*(1/(2*pi));
  931. end;
  932. {$ENDIF}
  933. {$ifdef FPC_HAS_TYPE_SINGLE}
  934. Function DegNormalize(deg : single) : single;
  935. begin
  936. Result:=Deg-Int(Deg/360)*360;
  937. If Result<0 then Result:=Result+360;
  938. end;
  939. {$ENDIF}
  940. {$ifdef FPC_HAS_TYPE_DOUBLE}
  941. Function DegNormalize(deg : double) : double; inline;
  942. begin
  943. Result:=Deg-Int(Deg/360)*360;
  944. If (Result<0) then Result:=Result+360;
  945. end;
  946. {$ENDIF}
  947. {$ifdef FPC_HAS_TYPE_EXTENDED}
  948. Function DegNormalize(deg : extended) : extended; inline;
  949. begin
  950. Result:=Deg-Int(Deg/360)*360;
  951. If Result<0 then Result:=Result+360;
  952. end;
  953. {$ENDIF}
  954. {$ifndef FPC_MATH_HAS_TAN}
  955. function tan(x : float) : float;
  956. var
  957. _sin,_cos : float;
  958. begin
  959. sincos(x,_sin,_cos);
  960. tan:=_sin/_cos;
  961. end;
  962. {$endif FPC_MATH_HAS_TAN}
  963. {$ifndef FPC_MATH_HAS_COTAN}
  964. function cotan(x : float) : float;
  965. var
  966. _sin,_cos : float;
  967. begin
  968. sincos(x,_sin,_cos);
  969. cotan:=_cos/_sin;
  970. end;
  971. {$endif FPC_MATH_HAS_COTAN}
  972. function cot(x : float) : float; inline;
  973. begin
  974. cot := cotan(x);
  975. end;
  976. {$ifndef FPC_MATH_HAS_SINCOS}
  977. {$ifdef FPC_HAS_TYPE_SINGLE}
  978. procedure sincos(theta : single;out sinus,cosinus : single);
  979. begin
  980. sinus:=sin(theta);
  981. cosinus:=cos(theta);
  982. end;
  983. {$endif}
  984. {$ifdef FPC_HAS_TYPE_DOUBLE}
  985. procedure sincos(theta : double;out sinus,cosinus : double);
  986. begin
  987. sinus:=sin(theta);
  988. cosinus:=cos(theta);
  989. end;
  990. {$endif}
  991. {$ifdef FPC_HAS_TYPE_EXTENDED}
  992. procedure sincos(theta : extended;out sinus,cosinus : extended);
  993. begin
  994. sinus:=sin(theta);
  995. cosinus:=cos(theta);
  996. end;
  997. {$endif}
  998. {$endif FPC_MATH_HAS_SINCOS}
  999. function secant(x : float) : float; inline;
  1000. begin
  1001. secant := 1 / cos(x);
  1002. end;
  1003. function cosecant(x : float) : float; inline;
  1004. begin
  1005. cosecant := 1 / sin(x);
  1006. end;
  1007. function sec(x : float) : float; inline;
  1008. begin
  1009. sec := secant(x);
  1010. end;
  1011. function csc(x : float) : float; inline;
  1012. begin
  1013. csc := cosecant(x);
  1014. end;
  1015. { arcsin and arccos functions from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1016. {$ifdef FPC_HAS_TYPE_SINGLE}
  1017. function arcsin(x : Single) : Single;
  1018. begin
  1019. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1020. end;
  1021. {$ENDIF}
  1022. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1023. function arcsin(x : Double) : Double;
  1024. begin
  1025. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1026. end;
  1027. {$ENDIF}
  1028. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1029. function arcsin(x : Extended) : Extended;
  1030. begin
  1031. arcsin:=arctan2(x,sqrt((1.0-x)*(1.0+x)));
  1032. end;
  1033. {$ENDIF}
  1034. {$ifdef FPC_HAS_TYPE_SINGLE}
  1035. function Arccos(x : Single) : Single;
  1036. begin
  1037. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1038. end;
  1039. {$ENDIF}
  1040. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1041. function Arccos(x : Double) : Double;
  1042. begin
  1043. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1044. end;
  1045. {$ENDIF}
  1046. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1047. function Arccos(x : Extended) : Extended;
  1048. begin
  1049. arccos:=arctan2(sqrt((1.0-x)*(1.0+x)),x);
  1050. end;
  1051. {$ENDIF}
  1052. {$ifndef FPC_MATH_HAS_ARCTAN2}
  1053. function arctan2(y,x : float) : float;
  1054. begin
  1055. if x=0 then
  1056. begin
  1057. if y=0 then
  1058. result:=0.0
  1059. else if y>0 then
  1060. result:=pi/2
  1061. else
  1062. result:=-pi/2;
  1063. end
  1064. else
  1065. begin
  1066. result:=ArcTan(y/x);
  1067. if x<0 then
  1068. if y<0 then
  1069. result:=result-pi
  1070. else
  1071. result:=result+pi;
  1072. end;
  1073. end;
  1074. {$endif FPC_MATH_HAS_ARCTAN2}
  1075. const
  1076. huge_single: single = 1e30;
  1077. huge_double: double = 1e300;
  1078. {$ifdef FPC_HAS_TYPE_SINGLE}
  1079. function cosh(x : Single) : Single;
  1080. var
  1081. temp : ValReal;
  1082. begin
  1083. if (x>8.94159862326326216608E+0001) or (x<-8.94159862326326216608E+0001) then
  1084. exit(huge_single*huge_single);
  1085. temp:=exp(x);
  1086. {$push}
  1087. {$safefpuexceptions on}
  1088. cosh:=0.5*(temp+1.0/temp);
  1089. {$pop}
  1090. end;
  1091. {$ENDIF}
  1092. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1093. function cosh(x : Double) : Double;
  1094. var
  1095. temp : ValReal;
  1096. begin
  1097. if (x>7.10475860073943942030E+0002) or (x<-7.10475860073943942030E+0002) then
  1098. exit(huge_double*huge_double);
  1099. temp:=exp(x);
  1100. {$push}
  1101. {$safefpuexceptions on}
  1102. cosh:=0.5*(temp+1.0/temp);
  1103. {$pop}
  1104. end;
  1105. {$ENDIF}
  1106. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1107. function cosh(x : Extended) : Extended;
  1108. var
  1109. temp : ValReal;
  1110. begin
  1111. temp:=exp(x);
  1112. cosh:=0.5*(temp+1.0/temp);
  1113. end;
  1114. {$ENDIF}
  1115. {$ifdef FPC_HAS_TYPE_SINGLE}
  1116. function sinh(x : Single) : Single;
  1117. var
  1118. temp : ValReal;
  1119. begin
  1120. if x>8.94159862326326216608E+0001 then
  1121. exit(huge_single*huge_single);
  1122. if x<-8.94159862326326216608E+0001 then
  1123. exit(-(huge_single*huge_single));
  1124. temp:=exp(x);
  1125. { gives better behavior around zero, and in particular ensures that sinh(-0.0)=-0.0 }
  1126. if temp=1 then
  1127. exit(x);
  1128. {$push}
  1129. {$safefpuexceptions on}
  1130. sinh:=0.5*(temp-1.0/temp);
  1131. {$pop}
  1132. end;
  1133. {$ENDIF}
  1134. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1135. function sinh(x : Double) : Double;
  1136. var
  1137. temp : ValReal;
  1138. begin
  1139. if x>7.10475860073943942030E+0002 then
  1140. exit(huge_double*huge_double);
  1141. if x<-7.10475860073943942030E+0002 then
  1142. exit(-(huge_double*huge_double));
  1143. temp:=exp(x);
  1144. if temp=1 then
  1145. exit(x);
  1146. {$push}
  1147. {$safefpuexceptions on}
  1148. sinh:=0.5*(temp-1.0/temp);
  1149. {$pop}
  1150. end;
  1151. {$ENDIF}
  1152. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1153. function sinh(x : Extended) : Extended;
  1154. var
  1155. temp : ValReal;
  1156. begin
  1157. temp:=exp(x);
  1158. if temp=1 then
  1159. exit(x);
  1160. sinh:=0.5*(temp-1.0/temp);
  1161. end;
  1162. {$ENDIF}
  1163. {$ifdef FPC_HAS_TYPE_SINGLE}
  1164. function tanh(x : Single) : Single;
  1165. var
  1166. tmp:ValReal;
  1167. begin
  1168. if x < 0 then begin
  1169. tmp:=exp(2*x);
  1170. if tmp=1 then
  1171. exit(x);
  1172. {$push}
  1173. {$safefpuexceptions on}
  1174. result:=(tmp-1)/(1+tmp)
  1175. {$pop}
  1176. end
  1177. else begin
  1178. tmp:=exp(-2*x);
  1179. if tmp=1 then
  1180. exit(x);
  1181. {$push}
  1182. {$safefpuexceptions on}
  1183. result:=(1-tmp)/(1+tmp)
  1184. {$pop}
  1185. end;
  1186. end;
  1187. {$ENDIF}
  1188. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1189. function tanh(x : Double) : Double;
  1190. var
  1191. tmp:ValReal;
  1192. begin
  1193. if x < 0 then begin
  1194. tmp:=exp(2*x);
  1195. if tmp=1 then
  1196. exit(x);
  1197. {$push}
  1198. {$safefpuexceptions on}
  1199. result:=(tmp-1)/(1+tmp)
  1200. {$pop}
  1201. end
  1202. else begin
  1203. tmp:=exp(-2*x);
  1204. if tmp=1 then
  1205. exit(x);
  1206. {$push}
  1207. {$safefpuexceptions on}
  1208. result:=(1-tmp)/(1+tmp)
  1209. {$pop}
  1210. end;
  1211. end;
  1212. {$ENDIF}
  1213. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1214. function tanh(x : Extended) : Extended;
  1215. var
  1216. tmp:Extended;
  1217. begin
  1218. if x < 0 then begin
  1219. tmp:=exp(2*x);
  1220. if tmp=1 then
  1221. exit(x);
  1222. result:=(tmp-1)/(1+tmp)
  1223. end
  1224. else begin
  1225. tmp:=exp(-2*x);
  1226. if tmp=1 then
  1227. exit(x);
  1228. result:=(1-tmp)/(1+tmp)
  1229. end;
  1230. end;
  1231. {$ENDIF}
  1232. {$ifdef FPC_HAS_TYPE_SINGLE}
  1233. function SecH(const X: Single): Single;
  1234. var
  1235. Ex: ValReal;
  1236. begin
  1237. //https://en.wikipedia.org/wiki/Hyperbolic_functions#Definitions
  1238. //SecH = 2 / (e^X + e^-X)
  1239. Ex:=Exp(X);
  1240. {$push}
  1241. {$safefpuexceptions on}
  1242. SecH:=2/(Ex+1/Ex);
  1243. {$pop}
  1244. end;
  1245. {$ENDIF}
  1246. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1247. function SecH(const X: Double): Double;
  1248. var
  1249. Ex: ValReal;
  1250. begin
  1251. Ex:=Exp(X);
  1252. {$push}
  1253. {$safefpuexceptions on}
  1254. SecH:=2/(Ex+1/Ex);
  1255. {$pop}
  1256. end;
  1257. {$ENDIF}
  1258. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1259. function SecH(const X: Extended): Extended;
  1260. var
  1261. Ex: ValReal;
  1262. begin
  1263. Ex:=Exp(X);
  1264. SecH:=2/(Ex+1/Ex);
  1265. end;
  1266. {$ENDIF}
  1267. {$ifdef FPC_HAS_TYPE_SINGLE}
  1268. function CscH(const X: Single): Single;
  1269. var
  1270. Ex: ValReal;
  1271. begin
  1272. //CscH = 2 / (e^X - e^-X)
  1273. Ex:=Exp(X);
  1274. {$push}
  1275. {$safefpuexceptions on}
  1276. CscH:=2/(Ex-1/Ex);
  1277. {$pop}
  1278. end;
  1279. {$ENDIF}
  1280. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1281. function CscH(const X: Double): Double;
  1282. var
  1283. Ex: ValReal;
  1284. begin
  1285. Ex:=Exp(X);
  1286. {$push}
  1287. {$safefpuexceptions on}
  1288. CscH:=2/(Ex-1/Ex);
  1289. {$pop}
  1290. end;
  1291. {$ENDIF}
  1292. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1293. function CscH(const X: Extended): Extended;
  1294. var
  1295. Ex: ValReal;
  1296. begin
  1297. Ex:=Exp(X);
  1298. CscH:=2/(Ex-1/Ex);
  1299. end;
  1300. {$ENDIF}
  1301. {$ifdef FPC_HAS_TYPE_SINGLE}
  1302. function CotH(const X: Single): Single;
  1303. var
  1304. e2: ValReal;
  1305. begin
  1306. if x < 0 then begin
  1307. e2:=exp(2*x);
  1308. if e2=1 then
  1309. exit(1/x);
  1310. {$push}
  1311. {$safefpuexceptions on}
  1312. result:=(1+e2)/(e2-1)
  1313. {$pop}
  1314. end
  1315. else begin
  1316. e2:=exp(-2*x);
  1317. if e2=1 then
  1318. exit(1/x);
  1319. {$push}
  1320. {$safefpuexceptions on}
  1321. result:=(1+e2)/(1-e2)
  1322. {$pop}
  1323. end;
  1324. end;
  1325. {$ENDIF}
  1326. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1327. function CotH(const X: Double): Double;
  1328. var
  1329. e2: ValReal;
  1330. begin
  1331. if x < 0 then begin
  1332. e2:=exp(2*x);
  1333. if e2=1 then
  1334. exit(1/x);
  1335. {$push}
  1336. {$safefpuexceptions on}
  1337. result:=(1+e2)/(e2-1)
  1338. {$pop}
  1339. end
  1340. else begin
  1341. e2:=exp(-2*x);
  1342. if e2=1 then
  1343. exit(1/x);
  1344. {$push}
  1345. {$safefpuexceptions on}
  1346. result:=(1+e2)/(1-e2)
  1347. {$pop}
  1348. end;
  1349. end;
  1350. {$ENDIF}
  1351. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1352. function CotH(const X: Extended): Extended;
  1353. var
  1354. e2: ValReal;
  1355. begin
  1356. if x < 0 then begin
  1357. e2:=exp(2*x);
  1358. if e2=1 then
  1359. exit(1/x);
  1360. result:=(1+e2)/(e2-1)
  1361. end
  1362. else begin
  1363. e2:=exp(-2*x);
  1364. if e2=1 then
  1365. exit(1/x);
  1366. result:=(1+e2)/(1-e2)
  1367. end;
  1368. end;
  1369. {$ENDIF}
  1370. function arccosh(x : float) : float; inline;
  1371. begin
  1372. arccosh:=arcosh(x);
  1373. end;
  1374. function arcsinh(x : float) : float;inline;
  1375. begin
  1376. arcsinh:=arsinh(x);
  1377. end;
  1378. function arctanh(x : float) : float;inline;
  1379. begin
  1380. arctanh:=artanh(x);
  1381. end;
  1382. function arcosh(x : float) : float;
  1383. begin
  1384. { Provides accuracy about 4*eps near 1.0 }
  1385. arcosh:=Ln(x+Sqrt((x-1.0)*(x+1.0)));
  1386. end;
  1387. function arsinh(x : float) : float;
  1388. var
  1389. z: float;
  1390. begin
  1391. z:=abs(x);
  1392. z:=Ln(z+Sqrt(1+z*z));
  1393. { copysign ensures that arsinh(-Inf)=-Inf and arsinh(-0.0)=-0.0 }
  1394. arsinh:=copysign(z,x);
  1395. end;
  1396. function artanh(x : float) : float;
  1397. begin
  1398. artanh:=(lnxp1(x)-lnxp1(-x))*0.5;
  1399. end;
  1400. {$ifdef FPC_HAS_TYPE_SINGLE}
  1401. function ArcSec(X: Single): Single;
  1402. begin
  1403. ArcSec:=ArcCos(1/X);
  1404. end;
  1405. {$ENDIF}
  1406. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1407. function ArcSec(X: Double): Double;
  1408. begin
  1409. ArcSec:=ArcCos(1/X);
  1410. end;
  1411. {$ENDIF}
  1412. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1413. function ArcSec(X: Extended): Extended;
  1414. begin
  1415. ArcSec:=ArcCos(1/X);
  1416. end;
  1417. {$ENDIF}
  1418. {$ifdef FPC_HAS_TYPE_SINGLE}
  1419. function ArcCsc(X: Single): Single;
  1420. begin
  1421. ArcCsc:=ArcSin(1/X);
  1422. end;
  1423. {$ENDIF}
  1424. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1425. function ArcCsc(X: Double): Double;
  1426. begin
  1427. ArcCsc:=ArcSin(1/X);
  1428. end;
  1429. {$ENDIF}
  1430. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1431. function ArcCsc(X: Extended): Extended;
  1432. begin
  1433. ArcCsc:=ArcSin(1/X);
  1434. end;
  1435. {$ENDIF}
  1436. {$ifdef FPC_HAS_TYPE_SINGLE}
  1437. function ArcCot(X: Single): Single;
  1438. begin
  1439. if x=0 then
  1440. ArcCot:=0.5*pi
  1441. else
  1442. ArcCot:=ArcTan(1/X);
  1443. end;
  1444. {$ENDIF}
  1445. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1446. function ArcCot(X: Double): Double;
  1447. begin
  1448. begin
  1449. if x=0 then
  1450. ArcCot:=0.5*pi
  1451. else
  1452. ArcCot:=ArcTan(1/X);
  1453. end;
  1454. end;
  1455. {$ENDIF}
  1456. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1457. function ArcCot(X: Extended): Extended;
  1458. begin
  1459. begin
  1460. if x=0 then
  1461. ArcCot:=0.5*pi
  1462. else
  1463. ArcCot:=ArcTan(1/X);
  1464. end;
  1465. end;
  1466. {$ENDIF}
  1467. {$ifdef FPC_HAS_TYPE_SINGLE}
  1468. function ArcSecH(X : Single): Single;
  1469. begin
  1470. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X); //replacing division inside ln() by subtracting 2 ln()'s seems to be slower
  1471. end;
  1472. {$ENDIF}
  1473. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1474. function ArcSecH(X : Double): Double;
  1475. begin
  1476. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1477. end;
  1478. {$ENDIF}
  1479. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1480. function ArcSecH(X : Extended): Extended;
  1481. begin
  1482. ArcSecH:=ln((1+(sqrt(1.0-sqr(X))))/X);
  1483. end;
  1484. {$ENDIF}
  1485. {$ifdef FPC_HAS_TYPE_SINGLE}
  1486. function ArcCscH(X: Single): Single;
  1487. begin
  1488. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1489. end;
  1490. {$ENDIF}
  1491. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1492. function ArcCscH(X: Double): Double;
  1493. begin
  1494. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1495. end;
  1496. {$ENDIF}
  1497. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1498. function ArcCscH(X: Extended): Extended;
  1499. begin
  1500. ArcCscH:=ln((1.0/X)+sqrt(1.0/(sqr(x))+1.0));
  1501. end;
  1502. {$ENDIF}
  1503. {$ifdef FPC_HAS_TYPE_SINGLE}
  1504. function ArcCotH(X: Single): Single;
  1505. begin
  1506. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1507. end;
  1508. {$ENDIF}
  1509. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1510. function ArcCotH(X: Double): Double;
  1511. begin
  1512. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1513. end;
  1514. {$ENDIF}
  1515. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1516. function ArcCotH(X: Extended): Extended;
  1517. begin
  1518. ArcCotH:=0.5*ln((x + 1.0)/(x - 1.0));
  1519. end;
  1520. {$ENDIF}
  1521. { hypot function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1522. function hypot(x,y : float) : float;
  1523. begin
  1524. x:=abs(x);
  1525. y:=abs(y);
  1526. if (x>y) then
  1527. hypot:=x*sqrt(1.0+sqr(y/x))
  1528. else if (x>0.0) then
  1529. hypot:=y*sqrt(1.0+sqr(x/y))
  1530. else
  1531. hypot:=y;
  1532. end;
  1533. function log10(x : float) : float;
  1534. begin
  1535. log10:=ln(x)*0.43429448190325182765; { 1/ln(10) }
  1536. end;
  1537. {$ifndef FPC_MATH_HAS_LOG2}
  1538. function log2(x : float) : float;
  1539. begin
  1540. log2:=ln(x)*1.4426950408889634079; { 1/ln(2) }
  1541. end;
  1542. {$endif FPC_MATH_HAS_LOG2}
  1543. function logn(n,x : float) : float;
  1544. begin
  1545. logn:=ln(x)/ln(n);
  1546. end;
  1547. { lnxp1 function from AMath library (C) Copyright 2009-2013 Wolfgang Ehrhardt }
  1548. function lnxp1(x : float) : float;
  1549. var
  1550. y: float;
  1551. begin
  1552. if (x>=4.0) then
  1553. lnxp1:=ln(1.0+x)
  1554. else
  1555. begin
  1556. y:=1.0+x;
  1557. if (y=1.0) then
  1558. lnxp1:=x
  1559. else
  1560. begin
  1561. lnxp1:=ln(y); { lnxp1(-1) = ln(0) = -Inf }
  1562. if y>0.0 then
  1563. lnxp1:=lnxp1+(x-(y-1.0))/y;
  1564. end;
  1565. end;
  1566. end;
  1567. function power(base,exponent : float) : float;
  1568. begin
  1569. if Exponent=0.0 then
  1570. result:=1.0
  1571. else if (base=0.0) and (exponent>0.0) then
  1572. result:=0.0
  1573. else if (frac(exponent)=0.0) and (abs(exponent)<=maxint) then
  1574. result:=intpower(base,trunc(exponent))
  1575. else
  1576. result:=exp(exponent * ln (base));
  1577. end;
  1578. function intpower(base : float;exponent : longint) : float;
  1579. begin
  1580. if exponent<0 then
  1581. begin
  1582. base:=1.0/base;
  1583. exponent:=-exponent;
  1584. end;
  1585. intpower:=1.0;
  1586. while exponent<>0 do
  1587. begin
  1588. if exponent and 1<>0 then
  1589. intpower:=intpower*base;
  1590. exponent:=exponent shr 1;
  1591. base:=sqr(base);
  1592. end;
  1593. end;
  1594. operator ** (base,exponent : float) e: float; inline;
  1595. begin
  1596. e:=power(base,exponent);
  1597. end;
  1598. operator ** (base,exponent : int64) res: int64;
  1599. begin
  1600. if exponent<0 then
  1601. begin
  1602. if base<=0 then
  1603. raise EInvalidArgument.Create('Non-positive base with negative exponent in **');
  1604. if base=1 then
  1605. res:=1
  1606. else
  1607. res:=0;
  1608. exit;
  1609. end;
  1610. res:=1;
  1611. while exponent<>0 do
  1612. begin
  1613. if exponent and 1<>0 then
  1614. res:=res*base;
  1615. exponent:=exponent shr 1;
  1616. base:=base*base;
  1617. end;
  1618. end;
  1619. function ceil(x : float) : integer;
  1620. begin
  1621. Result:=Trunc(x)+ord(Frac(x)>0);
  1622. end;
  1623. function ceil64(x: float): Int64;
  1624. begin
  1625. Result:=Trunc(x)+ord(Frac(x)>0);
  1626. end;
  1627. function floor(x : float) : integer;
  1628. begin
  1629. Result:=Trunc(x)-ord(Frac(x)<0);
  1630. end;
  1631. function floor64(x: float): Int64;
  1632. begin
  1633. Result:=Trunc(x)-ord(Frac(x)<0);
  1634. end;
  1635. // Correction for "rounding to nearest, ties to even".
  1636. // RoundToNearestTieToEven(QWE.RTYUIOP) = QWE + TieToEven(ER, TYUIOP <> 0).
  1637. function TieToEven(AB: cardinal; somethingAfter: boolean): cardinal;
  1638. begin
  1639. result := AB and 1;
  1640. if (result <> 0) and not somethingAfter then
  1641. result := AB shr 1;
  1642. end;
  1643. {$ifdef FPC_HAS_TYPE_SINGLE}
  1644. procedure Frexp(X: single; out Mantissa: single; out Exponent: integer);
  1645. var
  1646. M: uint32;
  1647. E, ExtraE: int32;
  1648. begin
  1649. Mantissa := X;
  1650. E := TSingleRec(X).Exp;
  1651. if (E > 0) and (E < 2 * TSingleRec.Bias + 1) then
  1652. begin
  1653. // Normal.
  1654. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1655. Exponent := E - (TSingleRec.Bias - 1);
  1656. exit;
  1657. end;
  1658. if E = 0 then
  1659. begin
  1660. M := TSingleRec(X).Frac;
  1661. if M <> 0 then
  1662. begin
  1663. // Subnormal.
  1664. ExtraE := 23 - BsrDWord(M);
  1665. TSingleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 23 - 1)" required to remove starting 1, but .SetFrac already does it.
  1666. TSingleRec(Mantissa).Exp := TSingleRec.Bias - 1;
  1667. Exponent := -TSingleRec.Bias + 2 - ExtraE;
  1668. exit;
  1669. end;
  1670. end;
  1671. // ±0, ±Inf, NaN.
  1672. Exponent := 0;
  1673. end;
  1674. function Ldexp(X: single; p: integer): single;
  1675. var
  1676. M, E: uint32;
  1677. xp, sh: integer;
  1678. begin
  1679. E := TSingleRec(X).Exp;
  1680. if (E = 0) and (TSingleRec(X).Frac = 0) or (E = 2 * TSingleRec.Bias + 1) then
  1681. // ±0, ±Inf, NaN.
  1682. exit(X);
  1683. Frexp(X, result, xp);
  1684. inc(xp, p);
  1685. if (xp >= -TSingleRec.Bias + 2) and (xp <= TSingleRec.Bias + 1) then
  1686. // Normalized.
  1687. TSingleRec(result).Exp := xp + (TSingleRec.Bias - 1)
  1688. else if xp > TSingleRec.Bias + 1 then
  1689. begin
  1690. // Overflow.
  1691. TSingleRec(result).Exp := 2 * TSingleRec.Bias + 1;
  1692. TSingleRec(result).Frac := 0;
  1693. end else
  1694. begin
  1695. TSingleRec(result).Exp := 0;
  1696. if xp >= -TSingleRec.Bias + 2 - 23 then
  1697. begin
  1698. // Denormalized.
  1699. M := TSingleRec(result).Frac or uint32(1) shl 23;
  1700. sh := -TSingleRec.Bias + 1 - xp;
  1701. TSingleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint32(1) shl sh - 1) <> 0);
  1702. end else
  1703. // Underflow.
  1704. TSingleRec(result).Frac := 0;
  1705. end;
  1706. end;
  1707. {$endif}
  1708. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1709. procedure Frexp(X: double; out Mantissa: double; out Exponent: integer);
  1710. var
  1711. M: uint64;
  1712. E, ExtraE: int32;
  1713. begin
  1714. Mantissa := X;
  1715. E := TDoubleRec(X).Exp;
  1716. if (E > 0) and (E < 2 * TDoubleRec.Bias + 1) then
  1717. begin
  1718. // Normal.
  1719. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1720. Exponent := E - (TDoubleRec.Bias - 1);
  1721. exit;
  1722. end;
  1723. if E = 0 then
  1724. begin
  1725. M := TDoubleRec(X).Frac;
  1726. if M <> 0 then
  1727. begin
  1728. // Subnormal.
  1729. ExtraE := 52 - BsrQWord(M);
  1730. TDoubleRec(Mantissa).Frac := M shl ExtraE; // "and (1 shl 52 - 1)" required to remove starting 1, but .SetFrac already does it.
  1731. TDoubleRec(Mantissa).Exp := TDoubleRec.Bias - 1;
  1732. Exponent := -TDoubleRec.Bias + 2 - ExtraE;
  1733. exit;
  1734. end;
  1735. end;
  1736. // ±0, ±Inf, NaN.
  1737. Exponent := 0;
  1738. end;
  1739. function Ldexp(X: double; p: integer): double;
  1740. var
  1741. M: uint64;
  1742. E: uint32;
  1743. xp, sh: integer;
  1744. begin
  1745. E := TDoubleRec(X).Exp;
  1746. if (E = 0) and (TDoubleRec(X).Frac = 0) or (E = 2 * TDoubleRec.Bias + 1) then
  1747. // ±0, ±Inf, NaN.
  1748. exit(X);
  1749. Frexp(X, result, xp);
  1750. inc(xp, p);
  1751. if (xp >= -TDoubleRec.Bias + 2) and (xp <= TDoubleRec.Bias + 1) then
  1752. // Normalized.
  1753. TDoubleRec(result).Exp := xp + (TDoubleRec.Bias - 1)
  1754. else if xp > TDoubleRec.Bias + 1 then
  1755. begin
  1756. // Overflow.
  1757. TDoubleRec(result).Exp := 2 * TDoubleRec.Bias + 1;
  1758. TDoubleRec(result).Frac := 0;
  1759. end else
  1760. begin
  1761. TDoubleRec(result).Exp := 0;
  1762. if xp >= -TDoubleRec.Bias + 2 - 52 then
  1763. begin
  1764. // Denormalized.
  1765. M := TDoubleRec(result).Frac or uint64(1) shl 52;
  1766. sh := -TSingleRec.Bias + 1 - xp;
  1767. TDoubleRec(result).Frac := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1768. end else
  1769. // Underflow.
  1770. TDoubleRec(result).Frac := 0;
  1771. end;
  1772. end;
  1773. {$endif}
  1774. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1775. procedure Frexp(X: extended; out Mantissa: extended; out Exponent: integer);
  1776. var
  1777. M: uint64;
  1778. E, ExtraE: int32;
  1779. begin
  1780. Mantissa := X;
  1781. E := TExtended80Rec(X).Exp;
  1782. if (E > 0) and (E < 2 * TExtended80Rec.Bias + 1) then
  1783. begin
  1784. // Normal.
  1785. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1786. Exponent := E - (TExtended80Rec.Bias - 1);
  1787. exit;
  1788. end;
  1789. if E = 0 then
  1790. begin
  1791. M := TExtended80Rec(X).Frac;
  1792. if M <> 0 then
  1793. begin
  1794. // Subnormal. Extended has explicit starting 1.
  1795. ExtraE := 63 - BsrQWord(M);
  1796. TExtended80Rec(Mantissa).Frac := M shl ExtraE;
  1797. TExtended80Rec(Mantissa).Exp := TExtended80Rec.Bias - 1;
  1798. Exponent := -TExtended80Rec.Bias + 2 - ExtraE;
  1799. exit;
  1800. end;
  1801. end;
  1802. // ±0, ±Inf, NaN.
  1803. Exponent := 0;
  1804. end;
  1805. function Ldexp(X: extended; p: integer): extended;
  1806. var
  1807. M: uint64;
  1808. E: uint32;
  1809. xp, sh: integer;
  1810. begin
  1811. E := TExtended80Rec(X).Exp;
  1812. if (E = 0) and (TExtended80Rec(X).Frac = 0) or (E = 2 * TExtended80Rec.Bias + 1) then
  1813. // ±0, ±Inf, NaN.
  1814. exit(X);
  1815. Frexp(X, result, xp);
  1816. inc(xp, p);
  1817. if (xp >= -TExtended80Rec.Bias + 2) and (xp <= TExtended80Rec.Bias + 1) then
  1818. // Normalized.
  1819. TExtended80Rec(result).Exp := xp + (TExtended80Rec.Bias - 1)
  1820. else if xp > TExtended80Rec.Bias + 1 then
  1821. begin
  1822. // Overflow.
  1823. TExtended80Rec(result).Exp := 2 * TExtended80Rec.Bias + 1;
  1824. TExtended80Rec(result).Frac := uint64(1) shl 63;
  1825. end
  1826. else if xp >= -TExtended80Rec.Bias + 2 - 63 then
  1827. begin
  1828. // Denormalized... usually.
  1829. // Mantissa of subnormal 'extended' (Exp = 0) must always start with 0.
  1830. // If the calculated mantissa starts with 1, extended instead becomes normalized with Exp = 1.
  1831. M := TExtended80Rec(result).Frac;
  1832. sh := -TExtended80Rec.Bias + 1 - xp;
  1833. M := M shr (sh + 1) + TieToEven(M shr sh and 3, M and (uint64(1) shl sh - 1) <> 0);
  1834. TExtended80Rec(result).Exp := M shr 63;
  1835. TExtended80Rec(result).Frac := M;
  1836. end else
  1837. begin
  1838. // Underflow.
  1839. TExtended80Rec(result).Exp := 0;
  1840. TExtended80Rec(result).Frac := 0;
  1841. end;
  1842. end;
  1843. {$endif}
  1844. const
  1845. { Cutoff for https://en.wikipedia.org/wiki/Pairwise_summation; sums of at least this many elements are split in two halves. }
  1846. RecursiveSumThreshold=12;
  1847. {$ifdef FPC_HAS_TYPE_SINGLE}
  1848. function mean(const data : array of Single) : float;
  1849. begin
  1850. Result:=Mean(PSingle(@data[0]),High(Data)+1);
  1851. end;
  1852. function mean(const data : PSingle; Const N : longint) : float;
  1853. begin
  1854. mean:=sum(Data,N);
  1855. mean:=mean/N;
  1856. end;
  1857. function sum(const data : array of Single) : float;inline;
  1858. begin
  1859. Result:=Sum(PSingle(@Data[0]),High(Data)+1);
  1860. end;
  1861. function sum(const data : PSingle;Const N : longint) : float;
  1862. var
  1863. i : SizeInt;
  1864. begin
  1865. if N>=RecursiveSumThreshold then
  1866. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1867. else
  1868. begin
  1869. result:=0;
  1870. for i:=0 to N-1 do
  1871. result:=result+data[i];
  1872. end;
  1873. end;
  1874. {$endif FPC_HAS_TYPE_SINGLE}
  1875. {$ifdef FPC_HAS_TYPE_DOUBLE}
  1876. function mean(const data : array of Double) : float; inline;
  1877. begin
  1878. Result:=Mean(PDouble(@data[0]),High(Data)+1);
  1879. end;
  1880. function mean(const data : PDouble; Const N : longint) : float;
  1881. begin
  1882. mean:=sum(Data,N);
  1883. mean:=mean/N;
  1884. end;
  1885. function sum(const data : array of Double) : float; inline;
  1886. begin
  1887. Result:=Sum(PDouble(@Data[0]),High(Data)+1);
  1888. end;
  1889. function sum(const data : PDouble;Const N : longint) : float;
  1890. var
  1891. i : SizeInt;
  1892. begin
  1893. if N>=RecursiveSumThreshold then
  1894. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1895. else
  1896. begin
  1897. result:=0;
  1898. for i:=0 to N-1 do
  1899. result:=result+data[i];
  1900. end;
  1901. end;
  1902. {$endif FPC_HAS_TYPE_DOUBLE}
  1903. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1904. function mean(const data : array of Extended) : float;
  1905. begin
  1906. Result:=Mean(PExtended(@data[0]),High(Data)+1);
  1907. end;
  1908. function mean(const data : PExtended; Const N : longint) : float;
  1909. begin
  1910. mean:=sum(Data,N);
  1911. mean:=mean/N;
  1912. end;
  1913. function sum(const data : array of Extended) : float; inline;
  1914. begin
  1915. Result:=Sum(PExtended(@Data[0]),High(Data)+1);
  1916. end;
  1917. function sum(const data : PExtended;Const N : longint) : float;
  1918. var
  1919. i : SizeInt;
  1920. begin
  1921. if N>=RecursiveSumThreshold then
  1922. result:=sum(data,longword(N) div 2)+sum(data+longword(N) div 2,N-longword(N) div 2)
  1923. else
  1924. begin
  1925. result:=0;
  1926. for i:=0 to N-1 do
  1927. result:=result+data[i];
  1928. end;
  1929. end;
  1930. {$endif FPC_HAS_TYPE_EXTENDED}
  1931. function sumInt(const data : PInt64;Const N : longint) : Int64;
  1932. var
  1933. i : SizeInt;
  1934. begin
  1935. sumInt:=0;
  1936. for i:=0 to N-1 do
  1937. sumInt:=sumInt+data[i];
  1938. end;
  1939. function sumInt(const data : array of Int64) : Int64; inline;
  1940. begin
  1941. Result:=SumInt(PInt64(@Data[0]),High(Data)+1);
  1942. end;
  1943. function mean(const data : PInt64; const N : Longint):Float;
  1944. begin
  1945. mean:=sumInt(Data,N);
  1946. mean:=mean/N;
  1947. end;
  1948. function mean(const data: array of Int64):Float;
  1949. begin
  1950. mean:=mean(PInt64(@data[0]),High(Data)+1);
  1951. end;
  1952. function sumInt(const data : PInteger; Const N : longint) : Int64;
  1953. var
  1954. i : SizeInt;
  1955. begin
  1956. sumInt:=0;
  1957. for i:=0 to N-1 do
  1958. sumInt:=sumInt+data[i];
  1959. end;
  1960. function sumInt(const data : array of Integer) : Int64;inline;
  1961. begin
  1962. Result:=sumInt(PInteger(@Data[0]),High(Data)+1);
  1963. end;
  1964. function mean(const data : PInteger; const N : Longint):Float;
  1965. begin
  1966. mean:=sumInt(Data,N);
  1967. mean:=mean/N;
  1968. end;
  1969. function mean(const data: array of Integer):Float;
  1970. begin
  1971. mean:=mean(PInteger(@data[0]),High(Data)+1);
  1972. end;
  1973. {$ifdef FPC_HAS_TYPE_SINGLE}
  1974. function sumofsquares(const data : array of Single) : float; inline;
  1975. begin
  1976. Result:=sumofsquares(PSingle(@data[0]),High(Data)+1);
  1977. end;
  1978. function sumofsquares(const data : PSingle; Const N : Integer) : float;
  1979. var
  1980. i : SizeInt;
  1981. begin
  1982. if N>=RecursiveSumThreshold then
  1983. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  1984. else
  1985. begin
  1986. result:=0;
  1987. for i:=0 to N-1 do
  1988. result:=result+sqr(data[i]);
  1989. end;
  1990. end;
  1991. procedure sumsandsquares(const data : array of Single;
  1992. var sum,sumofsquares : float); inline;
  1993. begin
  1994. sumsandsquares (PSingle(@Data[0]),High(Data)+1,Sum,sumofsquares);
  1995. end;
  1996. procedure sumsandsquares(const data : PSingle; Const N : Integer;
  1997. var sum,sumofsquares : float);
  1998. var
  1999. i : SizeInt;
  2000. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2001. begin
  2002. if N>=RecursiveSumThreshold then
  2003. begin
  2004. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2005. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2006. sum:=sum0+sum1;
  2007. sumofsquares:=sumofsquares0+sumofsquares1;
  2008. end
  2009. else
  2010. begin
  2011. tsum:=0;
  2012. tsumofsquares:=0;
  2013. for i:=0 to N-1 do
  2014. begin
  2015. temp:=data[i];
  2016. tsum:=tsum+temp;
  2017. tsumofsquares:=tsumofsquares+sqr(temp);
  2018. end;
  2019. sum:=tsum;
  2020. sumofsquares:=tsumofsquares;
  2021. end;
  2022. end;
  2023. {$endif FPC_HAS_TYPE_SINGLE}
  2024. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2025. function sumofsquares(const data : array of Double) : float; inline;
  2026. begin
  2027. Result:=sumofsquares(PDouble(@data[0]),High(Data)+1);
  2028. end;
  2029. function sumofsquares(const data : PDouble; Const N : Integer) : float;
  2030. var
  2031. i : SizeInt;
  2032. begin
  2033. if N>=RecursiveSumThreshold then
  2034. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  2035. else
  2036. begin
  2037. result:=0;
  2038. for i:=0 to N-1 do
  2039. result:=result+sqr(data[i]);
  2040. end;
  2041. end;
  2042. procedure sumsandsquares(const data : array of Double;
  2043. var sum,sumofsquares : float); inline;
  2044. begin
  2045. sumsandsquares (PDouble(@Data[0]),High(Data)+1,Sum,sumofsquares);
  2046. end;
  2047. procedure sumsandsquares(const data : PDouble; Const N : Integer;
  2048. var sum,sumofsquares : float);
  2049. var
  2050. i : SizeInt;
  2051. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2052. begin
  2053. if N>=RecursiveSumThreshold then
  2054. begin
  2055. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2056. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2057. sum:=sum0+sum1;
  2058. sumofsquares:=sumofsquares0+sumofsquares1;
  2059. end
  2060. else
  2061. begin
  2062. tsum:=0;
  2063. tsumofsquares:=0;
  2064. for i:=0 to N-1 do
  2065. begin
  2066. temp:=data[i];
  2067. tsum:=tsum+temp;
  2068. tsumofsquares:=tsumofsquares+sqr(temp);
  2069. end;
  2070. sum:=tsum;
  2071. sumofsquares:=tsumofsquares;
  2072. end;
  2073. end;
  2074. {$endif FPC_HAS_TYPE_DOUBLE}
  2075. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2076. function sumofsquares(const data : array of Extended) : float; inline;
  2077. begin
  2078. Result:=sumofsquares(PExtended(@data[0]),High(Data)+1);
  2079. end;
  2080. function sumofsquares(const data : PExtended; Const N : Integer) : float;
  2081. var
  2082. i : SizeInt;
  2083. begin
  2084. if N>=RecursiveSumThreshold then
  2085. result:=sumofsquares(data,cardinal(N) div 2)+sumofsquares(data+cardinal(N) div 2,N-cardinal(N) div 2)
  2086. else
  2087. begin
  2088. result:=0;
  2089. for i:=0 to N-1 do
  2090. result:=result+sqr(data[i]);
  2091. end;
  2092. end;
  2093. procedure sumsandsquares(const data : array of Extended;
  2094. var sum,sumofsquares : float); inline;
  2095. begin
  2096. sumsandsquares (PExtended(@Data[0]),High(Data)+1,Sum,sumofsquares);
  2097. end;
  2098. procedure sumsandsquares(const data : PExtended; Const N : Integer;
  2099. var sum,sumofsquares : float);
  2100. var
  2101. i : SizeInt;
  2102. temp,tsum,tsumofsquares,sum0,sumofsquares0,sum1,sumofsquares1 : float;
  2103. begin
  2104. if N>=RecursiveSumThreshold then
  2105. begin
  2106. sumsandsquares(data,cardinal(N) div 2,sum0,sumofsquares0);
  2107. sumsandsquares(data+cardinal(N) div 2,N-cardinal(N) div 2,sum1,sumofsquares1);
  2108. sum:=sum0+sum1;
  2109. sumofsquares:=sumofsquares0+sumofsquares1;
  2110. end
  2111. else
  2112. begin
  2113. tsum:=0;
  2114. tsumofsquares:=0;
  2115. for i:=0 to N-1 do
  2116. begin
  2117. temp:=data[i];
  2118. tsum:=tsum+temp;
  2119. tsumofsquares:=tsumofsquares+sqr(temp);
  2120. end;
  2121. sum:=tsum;
  2122. sumofsquares:=tsumofsquares;
  2123. end;
  2124. end;
  2125. {$endif FPC_HAS_TYPE_EXTENDED}
  2126. function randg(mean,stddev : float) : float;
  2127. Var U1,S2 : Float;
  2128. begin
  2129. repeat
  2130. u1:= 2*random-1;
  2131. S2:=Sqr(U1)+sqr(2*random-1);
  2132. until s2<1;
  2133. randg:=Sqrt(-2*ln(S2)/S2)*u1*stddev+Mean;
  2134. end;
  2135. function RandomRange(const aFrom, aTo: Integer): Integer;
  2136. begin
  2137. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  2138. end;
  2139. function RandomRange(const aFrom, aTo: Int64): Int64;
  2140. begin
  2141. Result:=Random(Abs(aFrom-aTo))+Min(aTo,AFrom);
  2142. end;
  2143. {$ifdef FPC_HAS_TYPE_SINGLE}
  2144. procedure MeanAndTotalVariance
  2145. (const data: PSingle; N: LongInt; var mu, variance: float);
  2146. function CalcVariance(data: PSingle; N: SizeInt; mu: float): float;
  2147. var
  2148. i: SizeInt;
  2149. begin
  2150. if N>=RecursiveSumThreshold then
  2151. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2152. else
  2153. begin
  2154. result:=0;
  2155. for i:=0 to N-1 do
  2156. result:=result+Sqr(data[i]-mu);
  2157. end;
  2158. end;
  2159. begin
  2160. mu := Mean( data, N );
  2161. variance := CalcVariance( data, N, mu );
  2162. end;
  2163. function stddev(const data : array of Single) : float; inline;
  2164. begin
  2165. Result:=Stddev(PSingle(@Data[0]),High(Data)+1);
  2166. end;
  2167. function stddev(const data : PSingle; Const N : Integer) : float;
  2168. begin
  2169. StdDev:=Sqrt(Variance(Data,N));
  2170. end;
  2171. procedure meanandstddev(const data : array of Single;
  2172. var mean,stddev : float); inline;
  2173. begin
  2174. Meanandstddev(PSingle(@Data[0]),High(Data)+1,Mean,stddev);
  2175. end;
  2176. procedure meanandstddev
  2177. ( const data: PSingle;
  2178. const N: Longint;
  2179. var mean,
  2180. stdDev: Float
  2181. );
  2182. var totalVariance: float;
  2183. begin
  2184. MeanAndTotalVariance( data, N, mean, totalVariance );
  2185. if N < 2 then stdDev := 0
  2186. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2187. end;
  2188. function variance(const data : array of Single) : float; inline;
  2189. begin
  2190. Variance:=Variance(PSingle(@Data[0]),High(Data)+1);
  2191. end;
  2192. function variance(const data : PSingle; Const N : Integer) : float;
  2193. begin
  2194. If N=1 then
  2195. Result:=0
  2196. else
  2197. Result:=TotalVariance(Data,N)/(N-1);
  2198. end;
  2199. function totalvariance(const data : array of Single) : float; inline;
  2200. begin
  2201. Result:=TotalVariance(PSingle(@Data[0]),High(Data)+1);
  2202. end;
  2203. function totalvariance(const data : PSingle; const N : Integer) : float;
  2204. var mu: float;
  2205. begin
  2206. MeanAndTotalVariance( data, N, mu, result );
  2207. end;
  2208. function popnstddev(const data : array of Single) : float;
  2209. begin
  2210. PopnStdDev:=Sqrt(PopnVariance(PSingle(@Data[0]),High(Data)+1));
  2211. end;
  2212. function popnstddev(const data : PSingle; Const N : Integer) : float;
  2213. begin
  2214. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2215. end;
  2216. function popnvariance(const data : array of Single) : float; inline;
  2217. begin
  2218. popnvariance:=popnvariance(PSingle(@data[0]),high(Data)+1);
  2219. end;
  2220. function popnvariance(const data : PSingle; Const N : Integer) : float;
  2221. begin
  2222. PopnVariance:=TotalVariance(Data,N)/N;
  2223. end;
  2224. procedure momentskewkurtosis(const data : array of single;
  2225. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2226. begin
  2227. momentskewkurtosis(PSingle(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2228. end;
  2229. type
  2230. TMoments2to4 = array[2 .. 4] of float;
  2231. procedure momentskewkurtosis(
  2232. const data: pSingle;
  2233. Const N: integer;
  2234. out m1: float;
  2235. out m2: float;
  2236. out m3: float;
  2237. out m4: float;
  2238. out skew: float;
  2239. out kurtosis: float
  2240. );
  2241. procedure CalcDevSums2to4(data: PSingle; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2242. var
  2243. tm2, tm3, tm4, dev, dev2: float;
  2244. i: SizeInt;
  2245. m2to4Part0, m2to4Part1: TMoments2to4;
  2246. begin
  2247. if N >= RecursiveSumThreshold then
  2248. begin
  2249. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2250. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2251. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2252. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2253. end
  2254. else
  2255. begin
  2256. tm2 := 0;
  2257. tm3 := 0;
  2258. tm4 := 0;
  2259. for i := 0 to N - 1 do
  2260. begin
  2261. dev := data[i] - m1;
  2262. dev2 := sqr(dev);
  2263. tm2 := tm2 + dev2;
  2264. tm3 := tm3 + dev2 * dev;
  2265. tm4 := tm4 + sqr(dev2);
  2266. end;
  2267. m2to4[2] := tm2;
  2268. m2to4[3] := tm3;
  2269. m2to4[4] := tm4;
  2270. end;
  2271. end;
  2272. var
  2273. reciprocalN: float;
  2274. m2to4: TMoments2to4;
  2275. begin
  2276. m1 := 0;
  2277. reciprocalN := 1/N;
  2278. m1 := reciprocalN * sum(data, N);
  2279. CalcDevSums2to4(data, N, m1, m2to4);
  2280. m2 := reciprocalN * m2to4[2];
  2281. m3 := reciprocalN * m2to4[3];
  2282. m4 := reciprocalN * m2to4[4];
  2283. skew := m3 / (sqrt(m2)*m2);
  2284. kurtosis := m4 / (m2 * m2);
  2285. end;
  2286. function norm(const data : array of Single) : float; inline;
  2287. begin
  2288. norm:=Norm(PSingle(@data[0]),High(Data)+1);
  2289. end;
  2290. function norm(const data : PSingle; Const N : Integer) : float;
  2291. begin
  2292. norm:=sqrt(sumofsquares(data,N));
  2293. end;
  2294. {$endif FPC_HAS_TYPE_SINGLE}
  2295. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2296. procedure MeanAndTotalVariance
  2297. (const data: PDouble; N: LongInt; var mu, variance: float);
  2298. function CalcVariance(data: PDouble; N: SizeInt; mu: float): float;
  2299. var
  2300. i: SizeInt;
  2301. begin
  2302. if N>=RecursiveSumThreshold then
  2303. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2304. else
  2305. begin
  2306. result:=0;
  2307. for i:=0 to N-1 do
  2308. result:=result+Sqr(data[i]-mu);
  2309. end;
  2310. end;
  2311. begin
  2312. mu := Mean( data, N );
  2313. variance := CalcVariance( data, N, mu );
  2314. end;
  2315. function stddev(const data : array of Double) : float; inline;
  2316. begin
  2317. Result:=Stddev(PDouble(@Data[0]),High(Data)+1)
  2318. end;
  2319. function stddev(const data : PDouble; Const N : Integer) : float;
  2320. begin
  2321. StdDev:=Sqrt(Variance(Data,N));
  2322. end;
  2323. procedure meanandstddev(const data : array of Double;
  2324. var mean,stddev : float);
  2325. begin
  2326. Meanandstddev(PDouble(@Data[0]),High(Data)+1,Mean,stddev);
  2327. end;
  2328. procedure meanandstddev
  2329. ( const data: PDouble;
  2330. const N: Longint;
  2331. var mean,
  2332. stdDev: Float
  2333. );
  2334. var totalVariance: float;
  2335. begin
  2336. MeanAndTotalVariance( data, N, mean, totalVariance );
  2337. if N < 2 then stdDev := 0
  2338. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2339. end;
  2340. function variance(const data : array of Double) : float; inline;
  2341. begin
  2342. Variance:=Variance(PDouble(@Data[0]),High(Data)+1);
  2343. end;
  2344. function variance(const data : PDouble; Const N : Integer) : float;
  2345. begin
  2346. If N=1 then
  2347. Result:=0
  2348. else
  2349. Result:=TotalVariance(Data,N)/(N-1);
  2350. end;
  2351. function totalvariance(const data : array of Double) : float; inline;
  2352. begin
  2353. Result:=TotalVariance(PDouble(@Data[0]),High(Data)+1);
  2354. end;
  2355. function totalvariance(const data : PDouble; const N : Integer) : float;
  2356. var mu: float;
  2357. begin
  2358. MeanAndTotalVariance( data, N, mu, result );
  2359. end;
  2360. function popnstddev(const data : array of Double) : float;
  2361. begin
  2362. PopnStdDev:=Sqrt(PopnVariance(PDouble(@Data[0]),High(Data)+1));
  2363. end;
  2364. function popnstddev(const data : PDouble; Const N : Integer) : float;
  2365. begin
  2366. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2367. end;
  2368. function popnvariance(const data : array of Double) : float; inline;
  2369. begin
  2370. popnvariance:=popnvariance(PDouble(@data[0]),high(Data)+1);
  2371. end;
  2372. function popnvariance(const data : PDouble; Const N : Integer) : float;
  2373. begin
  2374. PopnVariance:=TotalVariance(Data,N)/N;
  2375. end;
  2376. procedure momentskewkurtosis(const data : array of Double;
  2377. out m1,m2,m3,m4,skew,kurtosis : float);
  2378. begin
  2379. momentskewkurtosis(PDouble(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2380. end;
  2381. procedure momentskewkurtosis(
  2382. const data: pdouble;
  2383. Const N: integer;
  2384. out m1: float;
  2385. out m2: float;
  2386. out m3: float;
  2387. out m4: float;
  2388. out skew: float;
  2389. out kurtosis: float
  2390. );
  2391. procedure CalcDevSums2to4(data: PDouble; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2392. var
  2393. tm2, tm3, tm4, dev, dev2: float;
  2394. i: SizeInt;
  2395. m2to4Part0, m2to4Part1: TMoments2to4;
  2396. begin
  2397. if N >= RecursiveSumThreshold then
  2398. begin
  2399. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2400. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2401. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2402. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2403. end
  2404. else
  2405. begin
  2406. tm2 := 0;
  2407. tm3 := 0;
  2408. tm4 := 0;
  2409. for i := 0 to N - 1 do
  2410. begin
  2411. dev := data[i] - m1;
  2412. dev2 := sqr(dev);
  2413. tm2 := tm2 + dev2;
  2414. tm3 := tm3 + dev2 * dev;
  2415. tm4 := tm4 + sqr(dev2);
  2416. end;
  2417. m2to4[2] := tm2;
  2418. m2to4[3] := tm3;
  2419. m2to4[4] := tm4;
  2420. end;
  2421. end;
  2422. var
  2423. reciprocalN: float;
  2424. m2to4: TMoments2to4;
  2425. begin
  2426. m1 := 0;
  2427. reciprocalN := 1/N;
  2428. m1 := reciprocalN * sum(data, N);
  2429. CalcDevSums2to4(data, N, m1, m2to4);
  2430. m2 := reciprocalN * m2to4[2];
  2431. m3 := reciprocalN * m2to4[3];
  2432. m4 := reciprocalN * m2to4[4];
  2433. skew := m3 / (sqrt(m2)*m2);
  2434. kurtosis := m4 / (m2 * m2);
  2435. end;
  2436. function norm(const data : array of Double) : float; inline;
  2437. begin
  2438. norm:=Norm(PDouble(@data[0]),High(Data)+1);
  2439. end;
  2440. function norm(const data : PDouble; Const N : Integer) : float;
  2441. begin
  2442. norm:=sqrt(sumofsquares(data,N));
  2443. end;
  2444. {$endif FPC_HAS_TYPE_DOUBLE}
  2445. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2446. procedure MeanAndTotalVariance
  2447. (const data: PExtended; N: LongInt; var mu, variance: float);
  2448. function CalcVariance(data: PExtended; N: SizeInt; mu: float): float;
  2449. var
  2450. i: SizeInt;
  2451. begin
  2452. if N>=RecursiveSumThreshold then
  2453. result:=CalcVariance(data,SizeUint(N) div 2,mu)+CalcVariance(data+SizeUint(N) div 2,N-SizeUint(N) div 2,mu)
  2454. else
  2455. begin
  2456. result:=0;
  2457. for i:=0 to N-1 do
  2458. result:=result+Sqr(data[i]-mu);
  2459. end;
  2460. end;
  2461. begin
  2462. mu := Mean( data, N );
  2463. variance := CalcVariance( data, N, mu );
  2464. end;
  2465. function stddev(const data : array of Extended) : float; inline;
  2466. begin
  2467. Result:=Stddev(PExtended(@Data[0]),High(Data)+1)
  2468. end;
  2469. function stddev(const data : PExtended; Const N : Integer) : float;
  2470. begin
  2471. StdDev:=Sqrt(Variance(Data,N));
  2472. end;
  2473. procedure meanandstddev(const data : array of Extended;
  2474. var mean,stddev : float); inline;
  2475. begin
  2476. Meanandstddev(PExtended(@Data[0]),High(Data)+1,Mean,stddev);
  2477. end;
  2478. procedure meanandstddev
  2479. ( const data: PExtended;
  2480. const N: Longint;
  2481. var mean,
  2482. stdDev: Float
  2483. );
  2484. var totalVariance: float;
  2485. begin
  2486. MeanAndTotalVariance( data, N, mean, totalVariance );
  2487. if N < 2 then stdDev := 0
  2488. else stdDev := Sqrt( totalVariance / ( N - 1 ) );
  2489. end;
  2490. function variance(const data : array of Extended) : float; inline;
  2491. begin
  2492. Variance:=Variance(PExtended(@Data[0]),High(Data)+1);
  2493. end;
  2494. function variance(const data : PExtended; Const N : Integer) : float;
  2495. begin
  2496. If N=1 then
  2497. Result:=0
  2498. else
  2499. Result:=TotalVariance(Data,N)/(N-1);
  2500. end;
  2501. function totalvariance(const data : array of Extended) : float; inline;
  2502. begin
  2503. Result:=TotalVariance(PExtended(@Data[0]),High(Data)+1);
  2504. end;
  2505. function totalvariance(const data : PExtended;Const N : Integer) : float;
  2506. var mu: float;
  2507. begin
  2508. MeanAndTotalVariance( data, N, mu, result );
  2509. end;
  2510. function popnstddev(const data : array of Extended) : float;
  2511. begin
  2512. PopnStdDev:=Sqrt(PopnVariance(PExtended(@Data[0]),High(Data)+1));
  2513. end;
  2514. function popnstddev(const data : PExtended; Const N : Integer) : float;
  2515. begin
  2516. PopnStdDev:=Sqrt(PopnVariance(Data,N));
  2517. end;
  2518. function popnvariance(const data : array of Extended) : float; inline;
  2519. begin
  2520. popnvariance:=popnvariance(PExtended(@data[0]),high(Data)+1);
  2521. end;
  2522. function popnvariance(const data : PExtended; Const N : Integer) : float;
  2523. begin
  2524. PopnVariance:=TotalVariance(Data,N)/N;
  2525. end;
  2526. procedure momentskewkurtosis(const data : array of Extended;
  2527. out m1,m2,m3,m4,skew,kurtosis : float); inline;
  2528. begin
  2529. momentskewkurtosis(PExtended(@Data[0]),High(Data)+1,m1,m2,m3,m4,skew,kurtosis);
  2530. end;
  2531. procedure momentskewkurtosis(
  2532. const data: pExtended;
  2533. Const N: Integer;
  2534. out m1: float;
  2535. out m2: float;
  2536. out m3: float;
  2537. out m4: float;
  2538. out skew: float;
  2539. out kurtosis: float
  2540. );
  2541. procedure CalcDevSums2to4(data: PExtended; N: SizeInt; m1: float; out m2to4: TMoments2to4);
  2542. var
  2543. tm2, tm3, tm4, dev, dev2: float;
  2544. i: SizeInt;
  2545. m2to4Part0, m2to4Part1: TMoments2to4;
  2546. begin
  2547. if N >= RecursiveSumThreshold then
  2548. begin
  2549. CalcDevSums2to4(data, SizeUint(N) div 2, m1, m2to4Part0);
  2550. CalcDevSums2to4(data + SizeUint(N) div 2, N - SizeUint(N) div 2, m1, m2to4Part1);
  2551. for i := Low(TMoments2to4) to High(TMoments2to4) do
  2552. m2to4[i] := m2to4Part0[i] + m2to4Part1[i];
  2553. end
  2554. else
  2555. begin
  2556. tm2 := 0;
  2557. tm3 := 0;
  2558. tm4 := 0;
  2559. for i := 0 to N - 1 do
  2560. begin
  2561. dev := data[i] - m1;
  2562. dev2 := sqr(dev);
  2563. tm2 := tm2 + dev2;
  2564. tm3 := tm3 + dev2 * dev;
  2565. tm4 := tm4 + sqr(dev2);
  2566. end;
  2567. m2to4[2] := tm2;
  2568. m2to4[3] := tm3;
  2569. m2to4[4] := tm4;
  2570. end;
  2571. end;
  2572. var
  2573. reciprocalN: float;
  2574. m2to4: TMoments2to4;
  2575. begin
  2576. m1 := 0;
  2577. reciprocalN := 1/N;
  2578. m1 := reciprocalN * sum(data, N);
  2579. CalcDevSums2to4(data, N, m1, m2to4);
  2580. m2 := reciprocalN * m2to4[2];
  2581. m3 := reciprocalN * m2to4[3];
  2582. m4 := reciprocalN * m2to4[4];
  2583. skew := m3 / (sqrt(m2)*m2);
  2584. kurtosis := m4 / (m2 * m2);
  2585. end;
  2586. function norm(const data : array of Extended) : float; inline;
  2587. begin
  2588. norm:=Norm(PExtended(@data[0]),High(Data)+1);
  2589. end;
  2590. function norm(const data : PExtended; Const N : Integer) : float;
  2591. begin
  2592. norm:=sqrt(sumofsquares(data,N));
  2593. end;
  2594. {$endif FPC_HAS_TYPE_EXTENDED}
  2595. function MinIntValue(const Data: array of Integer): Integer;
  2596. var
  2597. I: SizeInt;
  2598. begin
  2599. Result := Data[Low(Data)];
  2600. For I := Succ(Low(Data)) To High(Data) Do
  2601. If Data[I] < Result Then Result := Data[I];
  2602. end;
  2603. function MaxIntValue(const Data: array of Integer): Integer;
  2604. var
  2605. I: SizeInt;
  2606. begin
  2607. Result := Data[Low(Data)];
  2608. For I := Succ(Low(Data)) To High(Data) Do
  2609. If Data[I] > Result Then Result := Data[I];
  2610. end;
  2611. function MinValue(const Data: array of Integer): Integer; inline;
  2612. begin
  2613. Result:=MinValue(Pinteger(@Data[0]),High(Data)+1)
  2614. end;
  2615. function MinValue(const Data: PInteger; Const N : Integer): Integer;
  2616. var
  2617. I: SizeInt;
  2618. begin
  2619. Result := Data[0];
  2620. For I := 1 To N-1 do
  2621. If Data[I] < Result Then Result := Data[I];
  2622. end;
  2623. function MaxValue(const Data: array of Integer): Integer; inline;
  2624. begin
  2625. Result:=MaxValue(PInteger(@Data[0]),High(Data)+1)
  2626. end;
  2627. function maxvalue(const data : PInteger; Const N : Integer) : Integer;
  2628. var
  2629. i : SizeInt;
  2630. begin
  2631. { get an initial value }
  2632. maxvalue:=data[0];
  2633. for i:=1 to N-1 do
  2634. if data[i]>maxvalue then
  2635. maxvalue:=data[i];
  2636. end;
  2637. {$ifdef FPC_HAS_TYPE_SINGLE}
  2638. function minvalue(const data : array of Single) : Single; inline;
  2639. begin
  2640. Result:=minvalue(PSingle(@data[0]),High(Data)+1);
  2641. end;
  2642. function minvalue(const data : PSingle; Const N : Integer) : Single;
  2643. var
  2644. i : SizeInt;
  2645. begin
  2646. { get an initial value }
  2647. minvalue:=data[0];
  2648. for i:=1 to N-1 do
  2649. if data[i]<minvalue then
  2650. minvalue:=data[i];
  2651. end;
  2652. function maxvalue(const data : array of Single) : Single; inline;
  2653. begin
  2654. Result:=maxvalue(PSingle(@data[0]),High(Data)+1);
  2655. end;
  2656. function maxvalue(const data : PSingle; Const N : Integer) : Single;
  2657. var
  2658. i : SizeInt;
  2659. begin
  2660. { get an initial value }
  2661. maxvalue:=data[0];
  2662. for i:=1 to N-1 do
  2663. if data[i]>maxvalue then
  2664. maxvalue:=data[i];
  2665. end;
  2666. {$endif FPC_HAS_TYPE_SINGLE}
  2667. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2668. function minvalue(const data : array of Double) : Double; inline;
  2669. begin
  2670. Result:=minvalue(PDouble(@data[0]),High(Data)+1);
  2671. end;
  2672. function minvalue(const data : PDouble; Const N : Integer) : Double;
  2673. var
  2674. i : SizeInt;
  2675. begin
  2676. { get an initial value }
  2677. minvalue:=data[0];
  2678. for i:=1 to N-1 do
  2679. if data[i]<minvalue then
  2680. minvalue:=data[i];
  2681. end;
  2682. function maxvalue(const data : array of Double) : Double; inline;
  2683. begin
  2684. Result:=maxvalue(PDouble(@data[0]),High(Data)+1);
  2685. end;
  2686. function maxvalue(const data : PDouble; Const N : Integer) : Double;
  2687. var
  2688. i : SizeInt;
  2689. begin
  2690. { get an initial value }
  2691. maxvalue:=data[0];
  2692. for i:=1 to N-1 do
  2693. if data[i]>maxvalue then
  2694. maxvalue:=data[i];
  2695. end;
  2696. {$endif FPC_HAS_TYPE_DOUBLE}
  2697. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2698. function minvalue(const data : array of Extended) : Extended; inline;
  2699. begin
  2700. Result:=minvalue(PExtended(@data[0]),High(Data)+1);
  2701. end;
  2702. function minvalue(const data : PExtended; Const N : Integer) : Extended;
  2703. var
  2704. i : SizeInt;
  2705. begin
  2706. { get an initial value }
  2707. minvalue:=data[0];
  2708. for i:=1 to N-1 do
  2709. if data[i]<minvalue then
  2710. minvalue:=data[i];
  2711. end;
  2712. function maxvalue(const data : array of Extended) : Extended; inline;
  2713. begin
  2714. Result:=maxvalue(PExtended(@data[0]),High(Data)+1);
  2715. end;
  2716. function maxvalue(const data : PExtended; Const N : Integer) : Extended;
  2717. var
  2718. i : SizeInt;
  2719. begin
  2720. { get an initial value }
  2721. maxvalue:=data[0];
  2722. for i:=1 to N-1 do
  2723. if data[i]>maxvalue then
  2724. maxvalue:=data[i];
  2725. end;
  2726. {$endif FPC_HAS_TYPE_EXTENDED}
  2727. function Min(a, b: Integer): Integer;inline;
  2728. begin
  2729. if a < b then
  2730. Result := a
  2731. else
  2732. Result := b;
  2733. end;
  2734. function Max(a, b: Integer): Integer;inline;
  2735. begin
  2736. if a > b then
  2737. Result := a
  2738. else
  2739. Result := b;
  2740. end;
  2741. {
  2742. function Min(a, b: Cardinal): Cardinal;inline;
  2743. begin
  2744. if a < b then
  2745. Result := a
  2746. else
  2747. Result := b;
  2748. end;
  2749. function Max(a, b: Cardinal): Cardinal;inline;
  2750. begin
  2751. if a > b then
  2752. Result := a
  2753. else
  2754. Result := b;
  2755. end;
  2756. }
  2757. function Min(a, b: Int64): Int64;inline;
  2758. begin
  2759. if a < b then
  2760. Result := a
  2761. else
  2762. Result := b;
  2763. end;
  2764. function Max(a, b: Int64): Int64;inline;
  2765. begin
  2766. if a > b then
  2767. Result := a
  2768. else
  2769. Result := b;
  2770. end;
  2771. function Min(a, b: QWord): QWord; inline;
  2772. begin
  2773. if a < b then
  2774. Result := a
  2775. else
  2776. Result := b;
  2777. end;
  2778. function Max(a, b: QWord): Qword;inline;
  2779. begin
  2780. if a > b then
  2781. Result := a
  2782. else
  2783. Result := b;
  2784. end;
  2785. {$ifdef FPC_HAS_TYPE_SINGLE}
  2786. function Min(a, b: Single): Single;inline;
  2787. begin
  2788. if a < b then
  2789. Result := a
  2790. else
  2791. Result := b;
  2792. end;
  2793. function Max(a, b: Single): Single;inline;
  2794. begin
  2795. if a > b then
  2796. Result := a
  2797. else
  2798. Result := b;
  2799. end;
  2800. {$endif FPC_HAS_TYPE_SINGLE}
  2801. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2802. function Min(a, b: Double): Double;inline;
  2803. begin
  2804. if a < b then
  2805. Result := a
  2806. else
  2807. Result := b;
  2808. end;
  2809. function Max(a, b: Double): Double;inline;
  2810. begin
  2811. if a > b then
  2812. Result := a
  2813. else
  2814. Result := b;
  2815. end;
  2816. {$endif FPC_HAS_TYPE_DOUBLE}
  2817. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2818. function Min(a, b: Extended): Extended;inline;
  2819. begin
  2820. if a < b then
  2821. Result := a
  2822. else
  2823. Result := b;
  2824. end;
  2825. function Max(a, b: Extended): Extended;inline;
  2826. begin
  2827. if a > b then
  2828. Result := a
  2829. else
  2830. Result := b;
  2831. end;
  2832. {$endif FPC_HAS_TYPE_EXTENDED}
  2833. function InRange(const AValue, AMin, AMax: Integer): Boolean;inline;
  2834. begin
  2835. Result:=(AValue>=AMin) and (AValue<=AMax);
  2836. end;
  2837. function InRange(const AValue, AMin, AMax: Int64): Boolean;inline;
  2838. begin
  2839. Result:=(AValue>=AMin) and (AValue<=AMax);
  2840. end;
  2841. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2842. function InRange(const AValue, AMin, AMax: Double): Boolean;inline;
  2843. begin
  2844. Result:=(AValue>=AMin) and (AValue<=AMax);
  2845. end;
  2846. {$endif FPC_HAS_TYPE_DOUBLE}
  2847. function EnsureRange(const AValue, AMin, AMax: Integer): Integer;inline;
  2848. begin
  2849. Result:=AValue;
  2850. If Result<AMin then
  2851. Result:=AMin;
  2852. if Result>AMax then
  2853. Result:=AMax;
  2854. end;
  2855. function EnsureRange(const AValue, AMin, AMax: Int64): Int64;inline;
  2856. begin
  2857. Result:=AValue;
  2858. If Result<AMin then
  2859. Result:=AMin;
  2860. if Result>AMax then
  2861. Result:=AMax;
  2862. end;
  2863. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2864. function EnsureRange(const AValue, AMin, AMax: Double): Double;inline;
  2865. begin
  2866. Result:=AValue;
  2867. If Result<AMin then
  2868. Result:=AMin;
  2869. if Result>AMax then
  2870. Result:=AMax;
  2871. end;
  2872. {$endif FPC_HAS_TYPE_DOUBLE}
  2873. Const
  2874. EZeroResolution = Extended(1E-16);
  2875. DZeroResolution = Double(1E-12);
  2876. SZeroResolution = Single(1E-4);
  2877. function IsZero(const A: Single; Epsilon: Single): Boolean;
  2878. begin
  2879. if (Epsilon=0) then
  2880. Epsilon:=SZeroResolution;
  2881. Result:=Abs(A)<=Epsilon;
  2882. end;
  2883. function IsZero(const A: Single): Boolean;inline;
  2884. begin
  2885. Result:=IsZero(A,single(SZeroResolution));
  2886. end;
  2887. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2888. function IsZero(const A: Double; Epsilon: Double): Boolean;
  2889. begin
  2890. if (Epsilon=0) then
  2891. Epsilon:=DZeroResolution;
  2892. Result:=Abs(A)<=Epsilon;
  2893. end;
  2894. function IsZero(const A: Double): Boolean;inline;
  2895. begin
  2896. Result:=IsZero(A,DZeroResolution);
  2897. end;
  2898. {$endif FPC_HAS_TYPE_DOUBLE}
  2899. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2900. function IsZero(const A: Extended; Epsilon: Extended): Boolean;
  2901. begin
  2902. if (Epsilon=0) then
  2903. Epsilon:=EZeroResolution;
  2904. Result:=Abs(A)<=Epsilon;
  2905. end;
  2906. function IsZero(const A: Extended): Boolean;inline;
  2907. begin
  2908. Result:=IsZero(A,EZeroResolution);
  2909. end;
  2910. {$endif FPC_HAS_TYPE_EXTENDED}
  2911. type
  2912. TSplitDouble = packed record
  2913. cards: Array[0..1] of cardinal;
  2914. end;
  2915. TSplitExtended = packed record
  2916. cards: Array[0..1] of cardinal;
  2917. w: word;
  2918. end;
  2919. function IsNan(const d : Single): Boolean; overload;
  2920. begin
  2921. result:=(longword(d) and $7fffffff)>$7f800000;
  2922. end;
  2923. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2924. function IsNan(const d : Double): Boolean;
  2925. var
  2926. fraczero, expMaximal: boolean;
  2927. begin
  2928. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2929. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2930. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2931. (TSplitDouble(d).cards[1] = 0);
  2932. {$else FPC_BIG_ENDIAN}
  2933. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2934. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2935. (TSplitDouble(d).cards[0] = 0);
  2936. {$endif FPC_BIG_ENDIAN}
  2937. Result:=expMaximal and not(fraczero);
  2938. end;
  2939. {$endif FPC_HAS_TYPE_DOUBLE}
  2940. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2941. function IsNan(const d : Extended): Boolean; overload;
  2942. var
  2943. fraczero, expMaximal: boolean;
  2944. begin
  2945. {$ifdef FPC_BIG_ENDIAN}
  2946. {$error no support for big endian extended type yet}
  2947. {$else FPC_BIG_ENDIAN}
  2948. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2949. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2950. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2951. {$endif FPC_BIG_ENDIAN}
  2952. Result:=expMaximal and not(fraczero);
  2953. end;
  2954. {$endif FPC_HAS_TYPE_EXTENDED}
  2955. function IsInfinite(const d : Single): Boolean; overload;
  2956. begin
  2957. result:=(longword(d) and $7fffffff)=$7f800000;
  2958. end;
  2959. {$ifdef FPC_HAS_TYPE_DOUBLE}
  2960. function IsInfinite(const d : Double): Boolean; overload;
  2961. var
  2962. fraczero, expMaximal: boolean;
  2963. begin
  2964. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2965. expMaximal := ((TSplitDouble(d).cards[0] shr 20) and $7ff) = 2047;
  2966. fraczero:= (TSplitDouble(d).cards[0] and $fffff = 0) and
  2967. (TSplitDouble(d).cards[1] = 0);
  2968. {$else FPC_BIG_ENDIAN}
  2969. expMaximal := ((TSplitDouble(d).cards[1] shr 20) and $7ff) = 2047;
  2970. fraczero := (TSplitDouble(d).cards[1] and $fffff = 0) and
  2971. (TSplitDouble(d).cards[0] = 0);
  2972. {$endif FPC_BIG_ENDIAN}
  2973. Result:=expMaximal and fraczero;
  2974. end;
  2975. {$endif FPC_HAS_TYPE_DOUBLE}
  2976. {$ifdef FPC_HAS_TYPE_EXTENDED}
  2977. function IsInfinite(const d : Extended): Boolean; overload;
  2978. var
  2979. fraczero, expMaximal: boolean;
  2980. begin
  2981. {$ifdef FPC_BIG_ENDIAN}
  2982. {$error no support for big endian extended type yet}
  2983. {$else FPC_BIG_ENDIAN}
  2984. expMaximal := (TSplitExtended(d).w and $7fff) = 32767;
  2985. fraczero := (TSplitExtended(d).cards[0] = 0) and
  2986. ((TSplitExtended(d).cards[1] and $7fffffff) = 0);
  2987. {$endif FPC_BIG_ENDIAN}
  2988. Result:=expMaximal and fraczero;
  2989. end;
  2990. {$endif FPC_HAS_TYPE_EXTENDED}
  2991. function copysign(x,y: float): float;
  2992. begin
  2993. {$if defined(FPC_HAS_TYPE_FLOAT128)}
  2994. {$error copysign not yet implemented for float128}
  2995. {$elseif defined(FPC_HAS_TYPE_EXTENDED)}
  2996. TSplitExtended(x).w:=(TSplitExtended(x).w and $7fff) or (TSplitExtended(y).w and $8000);
  2997. {$elseif defined(FPC_HAS_TYPE_DOUBLE)}
  2998. {$if defined(FPC_BIG_ENDIAN) or defined(FPC_DOUBLE_HILO_SWAPPED)}
  2999. TSplitDouble(x).cards[0]:=(TSplitDouble(x).cards[0] and $7fffffff) or (TSplitDouble(y).cards[0] and longword($80000000));
  3000. {$else}
  3001. TSplitDouble(x).cards[1]:=(TSplitDouble(x).cards[1] and $7fffffff) or (TSplitDouble(y).cards[1] and longword($80000000));
  3002. {$endif}
  3003. {$else}
  3004. longword(x):=longword(x and $7fffffff) or (longword(y) and longword($80000000));
  3005. {$endif}
  3006. result:=x;
  3007. end;
  3008. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3009. function SameValue(const A, B: Extended; Epsilon: Extended): Boolean;
  3010. begin
  3011. if (Epsilon=0) then
  3012. Epsilon:=Max(Min(Abs(A),Abs(B))*EZeroResolution,EZeroResolution);
  3013. if (A>B) then
  3014. Result:=((A-B)<=Epsilon)
  3015. else
  3016. Result:=((B-A)<=Epsilon);
  3017. end;
  3018. function SameValue(const A, B: Extended): Boolean;inline;
  3019. begin
  3020. Result:=SameValue(A,B,0.0);
  3021. end;
  3022. {$endif FPC_HAS_TYPE_EXTENDED}
  3023. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3024. function SameValue(const A, B: Double): Boolean;inline;
  3025. begin
  3026. Result:=SameValue(A,B,0.0);
  3027. end;
  3028. function SameValue(const A, B: Double; Epsilon: Double): Boolean;
  3029. begin
  3030. if (Epsilon=0) then
  3031. Epsilon:=Max(Min(Abs(A),Abs(B))*DZeroResolution,DZeroResolution);
  3032. if (A>B) then
  3033. Result:=((A-B)<=Epsilon)
  3034. else
  3035. Result:=((B-A)<=Epsilon);
  3036. end;
  3037. {$endif FPC_HAS_TYPE_DOUBLE}
  3038. function SameValue(const A, B: Single): Boolean;inline;
  3039. begin
  3040. Result:=SameValue(A,B,0);
  3041. end;
  3042. function SameValue(const A, B: Single; Epsilon: Single): Boolean;
  3043. begin
  3044. if (Epsilon=0) then
  3045. Epsilon:=Max(Min(Abs(A),Abs(B))*SZeroResolution,SZeroResolution);
  3046. if (A>B) then
  3047. Result:=((A-B)<=Epsilon)
  3048. else
  3049. Result:=((B-A)<=Epsilon);
  3050. end;
  3051. // Some CPUs probably allow a faster way of doing this in a single operation...
  3052. // There weshould define FPC_MATH_HAS_CPUDIVMOD in the header mathuh.inc and implement it using asm.
  3053. {$ifndef FPC_MATH_HAS_DIVMOD}
  3054. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: Word);
  3055. begin
  3056. if Dividend < 0 then
  3057. begin
  3058. { Use DivMod with >=0 dividend }
  3059. Dividend:=-Dividend;
  3060. { The documented behavior of Pascal's div/mod operators and DivMod
  3061. on negative dividends is to return Result closer to zero and
  3062. a negative Remainder. Which means that we can just negate both
  3063. Result and Remainder, and all it's Ok. }
  3064. Result:=-(Dividend Div Divisor);
  3065. Remainder:=-(Dividend+(Result*Divisor));
  3066. end
  3067. else
  3068. begin
  3069. Result:=Dividend Div Divisor;
  3070. Remainder:=Dividend-(Result*Divisor);
  3071. end;
  3072. end;
  3073. procedure DivMod(Dividend: LongInt; Divisor: Word; var Result, Remainder: SmallInt);
  3074. begin
  3075. if Dividend < 0 then
  3076. begin
  3077. { Use DivMod with >=0 dividend }
  3078. Dividend:=-Dividend;
  3079. { The documented behavior of Pascal's div/mod operators and DivMod
  3080. on negative dividends is to return Result closer to zero and
  3081. a negative Remainder. Which means that we can just negate both
  3082. Result and Remainder, and all it's Ok. }
  3083. Result:=-(Dividend Div Divisor);
  3084. Remainder:=-(Dividend+(Result*Divisor));
  3085. end
  3086. else
  3087. begin
  3088. Result:=Dividend Div Divisor;
  3089. Remainder:=Dividend-(Result*Divisor);
  3090. end;
  3091. end;
  3092. procedure DivMod(Dividend: DWord; Divisor: DWord; var Result, Remainder: DWord);
  3093. begin
  3094. Result:=Dividend Div Divisor;
  3095. Remainder:=Dividend-(Result*Divisor);
  3096. end;
  3097. procedure DivMod(Dividend: LongInt; Divisor: LongInt; var Result, Remainder: LongInt);
  3098. begin
  3099. if Dividend < 0 then
  3100. begin
  3101. { Use DivMod with >=0 dividend }
  3102. Dividend:=-Dividend;
  3103. { The documented behavior of Pascal's div/mod operators and DivMod
  3104. on negative dividends is to return Result closer to zero and
  3105. a negative Remainder. Which means that we can just negate both
  3106. Result and Remainder, and all it's Ok. }
  3107. Result:=-(Dividend Div Divisor);
  3108. Remainder:=-(Dividend+(Result*Divisor));
  3109. end
  3110. else
  3111. begin
  3112. Result:=Dividend Div Divisor;
  3113. Remainder:=Dividend-(Result*Divisor);
  3114. end;
  3115. end;
  3116. {$endif FPC_MATH_HAS_DIVMOD}
  3117. { Floating point modulo}
  3118. {$ifdef FPC_HAS_TYPE_SINGLE}
  3119. function FMod(const a, b: Single): Single;inline;overload;
  3120. begin
  3121. result:= a-b * Int(a/b);
  3122. end;
  3123. {$endif FPC_HAS_TYPE_SINGLE}
  3124. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3125. function FMod(const a, b: Double): Double;inline;overload;
  3126. begin
  3127. result:= a-b * Int(a/b);
  3128. end;
  3129. {$endif FPC_HAS_TYPE_DOUBLE}
  3130. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3131. function FMod(const a, b: Extended): Extended;inline;overload;
  3132. begin
  3133. result:= a-b * Int(a/b);
  3134. end;
  3135. {$endif FPC_HAS_TYPE_EXTENDED}
  3136. operator mod(const a,b:float) c:float;inline;
  3137. begin
  3138. c:= a-b * Int(a/b);
  3139. if SameValue(abs(c),abs(b)) then
  3140. c:=0.0;
  3141. end;
  3142. function ifthen(val:boolean;const iftrue:integer; const iffalse:integer= 0) :integer;
  3143. begin
  3144. if val then result:=iftrue else result:=iffalse;
  3145. end;
  3146. function ifthen(val:boolean;const iftrue:int64 ; const iffalse:int64 = 0) :int64;
  3147. begin
  3148. if val then result:=iftrue else result:=iffalse;
  3149. end;
  3150. function ifthen(val:boolean;const iftrue:double ; const iffalse:double =0.0):double;
  3151. begin
  3152. if val then result:=iftrue else result:=iffalse;
  3153. end;
  3154. // dilemma here. asm can do the two comparisons in one go?
  3155. // but pascal is portable and can be inlined. Ah well, we need purepascal's anyway:
  3156. function CompareValue(const A, B : Integer): TValueRelationship;
  3157. begin
  3158. result:=GreaterThanValue;
  3159. if a=b then
  3160. result:=EqualsValue
  3161. else
  3162. if a<b then
  3163. result:=LessThanValue;
  3164. end;
  3165. function CompareValue(const A, B: Int64): TValueRelationship;
  3166. begin
  3167. result:=GreaterThanValue;
  3168. if a=b then
  3169. result:=EqualsValue
  3170. else
  3171. if a<b then
  3172. result:=LessThanValue;
  3173. end;
  3174. function CompareValue(const A, B: QWord): TValueRelationship;
  3175. begin
  3176. result:=GreaterThanValue;
  3177. if a=b then
  3178. result:=EqualsValue
  3179. else
  3180. if a<b then
  3181. result:=LessThanValue;
  3182. end;
  3183. {$ifdef FPC_HAS_TYPE_SINGLE}
  3184. function CompareValue(const A, B: Single; delta: Single = 0.0): TValueRelationship;
  3185. begin
  3186. result:=GreaterThanValue;
  3187. if abs(a-b)<=delta then
  3188. result:=EqualsValue
  3189. else
  3190. if a<b then
  3191. result:=LessThanValue;
  3192. end;
  3193. {$endif}
  3194. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3195. function CompareValue(const A, B: Double; delta: Double = 0.0): TValueRelationship;
  3196. begin
  3197. result:=GreaterThanValue;
  3198. if abs(a-b)<=delta then
  3199. result:=EqualsValue
  3200. else
  3201. if a<b then
  3202. result:=LessThanValue;
  3203. end;
  3204. {$endif}
  3205. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3206. function CompareValue (const A, B: Extended; delta: Extended = 0.0): TValueRelationship;
  3207. begin
  3208. result:=GreaterThanValue;
  3209. if abs(a-b)<=delta then
  3210. result:=EqualsValue
  3211. else
  3212. if a<b then
  3213. result:=LessThanValue;
  3214. end;
  3215. {$endif}
  3216. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3217. function RoundTo(const AValue: Double; const Digits: TRoundToRange): Double;
  3218. var
  3219. RV : Double;
  3220. begin
  3221. RV:=IntPower(10,Digits);
  3222. Result:=Round(AValue/RV)*RV;
  3223. end;
  3224. {$endif}
  3225. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3226. function RoundTo(const AVAlue: Extended; const Digits: TRoundToRange): Extended;
  3227. var
  3228. RV : Extended;
  3229. begin
  3230. RV:=IntPower(10,Digits);
  3231. Result:=Round(AValue/RV)*RV;
  3232. end;
  3233. {$endif}
  3234. {$ifdef FPC_HAS_TYPE_SINGLE}
  3235. function RoundTo(const AValue: Single; const Digits: TRoundToRange): Single;
  3236. var
  3237. RV : Single;
  3238. begin
  3239. RV:=IntPower(10,Digits);
  3240. Result:=Round(AValue/RV)*RV;
  3241. end;
  3242. {$endif}
  3243. {$ifdef FPC_HAS_TYPE_SINGLE}
  3244. function SimpleRoundTo(const AValue: Single; const Digits: TRoundToRange = -2): Single;
  3245. var
  3246. RV : Single;
  3247. begin
  3248. RV := IntPower(10, -Digits);
  3249. if AValue < 0 then
  3250. Result := Int((AValue*RV) - 0.5)/RV
  3251. else
  3252. Result := Int((AValue*RV) + 0.5)/RV;
  3253. end;
  3254. {$endif}
  3255. {$ifdef FPC_HAS_TYPE_DOUBLE}
  3256. function SimpleRoundTo(const AValue: Double; const Digits: TRoundToRange = -2): Double;
  3257. var
  3258. RV : Double;
  3259. begin
  3260. RV := IntPower(10, -Digits);
  3261. if AValue < 0 then
  3262. Result := Int((AValue*RV) - 0.5)/RV
  3263. else
  3264. Result := Int((AValue*RV) + 0.5)/RV;
  3265. end;
  3266. {$endif}
  3267. {$ifdef FPC_HAS_TYPE_EXTENDED}
  3268. function SimpleRoundTo(const AValue: Extended; const Digits: TRoundToRange = -2): Extended;
  3269. var
  3270. RV : Extended;
  3271. begin
  3272. RV := IntPower(10, -Digits);
  3273. if AValue < 0 then
  3274. Result := Int((AValue*RV) - 0.5)/RV
  3275. else
  3276. Result := Int((AValue*RV) + 0.5)/RV;
  3277. end;
  3278. {$endif}
  3279. function RandomFrom(const AValues: array of Double): Double; overload;
  3280. begin
  3281. result:=AValues[random(High(AValues)+1)];
  3282. end;
  3283. function RandomFrom(const AValues: array of Integer): Integer; overload;
  3284. begin
  3285. result:=AValues[random(High(AValues)+1)];
  3286. end;
  3287. function RandomFrom(const AValues: array of Int64): Int64; overload;
  3288. begin
  3289. result:=AValues[random(High(AValues)+1)];
  3290. end;
  3291. {$if FPC_FULLVERSION >=30101}
  3292. generic function RandomFrom<T>(const AValues:array of T):T;
  3293. begin
  3294. result:=AValues[random(High(AValues)+1)];
  3295. end;
  3296. {$endif}
  3297. function FutureValue(ARate: Float; NPeriods: Integer;
  3298. APayment, APresentValue: Float; APaymentTime: TPaymentTime): Float;
  3299. var
  3300. q, qn, factor: Float;
  3301. begin
  3302. if ARate = 0 then
  3303. Result := -APresentValue - APayment * NPeriods
  3304. else begin
  3305. q := 1.0 + ARate;
  3306. qn := power(q, NPeriods);
  3307. factor := (qn - 1) / (q - 1);
  3308. if APaymentTime = ptStartOfPeriod then
  3309. factor := factor * q;
  3310. Result := -(APresentValue * qn + APayment*factor);
  3311. end;
  3312. end;
  3313. function InterestRate(NPeriods: Integer; APayment, APresentValue, AFutureValue: Float;
  3314. APaymentTime: TPaymentTime): Float;
  3315. { The interest rate cannot be calculated analytically. We solve the equation
  3316. numerically by means of the Newton method:
  3317. - guess value for the interest reate
  3318. - calculate at which interest rate the tangent of the curve fv(rate)
  3319. (straight line!) has the requested future vale.
  3320. - use this rate for the next iteration. }
  3321. const
  3322. DELTA = 0.001;
  3323. EPS = 1E-9; // required precision of interest rate (after typ. 6 iterations)
  3324. MAXIT = 20; // max iteration count to protect agains non-convergence
  3325. var
  3326. r1, r2, dr: Float;
  3327. fv1, fv2: Float;
  3328. iteration: Integer;
  3329. begin
  3330. iteration := 0;
  3331. r1 := 0.05; // inital guess
  3332. repeat
  3333. r2 := r1 + DELTA;
  3334. fv1 := FutureValue(r1, NPeriods, APayment, APresentValue, APaymentTime);
  3335. fv2 := FutureValue(r2, NPeriods, APayment, APresentValue, APaymentTime);
  3336. dr := (AFutureValue - fv1) / (fv2 - fv1) * delta; // tangent at fv(r)
  3337. r1 := r1 + dr; // next guess
  3338. inc(iteration);
  3339. until (abs(dr) < EPS) or (iteration >= MAXIT);
  3340. Result := r1;
  3341. end;
  3342. function NumberOfPeriods(ARate, APayment, APresentValue, AFutureValue: Float;
  3343. APaymentTime: TPaymentTime): Float;
  3344. { Solve the cash flow equation (1) for q^n and take the logarithm }
  3345. var
  3346. q, x1, x2: Float;
  3347. begin
  3348. if ARate = 0 then
  3349. Result := -(APresentValue + AFutureValue) / APayment
  3350. else begin
  3351. q := 1.0 + ARate;
  3352. if APaymentTime = ptStartOfPeriod then
  3353. APayment := APayment * q;
  3354. x1 := APayment - AFutureValue * ARate;
  3355. x2 := APayment + APresentValue * ARate;
  3356. if (x2 = 0) // we have to divide by x2
  3357. or (sign(x1) * sign(x2) < 0) // the argument of the log is negative
  3358. then
  3359. Result := Infinity
  3360. else begin
  3361. Result := ln(x1/x2) / ln(q);
  3362. end;
  3363. end;
  3364. end;
  3365. function Payment(ARate: Float; NPeriods: Integer;
  3366. APresentValue, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3367. var
  3368. q, qn, factor: Float;
  3369. begin
  3370. if ARate = 0 then
  3371. Result := -(AFutureValue + APresentValue) / NPeriods
  3372. else begin
  3373. q := 1.0 + ARate;
  3374. qn := power(q, NPeriods);
  3375. factor := (qn - 1) / (q - 1);
  3376. if APaymentTime = ptStartOfPeriod then
  3377. factor := factor * q;
  3378. Result := -(AFutureValue + APresentValue * qn) / factor;
  3379. end;
  3380. end;
  3381. function PresentValue(ARate: Float; NPeriods: Integer;
  3382. APayment, AFutureValue: Float; APaymentTime: TPaymentTime): Float;
  3383. var
  3384. q, qn, factor: Float;
  3385. begin
  3386. if ARate = 0.0 then
  3387. Result := -AFutureValue - APayment * NPeriods
  3388. else begin
  3389. q := 1.0 + ARate;
  3390. qn := power(q, NPeriods);
  3391. factor := (qn - 1) / (q - 1);
  3392. if APaymentTime = ptStartOfPeriod then
  3393. factor := factor * q;
  3394. Result := -(AFutureValue + APayment*factor) / qn;
  3395. end;
  3396. end;
  3397. {$else}
  3398. implementation
  3399. {$endif FPUNONE}
  3400. end.