fmtbcd.pp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005-2006 by the Free Pascal development team
  4. and Gehard Scholz
  5. It contains the Free Pascal BCD implementation
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { "Programming is the time between two bugs" }
  13. { (last words of the unknown programmer) }
  14. { this program was a good test for the compiler: some bugs have been found.
  15. 1. WITH in inline funcs produces a compiler error AFTER producing an .exe file
  16. (was already known; I didn't see it in the bug list)
  17. 2. macro names were checked for being a keyword, even when starting with
  18. an '_' (produces range check when compiler is compiled with { $r+ }-mode
  19. fixed.
  20. 3. { $define program } was not possible in { $macro on } mode
  21. (keywords not allowed: doesn't make sense here)
  22. fixed.
  23. 4. the Inc/Dec ( unsigned, signed ) problem (has been similar in the
  24. bug list already)
  25. 5. when the result of an overloaded (inline) operator is ABSOLUTEd:
  26. compiler error 200110205
  27. happens only when operator is defined in a unit.
  28. 6. two range check errors in scanner.pas
  29. a) array subscripting
  30. b) value out ouf range
  31. }
  32. { $define debug_version}
  33. {$r+,q+,s+}
  34. { $r-,q-,s-}
  35. {$mode objfpc}
  36. {$h-}
  37. {$inline on}
  38. {$macro on}
  39. {$define BCDMaxDigits := 64 } { should be even }
  40. { the next defines must be defined by hand,
  41. unless someone shows me a way how to to it with macros }
  42. {$define BCDgr4} { define this if MCDMaxDigits is greater 4, else undefine! }
  43. {$define BCDgr9} { define this if MCDMaxDigits is greater 9, else undefine! }
  44. {$define BCDgr18} { define this if MCDMaxDigits is greater 18, else undefine! }
  45. { $define BCDgr64} { define this if MCDMaxDigits is greater 64, else undefine! }
  46. { $define BCDgr180} { define this if MCDMaxDigits is greater 180, else undefine! }
  47. {$ifdef BCDgr4}
  48. {$note BCD Digits > 4}
  49. {$endif}
  50. {$ifdef BCDgr9}
  51. {$note BCD Digits > 9}
  52. {$endif}
  53. {$ifdef BCDgr18}
  54. {$note BCD Digits > 18}
  55. {$endif}
  56. {$ifdef BCDgr64}
  57. {$note BCD Digits > 64}
  58. {$endif}
  59. {$ifdef BCDgr180}
  60. {$note BCD Digits > 180}
  61. {$endif}
  62. {$ifndef NO_SMART_LINK}
  63. { $smartlink on}
  64. {$endif}
  65. {$define some_packed} { enable this to keep some local structures PACKED }
  66. { $define as_object} { to define the tBCD record as object instead;
  67. fields then are private }
  68. { not done yet! }
  69. {$define additional_routines} { to create additional routines and operators }
  70. (* only define one of them! *)
  71. { $define integ32}
  72. {$define integ64}
  73. (* only define one of them! *)
  74. { $define real8}
  75. {$define real10}
  76. {check}
  77. {$ifndef integ32}
  78. {$ifndef integ64}
  79. {$define integ64}
  80. {$endif}
  81. {$endif}
  82. {$ifdef integ32}
  83. {$ifdef integ64}
  84. {$undef integ32}
  85. {$endif}
  86. {$endif}
  87. {check}
  88. {$ifndef real8}
  89. {$ifndef real10}
  90. {$define real8}
  91. {$endif}
  92. {$endif}
  93. {$ifdef real8}
  94. {$ifdef real10}
  95. {$undef real10}
  96. {$endif}
  97. {$endif}
  98. {$ifdef some_packed}
  99. {$define maybe_packed := packed}
  100. {$else}
  101. {$define maybe_packed := (**)}
  102. {$endif}
  103. UNIT FmtBCD;
  104. INTERFACE
  105. USES
  106. SysUtils,
  107. { dateutils,}
  108. Variants;
  109. const
  110. MaxStringDigits = 100; { not used ! }
  111. _NoDecimal = -255; { not used ! }
  112. _DefaultDecimals = 10; { not used ! }
  113. { From DB.pas }
  114. { Max supported by Midas } { must be EVEN }
  115. MaxFmtBCDFractionSize = BCDMaxDigits + Ord ( Odd ( BCDMaxDigits ) );
  116. { Max supported by Midas }
  117. MaxFmtBCDDigits = 32; { not used ! }
  118. DefaultFmtBCDScale = 6; { not used ! }
  119. MaxBCDPrecision = 18; { not used ! }
  120. MaxBCDScale = 4; { not used ! }
  121. {$ifdef BCDgr64}
  122. { $fatal big 1}
  123. {$define bigger_BCD} { must be defined
  124. if MaxFmtBCDFractionSize > 64 }
  125. { not usable in the moment }
  126. {$endif}
  127. {$ifdef BCDgr180}
  128. { $fatal big 2}
  129. type
  130. FmtBCDStringtype = AnsiString;
  131. {$define use_Ansistring}
  132. {$else}
  133. type
  134. FmtBCDStringtype = string [ 255 ];
  135. {$undef use_Ansistring}
  136. {$endif}
  137. {$ifdef use_ansistring}
  138. {$note ansi}
  139. {$else}
  140. {$note -ansi}
  141. {$endif}
  142. {$ifdef integ32}
  143. {$define myInttype := LongInt}
  144. {$endif}
  145. {$ifdef integ64}
  146. {$define myInttype := int64}
  147. {$endif}
  148. {$ifdef real8}
  149. {$define myRealtype := double}
  150. {$endif}
  151. {$ifdef real10}
  152. {$define myRealtype := extended}
  153. {$endif}
  154. {$ifdef SUPPORT_COMP}
  155. {$define comproutines}
  156. {$endif SUPPORT_COMP}
  157. {$define __low_Fraction := 0 }
  158. {$define __high_Fraction := ( ( MaxFmtBCDFractionSize DIV 2 ) - 1 ) }
  159. type
  160. pBCD = ^ tBCD;
  161. tBCD = packed {$ifdef as_object} OBJECT {$else} record {$endif}
  162. {$ifdef as_object} PRIVATE {$endif}
  163. Precision : 0..maxfmtbcdfractionsize; { 1 (joke?)..64 }
  164. {$ifndef bigger_BCD}
  165. SignSpecialPlaces : Byte; { Sign:1, Special:1, Places:6 }
  166. {$else}
  167. Negativ : Boolean;
  168. {
  169. Special : Boolean;
  170. }
  171. Places : 0..maxfmtbcdfractionsize - 1;
  172. {$endif}
  173. Fraction : packed array [ __low_Fraction..__high_Fraction ] of Byte;
  174. { BCD Nibbles, 00..99 per Byte, high Nibble 1st }
  175. end;
  176. type
  177. tDecimalPoint = ( DecimalPoint_is_Point, DecimalPoint_is_Comma, DecimalPoint_is_System );
  178. { Exception classes }
  179. type
  180. eBCDException = CLASS ( Exception );
  181. eBCDOverflowException = CLASS ( eBCDException );
  182. eBCDNotImplementedException = CLASS ( eBCDException );
  183. var
  184. DecimalPoint : tDecimalPoint = DecimalPoint_is_Point;
  185. { Utility functions for TBCD access }
  186. function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
  187. function BCDScale ( const BCD : tBCD ) : Word; Inline;
  188. function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
  189. { BCD Arithmetic}
  190. procedure BCDNegate ( var BCD : tBCD ); Inline;
  191. { !!!!!!!!!! most routines are intentionally NOT inline !!!!!!!!!! }
  192. { Returns True if successful, False if Int Digits needed to be truncated }
  193. function NormalizeBCD ( const InBCD : tBCD;
  194. var OutBCD : tBCD;
  195. const Prec,
  196. Scale : Word ) : Boolean;
  197. procedure BCDAdd ( const BCDin1,
  198. BCDin2 : tBCD;
  199. var BCDout : tBCD );
  200. procedure BCDSubtract ( const BCDin1,
  201. BCDin2 : tBCD;
  202. var BCDout : tBCD );
  203. procedure BCDMultiply ( const BCDin1,
  204. BCDin2 : tBCD;
  205. var BCDout : tBCD );
  206. procedure BCDMultiply ( const BCDIn : tBCD;
  207. const DoubleIn : myRealtype;
  208. var BCDout : tBCD ); Inline;
  209. procedure BCDMultiply ( const BCDIn : tBCD;
  210. const StringIn : FmtBCDStringtype;
  211. var BCDout : tBCD ); Inline;
  212. { !!! params changed to const, shouldn't give a problem }
  213. procedure BCDMultiply ( const StringIn1,
  214. StringIn2 : FmtBCDStringtype;
  215. var BCDout : tBCD ); Inline;
  216. procedure BCDDivide ( const Dividend,
  217. Divisor : tBCD;
  218. var BCDout : tBCD );
  219. procedure BCDDivide ( const Dividend : tBCD;
  220. const Divisor : myRealtype;
  221. var BCDout : tBCD ); Inline;
  222. procedure BCDDivide ( const Dividend : tBCD;
  223. const Divisor : FmtBCDStringtype;
  224. var BCDout : tBCD ); Inline;
  225. { !!! params changed to const, shouldn't give a problem }
  226. procedure BCDDivide ( const Dividend,
  227. Divisor : FmtBCDStringtype;
  228. var BCDout : tBCD ); Inline;
  229. { TBCD variant creation utils }
  230. procedure VarFmtBCDCreate ( var aDest : Variant;
  231. const aBCD : tBCD );
  232. function VarFmtBCDCreate : Variant;
  233. function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
  234. Precision,
  235. Scale : Word ) : Variant;
  236. function VarFmtBCDCreate ( const aValue : myRealtype;
  237. Precision : Word = 18;
  238. Scale : Word = 4 ) : Variant;
  239. function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
  240. function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
  241. function VarFmtBCD : TVartype;
  242. { Convert string/Double/Integer to BCD struct }
  243. function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
  244. function TryStrToBCD ( const aValue : FmtBCDStringtype;
  245. var BCD : tBCD ) : Boolean;
  246. function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
  247. procedure DoubleToBCD ( const aValue : myRealtype;
  248. var BCD : tBCD );
  249. function IntegerToBCD ( const aValue : myInttype ) : tBCD;
  250. function VarToBCD ( const aValue : Variant ) : tBCD;
  251. { From DB.pas }
  252. function CurrToBCD ( const Curr : currency;
  253. var BCD : tBCD;
  254. Precision : Integer = 32;
  255. Decimals : Integer = 4 ) : Boolean;
  256. { Convert BCD struct to string/Double/Integer }
  257. function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
  258. function BCDToDouble ( const BCD : tBCD ) : myRealtype;
  259. function BCDToInteger ( const BCD : tBCD;
  260. Truncate : Boolean = False ) : myInttype;
  261. { From DB.pas }
  262. function BCDToCurr ( const BCD : tBCD;
  263. var Curr : currency ) : Boolean;
  264. { Formatting BCD as string }
  265. function BCDToStrF ( const BCD : tBCD;
  266. Format : TFloatFormat;
  267. const Precision,
  268. Digits : Integer ) : FmtBCDStringtype;
  269. function FormatBCD ( const Format : string;
  270. BCD : tBCD ) : FmtBCDStringtype;
  271. { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
  272. function BCDCompare ( const BCD1,
  273. BCD2 : tBCD ) : Integer;
  274. {$ifdef additional_routines}
  275. function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
  276. {$ifdef comproutines}
  277. function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
  278. function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
  279. {$endif}
  280. procedure BCDAdd ( const BCDIn : tBCD;
  281. const IntIn : myInttype;
  282. var BCDout : tBCD );
  283. procedure BCDAdd ( const IntIn : myInttype;
  284. const BCDIn : tBCD;
  285. var BCDout : tBCD ); Inline;
  286. procedure BCDAdd ( const BCDIn : tBCD;
  287. const DoubleIn : myRealtype;
  288. var BCDout : tBCD ); Inline;
  289. procedure BCDAdd ( const DoubleIn : myRealtype;
  290. const BCDIn : tBCD;
  291. var BCDout : tBCD ); Inline;
  292. procedure BCDAdd ( const BCDIn : tBCD;
  293. const Currin : currency;
  294. var BCDout : tBCD ); Inline;
  295. procedure BCDAdd ( const Currin : currency;
  296. const BCDIn : tBCD;
  297. var BCDout : tBCD ); Inline;
  298. {$ifdef comproutines}
  299. procedure BCDAdd ( const BCDIn : tBCD;
  300. const Compin : Comp;
  301. var BCDout : tBCD ); Inline;
  302. procedure BCDAdd ( const Compin : Comp;
  303. const BCDIn : tBCD;
  304. var BCDout : tBCD ); Inline;
  305. {$endif}
  306. procedure BCDAdd ( const BCDIn : tBCD;
  307. const StringIn : FmtBCDStringtype;
  308. var BCDout : tBCD ); Inline;
  309. procedure BCDAdd ( const StringIn : FmtBCDStringtype;
  310. const BCDIn : tBCD;
  311. var BCDout : tBCD ); Inline;
  312. procedure BCDAdd ( const StringIn1,
  313. StringIn2 : FmtBCDStringtype;
  314. var BCDout : tBCD ); Inline;
  315. procedure BCDSubtract ( const BCDIn : tBCD;
  316. const IntIn : myInttype;
  317. var BCDout : tBCD );
  318. procedure BCDSubtract ( const IntIn : myInttype;
  319. const BCDIn : tBCD;
  320. var BCDout : tBCD ); Inline;
  321. procedure BCDSubtract ( const BCDIn : tBCD;
  322. const DoubleIn : myRealtype;
  323. var BCDout : tBCD ); Inline;
  324. procedure BCDSubtract ( const DoubleIn : myRealtype;
  325. const BCDIn : tBCD;
  326. var BCDout : tBCD ); Inline;
  327. procedure BCDSubtract ( const BCDIn : tBCD;
  328. const Currin : currency;
  329. var BCDout : tBCD ); Inline;
  330. procedure BCDSubtract ( const Currin : currency;
  331. const BCDIn : tBCD;
  332. var BCDout : tBCD ); Inline;
  333. {$ifdef comproutines}
  334. procedure BCDSubtract ( const BCDIn : tBCD;
  335. const Compin : Comp;
  336. var BCDout : tBCD ); Inline;
  337. procedure BCDSubtract ( const Compin : Comp;
  338. const BCDIn : tBCD;
  339. var BCDout : tBCD ); Inline;
  340. {$endif}
  341. procedure BCDSubtract ( const BCDIn : tBCD;
  342. const StringIn : FmtBCDStringtype;
  343. var BCDout : tBCD ); Inline;
  344. procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
  345. const BCDIn : tBCD;
  346. var BCDout : tBCD ); Inline;
  347. procedure BCDSubtract ( const StringIn1,
  348. StringIn2 : FmtBCDStringtype;
  349. var BCDout : tBCD ); Inline;
  350. procedure BCDMultiply ( const BCDIn : tBCD;
  351. const IntIn : myInttype;
  352. var BCDout : tBCD );
  353. procedure BCDMultiply ( const IntIn : myInttype;
  354. const BCDIn : tBCD;
  355. var BCDout : tBCD ); Inline;
  356. procedure BCDMultiply ( const DoubleIn : myRealtype;
  357. const BCDIn : tBCD;
  358. var BCDout : tBCD ); Inline;
  359. procedure BCDMultiply ( const BCDIn : tBCD;
  360. const Currin : currency;
  361. var BCDout : tBCD ); Inline;
  362. procedure BCDMultiply ( const Currin : currency;
  363. const BCDIn : tBCD;
  364. var BCDout : tBCD ); Inline;
  365. {$ifdef comproutines}
  366. procedure BCDMultiply ( const BCDIn : tBCD;
  367. const Compin : Comp;
  368. var BCDout : tBCD ); Inline;
  369. procedure BCDMultiply ( const Compin : Comp;
  370. const BCDIn : tBCD;
  371. var BCDout : tBCD ); Inline;
  372. {$endif}
  373. procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
  374. const BCDIn : tBCD;
  375. var BCDout : tBCD ); Inline;
  376. procedure BCDDivide ( const Dividend : tBCD;
  377. const Divisor : myInttype;
  378. var BCDout : tBCD ); Inline;
  379. procedure BCDDivide ( const Dividend : myInttype;
  380. const Divisor : tBCD;
  381. var BCDout : tBCD ); Inline;
  382. procedure BCDDivide ( const Dividend : myRealtype;
  383. const Divisor : tBCD;
  384. var BCDout : tBCD ); Inline;
  385. procedure BCDDivide ( const BCDIn : tBCD;
  386. const Currin : currency;
  387. var BCDout : tBCD ); Inline;
  388. procedure BCDDivide ( const Currin : currency;
  389. const BCDIn : tBCD;
  390. var BCDout : tBCD ); Inline;
  391. {$ifdef comproutines}
  392. procedure BCDDivide ( const BCDIn : tBCD;
  393. const Compin : Comp;
  394. var BCDout : tBCD ); Inline;
  395. procedure BCDDivide ( const Compin : Comp;
  396. const BCDIn : tBCD;
  397. var BCDout : tBCD ); Inline;
  398. {$endif}
  399. procedure BCDDivide ( const Dividend : FmtBCDStringtype;
  400. const Divisor : tBCD;
  401. var BCDout : tBCD ); Inline;
  402. operator = ( const BCD1,
  403. BCD2 : tBCD ) z : Boolean; Inline;
  404. operator < ( const BCD1,
  405. BCD2 : tBCD ) z : Boolean; Inline;
  406. operator > ( const BCD1,
  407. BCD2 : tBCD ) z : Boolean; Inline;
  408. operator <= ( const BCD1,
  409. BCD2 : tBCD ) z : Boolean; Inline;
  410. operator >= ( const BCD1,
  411. BCD2 : tBCD ) z : Boolean; Inline;
  412. (* ######################## not allowed: why?
  413. operator + ( const BCD : tBCD ) z : tBCD; make_Inline
  414. ##################################################### *)
  415. operator - ( const BCD : tBCD ) z : tBCD; Inline;
  416. operator + ( const BCD1,
  417. BCD2 : tBCD ) z : tBCD; Inline;
  418. operator + ( const BCD : tBCD;
  419. const i : myInttype ) z : tBCD; Inline;
  420. operator + ( const i : myInttype;
  421. const BCD : tBCD ) z : tBCD; Inline;
  422. operator + ( const BCD : tBCD;
  423. const r : myRealtype ) z : tBCD; Inline;
  424. operator + ( const r : myRealtype;
  425. const BCD : tBCD ) z : tBCD; Inline;
  426. operator + ( const BCD : tBCD;
  427. const c : currency ) z : tBCD; Inline;
  428. operator + ( const c : currency;
  429. const BCD : tBCD ) z : tBCD; Inline;
  430. {$ifdef comproutines}
  431. operator + ( const BCD : tBCD;
  432. const c : Comp ) z : tBCD; Inline;
  433. operator + ( const c : Comp;
  434. const BCD : tBCD ) z : tBCD; Inline;
  435. {$endif}
  436. operator + ( const BCD : tBCD;
  437. const s : FmtBCDStringtype ) z : tBCD; Inline;
  438. operator + ( const s : FmtBCDStringtype;
  439. const BCD : tBCD ) z : tBCD; Inline;
  440. operator - ( const BCD1,
  441. BCD2 : tBCD ) z : tBCD; Inline;
  442. operator - ( const BCD : tBCD;
  443. const i : myInttype ) z : tBCD; Inline;
  444. operator - ( const i : myInttype;
  445. const BCD : tBCD ) z : tBCD; Inline;
  446. operator - ( const BCD : tBCD;
  447. const r : myRealtype ) z : tBCD; Inline;
  448. operator - ( const r : myRealtype;
  449. const BCD : tBCD ) z : tBCD; Inline;
  450. operator - ( const BCD : tBCD;
  451. const c : currency ) z : tBCD; Inline;
  452. operator - ( const c : currency;
  453. const BCD : tBCD ) z : tBCD; Inline;
  454. {$ifdef comproutines}
  455. operator - ( const BCD : tBCD;
  456. const c : Comp ) z : tBCD; Inline;
  457. operator - ( const c : Comp;
  458. const BCD : tBCD ) z : tBCD; Inline;
  459. {$endif}
  460. operator - ( const BCD : tBCD;
  461. const s : FmtBCDStringtype ) z : tBCD; Inline;
  462. operator - ( const s : FmtBCDStringtype;
  463. const BCD : tBCD ) z : tBCD; Inline;
  464. operator * ( const BCD1,
  465. BCD2 : tBCD ) z : tBCD; Inline;
  466. operator * ( const BCD : tBCD;
  467. const i : myInttype ) z : tBCD; Inline;
  468. operator * ( const i : myInttype;
  469. const BCD : tBCD ) z : tBCD; Inline;
  470. operator * ( const BCD : tBCD;
  471. const r : myRealtype ) z : tBCD; Inline;
  472. operator * ( const r : myRealtype;
  473. const BCD : tBCD ) z : tBCD; Inline;
  474. operator * ( const BCD : tBCD;
  475. const c : currency ) z : tBCD; Inline;
  476. operator * ( const c : currency;
  477. const BCD : tBCD ) z : tBCD; Inline;
  478. {$ifdef comproutines}
  479. operator * ( const BCD : tBCD;
  480. const c : Comp ) z : tBCD; Inline;
  481. operator * ( const c : Comp;
  482. const BCD : tBCD ) z : tBCD; Inline;
  483. {$endif}
  484. operator * ( const BCD : tBCD;
  485. const s : FmtBCDStringtype ) z : tBCD; Inline;
  486. operator * ( const s : FmtBCDStringtype;
  487. const BCD : tBCD ) z : tBCD; Inline;
  488. operator / ( const BCD1,
  489. BCD2 : tBCD ) z : tBCD; Inline;
  490. operator / ( const BCD : tBCD;
  491. const i : myInttype ) z : tBCD; Inline;
  492. operator / ( const i : myInttype;
  493. const BCD : tBCD ) z : tBCD; Inline;
  494. operator / ( const BCD : tBCD;
  495. const r : myRealtype ) z : tBCD; Inline;
  496. operator / ( const r : myRealtype;
  497. const BCD : tBCD ) z : tBCD; Inline;
  498. operator / ( const BCD : tBCD;
  499. const c : currency ) z : tBCD; Inline;
  500. operator / ( const c : currency;
  501. const BCD : tBCD ) z : tBCD; Inline;
  502. {$ifdef comproutines}
  503. operator / ( const BCD : tBCD;
  504. const c : Comp ) z : tBCD; Inline;
  505. operator / ( const c : Comp;
  506. const BCD : tBCD ) z : tBCD; Inline;
  507. {$endif}
  508. operator / ( const BCD : tBCD;
  509. const s : FmtBCDStringtype ) z : tBCD; Inline;
  510. operator / ( const s : FmtBCDStringtype;
  511. const BCD : tBCD ) z : tBCD; Inline;
  512. operator := ( const i : Byte ) z : tBCD; Inline;
  513. operator := ( const BCD : tBCD ) z : Byte; Inline;
  514. operator := ( const i : Word ) z : tBCD; Inline;
  515. operator := ( const BCD : tBCD ) z : Word; Inline;
  516. operator := ( const i : longword ) z : tBCD; Inline;
  517. operator := ( const BCD : tBCD ) z : longword; Inline;
  518. {$if declared ( qword ) }
  519. operator := ( const i : qword ) z : tBCD; Inline;
  520. operator := ( const BCD : tBCD ) z : qword; Inline;
  521. {$endif}
  522. operator := ( const i : ShortInt ) z : tBCD; Inline;
  523. operator := ( const BCD : tBCD ) z : ShortInt; Inline;
  524. operator := ( const i : smallint ) z : tBCD; Inline;
  525. operator := ( const BCD : tBCD ) z : smallint; Inline;
  526. operator := ( const i : LongInt ) z : tBCD; Inline;
  527. operator := ( const BCD : tBCD ) z : LongInt; Inline;
  528. {$if declared ( int64 ) }
  529. operator := ( const i : int64 ) z : tBCD; Inline;
  530. operator := ( const BCD : tBCD ) z : int64; Inline;
  531. {$endif}
  532. operator := ( const r : Single ) z : tBCD; Inline;
  533. operator := ( const BCD : tBCD ) z : Single; Inline;
  534. operator := ( const r : Double ) z : tBCD; Inline;
  535. operator := ( const BCD : tBCD ) z : Double; Inline;
  536. {$if sizeof ( extended ) <> sizeof ( double )}
  537. operator := ( const r : Extended ) z : tBCD; Inline;
  538. operator := ( const BCD : tBCD ) z : Extended; Inline;
  539. {$endif}
  540. operator := ( const c : currency ) z : tBCD; Inline;
  541. operator := ( const BCD : tBCD ) z : currency; Inline;
  542. {$ifdef comproutines}
  543. operator := ( const c : Comp ) z : tBCD; Inline;
  544. operator := ( const BCD : tBCD ) z : Comp; Inline;
  545. {$endif}
  546. operator := ( const s : string ) z : tBCD; Inline;
  547. operator := ( const BCD : tBCD ) z : string; Inline;
  548. operator := ( const s : AnsiString ) z : tBCD; Inline;
  549. operator := ( const BCD : tBCD ) z : AnsiString; Inline;
  550. {$endif}
  551. function __get_null : tBCD; Inline;
  552. function __get_one : tBCD; Inline;
  553. PROPERTY
  554. NullBCD : tBCD Read __get_null;
  555. OneBCD : tBCD Read __get_one;
  556. //{$define __lo_bh := 1 * ( -( MaxFmtBCDFractionSize * 1 + 2 ) ) }
  557. //{$define __hi_bh := 1 * ( MaxFmtBCDFractionSize * 1 + 1 ) }
  558. {$define helper_declarations :=
  559. const
  560. __lo_bh = -( MaxFmtBCDFractionSize + 2 );
  561. __hi_bh = ( MaxFmtBCDFractionSize + 1 );
  562. type
  563. tBCD_helper = Maybe_Packed record
  564. Prec : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
  565. Plac : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
  566. FDig,
  567. LDig : {$ifopt r+} __lo_bh..__hi_bh {$else} Integer {$endif};
  568. Singles : Maybe_packed array [ __lo_bh..__hi_bh ]
  569. of {$ifopt r+} 0..9 {$else} Byte {$endif};
  570. Neg : Boolean;
  571. end;
  572. { in the tBCD_helper the bcd is stored for computations,
  573. shifted to the right position }
  574. { {$define __lo_bhb := 1 * ( __lo_bh + __lo_bh ) } }
  575. { {$define __hi_bhb := 1 * ( __hi_bh + __hi_bh + 1 ) } }
  576. const
  577. __lo_bhb = __lo_bh + __lo_bh - 1;
  578. __hi_bhb = __hi_bh + __hi_bh;
  579. type
  580. tBCD_helper_big = Maybe_Packed record
  581. Prec : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
  582. Plac : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
  583. FDig,
  584. LDig : {$ifopt r+} __lo_bhb..__hi_bhb {$else} Integer {$endif};
  585. Singles : Maybe_packed array [ __lo_bhb..__hi_bhb ]
  586. of {$ifopt r+} 0 * 0..9 * 9 * Pred ( MaxFmtBCDDigits ) {$else} Integer {$endif};
  587. Neg : Boolean;
  588. end;
  589. }
  590. {$ifdef debug_version}
  591. helper_declarations
  592. procedure unpack_BCD ( const BCD : tBCD;
  593. var bh : tBCD_helper );
  594. function pack_BCD ( var bh : tBCD_helper;
  595. var BCD : tBCD ) : Boolean;
  596. procedure dumpBCD ( const v : tBCD );
  597. {$endif}
  598. IMPLEMENTATION
  599. USES
  600. classes;
  601. type
  602. TFMTBcdFactory = CLASS(TPublishableVarianttype)
  603. PROTECTED
  604. function GetInstance(const v : TVarData): tObject; OVERRIDE;
  605. PUBLIC
  606. procedure BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
  607. end;
  608. TFMTBcdVarData = CLASS(TPersistent)
  609. PRIVATE
  610. FBcd : tBCD;
  611. PUBLIC
  612. constructor create;
  613. constructor create(const BCD : tBCD);
  614. PROPERTY BCD : tBCD Read FBcd Write FBcd;
  615. end;
  616. var
  617. NullBCD_ : tBCD;
  618. OneBCD_ : tBCD;
  619. function __get_null : tBCD; Inline;
  620. begin
  621. __get_null := NullBCD_;
  622. end;
  623. function __get_one : tBCD; Inline;
  624. begin
  625. __get_one := OneBCD_;
  626. end;
  627. type
  628. range_digits = 1..maxfmtbcdfractionsize;
  629. range_digits0 = 0..maxfmtbcdfractionsize;
  630. range_fracdigits = 0..pred ( MaxFmtBCDFractionSize );
  631. {$ifopt r+}
  632. var
  633. rcheck : 0..0;
  634. rbad : Byte = 1;
  635. {$endif}
  636. {$ifndef debug_version}
  637. helper_declarations
  638. {$endif}
  639. var
  640. null_ : record
  641. case Boolean of
  642. False: ( bh : tBCD_helper );
  643. True: ( bhb : tBCD_helper_big );
  644. end;
  645. FMTBcdFactory : TFMTBcdFactory = NIL;
  646. {$ifndef bigger_BCD}
  647. const
  648. NegBit = 1 SHL 7;
  649. SpecialBit = 1 SHL 6;
  650. PlacesMask = $ff XOR ( NegBit OR SpecialBit );
  651. {$endif}
  652. {$define _select := {$define _when := if {$define _when := end else if } }
  653. {$define _then := then begin }
  654. {$define _whenother := end else begin }
  655. {$define _endselect := end } }
  656. {$ifdef debug_version}
  657. procedure dumpBCD ( const v : tBCD );
  658. var
  659. i,
  660. j : Integer;
  661. const
  662. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  663. begin
  664. {$ifndef bigger_BCD}
  665. Write ( 'Prec:', v.Precision, ' ',
  666. 'Neg:', ft[( v.SignSpecialPlaces AND NegBit ) <> 0], ' ',
  667. 'Special:', ft[( v.SignSpecialPlaces AND SpecialBit ) <> 0], ' ',
  668. 'Places:', v.SignSpecialPlaces AND PlacesMask, ' ' );
  669. {$else}
  670. Write ( 'Prec:', v.Precision, ' ',
  671. 'Neg:', ft[v.Negativ], ' ',
  672. 'Places:', v.Places, ' ' );
  673. {$endif}
  674. j := 0;
  675. for i := 1 TO v.Precision do
  676. if Odd ( i )
  677. then Write ( ( v.Fraction[j] AND $f0 ) SHR 4 )
  678. else begin
  679. Write ( v.Fraction[j] AND $0f );
  680. Inc ( j );
  681. end;
  682. WriteLn;
  683. end;
  684. procedure dumpbh ( const v : tBCD_helper );
  685. var
  686. i : Integer;
  687. const
  688. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  689. begin
  690. Write ( 'Prec:', v.Prec, ' ',
  691. 'Neg:', ft[v.Neg], ' ',
  692. 'Places:', v.Plac, ' ',
  693. 'FDig:', v.FDig, ' ',
  694. 'LDig:', v.LDig, ' ',
  695. 'Digits:', v.LDig - v.FDig + 1, ' ' );
  696. for i := v.FDig TO v.LDig do
  697. Write ( v.Singles[i] );
  698. WriteLn;
  699. end;
  700. {$endif}
  701. {$if sizeof ( integer ) = 2 }
  702. {$ifdef BCDgr4 }
  703. var
  704. myMinIntBCD : tBCD;
  705. {$endif}
  706. {$else}
  707. {$if sizeof ( integer ) = 4 }
  708. {$ifdef BCDgr9 }
  709. var
  710. myMinIntBCD : tBCD;
  711. {$endif}
  712. {$else}
  713. {$if sizeof ( integer ) = 8 }
  714. {$ifdef BCDgr18 }
  715. var
  716. myMinIntBCD : tBCD;
  717. {$endif}
  718. {$else}
  719. {$fatal You have an interesting integer type! Sorry, not supported}
  720. {$endif}
  721. {$endif}
  722. {$endif}
  723. procedure not_implemented;
  724. begin
  725. RAISE eBCDNotImplementedException.create ( 'not implemented' );
  726. end;
  727. procedure unpack_BCD ( const BCD : tBCD;
  728. var bh : tBCD_helper );
  729. var
  730. i : {$ifopt r+} __lo_bh + 1 ..__hi_bh {$else} Integer {$endif};
  731. j : {$ifopt r+} -1..__high_fraction {$else} Integer {$endif};
  732. vv : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  733. begin
  734. bh := null_.bh;
  735. WITH bh,
  736. BCD do
  737. begin
  738. Prec := Precision;
  739. if Prec > 0
  740. then begin
  741. {$ifndef bigger_BCD}
  742. Plac := SignSpecialPlaces AND PlacesMask;
  743. Neg := ( SignSpecialPlaces AND NegBit ) <> 0;
  744. {$else}
  745. Plac := Places;
  746. Neg := Negativ;
  747. {$endif}
  748. LDig := Plac;
  749. FDig := LDig - Prec + 1;
  750. j := -1;
  751. i := FDig;
  752. while i <= LDig do
  753. begin
  754. Inc ( j );
  755. vv := Fraction[j];
  756. Singles[i] := ( vv {AND $f0} ) SHR 4;
  757. if i < LDig
  758. then Singles[i+1] := vv AND $0f;
  759. Inc ( i, 2 );
  760. end;
  761. end;
  762. end;
  763. end;
  764. function pack_BCD ( var bh : tBCD_helper;
  765. var BCD : tBCD ) : Boolean;
  766. { return TRUE if successful (BCD valid) }
  767. var
  768. pre : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  769. fra : {$ifopt r+} -1 * ( __hi_bh - __lo_bh + 1 )..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  770. tm : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 - Pred ( MaxFmtBCDFractionSize ) {$else} Integer {$endif};
  771. i : {$ifopt r+} low ( bh.FDig ) - 1..high ( bh.LDig ) {$else} Integer {$endif};
  772. rp : {$ifopt r+} low ( BCD.Fraction )..high ( BCD.Fraction ) + 1 {$else} Integer {$endif};
  773. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  774. v : {$ifopt r+} 0..10 {$else} Integer {$endif};
  775. lnz : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  776. doround,
  777. lnzf : Boolean;
  778. begin
  779. pack_BCD := False;
  780. BCD := NullBCD;
  781. WITH BCD,
  782. bh do
  783. begin
  784. lnzf := FDig < 0;
  785. while lnzf do
  786. if Singles[FDig] = 0
  787. then begin
  788. Inc ( FDig );
  789. if FDig = 0
  790. then lnzf := False;
  791. end
  792. else lnzf := False;
  793. pre := LDig - FDig + 1;
  794. fra := Plac;
  795. doround := False;
  796. if fra >= MaxFmtBCDFractionSize
  797. then begin
  798. doround := True;
  799. tm := fra - Pred ( MaxFmtBCDFractionSize );
  800. { dec ( pre, tm ); Dec/Inc error? }
  801. pre := pre - tm;
  802. { Dec ( fra, tm ); Dec/Inc error? }
  803. fra := fra - tm;
  804. { Dec ( LDig, tm ); Dec/Inc error? }
  805. LDig := LDig - tm;
  806. end;
  807. if pre > MaxFmtBCDFractionSize
  808. then begin
  809. doround := True;
  810. tm := pre - MaxFmtBCDFractionSize;
  811. { Dec ( pre, tm ); Dec/Inc error? }
  812. pre := pre - tm;
  813. { Dec ( fra, tm ); Dec/Inc error? }
  814. fra := fra - tm;
  815. { Dec ( LDig, tm ); Dec/Inc error? }
  816. LDig := LDig - tm;
  817. end;
  818. if fra < 0
  819. then EXIT;
  820. if doround
  821. then begin
  822. v := Singles[fra + 1];
  823. if v > 4
  824. then begin
  825. ue := 1;
  826. i := LDig;
  827. while ( i >= FDig ) AND ( ue <> 0 ) do
  828. begin
  829. v := Singles[i] + ue;
  830. ue := v DIV 10;
  831. Singles[i] := v MOD 10;
  832. Dec ( i );
  833. end;
  834. if ue <> 0
  835. then begin
  836. Dec ( FDig );
  837. Singles[FDig] := ue;
  838. Dec ( LDig );
  839. Dec ( fra );
  840. if fra < 0
  841. then EXIT;
  842. end;
  843. end;
  844. end;
  845. lnzf := False;
  846. i := LDig;
  847. while ( i >= FDig ) AND ( NOT lnzf ) do
  848. begin
  849. if Singles[i] <> 0
  850. then begin
  851. lnz := i;
  852. lnzf := True;
  853. end;
  854. Dec ( i );
  855. end;
  856. if lnzf
  857. then begin
  858. tm := LDig - lnz;
  859. if tm <> 0
  860. then begin
  861. { Dec ( pre, tm ); Dec/Inc error? }
  862. pre := pre - tm;
  863. { Dec ( fra, tm ); Dec/Inc error? }
  864. fra := fra - tm;
  865. { Dec ( LDig, tm ); Dec/Inc error? }
  866. LDig := LDig - tm;
  867. if fra < 0
  868. then begin
  869. { Dec ( pre, fra ); Dec/Inc error? }
  870. pre := pre - fra;
  871. { Dec ( LDig, fra ); Dec/Inc error? }
  872. LDig := LDig - fra;
  873. fra := 0;
  874. end;
  875. end;
  876. end
  877. else begin
  878. LDig := FDig;
  879. fra := 0;
  880. pre := 0;
  881. Neg := False;
  882. end;
  883. if pre <> 0
  884. then begin
  885. Precision := pre;
  886. rp := 0;
  887. i := FDig;
  888. while i <= LDig do
  889. begin
  890. if i < LDig
  891. then Fraction[rp] := ( Singles[i] SHL 4 ) OR Singles[i + 1]
  892. else Fraction[rp] := Singles[i] SHL 4;
  893. Inc ( rp );
  894. Inc ( i, 2 );
  895. end;
  896. {$ifndef bigger_BCD}
  897. if Neg
  898. then SignSpecialPlaces := NegBit;
  899. SignSpecialPlaces := SignSpecialPlaces OR fra;
  900. {$else}
  901. Negativ := Neg;
  902. Places := fra;
  903. {$endif}
  904. end;
  905. end;
  906. pack_BCD := True;
  907. end;
  908. procedure SetDecimals ( var dp,
  909. dc : Char );
  910. begin
  911. case DecimalPoint of
  912. DecimalPoint_is_Point: begin
  913. dp := '.';
  914. dc := ',';
  915. end;
  916. DecimalPoint_is_Comma: begin
  917. dp := ',';
  918. dc := '.';
  919. end;
  920. { find out language-specific ? }
  921. DecimalPoint_is_System: begin
  922. dp := DecimalSeparator;
  923. dc := ThousandSeparator;
  924. end;
  925. end;
  926. end;
  927. function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
  928. begin
  929. BCDPrecision := BCD.Precision;
  930. end;
  931. function BCDScale ( const BCD : tBCD ) : Word; Inline;
  932. begin
  933. {$ifndef bigger_BCD}
  934. BCDScale := BCD.SignSpecialPlaces AND PlacesMask;
  935. {$else}
  936. BCDScale := BCD.Places;
  937. {$endif}
  938. end;
  939. function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
  940. begin
  941. {$ifndef bigger_BCD}
  942. IsBCDNegative := ( BCD.SignSpecialPlaces AND NegBit ) <> 0;
  943. {$else}
  944. IsBCDNegative := BCD.Negativ;
  945. {$endif}
  946. end;
  947. { BCD Arithmetic}
  948. procedure BCDNegate ( var BCD : tBCD ); Inline;
  949. begin
  950. { with-statement geht nicht !!
  951. with bcd do
  952. if precision <> 0
  953. then signspecialplaces := signspecialplaces xor negbit;
  954. }
  955. if BCD.Precision <> 0
  956. then
  957. {$ifndef bigger_BCD}
  958. BCD.SignSpecialPlaces := BCD.SignSpecialPlaces XOR NegBit;
  959. {$else}
  960. BCD.Negativ := NOT BCD.Negativ;
  961. {$endif}
  962. end;
  963. { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
  964. function BCDCompare ( const BCD1,
  965. BCD2 : tBCD ) : Integer;
  966. var
  967. pl1 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  968. pl2 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  969. pr1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  970. pr2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  971. pr : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  972. idig1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  973. idig2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  974. i : {$ifopt r+} __low_Fraction..__high_Fraction + 1 {$else} Integer {$endif};
  975. f1 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  976. f2 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  977. res : {$ifopt r+} -1..1 {$else} Integer {$endif};
  978. neg1,
  979. neg2 : Boolean;
  980. begin
  981. {$ifndef bigger_BCD}
  982. neg1 := ( BCD1.SignSpecialPlaces AND NegBit ) <> 0;
  983. neg2 := ( BCD2.SignSpecialPlaces AND NegBit ) <> 0;
  984. {$else}
  985. neg1 := BCD1.Negativ;
  986. neg2 := BCD2.Negativ;
  987. {$endif}
  988. _SELECT
  989. _WHEN neg1 AND ( NOT neg2 )
  990. _THEN result := -1;
  991. _WHEN ( NOT neg1 ) AND neg2
  992. _THEN result := +1;
  993. _WHENOTHER
  994. pr1 := BCD1.Precision;
  995. pr2 := BCD2.Precision;
  996. {$ifndef bigger_BCD}
  997. pl1 := BCD1.SignSpecialPlaces AND PlacesMask;
  998. pl2 := BCD2.SignSpecialPlaces AND PlacesMask;
  999. {$else}
  1000. pl1 := BCD1.Places;
  1001. pl2 := BCD2.Places;
  1002. {$endif}
  1003. idig1 := pr1 - pl1;
  1004. idig2 := pr2 - pl2;
  1005. if idig1 <> idig2
  1006. then begin
  1007. if ( idig1 > idig2 ) = neg1
  1008. then result := -1
  1009. else result := +1;
  1010. end
  1011. else begin
  1012. if pr1 < pr2
  1013. then pr := pr1
  1014. else pr := pr2;
  1015. res := 0;
  1016. i := __low_Fraction;
  1017. while ( res = 0 ) AND ( i < ( __low_Fraction + ( pr DIV 2 ) ) ) do
  1018. begin
  1019. {
  1020. if BCD1.Fraction[i] < BCD2.Fraction[i]
  1021. then res := -1
  1022. else
  1023. if BCD1.Fraction[i] > BCD2.Fraction[i]
  1024. then res := +1;
  1025. }
  1026. _SELECT
  1027. _WHEN BCD1.Fraction[i] < BCD2.Fraction[i]
  1028. _THEN res := -1
  1029. _WHEN BCD1.Fraction[i] > BCD2.Fraction[i]
  1030. _THEN res := +1;
  1031. _WHENOTHER
  1032. _endSELECT;
  1033. Inc ( i );
  1034. end;
  1035. if res = 0
  1036. then begin
  1037. if Odd ( pr )
  1038. then begin
  1039. f1 := BCD1.Fraction[i] AND $f0;
  1040. f2 := BCD2.Fraction[i] AND $f0;
  1041. {
  1042. if f1 < f2
  1043. then res := -1
  1044. else
  1045. if f1 > f2
  1046. then res := +1;
  1047. }
  1048. _SELECT
  1049. _WHEN f1 < f2
  1050. _THEN res := -1
  1051. _WHEN f1 > f2
  1052. _THEN res := +1;
  1053. _endSELECT;
  1054. end;
  1055. end;
  1056. if neg1
  1057. then result := 0 - res
  1058. else result := res;
  1059. end;
  1060. _endSELECT
  1061. end;
  1062. { Convert string/Double/Integer to BCD struct }
  1063. function TryStrToBCD ( const aValue : FmtBCDStringtype;
  1064. var BCD : tBCD ) : Boolean;
  1065. { shall this return TRUE when error and FALSE when o.k. or the other way round ? }
  1066. var
  1067. {$ifndef use_ansistring}
  1068. lav : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1069. i : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1070. {$else}
  1071. lav : {$ifopt r+} longword {$else} longword {$endif};
  1072. i : {$ifopt r+} longword {$else} longword {$endif};
  1073. {$endif}
  1074. ch : Char;
  1075. dp,
  1076. dc : Char;
  1077. type
  1078. ife = ( inint, infrac, inexp );
  1079. {$define max_exp_scanned := 9999 }
  1080. var
  1081. inife : ife;
  1082. lvars : record
  1083. fp,
  1084. lp : ARRAY [ ife ]
  1085. {$ifndef use_ansistring}
  1086. of {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1087. pfnb : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1088. ps : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1089. pse : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1090. errp : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1091. {$else}
  1092. of {$ifopt r+} longword {$else} longword {$endif};
  1093. pfnb : {$ifopt r+} longword {$else} longword {$endif};
  1094. ps : {$ifopt r+} longword {$else} longword {$endif};
  1095. pse : {$ifopt r+} longword {$else} longword {$endif};
  1096. errp : {$ifopt r+} longword {$else} longword {$endif};
  1097. {$endif}
  1098. exp : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1099. p : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1100. bh : tBCD_helper;
  1101. nbf : Boolean;
  1102. end;
  1103. begin
  1104. result := False;
  1105. FillChar ( lvars, SizeOf ( lvars ), #0 );
  1106. BCD := NullBCD;
  1107. lav := Length ( aValue );
  1108. if lav <> 0
  1109. then
  1110. WITH lvars,
  1111. bh do
  1112. begin
  1113. SetDecimals ( dp, dc );
  1114. while ( pfnb < lav ) AND ( NOT nbf ) do
  1115. begin
  1116. Inc ( pfnb );
  1117. nbf := aValue[pfnb] <> ' ';
  1118. end;
  1119. if nbf
  1120. then begin
  1121. if aValue[pfnb] IN [ '+', '-' ]
  1122. then begin
  1123. ps := pfnb;
  1124. Inc ( pfnb );
  1125. end;
  1126. inife := low ( inife );
  1127. for i := pfnb TO lav do
  1128. begin
  1129. ch := aValue[i];
  1130. case ch of
  1131. '0'..'9': begin
  1132. case inife of
  1133. inint,
  1134. inexp: if fp[inife] = 0
  1135. then begin
  1136. if ch <> '0'
  1137. then begin
  1138. fp[inife] := i;
  1139. lp[inife] := i;
  1140. end;
  1141. end
  1142. else lp[inife] := i;
  1143. infrac: begin
  1144. if fp[infrac] = 0
  1145. then fp[infrac] := i;
  1146. if ch <> '0'
  1147. then lp[infrac] := i;
  1148. end;
  1149. end;
  1150. end;
  1151. ',',
  1152. '.': if ch = dp
  1153. then begin
  1154. if inife <> inint
  1155. then result := True
  1156. else inife := infrac;
  1157. end;
  1158. 'e',
  1159. 'E': if inife = inexp
  1160. then result := True
  1161. else inife := inexp;
  1162. '+',
  1163. '-': if ( inife = inexp ) AND ( fp[inexp] = 0 )
  1164. then pse := i
  1165. else result := True;
  1166. else begin
  1167. result := True;
  1168. errp := i;
  1169. end;
  1170. end;
  1171. end;
  1172. if result
  1173. then begin
  1174. result := False;
  1175. for i := errp TO lav do
  1176. if aValue[i] <> ' '
  1177. then result := True;
  1178. end;
  1179. if result
  1180. then EXIT;
  1181. if ps <> 0
  1182. then Neg := aValue[ps] = '-';
  1183. if lp[infrac] = 0
  1184. then fp[infrac] := 0;
  1185. if fp[inexp] <> 0
  1186. then begin
  1187. exp := 0;
  1188. for i := fp[inexp] TO lp[inexp] do
  1189. if NOT result
  1190. then
  1191. if aValue[i] <> dc
  1192. then begin
  1193. exp := exp * 10 + ( Ord ( aValue[i] ) - Ord ( '0' ) );
  1194. if exp > 999
  1195. then result := True;
  1196. end;
  1197. if result
  1198. then EXIT;
  1199. if pse <> 0
  1200. then
  1201. if aValue[pse] = '-'
  1202. then exp := -exp;
  1203. end;
  1204. p := -exp;
  1205. if fp[infrac] <> 0
  1206. then begin
  1207. for i := fp[infrac] TO lp[infrac] do
  1208. if aValue[i] <> dc
  1209. then begin
  1210. if p < ( MaxFmtBCDFractionSize + 2 )
  1211. then begin
  1212. Inc ( p );
  1213. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1214. end;
  1215. end;
  1216. end;
  1217. LDig := p;
  1218. p := 1 - exp;
  1219. if fp[inint] <> 0
  1220. then
  1221. for i := lp[inint] DOWNTO fp[inint] do
  1222. if aValue[i] <> dc
  1223. then begin
  1224. if p > - ( MaxFmtBCDFractionSize + 2 )
  1225. then begin
  1226. Dec ( p );
  1227. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1228. end
  1229. else result := True;
  1230. end;
  1231. if result
  1232. then EXIT;
  1233. FDig := p;
  1234. if LDig < 0
  1235. then LDig := 0;
  1236. Plac := LDig;
  1237. result := NOT pack_BCD ( bh, BCD );
  1238. end;
  1239. end;
  1240. end;
  1241. function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
  1242. var
  1243. BCD : tBCD;
  1244. begin
  1245. if TryStrToBCD ( aValue, BCD )
  1246. then begin
  1247. RAISE eBCDOverflowException.create ( 'in StrToBCD' );
  1248. end
  1249. else StrToBCD := BCD;
  1250. end;
  1251. procedure DoubleToBCD ( const aValue : myRealtype;
  1252. var BCD : tBCD );
  1253. var
  1254. s : string [ 30 ];
  1255. dp : tDecimalPoint;
  1256. begin
  1257. Str ( aValue : 25, s );
  1258. dp := DecimalPoint;
  1259. DecimalPoint := DecimalPoint_is_Point;
  1260. BCD := StrToBCD ( s );
  1261. DecimalPoint := dp;
  1262. end;
  1263. function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
  1264. begin
  1265. DoubleToBCD ( aValue, result );
  1266. end;
  1267. function IntegerToBCD ( const aValue : myInttype ) : tBCD;
  1268. var
  1269. bh : tBCD_helper;
  1270. v : {$ifopt r+} 0..high ( myInttype ) {$else} Integer {$endif};
  1271. p : {$ifopt r+} low ( bh.Singles ) - 1..0 {$else} Integer {$endif};
  1272. Error,
  1273. exitloop : Boolean;
  1274. begin
  1275. _SELECT
  1276. _WHEN aValue = 0
  1277. _THEN result := NullBCD;
  1278. _WHEN aValue = 1
  1279. _THEN result := OneBCD;
  1280. _WHEN aValue = low ( myInttype )
  1281. _THEN
  1282. {$if declared ( myMinIntBCD ) }
  1283. result := myMinIntBCD;
  1284. {$else}
  1285. RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
  1286. {$endif}
  1287. _WHENOTHER
  1288. bh := null_.bh;
  1289. WITH bh do
  1290. begin
  1291. Neg := aValue < 0;
  1292. if Neg
  1293. then v := -aValue
  1294. else v := +aValue;
  1295. LDig := 0;
  1296. p := 0;
  1297. Error := False;
  1298. REPEAT
  1299. Singles[p] := v MOD 10;
  1300. v := v DIV 10;
  1301. exitloop := v = 0;
  1302. Dec ( p );
  1303. if p < low ( Singles )
  1304. then begin
  1305. exitloop := True;
  1306. Error := True;
  1307. (* what to do if error occured? *)
  1308. RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
  1309. end;
  1310. UNTIL exitloop;
  1311. FDig := p + 1;
  1312. end;
  1313. pack_BCD ( bh, result );
  1314. _endSELECT;
  1315. end;
  1316. function VarToBCD ( const aValue : Variant ) : tBCD;
  1317. begin
  1318. not_implemented;
  1319. end;
  1320. function CurrToBCD ( const Curr : currency;
  1321. var BCD : tBCD;
  1322. Precision : Integer = 32;
  1323. Decimals : Integer = 4 ) : Boolean;
  1324. {
  1325. this works under the assumption that a currency is an int64,
  1326. except for scale of 10000
  1327. }
  1328. var
  1329. i : int64 absolute Curr;
  1330. begin
  1331. BCD := IntegerToBCD ( i );
  1332. {$ifndef bigger_BCD}
  1333. BCD.SignSpecialPlaces := 4 OR ( BCD.SignSpecialPlaces AND NegBit );
  1334. {$else}
  1335. BCD.Places := 4;
  1336. {$endif}
  1337. CurrToBCD := False;
  1338. if Decimals <> 4
  1339. then NormalizeBCD ( BCD, BCD, Precision, Decimals );
  1340. end;
  1341. {$ifdef comproutines}
  1342. function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
  1343. var
  1344. cc : int64 absolute Curr;
  1345. begin
  1346. result := IntegerToBCD ( cc );
  1347. end;
  1348. function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
  1349. var
  1350. zz : record
  1351. case Boolean of
  1352. False: ( i : int64 );
  1353. True: ( c : Comp );
  1354. end;
  1355. begin
  1356. zz.i := BCDToInteger ( BCD );
  1357. BCDToComp := zz.c;
  1358. end;
  1359. {$endif}
  1360. { Convert BCD struct to string/Double/Integer }
  1361. function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
  1362. var
  1363. bh : tBCD_helper;
  1364. l : {$ifopt r+} 0..maxfmtbcdfractionsize + 1 + 1 {$else} Integer {$endif};
  1365. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1366. pp : {$ifopt r+} low ( bh.FDig ) - 1..1 {$else} Integer {$endif};
  1367. begin
  1368. {$ifdef use_ansistring}
  1369. result := '';
  1370. {$endif}
  1371. unpack_BCD ( BCD, bh );
  1372. WITH bh do
  1373. begin
  1374. l := 0;
  1375. if Neg
  1376. then begin
  1377. {$ifndef use_ansistring}
  1378. Inc ( l );
  1379. result[1] := '-';
  1380. {$else}
  1381. result := result + '-';
  1382. {$endif}
  1383. end;
  1384. if Prec = Plac
  1385. then begin
  1386. {$ifndef use_ansistring}
  1387. Inc ( l );
  1388. result[1] := '0';
  1389. {$else}
  1390. result := result + '0';
  1391. {$endif}
  1392. end;
  1393. if Prec > 0
  1394. then begin
  1395. pp := low ( bh.FDig ) - 1;
  1396. if Plac > 0
  1397. then pp := 1;
  1398. for i := FDig TO LDig do
  1399. begin
  1400. if i = pp
  1401. then begin
  1402. {$ifndef use_ansistring}
  1403. Inc ( l );
  1404. result[l] := '.';
  1405. {$else}
  1406. result := result + '.';
  1407. {$endif}
  1408. end;
  1409. {$ifndef use_ansistring}
  1410. Inc ( l );
  1411. result[l] := Chr ( Singles[i] + Ord ( '0' ) );
  1412. {$else}
  1413. result := result + Chr ( Singles[i] + Ord ( '0' ) );
  1414. {$endif}
  1415. end;
  1416. end;
  1417. end;
  1418. {$ifndef use_ansistring}
  1419. result[0] := Chr ( l );
  1420. {$endif}
  1421. end;
  1422. function BCDToDouble ( const BCD : tBCD ) : myRealtype;
  1423. var
  1424. bh : tBCD_helper;
  1425. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1426. r,
  1427. e : myRealtype;
  1428. begin
  1429. unpack_BCD ( BCD, bh );
  1430. WITH bh do
  1431. begin
  1432. r := 0;
  1433. e := 1;
  1434. for i := 0 DOWNTO FDig do
  1435. begin
  1436. r := r + Singles[i] * e;
  1437. e := e * 10;
  1438. end;
  1439. e := 1;
  1440. for i := 1 TO LDig do
  1441. begin
  1442. e := e / 10;
  1443. r := r + Singles[i] * e;
  1444. end;
  1445. if Neg
  1446. then BCDToDouble := -r
  1447. else BCDToDouble := +r;
  1448. end;
  1449. end;
  1450. function BCDToInteger ( const BCD : tBCD;
  1451. Truncate : Boolean = False ) : myInttype;
  1452. var
  1453. bh : tBCD_helper;
  1454. res : myInttype;
  1455. i : {$ifopt r+} low ( bh.FDig )..0 {$else} Integer {$endif};
  1456. {
  1457. unclear: behaviour if overflow: abort? return 0? return something?
  1458. so: checks are missing yet
  1459. }
  1460. begin
  1461. unpack_BCD ( BCD, bh );
  1462. res := 0;
  1463. WITH bh do
  1464. begin
  1465. for i := FDig TO 0 do
  1466. res := res * 10 - Singles[i];
  1467. if NOT Truncate
  1468. then
  1469. if Plac > 0
  1470. then
  1471. if Singles[1] > 4
  1472. then Dec ( res );
  1473. if Neg
  1474. then BCDToInteger := +res
  1475. else BCDToInteger := -res;
  1476. end;
  1477. end;
  1478. { From DB.pas }
  1479. function BCDToCurr ( const BCD : tBCD;
  1480. var Curr : currency ) : Boolean;
  1481. var
  1482. bh : tBCD_helper;
  1483. res : int64;
  1484. c : currency absolute res;
  1485. i : {$ifopt r+} low ( bh.FDig )..4 {$else} Integer {$endif};
  1486. {
  1487. unclear: behaviour if overflow: abort? return 0? return something?
  1488. }
  1489. begin
  1490. BCDToCurr := True;
  1491. unpack_BCD ( BCD, bh );
  1492. res := 0;
  1493. WITH bh do
  1494. begin
  1495. for i := FDig TO 4 do
  1496. res := res * 10 + Singles[i];
  1497. if Plac > 4
  1498. then
  1499. if Singles[5] > 4
  1500. then Inc ( res );
  1501. if Neg
  1502. then Curr := -c
  1503. else Curr := +c;
  1504. end;
  1505. end;
  1506. procedure BCDAdd ( const BCDin1,
  1507. BCDin2 : tBCD;
  1508. var BCDout : tBCD );
  1509. var
  1510. bhr,
  1511. bh1,
  1512. bh2 : tBCD_helper;
  1513. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1514. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1515. v : {$ifopt r+} 0..9 + 9 + 1 {$else} Integer {$endif};
  1516. BCD : tBCD;
  1517. negate : Boolean;
  1518. begin
  1519. negate := IsBCDNegative ( BCDin1 );
  1520. if negate <> IsBCDNegative ( BCDin2 )
  1521. then begin
  1522. if negate
  1523. then begin
  1524. BCD := BCDin1;
  1525. BCDNegate ( BCD );
  1526. BCDSubtract ( BCDin2, BCD, BCDout );
  1527. EXIT;
  1528. end;
  1529. BCD := BCDin2;
  1530. BCDNegate ( BCD );
  1531. BCDSubtract ( BCDin1, BCD, BCDout );
  1532. EXIT;
  1533. end;
  1534. bhr := null_.bh;
  1535. WITH bhr do
  1536. begin
  1537. unpack_BCD ( BCDin1, bh1 );
  1538. unpack_BCD ( BCDin2, bh2 );
  1539. if bh1.FDig < bh2.FDig
  1540. then FDig := bh1.FDig
  1541. else FDig := bh2.FDig;
  1542. if bh1.LDig > bh2.LDig
  1543. then LDig := bh1.LDig
  1544. else LDig := bh2.LDig;
  1545. Plac := LDig;
  1546. ue := 0;
  1547. for i := LDig DOWNTO FDig do
  1548. begin
  1549. v := bh1.Singles[i] + bh2.Singles[i] + ue;
  1550. ue := v DIV 10;
  1551. Singles[i] := v MOD 10;
  1552. end;
  1553. if ue <> 0
  1554. then begin
  1555. Dec ( FDig );
  1556. Singles[FDig] := ue;
  1557. end;
  1558. Neg := negate;
  1559. end;
  1560. if NOT pack_BCD ( bhr, BCDout )
  1561. then begin
  1562. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1563. end;
  1564. end;
  1565. procedure BCDSubtract ( const BCDin1,
  1566. BCDin2 : tBCD;
  1567. var BCDout : tBCD );
  1568. var
  1569. bhr,
  1570. bh1,
  1571. bh2 : tBCD_helper;
  1572. cmp : {$ifopt r+} -1..1 {$else} Integer {$endif};
  1573. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1574. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1575. v : {$ifopt r+} 0 - 9 - 1..9 - 0 - 0 {$else} Integer {$endif};
  1576. negate : Boolean;
  1577. BCD : tBCD;
  1578. begin
  1579. negate := IsBCDNegative ( BCDin1 );
  1580. if negate <> IsBCDNegative ( BCDin2 )
  1581. then begin
  1582. if negate
  1583. then begin
  1584. BCD := BCDin1;
  1585. BCDNegate ( BCD );
  1586. BCDAdd ( BCDin2, BCD, BCDout );
  1587. BCDNegate ( BCDout );
  1588. EXIT;
  1589. end;
  1590. BCD := BCDin2;
  1591. BCDNegate ( BCD );
  1592. BCDAdd ( BCDin1, BCD, BCDout );
  1593. EXIT;
  1594. end;
  1595. cmp := BCDCompare ( BCDin1, BCDin2 );
  1596. if cmp = 0
  1597. then begin
  1598. BCDout := NullBCD;
  1599. EXIT;
  1600. end;
  1601. bhr := null_.bh; { n n }
  1602. WITH bhr do { > < > < }
  1603. begin { }
  1604. if ( cmp > 0 ) = negate { +123 +12 -12 -123 }
  1605. then begin { - +12 - +123 - -123 - -12 }
  1606. unpack_BCD ( BCDin1, bh2 ); { x x }
  1607. unpack_BCD ( BCDin2, bh1 ); { s s s s }
  1608. negate := NOT negate; { nn n nn n }
  1609. end
  1610. else begin
  1611. unpack_BCD ( BCDin1, bh1 );
  1612. unpack_BCD ( BCDin2, bh2 );
  1613. end;
  1614. if bh1.FDig < bh2.FDig
  1615. then FDig := bh1.FDig
  1616. else FDig := bh2.FDig;
  1617. if bh1.LDig > bh2.LDig
  1618. then LDig := bh1.LDig
  1619. else LDig := bh2.LDig;
  1620. Plac := LDig;
  1621. ue := 0;
  1622. for i := LDig DOWNTO FDig do
  1623. begin
  1624. v := Integer ( bh1.Singles[i] ) - bh2.Singles[i] - ue;
  1625. ue := 0;
  1626. if v < 0
  1627. then begin
  1628. ue := 1;
  1629. Inc ( v, 10 );
  1630. end;
  1631. Singles[i] := v;
  1632. end;
  1633. Neg := negate;
  1634. if NOT pack_BCD ( bhr, BCDout )
  1635. then begin
  1636. {should never occur!}
  1637. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  1638. end;
  1639. end;
  1640. end;
  1641. { Returns True if successful, False if Int Digits needed to be truncated }
  1642. function NormalizeBCD ( const InBCD : tBCD;
  1643. var OutBCD : tBCD;
  1644. const Prec,
  1645. Scale : Word ) : Boolean;
  1646. var
  1647. bh : tBCD_helper;
  1648. tm : {$ifopt r+} 1..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  1649. begin
  1650. NormalizeBCD := True;
  1651. {$ifopt r+}
  1652. if ( Prec < 0 ) OR ( Prec > MaxFmtBCDFractionSize ) then rcheck := rbad;
  1653. if ( Scale < 0 ) OR ( Prec >= MaxFmtBCDFractionSize ) then rcheck := rbad;
  1654. {$endif}
  1655. if BCDScale ( InBCD ) > Scale
  1656. then begin
  1657. unpack_BCD ( InBCD, bh );
  1658. WITH bh do
  1659. begin
  1660. tm := Plac - Scale;
  1661. Plac := Scale;
  1662. { dec ( prec, tm ); Dec/Inc error? }
  1663. Prec := Prec - tm;
  1664. { dec ( ldig, tm ); Dec/Inc error? }
  1665. LDig := LDig - tm;
  1666. NormalizeBCD := False;
  1667. end;
  1668. if NOT pack_BCD ( bh, OutBCD )
  1669. then begin
  1670. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1671. end;
  1672. end;
  1673. end;
  1674. procedure BCDMultiply ( const BCDin1,
  1675. BCDin2 : tBCD;
  1676. var BCDout : tBCD );
  1677. var
  1678. bh1,
  1679. bh2,
  1680. bhr : tBCD_helper;
  1681. bhrr : tBCD_helper_big;
  1682. i1 : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1683. i2 : {$ifopt r+} low ( bh2.FDig )..high ( bh2.LDig ) {$else} Integer {$endif};
  1684. i3 : {$ifopt r+} low ( bhrr.FDig )..high ( bhrr.LDig ) {$else} Integer {$endif};
  1685. v : {$ifopt r+} low ( bhrr.Singles[0] )..high ( bhrr.Singles[0] ) {$else} Integer {$endif};
  1686. ue : {$ifopt r+} low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  1687. begin
  1688. unpack_BCD ( BCDin1, bh1 );
  1689. unpack_BCD ( BCDin2, bh2 );
  1690. if ( bh1.Prec = 0 ) OR ( bh2.Prec = 0 )
  1691. then begin
  1692. BCDout := NullBCD;
  1693. EXIT;
  1694. end;
  1695. bhr := null_.bh;
  1696. bhrr := null_.bhb;
  1697. WITH bhrr do
  1698. begin
  1699. Neg := bh1.Neg XOR bh2.Neg;
  1700. {
  1701. writeln ( __lo_bhb, ' ', __hi_bhb, ' ', bh1.fdig, ' ', bh2.fdig, ' ', low ( fdig ), ' ', low ( ldig ) );
  1702. }
  1703. FDig := bh1.FDig + bh2.FDig;
  1704. LDig := bh1.LDig + bh2.LDig;
  1705. for i1 := bh1.FDig TO bh1.LDig do
  1706. for i2 := bh2.FDig TO bh2.LDig do
  1707. begin
  1708. Inc ( Singles[i1 + i2],
  1709. bh1.Singles[i1]
  1710. * bh2.Singles[i2] );
  1711. {
  1712. write ( Singles[i1 + i2], ' ', bh1.Singles[i1], ' ', bh2.Singles[i2], ' : ' );
  1713. writeln ( Singles[i1 + i2] + bh1.Singles[i1] + bh2.Singles[i2] );
  1714. }
  1715. {
  1716. Singles[i1 + i2] := Singles[i1 + i2]
  1717. + bh1.Singles[i1]
  1718. * bh2.Singles[i2];
  1719. }
  1720. end;
  1721. {
  1722. for i3 := fdig to ldig do
  1723. write ( ' ', singles[i3] );
  1724. writeln;
  1725. }
  1726. if FDig < low ( bhr.Singles )
  1727. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1728. ue := 0;
  1729. for i3 := LDig DOWNTO FDig do
  1730. begin
  1731. v := Singles[i3] + ue;
  1732. ue := v DIV 10;
  1733. v := v MOD 10;
  1734. bhr.Singles[i3] := v;
  1735. end;
  1736. while ue <> 0 do
  1737. begin
  1738. Dec ( FDig );
  1739. if FDig < low ( bhr.Singles )
  1740. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1741. bhr.Singles[FDig] := ue MOD 10;
  1742. ue := ue DIV 10;
  1743. end;
  1744. bhr.Plac := LDig;
  1745. bhr.FDig := FDig;
  1746. if LDig > high ( bhr.Singles )
  1747. then bhr.LDig := high ( bhr.Singles )
  1748. else bhr.LDig := LDig;
  1749. end;
  1750. if NOT pack_BCD ( bhr, BCDout )
  1751. then begin
  1752. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1753. end;
  1754. end;
  1755. procedure BCDMultiply ( const BCDIn : tBCD;
  1756. const DoubleIn : myRealtype;
  1757. var BCDout : tBCD ); Inline;
  1758. begin
  1759. BCDMultiply ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  1760. end;
  1761. procedure BCDMultiply ( const BCDIn : tBCD;
  1762. const StringIn : FmtBCDStringtype;
  1763. var BCDout : tBCD ); Inline;
  1764. begin
  1765. BCDMultiply ( BCDIn, StrToBCD ( StringIn ), BCDout );
  1766. end;
  1767. procedure BCDMultiply ( const StringIn1,
  1768. StringIn2 : FmtBCDStringtype;
  1769. var BCDout : tBCD ); Inline;
  1770. begin
  1771. BCDMultiply ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  1772. end;
  1773. procedure BCDDivide ( const Dividend,
  1774. Divisor : tBCD;
  1775. var BCDout : tBCD );
  1776. var
  1777. bh1 : ARRAY [ Boolean ] of tBCD_helper;
  1778. bh2,
  1779. bh : tBCD_helper;
  1780. p : {$ifopt r+} low ( bh.FDig ) - high ( bh.FDig )..high ( bh.FDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1781. v1 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1782. v2 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1783. lFDig : {$ifopt r+} low ( bh.FDig )..high ( bh.FDig ) {$else} Integer {$endif};
  1784. d1 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1785. d2 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1786. d : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1787. lLdig : {$ifopt r+} low ( lFDig ) + low ( d )..high ( lFDig ) + high ( d ) {$else} Integer {$endif};
  1788. tm : {$ifopt r+} low ( lLdig ) - high ( bh2.Singles )..high ( lLdig ) - high ( bh2.Singles ) {$else} Integer {$endif};
  1789. i2 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1790. i3 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1791. ie : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1792. i4 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1793. nFDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1794. nLDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1795. dd : {$ifopt r+} 0..9 {$else} Integer {$endif};
  1796. Add : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1797. ue : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1798. v3 : {$ifopt r+} low ( bh.Singles[0] ) - high ( bh2.singles[9] ) * high ( dd ) - high ( ue )..high ( bh.Singles[0] ) - low ( bh2.singles[9] ) * low ( dd ) - low ( ue ) {$else} Integer {$endif};
  1799. v4 : {$ifopt r+} low ( bh.Singles[0] ) + low ( add )..high ( bh.Singles[0] ) + high ( add ) {$else} Integer {$endif};
  1800. FlipFlop,
  1801. nz,
  1802. sf,
  1803. sh,
  1804. fdset : Boolean;
  1805. {
  1806. bh1p : ARRAY [ Boolean ] of ^ tBCD_helper;
  1807. }
  1808. begin
  1809. { test:
  1810. bh1p[false] := @ bh1[false];
  1811. bh1p[true] := @ bh1[true];
  1812. v := bh1[false].singles[0];
  1813. v := bh1[true].singles[0];
  1814. v := bh1p[false]^.singles[0];
  1815. v := bh1p[true]^.singles[0];
  1816. v := bh1[nz].singles[0];
  1817. v := bh1p[nz]^.singles[0];
  1818. }
  1819. unpack_BCD ( Divisor, bh2 );
  1820. unpack_BCD ( Dividend, bh1[False] );
  1821. p := bh1[False].FDig - bh2.FDig;
  1822. _SELECT
  1823. _WHEN bh2.Prec = 0
  1824. _THEN RAISE eBCDException.create ( 'Division by zero' );
  1825. _WHEN bh1[False].Prec = 0
  1826. _THEN BCDout := NullBCD;
  1827. _WHEN p < low ( bh2.Singles )
  1828. _THEN RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  1829. _WHENOTHER
  1830. bh := null_.bh;
  1831. bh.Neg := bh1[False].Neg XOR bh2.Neg;
  1832. if p <= high ( bh.Singles )
  1833. then begin
  1834. bh1[True] := null_.bh;
  1835. FlipFlop := False;
  1836. fdset := p > 0;
  1837. if fdset
  1838. then bh.FDig := 0;
  1839. add := 0;
  1840. nz := True;
  1841. while nz do
  1842. WITH bh1[FlipFlop] do
  1843. begin
  1844. {
  1845. WriteLn('#####');
  1846. dumpbh ( bh1[flipflop] );
  1847. dumpbh ( bh2 );
  1848. dumpbh ( bh );
  1849. }
  1850. if ( Singles[FDig] + bh2.Singles[bh2.FDig] ) = 0
  1851. then begin
  1852. if ( FDig >= LDig )
  1853. OR ( bh2.FDig >= bh2.LDig )
  1854. then nz := False
  1855. else begin
  1856. Inc ( FDig );
  1857. Inc ( bh2.FDig );
  1858. end;
  1859. end
  1860. else begin
  1861. v1 := Singles[FDig];
  1862. v2 := bh2.Singles[bh2.FDig];
  1863. sh := v1 < v2;
  1864. if ( v1 = v2 )
  1865. then begin
  1866. nz := False;
  1867. i3 := Succ ( FDig );
  1868. ie := LDig;
  1869. while ( i3 <= ie ) AND ( NOT nz ) AND ( NOT sh ) do
  1870. begin
  1871. v1 := Singles[i3];
  1872. v2 := bh2.Singles[i3 - p];
  1873. if v1 <> v2
  1874. then begin
  1875. nz := True;
  1876. if v1 < v2
  1877. then sh := True;
  1878. end;
  1879. Inc ( i3 );
  1880. end;
  1881. end;
  1882. if NOT nz
  1883. then Add := 1
  1884. else begin
  1885. if sh
  1886. then begin
  1887. Inc ( p );
  1888. {
  1889. if p > 3 then halt;
  1890. }
  1891. if p > high ( bh.Singles )
  1892. then nz := False
  1893. else Dec ( bh2.FDig );
  1894. end
  1895. else begin
  1896. lFDig := FDig;
  1897. d1 := LDig - FDig;
  1898. d2 := bh2.LDig - bh2.FDig;
  1899. if d1 > d2
  1900. then d := d1
  1901. else d := d2;
  1902. lLdig := lFDig + d;
  1903. if lLdig > high ( bh2.Singles )
  1904. then begin
  1905. tm := ( lLdig ) - high ( bh2.Singles );
  1906. d := d - tm;
  1907. lLdig := lLdig - tm;
  1908. {runden?}
  1909. end;
  1910. sf := True;
  1911. Add := 0;
  1912. nFDig := 0;
  1913. nLDig := 0;
  1914. ue := 0;
  1915. dd := Singles[lFDig] DIV ( bh2.Singles[lFDig - p] + 1 );
  1916. {
  1917. dd := 1;
  1918. }
  1919. if dd < 1
  1920. then dd := 1;
  1921. {
  1922. writeln ( 'p=', p, ' dd=', dd, ' lFdig=', lfdig, ' lldig=', lldig );
  1923. }
  1924. for i2 := lLdig DOWNTO lFDig do
  1925. begin
  1926. v3 := Singles[i2] - bh2.Singles[i2 - p] * dd - ue;
  1927. ue := 0;
  1928. while v3 < 0 do
  1929. begin
  1930. Inc ( ue );;
  1931. v3 := v3 + 10;
  1932. end;
  1933. {
  1934. if v3 <> 0
  1935. then begin
  1936. }
  1937. bh1[NOT FlipFlop].Singles[i2] := v3;
  1938. {
  1939. nFDig := i2;
  1940. if sf
  1941. then begin
  1942. nLDig := i2;
  1943. sf := False;
  1944. end;
  1945. end;
  1946. }
  1947. end;
  1948. sf := False;
  1949. nfdig := lfdig;
  1950. nldig := lldig;
  1951. Inc ( Add, dd );
  1952. if NOT fdset
  1953. then begin
  1954. bh.FDig := p;
  1955. fdset := True;
  1956. end;
  1957. if bh.LDig < p
  1958. then begin
  1959. bh.LDig := p;
  1960. if ( bh.LDig - bh.FDig ) > Succ ( MaxFmtBCDFractionSize )
  1961. then nz := False;
  1962. end;
  1963. if sf
  1964. then nz := False
  1965. else begin
  1966. FillChar ( bh1[FlipFlop], SizeOf ( bh1[FlipFlop] ), #0 );
  1967. FlipFlop := NOT FlipFlop;
  1968. WITH bh1[FlipFlop] do
  1969. begin
  1970. FDig := nFDig;
  1971. LDig := nLDig;
  1972. end;
  1973. end;
  1974. end;
  1975. end;
  1976. if Add <> 0
  1977. then begin
  1978. i4 := p;
  1979. while ( Add <> 0 ) AND ( i4 >= bh.FDig ) do
  1980. begin
  1981. {
  1982. writeln ( '> ', i4, ' ', bh.Singles[i4], ' ', Add );
  1983. }
  1984. v4 := bh.Singles[i4] + Add;
  1985. Add := v4 DIV 10;
  1986. bh.Singles[i4] := v4 MOD 10;
  1987. Dec ( i4 );
  1988. end;
  1989. if Add <> 0
  1990. then begin
  1991. Dec ( bh.FDig );
  1992. bh.Singles[bh.FDig] := Add;
  1993. Add := 0;
  1994. end;
  1995. end;
  1996. end;
  1997. end;
  1998. end;
  1999. WITH bh do
  2000. begin
  2001. if LDig < 0
  2002. then LDig := 0;
  2003. if LDig > 0
  2004. then Plac := LDig
  2005. else Plac := 0;
  2006. end;
  2007. if NOT pack_BCD ( bh, BCDout )
  2008. then begin
  2009. RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  2010. end;
  2011. _endSELECT
  2012. end;
  2013. procedure BCDDivide ( const Dividend,
  2014. Divisor : FmtBCDStringtype;
  2015. var BCDout : tBCD ); Inline;
  2016. begin
  2017. BCDDivide ( StrToBCD ( Dividend ), StrToBCD ( Divisor ), BCDout );
  2018. end;
  2019. procedure BCDDivide ( const Dividend : tBCD;
  2020. const Divisor : myRealtype;
  2021. var BCDout : tBCD ); Inline;
  2022. begin
  2023. BCDDivide ( Dividend, DoubleToBCD ( Divisor ), BCDout );
  2024. end;
  2025. procedure BCDDivide ( const Dividend : tBCD;
  2026. const Divisor : FmtBCDStringtype;
  2027. var BCDout : tBCD ); Inline;
  2028. begin
  2029. BCDDivide ( Dividend, StrToBCD ( Divisor ), BCDout );
  2030. end;
  2031. { TBCD variant creation utils }
  2032. procedure VarFmtBCDCreate ( var aDest : Variant;
  2033. const aBCD : tBCD );
  2034. begin
  2035. VarClear(aDest);
  2036. TVarData(aDest).Vtype:=FMTBcdFactory.Vartype;
  2037. TVarData(aDest).VPointer:=TFMTBcdVarData.create(aBCD);
  2038. end;
  2039. function VarFmtBCDCreate : Variant;
  2040. begin
  2041. VarFmtBCDCreate ( result, NullBCD );
  2042. end;
  2043. function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
  2044. Precision,
  2045. Scale : Word ) : Variant;
  2046. begin
  2047. VarFmtBCDCreate ( result, StrToBCD ( aValue ) );
  2048. end;
  2049. function VarFmtBCDCreate ( const aValue : myRealtype;
  2050. Precision : Word = 18;
  2051. Scale : Word = 4 ) : Variant;
  2052. begin
  2053. VarFmtBCDCreate ( result, DoubleToBCD ( aValue ) );
  2054. end;
  2055. function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
  2056. begin
  2057. VarFmtBCDCreate ( result, aBCD );
  2058. end;
  2059. function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
  2060. begin
  2061. result:=false;
  2062. not_implemented;
  2063. end;
  2064. function VarFmtBCD : TVartype;
  2065. begin
  2066. result:=0;
  2067. not_implemented;
  2068. end;
  2069. { Formatting BCD as string }
  2070. function BCDToStrF ( const BCD : tBCD;
  2071. Format : TFloatFormat;
  2072. const Precision,
  2073. Digits : Integer ) : FmtBCDStringtype;
  2074. begin
  2075. not_implemented;
  2076. result:='';
  2077. end;
  2078. function FormatBCD ( const Format : string;
  2079. BCD : tBCD ) : FmtBCDStringtype;
  2080. begin
  2081. not_implemented;
  2082. result:='';
  2083. end;
  2084. {$ifdef additional_routines}
  2085. function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
  2086. begin
  2087. CurrToBCD ( Curr, result );
  2088. end;
  2089. procedure BCDAdd ( const BCDIn : tBCD;
  2090. const IntIn : myInttype;
  2091. var BCDout : tBCD );
  2092. var
  2093. BCD : tBCD;
  2094. bhr : tBCD_helper;
  2095. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2096. ue : {$ifopt r+} 0..high ( IntIn ) - 9 {$else} Integer {$endif};
  2097. v : {$ifopt r+} 0..{high ( ue ) + 9} high ( IntIn ) {$else} Integer {$endif};
  2098. nz : Boolean;
  2099. begin
  2100. if IntIn = 0
  2101. then begin
  2102. BCDout := BCDIn;
  2103. EXIT;
  2104. end;
  2105. if IntIn = low ( myInttype )
  2106. then begin
  2107. {$if declared ( myMinIntBCD ) }
  2108. BCDAdd ( BCDIn, myMinIntBCD, BCDout );
  2109. EXIT;
  2110. {$else}
  2111. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2112. {$endif}
  2113. end;
  2114. if IsBCDNegative ( BCDIn )
  2115. then begin
  2116. BCD := BCDIn;
  2117. BCDNegate ( BCD );
  2118. if IntIn < 0
  2119. then BCDAdd ( BCD, -IntIn, BCDout )
  2120. else BCDSubtract ( BCD, IntIn, BCDout );
  2121. BCDNegate ( BCDout );
  2122. EXIT;
  2123. end;
  2124. if IntIn < 0
  2125. then begin
  2126. BCDSubtract ( BCDIn, -IntIn, BCDout );
  2127. EXIT;
  2128. end;
  2129. if IntIn > ( high ( IntIn ) - 9 )
  2130. then begin
  2131. BCDAdd ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2132. EXIT;
  2133. end;
  2134. unpack_BCD ( BCDIn, bhr );
  2135. p := 0;
  2136. nz := True;
  2137. ue := IntIn;
  2138. while nz do
  2139. begin
  2140. v := bhr.Singles[p] + ue;
  2141. bhr.Singles[p] := v MOD 10;
  2142. ue := v DIV 10;
  2143. if ue = 0
  2144. then nz := False
  2145. else Dec ( p );
  2146. end;
  2147. if p < bhr.FDig
  2148. then begin
  2149. bhr.FDig := p;
  2150. bhr.Prec := bhr.Prec + ( bhr.FDig - p );
  2151. end;
  2152. if NOT pack_BCD ( bhr, BCDout )
  2153. then begin
  2154. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2155. end;
  2156. end;
  2157. procedure BCDSubtract ( const BCDIn : tBCD;
  2158. const IntIn : myInttype;
  2159. var BCDout : tBCD );
  2160. {}
  2161. var
  2162. BCD : tBCD;
  2163. bhr : tBCD_helper;
  2164. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2165. ue : {$ifopt r+} 0..pred ( 100000000 ) {$else} Integer {$endif};
  2166. v : {$ifopt r+} -9..9 {$else} Integer {$endif};
  2167. direct : Boolean;
  2168. {}
  2169. begin
  2170. if IntIn = 0
  2171. then begin
  2172. BCDout := BCDIn;
  2173. EXIT;
  2174. end;
  2175. if IntIn = low ( myInttype )
  2176. then begin
  2177. {$if declared ( myMinIntBCD ) }
  2178. BCDSubtract ( BCDIn, myMinIntBCD, BCDout );
  2179. EXIT;
  2180. {$else}
  2181. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2182. {$endif}
  2183. end;
  2184. if IsBCDNegative ( BCDIn )
  2185. then begin
  2186. BCD := BCDIn;
  2187. BCDNegate ( BCD );
  2188. if IntIn < 0
  2189. then BCDSubtract ( BCD, -IntIn, BCDout )
  2190. else BCDAdd ( BCD, IntIn, BCDout );
  2191. BCDNegate ( BCDout );
  2192. EXIT;
  2193. end;
  2194. if IntIn < 0
  2195. then begin
  2196. BCDAdd ( BCDIn, -IntIn, BCDout );
  2197. EXIT;
  2198. end;
  2199. direct := False;
  2200. case BCDIn.Precision
  2201. -
  2202. {$ifndef bigger_BCD}
  2203. ( BCDIn.SignSpecialPlaces AND PlacesMask )
  2204. {$else}
  2205. BCDIn.Places
  2206. {$endif}
  2207. of
  2208. 2: direct := IntIn < 10;
  2209. 3: direct := IntIn < 100;
  2210. 4: direct := IntIn < 1000;
  2211. 5: direct := IntIn < 10000;
  2212. 6: direct := IntIn < 100000;
  2213. 7: direct := IntIn < 1000000;
  2214. 8: direct := IntIn < 10000000;
  2215. 9: direct := IntIn < 100000000;
  2216. end;
  2217. {
  2218. write(direct);dumpbcd(bcdin);write('[',intin,']');
  2219. }
  2220. if direct
  2221. then begin
  2222. unpack_BCD ( BCDIn, bhr );
  2223. WITH bhr do
  2224. begin
  2225. p := 0;
  2226. ue := IntIn;
  2227. while p >= FDig do
  2228. begin
  2229. v := Singles[p] - ue MOD 10;
  2230. ue := ue DIV 10;
  2231. if v < 0
  2232. then begin
  2233. v := v + 10;
  2234. ue := ue + 1;
  2235. end;
  2236. Singles[p] := v;
  2237. Dec ( p );
  2238. end;
  2239. end;
  2240. if NOT pack_BCD ( bhr, BCDout )
  2241. then begin
  2242. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2243. end;
  2244. end
  2245. else
  2246. {}
  2247. BCDSubtract ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2248. end;
  2249. procedure BCDAdd ( const IntIn : myInttype;
  2250. const BCDIn : tBCD;
  2251. var BCDout : tBCD ); Inline;
  2252. begin
  2253. BCDAdd ( BCDIn, IntIn, BCDout );
  2254. end;
  2255. procedure BCDAdd ( const BCDIn : tBCD;
  2256. const DoubleIn : myRealtype;
  2257. var BCDout : tBCD ); Inline;
  2258. begin
  2259. BCDAdd ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2260. end;
  2261. procedure BCDAdd ( const DoubleIn : myRealtype;
  2262. const BCDIn : tBCD;
  2263. var BCDout : tBCD ); Inline;
  2264. begin
  2265. BCDAdd ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2266. end;
  2267. procedure BCDAdd ( const BCDIn : tBCD;
  2268. const Currin : currency;
  2269. var BCDout : tBCD ); Inline;
  2270. begin
  2271. BCDAdd ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2272. end;
  2273. procedure BCDAdd ( const Currin : currency;
  2274. const BCDIn : tBCD;
  2275. var BCDout : tBCD ); Inline;
  2276. begin
  2277. BCDAdd ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2278. end;
  2279. {$ifdef comproutines}
  2280. procedure BCDAdd ( const BCDIn : tBCD;
  2281. const Compin : Comp;
  2282. var BCDout : tBCD ); Inline;
  2283. begin
  2284. BCDAdd ( BCDIn, CompToBCD ( Compin ), BCDout );
  2285. end;
  2286. procedure BCDAdd ( const Compin : Comp;
  2287. const BCDIn : tBCD;
  2288. var BCDout : tBCD ); Inline;
  2289. begin
  2290. BCDAdd ( CompToBCD ( Compin ), BCDIn, BCDout );
  2291. end;
  2292. {$endif}
  2293. procedure BCDAdd ( const BCDIn : tBCD;
  2294. const StringIn : FmtBCDStringtype;
  2295. var BCDout : tBCD ); Inline;
  2296. begin
  2297. BCDAdd ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2298. end;
  2299. procedure BCDAdd ( const StringIn : FmtBCDStringtype;
  2300. const BCDIn : tBCD;
  2301. var BCDout : tBCD ); Inline;
  2302. begin
  2303. BCDAdd ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2304. end;
  2305. procedure BCDAdd ( const StringIn1,
  2306. StringIn2 : FmtBCDStringtype;
  2307. var BCDout : tBCD ); Inline;
  2308. begin
  2309. BCDAdd ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2310. end;
  2311. procedure BCDSubtract ( const IntIn : myInttype;
  2312. const BCDIn : tBCD;
  2313. var BCDout : tBCD ); Inline;
  2314. begin
  2315. BCDSubtract ( BCDIn, IntIn, BCDout );
  2316. BCDNegate ( BCDout );
  2317. end;
  2318. procedure BCDSubtract ( const BCDIn : tBCD;
  2319. const DoubleIn : myRealtype;
  2320. var BCDout : tBCD ); Inline;
  2321. begin
  2322. BCDSubtract ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2323. end;
  2324. procedure BCDSubtract ( const DoubleIn : myRealtype;
  2325. const BCDIn : tBCD;
  2326. var BCDout : tBCD ); Inline;
  2327. begin
  2328. BCDSubtract ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2329. end;
  2330. procedure BCDSubtract ( const BCDIn : tBCD;
  2331. const Currin : currency;
  2332. var BCDout : tBCD ); Inline;
  2333. begin
  2334. BCDSubtract ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2335. end;
  2336. procedure BCDSubtract ( const Currin : currency;
  2337. const BCDIn : tBCD;
  2338. var BCDout : tBCD ); Inline;
  2339. begin
  2340. BCDSubtract ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2341. end;
  2342. {$ifdef comproutines}
  2343. procedure BCDSubtract ( const BCDIn : tBCD;
  2344. const Compin : Comp;
  2345. var BCDout : tBCD ); Inline;
  2346. begin
  2347. BCDSubtract ( BCDIn, CompToBCD ( Compin ), BCDout );
  2348. end;
  2349. procedure BCDSubtract ( const Compin : Comp;
  2350. const BCDIn : tBCD;
  2351. var BCDout : tBCD ); Inline;
  2352. begin
  2353. BCDSubtract ( CompToBCD ( Compin ), BCDIn, BCDout );
  2354. end;
  2355. {$endif}
  2356. procedure BCDSubtract ( const BCDIn : tBCD;
  2357. const StringIn : FmtBCDStringtype;
  2358. var BCDout : tBCD ); Inline;
  2359. begin
  2360. BCDSubtract ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2361. end;
  2362. procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
  2363. const BCDIn : tBCD;
  2364. var BCDout : tBCD ); Inline;
  2365. begin
  2366. BCDSubtract ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2367. end;
  2368. procedure BCDSubtract ( const StringIn1,
  2369. StringIn2 : FmtBCDStringtype;
  2370. var BCDout : tBCD ); Inline;
  2371. begin
  2372. BCDSubtract ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2373. end;
  2374. procedure BCDMultiply ( const BCDIn : tBCD;
  2375. const IntIn : myInttype;
  2376. var BCDout : tBCD );
  2377. var
  2378. bh : tBCD_helper;
  2379. bhr : tBCD_helper;
  2380. bhrr : tBCD_helper_big;
  2381. int : {$ifopt r+} 0..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  2382. i1 : {$ifopt r+} low ( bh.Singles )..high ( bh.Singles ) {$else} Integer {$endif};
  2383. i3 : {$ifopt r+} low ( bhr.Singles )..high ( bhr.Singles ) {$else} Integer {$endif};
  2384. v : {$ifopt r+} low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  2385. ue : {$ifopt r+} 1 * ( low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10 ) DIV 10
  2386. ..( high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 ) DIV 10 {$else} Integer {$endif};
  2387. begin
  2388. if IntIn = 0
  2389. then begin
  2390. BCDout := NullBCD;
  2391. EXIT;
  2392. end;
  2393. if IntIn = 1
  2394. then begin
  2395. BCDout := BCDIn;
  2396. EXIT;
  2397. end;
  2398. if IntIn = -1
  2399. then begin
  2400. BCDout := BCDIn;
  2401. BCDNegate ( BCDout );
  2402. EXIT;
  2403. end;
  2404. if IntIn = low ( myInttype )
  2405. then begin
  2406. {$if declared ( myMinIntBCD ) }
  2407. BCDMultiply ( BCDIn, myMinIntBCD, BCDout );
  2408. EXIT;
  2409. {$else}
  2410. RAISE eBCDOverflowException.create ( 'in BCDmultiply' );
  2411. {$endif}
  2412. end;
  2413. if Abs ( IntIn ) > low ( bhrr.Singles[0] ) DIV 10
  2414. then begin
  2415. BCDMultiply ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2416. EXIT;
  2417. end;
  2418. unpack_BCD ( BCDIn, bh );
  2419. if bh.Prec = 0
  2420. then begin
  2421. BCDout := NullBCD;
  2422. EXIT;
  2423. end;
  2424. bhr := null_.bh;
  2425. bhrr := null_.bhb;
  2426. int := Abs ( IntIn );
  2427. WITH bhrr do
  2428. begin
  2429. Neg := bh.Neg XOR ( IntIn < 0 );
  2430. FDig := bh.FDig;
  2431. LDig := bh.LDig;
  2432. for i1 := bh.FDig TO bh.LDig do
  2433. Singles[i1] := bh.Singles[i1] * int;
  2434. {
  2435. for i3 := fdig to ldig do
  2436. write ( ' ', singles[i3] );
  2437. writeln;
  2438. }
  2439. ue := 0;
  2440. for i3 := LDig DOWNTO FDig do
  2441. begin
  2442. v := Singles[i3] + ue;
  2443. ue := v DIV 10;
  2444. v := v MOD 10;
  2445. bhr.Singles[i3] := v;
  2446. end;
  2447. while ue <> 0 do
  2448. begin
  2449. Dec ( FDig );
  2450. if FDig < low ( bhr.Singles )
  2451. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2452. bhr.Singles[FDig] := ue MOD 10;
  2453. ue := ue DIV 10;
  2454. end;
  2455. bhr.Plac := LDig;
  2456. bhr.FDig := FDig;
  2457. if LDig > high ( bhr.Singles )
  2458. then bhr.LDig := high ( bhr.Singles )
  2459. else bhr.LDig := LDig;
  2460. end;
  2461. if NOT pack_BCD ( bhr, BCDout )
  2462. then begin
  2463. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2464. end;
  2465. end;
  2466. procedure BCDMultiply ( const IntIn : myInttype;
  2467. const BCDIn : tBCD;
  2468. var BCDout : tBCD ); Inline;
  2469. begin
  2470. BCDMultiply ( BCDIn, IntIn, BCDout );
  2471. end;
  2472. procedure BCDMultiply ( const DoubleIn : myRealtype;
  2473. const BCDIn : tBCD;
  2474. var BCDout : tBCD ); Inline;
  2475. begin
  2476. BCDMultiply ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2477. end;
  2478. procedure BCDMultiply ( const BCDIn : tBCD;
  2479. const Currin : currency;
  2480. var BCDout : tBCD ); Inline;
  2481. begin
  2482. BCDMultiply ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2483. end;
  2484. procedure BCDMultiply ( const Currin : currency;
  2485. const BCDIn : tBCD;
  2486. var BCDout : tBCD ); Inline;
  2487. begin
  2488. BCDMultiply ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2489. end;
  2490. {$ifdef comproutines}
  2491. procedure BCDMultiply ( const BCDIn : tBCD;
  2492. const Compin : Comp;
  2493. var BCDout : tBCD ); Inline;
  2494. begin
  2495. BCDMultiply ( BCDIn, CompToBCD ( Compin ), BCDout );
  2496. end;
  2497. procedure BCDMultiply ( const Compin : Comp;
  2498. const BCDIn : tBCD;
  2499. var BCDout : tBCD ); Inline;
  2500. begin
  2501. BCDMultiply ( CompToBCD ( Compin ), BCDIn, BCDout );
  2502. end;
  2503. {$endif}
  2504. procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
  2505. const BCDIn : tBCD;
  2506. var BCDout : tBCD ); Inline;
  2507. begin
  2508. BCDMultiply ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2509. end;
  2510. procedure BCDDivide ( const Dividend : tBCD;
  2511. const Divisor : myInttype;
  2512. var BCDout : tBCD ); Inline;
  2513. begin
  2514. BCDDivide ( Dividend, IntegerToBCD ( Divisor ), BCDout );
  2515. end;
  2516. procedure BCDDivide ( const Dividend : myInttype;
  2517. const Divisor : tBCD;
  2518. var BCDout : tBCD ); Inline;
  2519. begin
  2520. BCDDivide ( IntegerToBCD ( Dividend ), Divisor, BCDout );
  2521. end;
  2522. procedure BCDDivide ( const Dividend : myRealtype;
  2523. const Divisor : tBCD;
  2524. var BCDout : tBCD ); Inline;
  2525. begin
  2526. BCDDivide ( DoubleToBCD ( Dividend ), Divisor, BCDout );
  2527. end;
  2528. procedure BCDDivide ( const BCDIn : tBCD;
  2529. const Currin : currency;
  2530. var BCDout : tBCD ); Inline;
  2531. begin
  2532. BCDDivide ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2533. end;
  2534. procedure BCDDivide ( const Currin : currency;
  2535. const BCDIn : tBCD;
  2536. var BCDout : tBCD ); Inline;
  2537. begin
  2538. BCDDivide ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2539. end;
  2540. {$ifdef comproutines}
  2541. procedure BCDDivide ( const BCDIn : tBCD;
  2542. const Compin : Comp;
  2543. var BCDout : tBCD ); Inline;
  2544. begin
  2545. BCDDivide ( BCDIn, CompToBCD ( Compin ), BCDout );
  2546. end;
  2547. procedure BCDDivide ( const Compin : Comp;
  2548. const BCDIn : tBCD;
  2549. var BCDout : tBCD ); Inline;
  2550. begin
  2551. BCDDivide ( CompToBCD ( Compin ), BCDIn, BCDout );
  2552. end;
  2553. {$endif}
  2554. procedure BCDDivide ( const Dividend : FmtBCDStringtype;
  2555. const Divisor : tBCD;
  2556. var BCDout : tBCD ); Inline;
  2557. begin
  2558. BCDDivide ( StrToBCD ( Dividend ), Divisor, BCDout );
  2559. end;
  2560. operator = ( const BCD1,
  2561. BCD2 : tBCD ) z : Boolean; Inline;
  2562. begin
  2563. z := BCDCompare ( BCD1, BCD2 ) = 0;
  2564. end;
  2565. operator < ( const BCD1,
  2566. BCD2 : tBCD ) z : Boolean; Inline;
  2567. begin
  2568. z := BCDCompare ( BCD1, BCD2 ) < 0;
  2569. end;
  2570. operator > ( const BCD1,
  2571. BCD2 : tBCD ) z : Boolean; Inline;
  2572. begin
  2573. z := BCDCompare ( BCD1, BCD2 ) > 0;
  2574. end;
  2575. operator <= ( const BCD1,
  2576. BCD2 : tBCD ) z : Boolean; Inline;
  2577. begin
  2578. z := BCDCompare ( BCD1, BCD2 ) <= 0;
  2579. end;
  2580. operator >= ( const BCD1,
  2581. BCD2 : tBCD ) z : Boolean; Inline;
  2582. begin
  2583. z := BCDCompare ( BCD1, BCD2 ) >= 0;
  2584. end;
  2585. (* ######################## not allowed: why?
  2586. operator + ( const BCD : tBCD ) z : tBCD; Inline;
  2587. begin
  2588. z := bcd;
  2589. end;
  2590. ##################################################### *)
  2591. operator - ( const BCD : tBCD ) z : tBCD; Inline;
  2592. begin
  2593. z := BCD;
  2594. BCDNegate ( z );
  2595. end;
  2596. operator + ( const BCD1,
  2597. BCD2 : tBCD ) z : tBCD; Inline;
  2598. begin
  2599. BCDAdd ( BCD1, BCD2, z );
  2600. end;
  2601. operator + ( const BCD : tBCD;
  2602. const i : myInttype ) z : tBCD; Inline;
  2603. begin
  2604. BCDAdd ( BCD, i, z );
  2605. end;
  2606. operator + ( const i : myInttype;
  2607. const BCD : tBCD ) z : tBCD; Inline;
  2608. begin
  2609. BCDAdd ( i, BCD, z );
  2610. end;
  2611. operator + ( const BCD : tBCD;
  2612. const r : myRealtype ) z : tBCD; Inline;
  2613. begin
  2614. BCDAdd ( BCD, DoubleToBCD ( r ), z );
  2615. end;
  2616. operator + ( const r : myRealtype;
  2617. const BCD : tBCD ) z : tBCD; Inline;
  2618. begin
  2619. BCDAdd ( DoubleToBCD ( r ), BCD, z );
  2620. end;
  2621. operator + ( const BCD : tBCD;
  2622. const c : currency ) z : tBCD; Inline;
  2623. begin
  2624. BCDAdd ( BCD, CurrToBCD ( c ), z );
  2625. end;
  2626. operator + ( const c : currency;
  2627. const BCD : tBCD ) z : tBCD; Inline;
  2628. begin
  2629. BCDAdd ( CurrToBCD ( c ), BCD, z );
  2630. end;
  2631. {$ifdef comproutines}
  2632. operator + ( const BCD : tBCD;
  2633. const c : Comp ) z : tBCD; Inline;
  2634. begin
  2635. BCDAdd ( BCD, CompToBCD ( c ), z );
  2636. end;
  2637. operator + ( const c : Comp;
  2638. const BCD : tBCD ) z : tBCD; Inline;
  2639. begin
  2640. BCDAdd ( CompToBCD ( c ), BCD, z );
  2641. end;
  2642. {$endif}
  2643. operator + ( const BCD : tBCD;
  2644. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2645. begin
  2646. BCDAdd ( BCD, StrToBCD ( s ), z );
  2647. end;
  2648. operator + ( const s : FmtBCDStringtype;
  2649. const BCD : tBCD ) z : tBCD; Inline;
  2650. begin
  2651. BCDAdd ( StrToBCD ( s ), BCD, z );
  2652. end;
  2653. operator - ( const BCD1,
  2654. BCD2 : tBCD ) z : tBCD; Inline;
  2655. begin
  2656. BCDSubtract ( BCD1, BCD2, z );
  2657. end;
  2658. operator - ( const BCD : tBCD;
  2659. const i : myInttype ) z : tBCD; Inline;
  2660. begin
  2661. BCDSubtract ( BCD, i, z );
  2662. end;
  2663. operator - ( const i : myInttype;
  2664. const BCD : tBCD ) z : tBCD; Inline;
  2665. begin
  2666. BCDSubtract ( BCD, i, z );
  2667. BCDNegate ( z );
  2668. end;
  2669. operator - ( const BCD : tBCD;
  2670. const r : myRealtype ) z : tBCD; Inline;
  2671. begin
  2672. BCDSubtract ( BCD, DoubleToBCD ( r ), z );
  2673. end;
  2674. operator - ( const r : myRealtype;
  2675. const BCD : tBCD ) z : tBCD; Inline;
  2676. begin
  2677. BCDSubtract ( DoubleToBCD ( r ), BCD, z );
  2678. end;
  2679. operator - ( const BCD : tBCD;
  2680. const c : currency ) z : tBCD; Inline;
  2681. begin
  2682. BCDSubtract ( BCD, CurrToBCD ( c ), z );
  2683. end;
  2684. operator - ( const c : currency;
  2685. const BCD : tBCD ) z : tBCD; Inline;
  2686. begin
  2687. BCDSubtract ( CurrToBCD ( c ), BCD, z );
  2688. end;
  2689. {$ifdef comproutines}
  2690. operator - ( const BCD : tBCD;
  2691. const c : Comp ) z : tBCD; Inline;
  2692. begin
  2693. BCDSubtract ( BCD, CompToBCD ( c ), z );
  2694. end;
  2695. operator - ( const c : Comp;
  2696. const BCD : tBCD ) z : tBCD; Inline;
  2697. begin
  2698. BCDSubtract ( CompToBCD ( c ), BCD, z );
  2699. end;
  2700. {$endif}
  2701. operator - ( const BCD : tBCD;
  2702. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2703. begin
  2704. BCDSubtract ( BCD, StrToBCD ( s ), z );
  2705. end;
  2706. operator - ( const s : FmtBCDStringtype;
  2707. const BCD : tBCD ) z : tBCD; Inline;
  2708. begin
  2709. BCDSubtract ( StrToBCD ( s ), BCD, z );
  2710. end;
  2711. operator * ( const BCD1,
  2712. BCD2 : tBCD ) z : tBCD; Inline;
  2713. begin
  2714. BCDMultiply ( BCD1, BCD2, z );
  2715. end;
  2716. operator * ( const BCD : tBCD;
  2717. const i : myInttype ) z : tBCD; Inline;
  2718. begin
  2719. BCDMultiply ( BCD, i, z );
  2720. end;
  2721. operator * ( const i : myInttype;
  2722. const BCD : tBCD ) z : tBCD; Inline;
  2723. begin
  2724. BCDMultiply ( BCD, i, z );
  2725. end;
  2726. operator * ( const BCD : tBCD;
  2727. const r : myRealtype ) z : tBCD; Inline;
  2728. begin
  2729. BCDMultiply ( BCD, DoubleToBCD ( r ), z );
  2730. end;
  2731. operator * ( const r : myRealtype;
  2732. const BCD : tBCD ) z : tBCD; Inline;
  2733. begin
  2734. BCDMultiply ( DoubleToBCD ( r ), BCD, z );
  2735. end;
  2736. operator * ( const BCD : tBCD;
  2737. const c : currency ) z : tBCD; Inline;
  2738. begin
  2739. BCDMultiply ( BCD, CurrToBCD ( c ), z );
  2740. end;
  2741. operator * ( const c : currency;
  2742. const BCD : tBCD ) z : tBCD; Inline;
  2743. begin
  2744. BCDMultiply ( CurrToBCD ( c ), BCD, z );
  2745. end;
  2746. {$ifdef comproutines}
  2747. operator * ( const BCD : tBCD;
  2748. const c : Comp ) z : tBCD; Inline;
  2749. begin
  2750. BCDMultiply ( BCD, CompToBCD ( c ), z );
  2751. end;
  2752. operator * ( const c : Comp;
  2753. const BCD : tBCD ) z : tBCD; Inline;
  2754. begin
  2755. BCDMultiply ( CompToBCD ( c ), BCD, z );
  2756. end;
  2757. {$endif}
  2758. operator * ( const BCD : tBCD;
  2759. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2760. begin
  2761. BCDMultiply ( BCD, StrToBCD ( s ), z );
  2762. end;
  2763. operator * ( const s : FmtBCDStringtype;
  2764. const BCD : tBCD ) z : tBCD; Inline;
  2765. begin
  2766. BCDMultiply ( StrToBCD ( s ), BCD, z );
  2767. end;
  2768. operator / ( const BCD1,
  2769. BCD2 : tBCD ) z : tBCD; Inline;
  2770. begin
  2771. BCDDivide ( BCD1, BCD2, z );
  2772. end;
  2773. operator / ( const BCD : tBCD;
  2774. const i : myInttype ) z : tBCD; Inline;
  2775. begin
  2776. BCDDivide ( BCD, i, z );
  2777. end;
  2778. operator / ( const i : myInttype;
  2779. const BCD : tBCD ) z : tBCD; Inline;
  2780. begin
  2781. BCDDivide ( IntegerToBCD ( i ), BCD, z );
  2782. end;
  2783. operator / ( const BCD : tBCD;
  2784. const r : myRealtype ) z : tBCD; Inline;
  2785. begin
  2786. BCDDivide ( BCD, DoubleToBCD ( r ), z );
  2787. end;
  2788. operator / ( const r : myRealtype;
  2789. const BCD : tBCD ) z : tBCD; Inline;
  2790. begin
  2791. BCDDivide ( DoubleToBCD ( r ), BCD, z );
  2792. end;
  2793. operator / ( const BCD : tBCD;
  2794. const c : currency ) z : tBCD; Inline;
  2795. begin
  2796. BCDDivide ( BCD, CurrToBCD ( c ), z );
  2797. end;
  2798. operator / ( const c : currency;
  2799. const BCD : tBCD ) z : tBCD; Inline;
  2800. begin
  2801. BCDDivide ( CurrToBCD ( c ), BCD, z );
  2802. end;
  2803. {$ifdef comproutines}
  2804. operator / ( const BCD : tBCD;
  2805. const c : Comp ) z : tBCD; Inline;
  2806. begin
  2807. BCDDivide ( BCD, CompToBCD ( c ), z );
  2808. end;
  2809. operator / ( const c : Comp;
  2810. const BCD : tBCD ) z : tBCD; Inline;
  2811. begin
  2812. BCDDivide ( CompToBCD ( c ), BCD, z );
  2813. end;
  2814. {$endif}
  2815. operator / ( const BCD : tBCD;
  2816. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2817. begin
  2818. BCDDivide ( BCD, StrToBCD ( s ), z );
  2819. end;
  2820. operator / ( const s : FmtBCDStringtype;
  2821. const BCD : tBCD ) z : tBCD; Inline;
  2822. begin
  2823. BCDDivide ( StrToBCD ( s ), BCD, z );
  2824. end;
  2825. operator := ( const i : Byte ) z : tBCD; Inline;
  2826. begin
  2827. z := IntegerToBCD ( myInttype ( i ) );
  2828. end;
  2829. operator := ( const BCD : tBCD ) z : Byte; Inline;
  2830. begin
  2831. z := BCDToInteger ( BCD );
  2832. end;
  2833. operator := ( const i : Word ) z : tBCD; Inline;
  2834. begin
  2835. z := IntegerToBCD ( myInttype ( i ) );
  2836. end;
  2837. operator := ( const BCD : tBCD ) z : Word; Inline;
  2838. begin
  2839. z := BCDToInteger ( BCD );
  2840. end;
  2841. operator := ( const i : longword ) z : tBCD; Inline;
  2842. begin
  2843. z := IntegerToBCD ( myInttype ( i ) );
  2844. end;
  2845. operator := ( const BCD : tBCD ) z : longword; Inline;
  2846. begin
  2847. z := BCDToInteger ( BCD );
  2848. end;
  2849. {$if declared ( qword ) }
  2850. operator := ( const i : qword ) z : tBCD; Inline;
  2851. begin
  2852. z := IntegerToBCD ( myInttype ( i ) );
  2853. end;
  2854. operator := ( const BCD : tBCD ) z : qword; Inline;
  2855. begin
  2856. z := BCDToInteger ( BCD );
  2857. end;
  2858. {$endif}
  2859. operator := ( const i : ShortInt ) z : tBCD; Inline;
  2860. begin
  2861. z := IntegerToBCD ( myInttype ( i ) );
  2862. end;
  2863. operator := ( const BCD : tBCD ) z : ShortInt; Inline;
  2864. begin
  2865. z := BCDToInteger ( BCD );
  2866. end;
  2867. operator := ( const i : smallint ) z : tBCD; Inline;
  2868. begin
  2869. z := IntegerToBCD ( myInttype ( i ) );
  2870. end;
  2871. operator := ( const BCD : tBCD ) z : smallint; Inline;
  2872. begin
  2873. z := BCDToInteger ( BCD );
  2874. end;
  2875. operator := ( const i : LongInt ) z : tBCD; Inline;
  2876. begin
  2877. z := IntegerToBCD ( myInttype ( i ) );
  2878. end;
  2879. operator := ( const BCD : tBCD ) z : LongInt; Inline;
  2880. begin
  2881. z := BCDToInteger ( BCD );
  2882. end;
  2883. {$if declared ( int64 ) }
  2884. operator := ( const i : int64 ) z : tBCD; Inline;
  2885. begin
  2886. z := IntegerToBCD ( myInttype ( i ) );
  2887. end;
  2888. operator := ( const BCD : tBCD ) z : int64; Inline;
  2889. begin
  2890. z := BCDToInteger ( BCD );
  2891. end;
  2892. {$endif}
  2893. operator := ( const r : Single ) z : tBCD; Inline;
  2894. begin
  2895. z := DoubleToBCD ( myRealtype ( r ) );
  2896. end;
  2897. operator := ( const BCD : tBCD ) z : Single; Inline;
  2898. begin
  2899. z := BCDToDouble ( BCD );
  2900. end;
  2901. operator := ( const r : Double ) z : tBCD; Inline;
  2902. begin
  2903. z := DoubleToBCD ( myRealtype ( r ) );
  2904. end;
  2905. operator := ( const BCD : tBCD ) z : Double; Inline;
  2906. begin
  2907. z := BCDToDouble ( BCD );
  2908. end;
  2909. {$if sizeof ( extended ) <> sizeof ( double )}
  2910. operator := ( const r : Extended ) z : tBCD; Inline;
  2911. begin
  2912. z := DoubleToBCD ( {myRealtype (} r {)} );
  2913. end;
  2914. operator := ( const BCD : tBCD ) z : Extended; Inline;
  2915. begin
  2916. z := BCDToDouble ( BCD );
  2917. end;
  2918. {$endif}
  2919. operator := ( const c : currency ) z : tBCD; Inline;
  2920. begin
  2921. CurrToBCD ( c, z );
  2922. end;
  2923. operator := ( const BCD : tBCD ) z : currency; Inline;
  2924. begin
  2925. BCDToCurr ( BCD, z );
  2926. end;
  2927. {$ifdef comproutines}
  2928. {$undef makedirect}
  2929. {$ifdef makedirect}
  2930. operator := ( const c : Comp ) z : tBCD; Inline;
  2931. var
  2932. cc : int64 absolute c;
  2933. begin
  2934. z := IntegerToBCD ( cc );
  2935. end;
  2936. { $define version1} { only one of these may be defined! }
  2937. { $define version2} { version 1 produces a compiler error (with INLINE only!)}
  2938. {$define version3} { I wasn't able to reduce the problem, sorry }
  2939. {$ifdef version1}
  2940. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2941. var
  2942. zz : Comp absolute z;
  2943. begin
  2944. zz := BCDToInteger ( BCD );
  2945. end;
  2946. {$endif}
  2947. {$ifdef version2}
  2948. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2949. var
  2950. zz : int64;
  2951. zzz : Comp absolute zz;
  2952. begin
  2953. zz := BCDToInteger ( BCD );
  2954. z := zzz;
  2955. end;
  2956. {$endif}
  2957. {$ifdef version3}
  2958. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2959. var
  2960. zz : record
  2961. case Boolean of
  2962. False: ( i : int64 );
  2963. True: ( c : Comp );
  2964. end;
  2965. begin
  2966. zz.i := BCDToInteger ( BCD );
  2967. z := zz.c;
  2968. end;
  2969. {$endif}
  2970. {$else}
  2971. operator := ( const c : Comp ) z : tBCD; Inline;
  2972. begin
  2973. z := CompToBCD ( c );
  2974. end;
  2975. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2976. begin
  2977. z := BCDToComp ( BCD );
  2978. end;
  2979. {$endif}
  2980. {$endif}
  2981. operator := ( const s : string ) z : tBCD; Inline;
  2982. begin
  2983. z := StrToBCD ( s );
  2984. end;
  2985. operator := ( const BCD : tBCD ) z : string; Inline;
  2986. begin
  2987. z := BCDToStr ( BCD );
  2988. end;
  2989. operator := ( const s : AnsiString ) z : tBCD; Inline;
  2990. begin
  2991. z := StrToBCD ( s );
  2992. end;
  2993. operator := ( const BCD : tBCD ) z : AnsiString; Inline;
  2994. begin
  2995. z := BCDToStr ( BCD );
  2996. end;
  2997. {$endif}
  2998. constructor TFMTBcdVarData.create;
  2999. begin
  3000. inherited create;
  3001. FBcd:=NullBCD;
  3002. end;
  3003. constructor TFMTBcdVarData.create(const BCD : tBCD);
  3004. begin
  3005. inherited create;
  3006. FBcd:=BCD;
  3007. end;
  3008. function TFMTBcdFactory.GetInstance(const v : TVarData): tObject;
  3009. begin
  3010. result:=tObject(v.VPointer);
  3011. end;
  3012. procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
  3013. begin
  3014. case Operation of
  3015. opAdd:
  3016. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD+TFMTBcdVarData(Right.VPointer).BCD;
  3017. opSubtract:
  3018. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD-TFMTBcdVarData(Right.VPointer).BCD;
  3019. opMultiply:
  3020. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD*TFMTBcdVarData(Right.VPointer).BCD;
  3021. opDivide:
  3022. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD/TFMTBcdVarData(Right.VPointer).BCD;
  3023. else
  3024. RaiseInvalidOp;
  3025. end;
  3026. end;
  3027. {$if declared ( myMinIntBCD ) }
  3028. (*
  3029. {$if sizeof ( integer ) = 2 }
  3030. {$ifdef BCDgr4 }
  3031. const
  3032. myMinIntBCDValue : packed array [ 1..3 ] of Char = #$32#$76#$80;
  3033. {$endif}
  3034. {$else}
  3035. {$if sizeof ( integer ) = 4 }
  3036. *)
  3037. {$ifdef BCDgr9 }
  3038. const
  3039. myMinIntBCDValue : packed array [ 1..10 ] of Char = #$21#$47#$48#$36#$48;
  3040. {$endif}
  3041. (*
  3042. {$else}
  3043. {$if sizeof ( integer ) = 8 }
  3044. {$ifdef BCDgr18 }
  3045. const
  3046. myMinIntBCDValue : packed array [ 1..19 ] of Char = #$92#$23#$37#$20#$36#$85#$47#$75#$80#$80;
  3047. {$endif}
  3048. {$else}
  3049. {$fatal You have an interesting integer type! Sorry, not supported}
  3050. {$endif}
  3051. {$endif}
  3052. {$endif}
  3053. *)
  3054. {$endif}
  3055. initialization
  3056. FillChar ( null_, SizeOf ( null_ ), #0 );
  3057. FillChar ( NullBCD_, SizeOf ( NullBCD_ ), #0 );
  3058. FillChar ( OneBCD_, SizeOf ( OneBCD_ ), #0 );
  3059. OneBCD_.Precision := 1;
  3060. OneBCD_.Fraction[low ( OneBCD_.Fraction )] := $10;
  3061. {$if declared ( myMinIntBCD ) }
  3062. FillChar ( myMinIntBCD, SizeOf ( myMinIntBCD ), #0 );
  3063. {$ifndef bigger_BCD}
  3064. myMinIntBCD.SignSpecialPlaces := NegBit;
  3065. {$else}
  3066. myMinIntBCD.Negativ := True;
  3067. {$endif}
  3068. {$if sizeof ( integer ) = 2 }
  3069. {$ifdef BCDgr4 }
  3070. myMinIntBCD.Precision := 5;
  3071. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3072. {$endif}
  3073. {$else}
  3074. {$if sizeof ( integer ) = 4 }
  3075. {$ifdef BCDgr9 }
  3076. myMinIntBCD.Precision := 10;
  3077. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3078. {$endif}
  3079. {$else}
  3080. {$if sizeof ( integer ) = 8 }
  3081. {$ifdef BCDgr18 }
  3082. myMinIntBCD.Precision := 19;
  3083. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3084. {$endif}
  3085. {$else}
  3086. {$fatal You have an interesting integer type! Sorry, not supported}
  3087. {$endif}
  3088. {$endif}
  3089. {$endif}
  3090. {$endif}
  3091. FMTBcdFactory:=TFMTBcdFactory.create;
  3092. finalization
  3093. FreeAndNil(FMTBcdFactory)
  3094. end.