math.pp 95 KB

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