fmtbcd.pp 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  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. {$hint BCD Digits > 4}
  49. {$endif}
  50. {$ifdef BCDgr9}
  51. {$hint BCD Digits > 9}
  52. {$endif}
  53. {$ifdef BCDgr18}
  54. {$hint BCD Digits > 18}
  55. {$endif}
  56. {$ifdef BCDgr64}
  57. {$hint BCD Digits > 64}
  58. {$endif}
  59. {$ifdef BCDgr180}
  60. {$hint 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. {$hint ansi}
  139. {$else}
  140. {$hint -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 {$ifopt r+}, sysconst {$endif};
  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); override;
  607. procedure Clear(var V: TVarData); override;
  608. procedure Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean); override;
  609. end;
  610. TFMTBcdVarData = CLASS(TPersistent)
  611. PRIVATE
  612. FBcd : tBCD;
  613. PUBLIC
  614. constructor create;
  615. constructor create(const BCD : tBCD);
  616. PROPERTY BCD : tBCD Read FBcd Write FBcd;
  617. end;
  618. var
  619. NullBCD_ : tBCD;
  620. OneBCD_ : tBCD;
  621. function __get_null : tBCD; Inline;
  622. begin
  623. __get_null := NullBCD_;
  624. end;
  625. function __get_one : tBCD; Inline;
  626. begin
  627. __get_one := OneBCD_;
  628. end;
  629. type
  630. range_digits = 1..maxfmtbcdfractionsize;
  631. range_digits0 = 0..maxfmtbcdfractionsize;
  632. range_fracdigits = 0..pred ( MaxFmtBCDFractionSize );
  633. {$ifopt r+}
  634. procedure RangeError;
  635. begin
  636. raise ERangeError.Create(SRangeError);
  637. end;
  638. {$endif}
  639. {$ifndef debug_version}
  640. helper_declarations
  641. {$endif}
  642. var
  643. null_ : record
  644. case Boolean of
  645. False: ( bh : tBCD_helper );
  646. True: ( bhb : tBCD_helper_big );
  647. end;
  648. FMTBcdFactory : TFMTBcdFactory = NIL;
  649. {$ifndef bigger_BCD}
  650. const
  651. NegBit = 1 SHL 7;
  652. SpecialBit = 1 SHL 6;
  653. PlacesMask = $ff XOR ( NegBit OR SpecialBit );
  654. {$endif}
  655. {$define _select := {$define _when := if {$define _when := end else if } }
  656. {$define _then := then begin }
  657. {$define _whenother := end else begin }
  658. {$define _endselect := end } }
  659. {$ifdef debug_version}
  660. procedure dumpBCD ( const v : tBCD );
  661. var
  662. i,
  663. j : Integer;
  664. const
  665. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  666. begin
  667. {$ifndef bigger_BCD}
  668. Write ( 'Prec:', v.Precision, ' ',
  669. 'Neg:', ft[( v.SignSpecialPlaces AND NegBit ) <> 0], ' ',
  670. 'Special:', ft[( v.SignSpecialPlaces AND SpecialBit ) <> 0], ' ',
  671. 'Places:', v.SignSpecialPlaces AND PlacesMask, ' ' );
  672. {$else}
  673. Write ( 'Prec:', v.Precision, ' ',
  674. 'Neg:', ft[v.Negativ], ' ',
  675. 'Places:', v.Places, ' ' );
  676. {$endif}
  677. j := 0;
  678. for i := 1 TO v.Precision do
  679. if Odd ( i )
  680. then Write ( ( v.Fraction[j] AND $f0 ) SHR 4 )
  681. else begin
  682. Write ( v.Fraction[j] AND $0f );
  683. Inc ( j );
  684. end;
  685. WriteLn;
  686. end;
  687. procedure dumpbh ( const v : tBCD_helper );
  688. var
  689. i : Integer;
  690. const
  691. ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
  692. begin
  693. Write ( 'Prec:', v.Prec, ' ',
  694. 'Neg:', ft[v.Neg], ' ',
  695. 'Places:', v.Plac, ' ',
  696. 'FDig:', v.FDig, ' ',
  697. 'LDig:', v.LDig, ' ',
  698. 'Digits:', v.LDig - v.FDig + 1, ' ' );
  699. for i := v.FDig TO v.LDig do
  700. Write ( v.Singles[i] );
  701. WriteLn;
  702. end;
  703. {$endif}
  704. {$if sizeof ( integer ) = 2 }
  705. {$ifdef BCDgr4 }
  706. var
  707. myMinIntBCD : tBCD;
  708. {$endif}
  709. {$else}
  710. {$if sizeof ( integer ) = 4 }
  711. {$ifdef BCDgr9 }
  712. var
  713. myMinIntBCD : tBCD;
  714. {$endif}
  715. {$else}
  716. {$if sizeof ( integer ) = 8 }
  717. {$ifdef BCDgr18 }
  718. var
  719. myMinIntBCD : tBCD;
  720. {$endif}
  721. {$else}
  722. {$fatal You have an interesting integer type! Sorry, not supported}
  723. {$endif}
  724. {$endif}
  725. {$endif}
  726. procedure not_implemented;
  727. begin
  728. RAISE eBCDNotImplementedException.create ( 'not implemented' );
  729. end;
  730. procedure unpack_BCD ( const BCD : tBCD;
  731. var bh : tBCD_helper );
  732. var
  733. i : {$ifopt r+} __lo_bh + 1 ..__hi_bh {$else} Integer {$endif};
  734. j : {$ifopt r+} -1..__high_fraction {$else} Integer {$endif};
  735. vv : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  736. begin
  737. bh := null_.bh;
  738. WITH bh,
  739. BCD do
  740. begin
  741. Prec := Precision;
  742. if Prec > 0
  743. then begin
  744. {$ifndef bigger_BCD}
  745. Plac := SignSpecialPlaces AND PlacesMask;
  746. Neg := ( SignSpecialPlaces AND NegBit ) <> 0;
  747. {$else}
  748. Plac := Places;
  749. Neg := Negativ;
  750. {$endif}
  751. LDig := Plac;
  752. FDig := LDig - Prec + 1;
  753. j := -1;
  754. i := FDig;
  755. while i <= LDig do
  756. begin
  757. Inc ( j );
  758. vv := Fraction[j];
  759. Singles[i] := ( vv {AND $f0} ) SHR 4;
  760. if i < LDig
  761. then Singles[i+1] := vv AND $0f;
  762. Inc ( i, 2 );
  763. end;
  764. end;
  765. end;
  766. end;
  767. function pack_BCD ( var bh : tBCD_helper;
  768. var BCD : tBCD ) : Boolean;
  769. { return TRUE if successful (BCD valid) }
  770. var
  771. pre : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  772. fra : {$ifopt r+} -1 * ( __hi_bh - __lo_bh + 1 )..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
  773. tm : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 - Pred ( MaxFmtBCDFractionSize ) {$else} Integer {$endif};
  774. i : {$ifopt r+} low ( bh.FDig ) - 1..high ( bh.LDig ) {$else} Integer {$endif};
  775. rp : {$ifopt r+} low ( BCD.Fraction )..high ( BCD.Fraction ) + 1 {$else} Integer {$endif};
  776. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  777. v : {$ifopt r+} 0..10 {$else} Integer {$endif};
  778. lnz : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  779. doround,
  780. lnzf : Boolean;
  781. begin
  782. pack_BCD := False;
  783. BCD := NullBCD;
  784. WITH BCD,
  785. bh do
  786. begin
  787. lnzf := FDig < 0;
  788. while lnzf do
  789. if Singles[FDig] = 0
  790. then begin
  791. Inc ( FDig );
  792. if FDig = 0
  793. then lnzf := False;
  794. end
  795. else lnzf := False;
  796. pre := LDig - FDig + 1;
  797. fra := Plac;
  798. doround := False;
  799. if fra >= MaxFmtBCDFractionSize
  800. then begin
  801. doround := True;
  802. tm := fra - Pred ( MaxFmtBCDFractionSize );
  803. { dec ( pre, tm ); Dec/Inc error? }
  804. pre := pre - tm;
  805. { Dec ( fra, tm ); Dec/Inc error? }
  806. fra := fra - tm;
  807. { Dec ( LDig, tm ); Dec/Inc error? }
  808. LDig := LDig - tm;
  809. end;
  810. if pre > MaxFmtBCDFractionSize
  811. then begin
  812. doround := True;
  813. tm := pre - MaxFmtBCDFractionSize;
  814. { Dec ( pre, tm ); Dec/Inc error? }
  815. pre := pre - tm;
  816. { Dec ( fra, tm ); Dec/Inc error? }
  817. fra := fra - tm;
  818. { Dec ( LDig, tm ); Dec/Inc error? }
  819. LDig := LDig - tm;
  820. end;
  821. if fra < 0
  822. then EXIT;
  823. if doround
  824. then begin
  825. v := Singles[fra + 1];
  826. if v > 4
  827. then begin
  828. ue := 1;
  829. i := LDig;
  830. while ( i >= FDig ) AND ( ue <> 0 ) do
  831. begin
  832. v := Singles[i] + ue;
  833. ue := v DIV 10;
  834. Singles[i] := v MOD 10;
  835. Dec ( i );
  836. end;
  837. if ue <> 0
  838. then begin
  839. Dec ( FDig );
  840. Singles[FDig] := ue;
  841. Dec ( LDig );
  842. Dec ( fra );
  843. if fra < 0
  844. then EXIT;
  845. end;
  846. end;
  847. end;
  848. lnzf := False;
  849. i := LDig;
  850. while ( i >= FDig ) AND ( NOT lnzf ) do
  851. begin
  852. if Singles[i] <> 0
  853. then begin
  854. lnz := i;
  855. lnzf := True;
  856. end;
  857. Dec ( i );
  858. end;
  859. if lnzf
  860. then begin
  861. tm := LDig - lnz;
  862. if tm <> 0
  863. then begin
  864. { Dec ( pre, tm ); Dec/Inc error? }
  865. pre := pre - tm;
  866. { Dec ( fra, tm ); Dec/Inc error? }
  867. fra := fra - tm;
  868. { Dec ( LDig, tm ); Dec/Inc error? }
  869. LDig := LDig - tm;
  870. if fra < 0
  871. then begin
  872. { Dec ( pre, fra ); Dec/Inc error? }
  873. pre := pre - fra;
  874. { Dec ( LDig, fra ); Dec/Inc error? }
  875. LDig := LDig - fra;
  876. fra := 0;
  877. end;
  878. end;
  879. end
  880. else begin
  881. LDig := FDig;
  882. fra := 0;
  883. pre := 0;
  884. Neg := False;
  885. end;
  886. if pre <> 0
  887. then begin
  888. Precision := pre;
  889. rp := 0;
  890. i := FDig;
  891. while i <= LDig do
  892. begin
  893. if i < LDig
  894. then Fraction[rp] := ( Singles[i] SHL 4 ) OR Singles[i + 1]
  895. else Fraction[rp] := Singles[i] SHL 4;
  896. Inc ( rp );
  897. Inc ( i, 2 );
  898. end;
  899. {$ifndef bigger_BCD}
  900. if Neg
  901. then SignSpecialPlaces := NegBit;
  902. SignSpecialPlaces := SignSpecialPlaces OR fra;
  903. {$else}
  904. Negativ := Neg;
  905. Places := fra;
  906. {$endif}
  907. end;
  908. end;
  909. pack_BCD := True;
  910. end;
  911. procedure SetDecimals ( var dp,
  912. dc : Char );
  913. begin
  914. case DecimalPoint of
  915. DecimalPoint_is_Point: begin
  916. dp := '.';
  917. dc := ',';
  918. end;
  919. DecimalPoint_is_Comma: begin
  920. dp := ',';
  921. dc := '.';
  922. end;
  923. { find out language-specific ? }
  924. DecimalPoint_is_System: begin
  925. dp := DecimalSeparator;
  926. dc := ThousandSeparator;
  927. end;
  928. end;
  929. end;
  930. function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
  931. begin
  932. BCDPrecision := BCD.Precision;
  933. end;
  934. function BCDScale ( const BCD : tBCD ) : Word; Inline;
  935. begin
  936. {$ifndef bigger_BCD}
  937. BCDScale := BCD.SignSpecialPlaces AND PlacesMask;
  938. {$else}
  939. BCDScale := BCD.Places;
  940. {$endif}
  941. end;
  942. function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
  943. begin
  944. {$ifndef bigger_BCD}
  945. IsBCDNegative := ( BCD.SignSpecialPlaces AND NegBit ) <> 0;
  946. {$else}
  947. IsBCDNegative := BCD.Negativ;
  948. {$endif}
  949. end;
  950. { BCD Arithmetic}
  951. procedure BCDNegate ( var BCD : tBCD ); Inline;
  952. begin
  953. { with-statement geht nicht !!
  954. with bcd do
  955. if precision <> 0
  956. then signspecialplaces := signspecialplaces xor negbit;
  957. }
  958. if BCD.Precision <> 0
  959. then
  960. {$ifndef bigger_BCD}
  961. BCD.SignSpecialPlaces := BCD.SignSpecialPlaces XOR NegBit;
  962. {$else}
  963. BCD.Negativ := NOT BCD.Negativ;
  964. {$endif}
  965. end;
  966. { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
  967. function BCDCompare ( const BCD1,
  968. BCD2 : tBCD ) : Integer;
  969. var
  970. pl1 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  971. pl2 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  972. pr1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  973. pr2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  974. pr : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  975. idig1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  976. idig2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
  977. i : {$ifopt r+} __low_Fraction..__high_Fraction + 1 {$else} Integer {$endif};
  978. f1 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  979. f2 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
  980. res : {$ifopt r+} -1..1 {$else} Integer {$endif};
  981. neg1,
  982. neg2 : Boolean;
  983. begin
  984. {$ifndef bigger_BCD}
  985. neg1 := ( BCD1.SignSpecialPlaces AND NegBit ) <> 0;
  986. neg2 := ( BCD2.SignSpecialPlaces AND NegBit ) <> 0;
  987. {$else}
  988. neg1 := BCD1.Negativ;
  989. neg2 := BCD2.Negativ;
  990. {$endif}
  991. _SELECT
  992. _WHEN neg1 AND ( NOT neg2 )
  993. _THEN result := -1;
  994. _WHEN ( NOT neg1 ) AND neg2
  995. _THEN result := +1;
  996. _WHENOTHER
  997. pr1 := BCD1.Precision;
  998. pr2 := BCD2.Precision;
  999. {$ifndef bigger_BCD}
  1000. pl1 := BCD1.SignSpecialPlaces AND PlacesMask;
  1001. pl2 := BCD2.SignSpecialPlaces AND PlacesMask;
  1002. {$else}
  1003. pl1 := BCD1.Places;
  1004. pl2 := BCD2.Places;
  1005. {$endif}
  1006. idig1 := pr1 - pl1;
  1007. idig2 := pr2 - pl2;
  1008. if idig1 <> idig2
  1009. then begin
  1010. if ( idig1 > idig2 ) = neg1
  1011. then result := -1
  1012. else result := +1;
  1013. end
  1014. else begin
  1015. if pr1 < pr2
  1016. then pr := pr1
  1017. else pr := pr2;
  1018. res := 0;
  1019. i := __low_Fraction;
  1020. while ( res = 0 ) AND ( i < ( __low_Fraction + ( pr DIV 2 ) ) ) do
  1021. begin
  1022. {
  1023. if BCD1.Fraction[i] < BCD2.Fraction[i]
  1024. then res := -1
  1025. else
  1026. if BCD1.Fraction[i] > BCD2.Fraction[i]
  1027. then res := +1;
  1028. }
  1029. _SELECT
  1030. _WHEN BCD1.Fraction[i] < BCD2.Fraction[i]
  1031. _THEN res := -1
  1032. _WHEN BCD1.Fraction[i] > BCD2.Fraction[i]
  1033. _THEN res := +1;
  1034. _WHENOTHER
  1035. _endSELECT;
  1036. Inc ( i );
  1037. end;
  1038. if res = 0
  1039. then begin
  1040. if Odd ( pr )
  1041. then begin
  1042. f1 := BCD1.Fraction[i] AND $f0;
  1043. f2 := BCD2.Fraction[i] AND $f0;
  1044. {
  1045. if f1 < f2
  1046. then res := -1
  1047. else
  1048. if f1 > f2
  1049. then res := +1;
  1050. }
  1051. _SELECT
  1052. _WHEN f1 < f2
  1053. _THEN res := -1
  1054. _WHEN f1 > f2
  1055. _THEN res := +1;
  1056. _endSELECT;
  1057. end;
  1058. end;
  1059. if neg1
  1060. then result := 0 - res
  1061. else result := res;
  1062. end;
  1063. _endSELECT
  1064. end;
  1065. { Convert string/Double/Integer to BCD struct }
  1066. function TryStrToBCD ( const aValue : FmtBCDStringtype;
  1067. var BCD : tBCD ) : Boolean;
  1068. { shall this return TRUE when error and FALSE when o.k. or the other way round ? }
  1069. var
  1070. {$ifndef use_ansistring}
  1071. lav : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1072. i : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1073. {$else}
  1074. lav : {$ifopt r+} longword {$else} longword {$endif};
  1075. i : {$ifopt r+} longword {$else} longword {$endif};
  1076. {$endif}
  1077. ch : Char;
  1078. dp,
  1079. dc : Char;
  1080. type
  1081. ife = ( inint, infrac, inexp );
  1082. {$define max_exp_scanned := 9999 }
  1083. var
  1084. inife : ife;
  1085. lvars : record
  1086. fp,
  1087. lp : ARRAY [ ife ]
  1088. {$ifndef use_ansistring}
  1089. of {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1090. pfnb : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1091. ps : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1092. pse : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1093. errp : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
  1094. {$else}
  1095. of {$ifopt r+} longword {$else} longword {$endif};
  1096. pfnb : {$ifopt r+} longword {$else} longword {$endif};
  1097. ps : {$ifopt r+} longword {$else} longword {$endif};
  1098. pse : {$ifopt r+} longword {$else} longword {$endif};
  1099. errp : {$ifopt r+} longword {$else} longword {$endif};
  1100. {$endif}
  1101. exp : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1102. p : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
  1103. bh : tBCD_helper;
  1104. nbf : Boolean;
  1105. end;
  1106. begin
  1107. result := False;
  1108. FillChar ( lvars, SizeOf ( lvars ), #0 );
  1109. BCD := NullBCD;
  1110. lav := Length ( aValue );
  1111. if lav <> 0
  1112. then
  1113. WITH lvars,
  1114. bh do
  1115. begin
  1116. SetDecimals ( dp, dc );
  1117. while ( pfnb < lav ) AND ( NOT nbf ) do
  1118. begin
  1119. Inc ( pfnb );
  1120. nbf := aValue[pfnb] <> ' ';
  1121. end;
  1122. if nbf
  1123. then begin
  1124. if aValue[pfnb] IN [ '+', '-' ]
  1125. then begin
  1126. ps := pfnb;
  1127. Inc ( pfnb );
  1128. end;
  1129. inife := low ( inife );
  1130. for i := pfnb TO lav do
  1131. begin
  1132. ch := aValue[i];
  1133. case ch of
  1134. '0'..'9': begin
  1135. case inife of
  1136. inint,
  1137. inexp: if fp[inife] = 0
  1138. then begin
  1139. if ch <> '0'
  1140. then begin
  1141. fp[inife] := i;
  1142. lp[inife] := i;
  1143. end;
  1144. end
  1145. else lp[inife] := i;
  1146. infrac: begin
  1147. if fp[infrac] = 0
  1148. then fp[infrac] := i;
  1149. if ch <> '0'
  1150. then lp[infrac] := i;
  1151. end;
  1152. end;
  1153. end;
  1154. ',',
  1155. '.': if ch = dp
  1156. then begin
  1157. if inife <> inint
  1158. then result := True
  1159. else inife := infrac;
  1160. end;
  1161. 'e',
  1162. 'E': if inife = inexp
  1163. then result := True
  1164. else inife := inexp;
  1165. '+',
  1166. '-': if ( inife = inexp ) AND ( fp[inexp] = 0 )
  1167. then pse := i
  1168. else result := True;
  1169. else begin
  1170. result := True;
  1171. errp := i;
  1172. end;
  1173. end;
  1174. end;
  1175. if result
  1176. then begin
  1177. result := False;
  1178. for i := errp TO lav do
  1179. if aValue[i] <> ' '
  1180. then result := True;
  1181. end;
  1182. if result
  1183. then EXIT;
  1184. if ps <> 0
  1185. then Neg := aValue[ps] = '-';
  1186. if lp[infrac] = 0
  1187. then fp[infrac] := 0;
  1188. if fp[inexp] <> 0
  1189. then begin
  1190. exp := 0;
  1191. for i := fp[inexp] TO lp[inexp] do
  1192. if NOT result
  1193. then
  1194. if aValue[i] <> dc
  1195. then begin
  1196. exp := exp * 10 + ( Ord ( aValue[i] ) - Ord ( '0' ) );
  1197. if exp > 999
  1198. then result := True;
  1199. end;
  1200. if result
  1201. then EXIT;
  1202. if pse <> 0
  1203. then
  1204. if aValue[pse] = '-'
  1205. then exp := -exp;
  1206. end;
  1207. p := -exp;
  1208. if fp[infrac] <> 0
  1209. then begin
  1210. for i := fp[infrac] TO lp[infrac] do
  1211. if aValue[i] <> dc
  1212. then begin
  1213. if p < ( MaxFmtBCDFractionSize + 2 )
  1214. then begin
  1215. Inc ( p );
  1216. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1217. end;
  1218. end;
  1219. end;
  1220. LDig := p;
  1221. p := 1 - exp;
  1222. if fp[inint] <> 0
  1223. then
  1224. for i := lp[inint] DOWNTO fp[inint] do
  1225. if aValue[i] <> dc
  1226. then begin
  1227. if p > - ( MaxFmtBCDFractionSize + 2 )
  1228. then begin
  1229. Dec ( p );
  1230. Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
  1231. end
  1232. else result := True;
  1233. end;
  1234. if result
  1235. then EXIT;
  1236. FDig := p;
  1237. if LDig < 0
  1238. then LDig := 0;
  1239. Plac := LDig;
  1240. result := NOT pack_BCD ( bh, BCD );
  1241. end;
  1242. end;
  1243. end;
  1244. function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
  1245. var
  1246. BCD : tBCD;
  1247. begin
  1248. if TryStrToBCD ( aValue, BCD )
  1249. then begin
  1250. RAISE eBCDOverflowException.create ( 'in StrToBCD' );
  1251. end
  1252. else StrToBCD := BCD;
  1253. end;
  1254. procedure DoubleToBCD ( const aValue : myRealtype;
  1255. var BCD : tBCD );
  1256. var
  1257. s : string [ 30 ];
  1258. dp : tDecimalPoint;
  1259. begin
  1260. Str ( aValue : 25, s );
  1261. dp := DecimalPoint;
  1262. DecimalPoint := DecimalPoint_is_Point;
  1263. BCD := StrToBCD ( s );
  1264. DecimalPoint := dp;
  1265. end;
  1266. function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
  1267. begin
  1268. DoubleToBCD ( aValue, result );
  1269. end;
  1270. function IntegerToBCD ( const aValue : myInttype ) : tBCD;
  1271. var
  1272. bh : tBCD_helper;
  1273. v : {$ifopt r+} 0..high ( myInttype ) {$else} Integer {$endif};
  1274. p : {$ifopt r+} low ( bh.Singles ) - 1..0 {$else} Integer {$endif};
  1275. exitloop : Boolean;
  1276. begin
  1277. _SELECT
  1278. _WHEN aValue = 0
  1279. _THEN result := NullBCD;
  1280. _WHEN aValue = 1
  1281. _THEN result := OneBCD;
  1282. _WHEN aValue = low ( myInttype )
  1283. _THEN
  1284. {$if declared ( myMinIntBCD ) }
  1285. result := myMinIntBCD;
  1286. {$else}
  1287. RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
  1288. {$endif}
  1289. _WHENOTHER
  1290. bh := null_.bh;
  1291. WITH bh do
  1292. begin
  1293. Neg := aValue < 0;
  1294. if Neg
  1295. then v := -aValue
  1296. else v := +aValue;
  1297. LDig := 0;
  1298. p := 0;
  1299. REPEAT
  1300. Singles[p] := v MOD 10;
  1301. v := v DIV 10;
  1302. exitloop := v = 0;
  1303. Dec ( p );
  1304. if p < low ( Singles )
  1305. then begin
  1306. exitloop := 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. {$warnings off}
  1317. function VarToBCD ( const aValue : Variant ) : tBCD;
  1318. begin
  1319. not_implemented;
  1320. end;
  1321. {$warnings on}
  1322. function CurrToBCD ( const Curr : currency;
  1323. var BCD : tBCD;
  1324. Precision : Integer = 32;
  1325. Decimals : Integer = 4 ) : Boolean;
  1326. {
  1327. this works under the assumption that a currency is an int64,
  1328. except for scale of 10000
  1329. }
  1330. var
  1331. i : int64 absolute Curr;
  1332. begin
  1333. BCD := IntegerToBCD ( i );
  1334. {$ifndef bigger_BCD}
  1335. BCD.SignSpecialPlaces := 4 OR ( BCD.SignSpecialPlaces AND NegBit );
  1336. {$else}
  1337. BCD.Places := 4;
  1338. {$endif}
  1339. if Decimals <> 4 then
  1340. Result := NormalizeBCD ( BCD, BCD, Precision, Decimals )
  1341. else
  1342. CurrToBCD := True;
  1343. end;
  1344. {$ifdef comproutines}
  1345. function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
  1346. var
  1347. cc : int64 absolute Curr;
  1348. begin
  1349. result := IntegerToBCD ( cc );
  1350. end;
  1351. function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
  1352. var
  1353. zz : record
  1354. case Boolean of
  1355. False: ( i : int64 );
  1356. True: ( c : Comp );
  1357. end;
  1358. begin
  1359. zz.i := BCDToInteger ( BCD );
  1360. BCDToComp := zz.c;
  1361. end;
  1362. {$endif}
  1363. { Convert BCD struct to string/Double/Integer }
  1364. function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
  1365. var
  1366. bh : tBCD_helper;
  1367. l : {$ifopt r+} 0..maxfmtbcdfractionsize + 1 + 1 {$else} Integer {$endif};
  1368. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1369. pp : {$ifopt r+} low ( bh.FDig ) - 1..1 {$else} Integer {$endif};
  1370. begin
  1371. {$ifdef use_ansistring}
  1372. result := '';
  1373. {$endif}
  1374. unpack_BCD ( BCD, bh );
  1375. WITH bh do
  1376. begin
  1377. l := 0;
  1378. if Neg
  1379. then begin
  1380. {$ifndef use_ansistring}
  1381. Inc ( l );
  1382. result[1] := '-';
  1383. {$else}
  1384. result := result + '-';
  1385. {$endif}
  1386. end;
  1387. if Prec = Plac
  1388. then begin
  1389. {$ifndef use_ansistring}
  1390. Inc ( l );
  1391. result[1] := '0';
  1392. {$else}
  1393. result := result + '0';
  1394. {$endif}
  1395. end;
  1396. if Prec > 0
  1397. then begin
  1398. pp := low ( bh.FDig ) - 1;
  1399. if Plac > 0
  1400. then pp := 1;
  1401. for i := FDig TO LDig do
  1402. begin
  1403. if i = pp
  1404. then begin
  1405. {$ifndef use_ansistring}
  1406. Inc ( l );
  1407. result[l] := '.';
  1408. {$else}
  1409. result := result + '.';
  1410. {$endif}
  1411. end;
  1412. {$ifndef use_ansistring}
  1413. Inc ( l );
  1414. result[l] := Chr ( Singles[i] + Ord ( '0' ) );
  1415. {$else}
  1416. result := result + Chr ( Singles[i] + Ord ( '0' ) );
  1417. {$endif}
  1418. end;
  1419. end;
  1420. end;
  1421. {$ifndef use_ansistring}
  1422. result[0] := Chr ( l );
  1423. {$endif}
  1424. end;
  1425. function BCDToDouble ( const BCD : tBCD ) : myRealtype;
  1426. var
  1427. bh : tBCD_helper;
  1428. i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
  1429. r,
  1430. e : myRealtype;
  1431. begin
  1432. unpack_BCD ( BCD, bh );
  1433. WITH bh do
  1434. begin
  1435. r := 0;
  1436. e := 1;
  1437. for i := 0 DOWNTO FDig do
  1438. begin
  1439. r := r + Singles[i] * e;
  1440. e := e * 10;
  1441. end;
  1442. e := 1;
  1443. for i := 1 TO LDig do
  1444. begin
  1445. e := e / 10;
  1446. r := r + Singles[i] * e;
  1447. end;
  1448. if Neg
  1449. then BCDToDouble := -r
  1450. else BCDToDouble := +r;
  1451. end;
  1452. end;
  1453. function BCDToInteger ( const BCD : tBCD;
  1454. Truncate : Boolean = False ) : myInttype;
  1455. var
  1456. bh : tBCD_helper;
  1457. res : myInttype;
  1458. i : {$ifopt r+} low ( bh.FDig )..0 {$else} Integer {$endif};
  1459. {
  1460. unclear: behaviour if overflow: abort? return 0? return something?
  1461. so: checks are missing yet
  1462. }
  1463. begin
  1464. unpack_BCD ( BCD, bh );
  1465. res := 0;
  1466. WITH bh do
  1467. begin
  1468. for i := FDig TO 0 do
  1469. res := res * 10 - Singles[i];
  1470. if NOT Truncate
  1471. then
  1472. if Plac > 0
  1473. then
  1474. if Singles[1] > 4
  1475. then Dec ( res );
  1476. if Neg
  1477. then BCDToInteger := +res
  1478. else BCDToInteger := -res;
  1479. end;
  1480. end;
  1481. { From DB.pas }
  1482. function BCDToCurr ( const BCD : tBCD;
  1483. var Curr : currency ) : Boolean;
  1484. var
  1485. bh : tBCD_helper;
  1486. res : int64;
  1487. c : currency absolute res;
  1488. i : {$ifopt r+} low ( bh.FDig )..4 {$else} Integer {$endif};
  1489. {
  1490. unclear: behaviour if overflow: abort? return 0? return something?
  1491. }
  1492. begin
  1493. BCDToCurr := True;
  1494. unpack_BCD ( BCD, bh );
  1495. res := 0;
  1496. WITH bh do
  1497. begin
  1498. for i := FDig TO 4 do
  1499. res := res * 10 + Singles[i];
  1500. if Plac > 4
  1501. then
  1502. if Singles[5] > 4
  1503. then Inc ( res );
  1504. if Neg
  1505. then Curr := -c
  1506. else Curr := +c;
  1507. end;
  1508. end;
  1509. procedure BCDAdd ( const BCDin1,
  1510. BCDin2 : tBCD;
  1511. var BCDout : tBCD );
  1512. var
  1513. bhr,
  1514. bh1,
  1515. bh2 : tBCD_helper;
  1516. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1517. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1518. v : {$ifopt r+} 0..9 + 9 + 1 {$else} Integer {$endif};
  1519. BCD : tBCD;
  1520. negate : Boolean;
  1521. begin
  1522. negate := IsBCDNegative ( BCDin1 );
  1523. if negate <> IsBCDNegative ( BCDin2 )
  1524. then begin
  1525. if negate
  1526. then begin
  1527. BCD := BCDin1;
  1528. BCDNegate ( BCD );
  1529. BCDSubtract ( BCDin2, BCD, BCDout );
  1530. EXIT;
  1531. end;
  1532. BCD := BCDin2;
  1533. BCDNegate ( BCD );
  1534. BCDSubtract ( BCDin1, BCD, BCDout );
  1535. EXIT;
  1536. end;
  1537. bhr := null_.bh;
  1538. WITH bhr do
  1539. begin
  1540. unpack_BCD ( BCDin1, bh1 );
  1541. unpack_BCD ( BCDin2, bh2 );
  1542. if bh1.FDig < bh2.FDig
  1543. then FDig := bh1.FDig
  1544. else FDig := bh2.FDig;
  1545. if bh1.LDig > bh2.LDig
  1546. then LDig := bh1.LDig
  1547. else LDig := bh2.LDig;
  1548. Plac := LDig;
  1549. ue := 0;
  1550. for i := LDig DOWNTO FDig do
  1551. begin
  1552. v := bh1.Singles[i] + bh2.Singles[i] + ue;
  1553. ue := v DIV 10;
  1554. Singles[i] := v MOD 10;
  1555. end;
  1556. if ue <> 0
  1557. then begin
  1558. Dec ( FDig );
  1559. Singles[FDig] := ue;
  1560. end;
  1561. Neg := negate;
  1562. end;
  1563. if NOT pack_BCD ( bhr, BCDout )
  1564. then begin
  1565. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1566. end;
  1567. end;
  1568. procedure BCDSubtract ( const BCDin1,
  1569. BCDin2 : tBCD;
  1570. var BCDout : tBCD );
  1571. var
  1572. bhr,
  1573. bh1,
  1574. bh2 : tBCD_helper;
  1575. cmp : {$ifopt r+} -1..1 {$else} Integer {$endif};
  1576. ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
  1577. i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1578. v : {$ifopt r+} 0 - 9 - 1..9 - 0 - 0 {$else} Integer {$endif};
  1579. negate : Boolean;
  1580. BCD : tBCD;
  1581. begin
  1582. negate := IsBCDNegative ( BCDin1 );
  1583. if negate <> IsBCDNegative ( BCDin2 )
  1584. then begin
  1585. if negate
  1586. then begin
  1587. BCD := BCDin1;
  1588. BCDNegate ( BCD );
  1589. BCDAdd ( BCDin2, BCD, BCDout );
  1590. BCDNegate ( BCDout );
  1591. EXIT;
  1592. end;
  1593. BCD := BCDin2;
  1594. BCDNegate ( BCD );
  1595. BCDAdd ( BCDin1, BCD, BCDout );
  1596. EXIT;
  1597. end;
  1598. cmp := BCDCompare ( BCDin1, BCDin2 );
  1599. if cmp = 0
  1600. then begin
  1601. BCDout := NullBCD;
  1602. EXIT;
  1603. end;
  1604. bhr := null_.bh; { n n }
  1605. WITH bhr do { > < > < }
  1606. begin { }
  1607. if ( cmp > 0 ) = negate { +123 +12 -12 -123 }
  1608. then begin { - +12 - +123 - -123 - -12 }
  1609. unpack_BCD ( BCDin1, bh2 ); { x x }
  1610. unpack_BCD ( BCDin2, bh1 ); { s s s s }
  1611. negate := NOT negate; { nn n nn n }
  1612. end
  1613. else begin
  1614. unpack_BCD ( BCDin1, bh1 );
  1615. unpack_BCD ( BCDin2, bh2 );
  1616. end;
  1617. if bh1.FDig < bh2.FDig
  1618. then FDig := bh1.FDig
  1619. else FDig := bh2.FDig;
  1620. if bh1.LDig > bh2.LDig
  1621. then LDig := bh1.LDig
  1622. else LDig := bh2.LDig;
  1623. Plac := LDig;
  1624. ue := 0;
  1625. for i := LDig DOWNTO FDig do
  1626. begin
  1627. v := Integer ( bh1.Singles[i] ) - bh2.Singles[i] - ue;
  1628. ue := 0;
  1629. if v < 0
  1630. then begin
  1631. ue := 1;
  1632. Inc ( v, 10 );
  1633. end;
  1634. Singles[i] := v;
  1635. end;
  1636. Neg := negate;
  1637. if NOT pack_BCD ( bhr, BCDout )
  1638. then begin
  1639. {should never occur!}
  1640. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  1641. end;
  1642. end;
  1643. end;
  1644. { Returns True if successful, False if Int Digits needed to be truncated }
  1645. function NormalizeBCD ( const InBCD : tBCD;
  1646. var OutBCD : tBCD;
  1647. const Prec,
  1648. Scale : Word ) : Boolean;
  1649. var
  1650. bh : tBCD_helper;
  1651. tm : {$ifopt r+} 1..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
  1652. begin
  1653. NormalizeBCD := True;
  1654. {$ifopt r+}
  1655. if ( Prec < 0 ) OR ( Prec > MaxFmtBCDFractionSize ) then RangeError;
  1656. if ( Scale < 0 ) OR ( Prec >= MaxFmtBCDFractionSize ) then RangeError;
  1657. {$endif}
  1658. if BCDScale ( InBCD ) > Scale
  1659. then begin
  1660. unpack_BCD ( InBCD, bh );
  1661. WITH bh do
  1662. begin
  1663. tm := Plac - Scale;
  1664. Plac := Scale;
  1665. { dec ( prec, tm ); Dec/Inc error? }
  1666. Prec := Prec - tm;
  1667. { dec ( ldig, tm ); Dec/Inc error? }
  1668. LDig := LDig - tm;
  1669. NormalizeBCD := False;
  1670. end;
  1671. if NOT pack_BCD ( bh, OutBCD )
  1672. then begin
  1673. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  1674. end;
  1675. end;
  1676. end;
  1677. procedure BCDMultiply ( const BCDin1,
  1678. BCDin2 : tBCD;
  1679. var BCDout : tBCD );
  1680. var
  1681. bh1,
  1682. bh2,
  1683. bhr : tBCD_helper;
  1684. bhrr : tBCD_helper_big;
  1685. i1 : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
  1686. i2 : {$ifopt r+} low ( bh2.FDig )..high ( bh2.LDig ) {$else} Integer {$endif};
  1687. i3 : {$ifopt r+} low ( bhrr.FDig )..high ( bhrr.LDig ) {$else} Integer {$endif};
  1688. v : {$ifopt r+} low ( bhrr.Singles[0] )..high ( bhrr.Singles[0] ) {$else} Integer {$endif};
  1689. ue : {$ifopt r+} low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  1690. begin
  1691. unpack_BCD ( BCDin1, bh1 );
  1692. unpack_BCD ( BCDin2, bh2 );
  1693. if ( bh1.Prec = 0 ) OR ( bh2.Prec = 0 )
  1694. then begin
  1695. BCDout := NullBCD;
  1696. EXIT;
  1697. end;
  1698. bhr := null_.bh;
  1699. bhrr := null_.bhb;
  1700. WITH bhrr do
  1701. begin
  1702. Neg := bh1.Neg XOR bh2.Neg;
  1703. {
  1704. writeln ( __lo_bhb, ' ', __hi_bhb, ' ', bh1.fdig, ' ', bh2.fdig, ' ', low ( fdig ), ' ', low ( ldig ) );
  1705. }
  1706. FDig := bh1.FDig + bh2.FDig;
  1707. LDig := bh1.LDig + bh2.LDig;
  1708. for i1 := bh1.FDig TO bh1.LDig do
  1709. for i2 := bh2.FDig TO bh2.LDig do
  1710. begin
  1711. Inc ( Singles[i1 + i2],
  1712. bh1.Singles[i1]
  1713. * bh2.Singles[i2] );
  1714. {
  1715. write ( Singles[i1 + i2], ' ', bh1.Singles[i1], ' ', bh2.Singles[i2], ' : ' );
  1716. writeln ( Singles[i1 + i2] + bh1.Singles[i1] + bh2.Singles[i2] );
  1717. }
  1718. {
  1719. Singles[i1 + i2] := Singles[i1 + i2]
  1720. + bh1.Singles[i1]
  1721. * bh2.Singles[i2];
  1722. }
  1723. end;
  1724. {
  1725. for i3 := fdig to ldig do
  1726. write ( ' ', singles[i3] );
  1727. writeln;
  1728. }
  1729. if FDig < low ( bhr.Singles )
  1730. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1731. ue := 0;
  1732. for i3 := LDig DOWNTO FDig do
  1733. begin
  1734. v := Singles[i3] + ue;
  1735. ue := v DIV 10;
  1736. v := v MOD 10;
  1737. bhr.Singles[i3] := v;
  1738. end;
  1739. while ue <> 0 do
  1740. begin
  1741. Dec ( FDig );
  1742. if FDig < low ( bhr.Singles )
  1743. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1744. bhr.Singles[FDig] := ue MOD 10;
  1745. ue := ue DIV 10;
  1746. end;
  1747. bhr.Plac := LDig;
  1748. bhr.FDig := FDig;
  1749. if LDig > high ( bhr.Singles )
  1750. then bhr.LDig := high ( bhr.Singles )
  1751. else bhr.LDig := LDig;
  1752. end;
  1753. if NOT pack_BCD ( bhr, BCDout )
  1754. then begin
  1755. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  1756. end;
  1757. end;
  1758. procedure BCDMultiply ( const BCDIn : tBCD;
  1759. const DoubleIn : myRealtype;
  1760. var BCDout : tBCD ); Inline;
  1761. begin
  1762. BCDMultiply ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  1763. end;
  1764. procedure BCDMultiply ( const BCDIn : tBCD;
  1765. const StringIn : FmtBCDStringtype;
  1766. var BCDout : tBCD ); Inline;
  1767. begin
  1768. BCDMultiply ( BCDIn, StrToBCD ( StringIn ), BCDout );
  1769. end;
  1770. procedure BCDMultiply ( const StringIn1,
  1771. StringIn2 : FmtBCDStringtype;
  1772. var BCDout : tBCD ); Inline;
  1773. begin
  1774. BCDMultiply ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  1775. end;
  1776. procedure BCDDivide ( const Dividend,
  1777. Divisor : tBCD;
  1778. var BCDout : tBCD );
  1779. var
  1780. bh1 : ARRAY [ Boolean ] of tBCD_helper;
  1781. bh2,
  1782. bh : tBCD_helper;
  1783. p : {$ifopt r+} low ( bh.FDig ) - high ( bh.FDig )..high ( bh.FDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1784. v1 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1785. v2 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
  1786. lFDig : {$ifopt r+} low ( bh.FDig )..high ( bh.FDig ) {$else} Integer {$endif};
  1787. d1 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1788. d2 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1789. d : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
  1790. lLdig : {$ifopt r+} low ( lFDig ) + low ( d )..high ( lFDig ) + high ( d ) {$else} Integer {$endif};
  1791. tm : {$ifopt r+} low ( lLdig ) - high ( bh2.Singles )..high ( lLdig ) - high ( bh2.Singles ) {$else} Integer {$endif};
  1792. i2 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1793. i3 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1794. ie : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1795. i4 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
  1796. nFDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1797. nLDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
  1798. dd : {$ifopt r+} 0..9 {$else} Integer {$endif};
  1799. Add : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1800. ue : {$ifopt r+} 0..99 {$else} Integer {$endif};
  1801. 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};
  1802. v4 : {$ifopt r+} low ( bh.Singles[0] ) + low ( add )..high ( bh.Singles[0] ) + high ( add ) {$else} Integer {$endif};
  1803. FlipFlop,
  1804. nz,
  1805. sf,
  1806. sh,
  1807. fdset : Boolean;
  1808. {
  1809. bh1p : ARRAY [ Boolean ] of ^ tBCD_helper;
  1810. }
  1811. begin
  1812. { test:
  1813. bh1p[false] := @ bh1[false];
  1814. bh1p[true] := @ bh1[true];
  1815. v := bh1[false].singles[0];
  1816. v := bh1[true].singles[0];
  1817. v := bh1p[false]^.singles[0];
  1818. v := bh1p[true]^.singles[0];
  1819. v := bh1[nz].singles[0];
  1820. v := bh1p[nz]^.singles[0];
  1821. }
  1822. unpack_BCD ( Divisor, bh2 );
  1823. unpack_BCD ( Dividend, bh1[False] );
  1824. p := bh1[False].FDig - bh2.FDig;
  1825. _SELECT
  1826. _WHEN bh2.Prec = 0
  1827. _THEN RAISE eBCDException.create ( 'Division by zero' );
  1828. _WHEN bh1[False].Prec = 0
  1829. _THEN BCDout := NullBCD;
  1830. _WHEN p < low ( bh2.Singles )
  1831. _THEN RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  1832. _WHENOTHER
  1833. bh := null_.bh;
  1834. bh.Neg := bh1[False].Neg XOR bh2.Neg;
  1835. if p <= high ( bh.Singles )
  1836. then begin
  1837. bh1[True] := null_.bh;
  1838. FlipFlop := False;
  1839. fdset := p > 0;
  1840. if fdset
  1841. then bh.FDig := 0;
  1842. add := 0;
  1843. nz := True;
  1844. while nz do
  1845. WITH bh1[FlipFlop] do
  1846. begin
  1847. {
  1848. WriteLn('#####');
  1849. dumpbh ( bh1[flipflop] );
  1850. dumpbh ( bh2 );
  1851. dumpbh ( bh );
  1852. }
  1853. if ( Singles[FDig] + bh2.Singles[bh2.FDig] ) = 0
  1854. then begin
  1855. if ( FDig >= LDig )
  1856. OR ( bh2.FDig >= bh2.LDig )
  1857. then nz := False
  1858. else begin
  1859. Inc ( FDig );
  1860. Inc ( bh2.FDig );
  1861. end;
  1862. end
  1863. else begin
  1864. v1 := Singles[FDig];
  1865. v2 := bh2.Singles[bh2.FDig];
  1866. sh := v1 < v2;
  1867. if ( v1 = v2 )
  1868. then begin
  1869. nz := False;
  1870. i3 := Succ ( FDig );
  1871. ie := LDig;
  1872. while ( i3 <= ie ) AND ( NOT nz ) AND ( NOT sh ) do
  1873. begin
  1874. v1 := Singles[i3];
  1875. v2 := bh2.Singles[i3 - p];
  1876. if v1 <> v2
  1877. then begin
  1878. nz := True;
  1879. if v1 < v2
  1880. then sh := True;
  1881. end;
  1882. Inc ( i3 );
  1883. end;
  1884. end;
  1885. if NOT nz
  1886. then Add := 1
  1887. else begin
  1888. if sh
  1889. then begin
  1890. Inc ( p );
  1891. {
  1892. if p > 3 then halt;
  1893. }
  1894. if p > high ( bh.Singles )
  1895. then nz := False
  1896. else Dec ( bh2.FDig );
  1897. end
  1898. else begin
  1899. lFDig := FDig;
  1900. d1 := LDig - FDig;
  1901. d2 := bh2.LDig - bh2.FDig;
  1902. if d1 > d2
  1903. then d := d1
  1904. else d := d2;
  1905. lLdig := lFDig + d;
  1906. if lLdig > high ( bh2.Singles )
  1907. then begin
  1908. tm := ( lLdig ) - high ( bh2.Singles );
  1909. d := d - tm;
  1910. lLdig := lLdig - tm;
  1911. {runden?}
  1912. end;
  1913. sf := True;
  1914. Add := 0;
  1915. nFDig := 0;
  1916. nLDig := 0;
  1917. ue := 0;
  1918. dd := Singles[lFDig] DIV ( bh2.Singles[lFDig - p] + 1 );
  1919. {
  1920. dd := 1;
  1921. }
  1922. if dd < 1
  1923. then dd := 1;
  1924. {
  1925. writeln ( 'p=', p, ' dd=', dd, ' lFdig=', lfdig, ' lldig=', lldig );
  1926. }
  1927. for i2 := lLdig DOWNTO lFDig do
  1928. begin
  1929. v3 := Singles[i2] - bh2.Singles[i2 - p] * dd - ue;
  1930. ue := 0;
  1931. while v3 < 0 do
  1932. begin
  1933. Inc ( ue );;
  1934. v3 := v3 + 10;
  1935. end;
  1936. {
  1937. if v3 <> 0
  1938. then begin
  1939. }
  1940. bh1[NOT FlipFlop].Singles[i2] := v3;
  1941. {
  1942. nFDig := i2;
  1943. if sf
  1944. then begin
  1945. nLDig := i2;
  1946. sf := False;
  1947. end;
  1948. end;
  1949. }
  1950. end;
  1951. sf := False;
  1952. nfdig := lfdig;
  1953. nldig := lldig;
  1954. Inc ( Add, dd );
  1955. if NOT fdset
  1956. then begin
  1957. bh.FDig := p;
  1958. fdset := True;
  1959. end;
  1960. if bh.LDig < p
  1961. then begin
  1962. bh.LDig := p;
  1963. if ( bh.LDig - bh.FDig ) > Succ ( MaxFmtBCDFractionSize )
  1964. then nz := False;
  1965. end;
  1966. if sf
  1967. then nz := False
  1968. else begin
  1969. FillChar ( bh1[FlipFlop], SizeOf ( bh1[FlipFlop] ), #0 );
  1970. FlipFlop := NOT FlipFlop;
  1971. WITH bh1[FlipFlop] do
  1972. begin
  1973. FDig := nFDig;
  1974. LDig := nLDig;
  1975. end;
  1976. end;
  1977. end;
  1978. end;
  1979. if Add <> 0
  1980. then begin
  1981. i4 := p;
  1982. while ( Add <> 0 ) AND ( i4 >= bh.FDig ) do
  1983. begin
  1984. {
  1985. writeln ( '> ', i4, ' ', bh.Singles[i4], ' ', Add );
  1986. }
  1987. v4 := bh.Singles[i4] + Add;
  1988. Add := v4 DIV 10;
  1989. bh.Singles[i4] := v4 MOD 10;
  1990. Dec ( i4 );
  1991. end;
  1992. if Add <> 0
  1993. then begin
  1994. Dec ( bh.FDig );
  1995. bh.Singles[bh.FDig] := Add;
  1996. Add := 0;
  1997. end;
  1998. end;
  1999. end;
  2000. end;
  2001. end;
  2002. WITH bh do
  2003. begin
  2004. if LDig < 0
  2005. then LDig := 0;
  2006. if LDig > 0
  2007. then Plac := LDig
  2008. else Plac := 0;
  2009. end;
  2010. if NOT pack_BCD ( bh, BCDout )
  2011. then begin
  2012. RAISE eBCDOverflowException.create ( 'in BCDDivide' );
  2013. end;
  2014. _endSELECT
  2015. end;
  2016. procedure BCDDivide ( const Dividend,
  2017. Divisor : FmtBCDStringtype;
  2018. var BCDout : tBCD ); Inline;
  2019. begin
  2020. BCDDivide ( StrToBCD ( Dividend ), StrToBCD ( Divisor ), BCDout );
  2021. end;
  2022. procedure BCDDivide ( const Dividend : tBCD;
  2023. const Divisor : myRealtype;
  2024. var BCDout : tBCD ); Inline;
  2025. begin
  2026. BCDDivide ( Dividend, DoubleToBCD ( Divisor ), BCDout );
  2027. end;
  2028. procedure BCDDivide ( const Dividend : tBCD;
  2029. const Divisor : FmtBCDStringtype;
  2030. var BCDout : tBCD ); Inline;
  2031. begin
  2032. BCDDivide ( Dividend, StrToBCD ( Divisor ), BCDout );
  2033. end;
  2034. { TBCD variant creation utils }
  2035. procedure VarFmtBCDCreate ( var aDest : Variant;
  2036. const aBCD : tBCD );
  2037. begin
  2038. VarClear(aDest);
  2039. TVarData(aDest).Vtype:=FMTBcdFactory.Vartype;
  2040. TVarData(aDest).VPointer:=TFMTBcdVarData.create(aBCD);
  2041. end;
  2042. function VarFmtBCDCreate : Variant;
  2043. begin
  2044. VarFmtBCDCreate ( result, NullBCD );
  2045. end;
  2046. function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
  2047. Precision,
  2048. Scale : Word ) : Variant;
  2049. begin
  2050. VarFmtBCDCreate ( result, StrToBCD ( aValue ) );
  2051. end;
  2052. function VarFmtBCDCreate ( const aValue : myRealtype;
  2053. Precision : Word = 18;
  2054. Scale : Word = 4 ) : Variant;
  2055. begin
  2056. VarFmtBCDCreate ( result, DoubleToBCD ( aValue ) );
  2057. end;
  2058. function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
  2059. begin
  2060. VarFmtBCDCreate ( result, aBCD );
  2061. end;
  2062. function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
  2063. begin
  2064. result:=false;
  2065. not_implemented;
  2066. end;
  2067. function VarFmtBCD : TVartype;
  2068. begin
  2069. result:=0;
  2070. not_implemented;
  2071. end;
  2072. { Formatting BCD as string }
  2073. function BCDToStrF ( const BCD : tBCD;
  2074. Format : TFloatFormat;
  2075. const Precision,
  2076. Digits : Integer ) : FmtBCDStringtype;
  2077. begin
  2078. not_implemented;
  2079. result:='';
  2080. end;
  2081. function FormatBCD ( const Format : string;
  2082. BCD : tBCD ) : FmtBCDStringtype;
  2083. begin
  2084. not_implemented;
  2085. result:='';
  2086. end;
  2087. {$ifdef additional_routines}
  2088. function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
  2089. begin
  2090. CurrToBCD ( Curr, result );
  2091. end;
  2092. procedure BCDAdd ( const BCDIn : tBCD;
  2093. const IntIn : myInttype;
  2094. var BCDout : tBCD );
  2095. var
  2096. BCD : tBCD;
  2097. bhr : tBCD_helper;
  2098. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2099. ue : {$ifopt r+} 0..high ( IntIn ) - 9 {$else} Integer {$endif};
  2100. v : {$ifopt r+} 0..{high ( ue ) + 9} high ( IntIn ) {$else} Integer {$endif};
  2101. nz : Boolean;
  2102. begin
  2103. if IntIn = 0
  2104. then begin
  2105. BCDout := BCDIn;
  2106. EXIT;
  2107. end;
  2108. if IntIn = low ( myInttype )
  2109. then begin
  2110. {$if declared ( myMinIntBCD ) }
  2111. BCDAdd ( BCDIn, myMinIntBCD, BCDout );
  2112. EXIT;
  2113. {$else}
  2114. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2115. {$endif}
  2116. end;
  2117. if IsBCDNegative ( BCDIn )
  2118. then begin
  2119. BCD := BCDIn;
  2120. BCDNegate ( BCD );
  2121. if IntIn < 0
  2122. then BCDAdd ( BCD, -IntIn, BCDout )
  2123. else BCDSubtract ( BCD, IntIn, BCDout );
  2124. BCDNegate ( BCDout );
  2125. EXIT;
  2126. end;
  2127. if IntIn < 0
  2128. then begin
  2129. BCDSubtract ( BCDIn, -IntIn, BCDout );
  2130. EXIT;
  2131. end;
  2132. if IntIn > ( high ( IntIn ) - 9 )
  2133. then begin
  2134. BCDAdd ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2135. EXIT;
  2136. end;
  2137. unpack_BCD ( BCDIn, bhr );
  2138. p := 0;
  2139. nz := True;
  2140. ue := IntIn;
  2141. while nz do
  2142. begin
  2143. v := bhr.Singles[p] + ue;
  2144. bhr.Singles[p] := v MOD 10;
  2145. ue := v DIV 10;
  2146. if ue = 0
  2147. then nz := False
  2148. else Dec ( p );
  2149. end;
  2150. if p < bhr.FDig
  2151. then begin
  2152. bhr.FDig := p;
  2153. bhr.Prec := bhr.Prec + ( bhr.FDig - p );
  2154. end;
  2155. if NOT pack_BCD ( bhr, BCDout )
  2156. then begin
  2157. RAISE eBCDOverflowException.create ( 'in BCDAdd' );
  2158. end;
  2159. end;
  2160. procedure BCDSubtract ( const BCDIn : tBCD;
  2161. const IntIn : myInttype;
  2162. var BCDout : tBCD );
  2163. {}
  2164. var
  2165. BCD : tBCD;
  2166. bhr : tBCD_helper;
  2167. p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
  2168. ue : {$ifopt r+} 0..pred ( 100000000 ) {$else} Integer {$endif};
  2169. v : {$ifopt r+} -9..9 {$else} Integer {$endif};
  2170. direct : Boolean;
  2171. {}
  2172. begin
  2173. if IntIn = 0
  2174. then begin
  2175. BCDout := BCDIn;
  2176. EXIT;
  2177. end;
  2178. if IntIn = low ( myInttype )
  2179. then begin
  2180. {$if declared ( myMinIntBCD ) }
  2181. BCDSubtract ( BCDIn, myMinIntBCD, BCDout );
  2182. EXIT;
  2183. {$else}
  2184. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2185. {$endif}
  2186. end;
  2187. if IsBCDNegative ( BCDIn )
  2188. then begin
  2189. BCD := BCDIn;
  2190. BCDNegate ( BCD );
  2191. if IntIn < 0
  2192. then BCDSubtract ( BCD, -IntIn, BCDout )
  2193. else BCDAdd ( BCD, IntIn, BCDout );
  2194. BCDNegate ( BCDout );
  2195. EXIT;
  2196. end;
  2197. if IntIn < 0
  2198. then begin
  2199. BCDAdd ( BCDIn, -IntIn, BCDout );
  2200. EXIT;
  2201. end;
  2202. direct := False;
  2203. case BCDIn.Precision
  2204. -
  2205. {$ifndef bigger_BCD}
  2206. ( BCDIn.SignSpecialPlaces AND PlacesMask )
  2207. {$else}
  2208. BCDIn.Places
  2209. {$endif}
  2210. of
  2211. 2: direct := IntIn < 10;
  2212. 3: direct := IntIn < 100;
  2213. 4: direct := IntIn < 1000;
  2214. 5: direct := IntIn < 10000;
  2215. 6: direct := IntIn < 100000;
  2216. 7: direct := IntIn < 1000000;
  2217. 8: direct := IntIn < 10000000;
  2218. 9: direct := IntIn < 100000000;
  2219. end;
  2220. {
  2221. write(direct);dumpbcd(bcdin);write('[',intin,']');
  2222. }
  2223. if direct
  2224. then begin
  2225. unpack_BCD ( BCDIn, bhr );
  2226. WITH bhr do
  2227. begin
  2228. p := 0;
  2229. ue := IntIn;
  2230. while p >= FDig do
  2231. begin
  2232. v := Singles[p] - ue MOD 10;
  2233. ue := ue DIV 10;
  2234. if v < 0
  2235. then begin
  2236. v := v + 10;
  2237. ue := ue + 1;
  2238. end;
  2239. Singles[p] := v;
  2240. Dec ( p );
  2241. end;
  2242. end;
  2243. if NOT pack_BCD ( bhr, BCDout )
  2244. then begin
  2245. RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
  2246. end;
  2247. end
  2248. else
  2249. {}
  2250. BCDSubtract ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2251. end;
  2252. procedure BCDAdd ( const IntIn : myInttype;
  2253. const BCDIn : tBCD;
  2254. var BCDout : tBCD ); Inline;
  2255. begin
  2256. BCDAdd ( BCDIn, IntIn, BCDout );
  2257. end;
  2258. procedure BCDAdd ( const BCDIn : tBCD;
  2259. const DoubleIn : myRealtype;
  2260. var BCDout : tBCD ); Inline;
  2261. begin
  2262. BCDAdd ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2263. end;
  2264. procedure BCDAdd ( const DoubleIn : myRealtype;
  2265. const BCDIn : tBCD;
  2266. var BCDout : tBCD ); Inline;
  2267. begin
  2268. BCDAdd ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2269. end;
  2270. procedure BCDAdd ( const BCDIn : tBCD;
  2271. const Currin : currency;
  2272. var BCDout : tBCD ); Inline;
  2273. begin
  2274. BCDAdd ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2275. end;
  2276. procedure BCDAdd ( const Currin : currency;
  2277. const BCDIn : tBCD;
  2278. var BCDout : tBCD ); Inline;
  2279. begin
  2280. BCDAdd ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2281. end;
  2282. {$ifdef comproutines}
  2283. procedure BCDAdd ( const BCDIn : tBCD;
  2284. const Compin : Comp;
  2285. var BCDout : tBCD ); Inline;
  2286. begin
  2287. BCDAdd ( BCDIn, CompToBCD ( Compin ), BCDout );
  2288. end;
  2289. procedure BCDAdd ( const Compin : Comp;
  2290. const BCDIn : tBCD;
  2291. var BCDout : tBCD ); Inline;
  2292. begin
  2293. BCDAdd ( CompToBCD ( Compin ), BCDIn, BCDout );
  2294. end;
  2295. {$endif}
  2296. procedure BCDAdd ( const BCDIn : tBCD;
  2297. const StringIn : FmtBCDStringtype;
  2298. var BCDout : tBCD ); Inline;
  2299. begin
  2300. BCDAdd ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2301. end;
  2302. procedure BCDAdd ( const StringIn : FmtBCDStringtype;
  2303. const BCDIn : tBCD;
  2304. var BCDout : tBCD ); Inline;
  2305. begin
  2306. BCDAdd ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2307. end;
  2308. procedure BCDAdd ( const StringIn1,
  2309. StringIn2 : FmtBCDStringtype;
  2310. var BCDout : tBCD ); Inline;
  2311. begin
  2312. BCDAdd ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2313. end;
  2314. procedure BCDSubtract ( const IntIn : myInttype;
  2315. const BCDIn : tBCD;
  2316. var BCDout : tBCD ); Inline;
  2317. begin
  2318. BCDSubtract ( BCDIn, IntIn, BCDout );
  2319. BCDNegate ( BCDout );
  2320. end;
  2321. procedure BCDSubtract ( const BCDIn : tBCD;
  2322. const DoubleIn : myRealtype;
  2323. var BCDout : tBCD ); Inline;
  2324. begin
  2325. BCDSubtract ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
  2326. end;
  2327. procedure BCDSubtract ( const DoubleIn : myRealtype;
  2328. const BCDIn : tBCD;
  2329. var BCDout : tBCD ); Inline;
  2330. begin
  2331. BCDSubtract ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2332. end;
  2333. procedure BCDSubtract ( const BCDIn : tBCD;
  2334. const Currin : currency;
  2335. var BCDout : tBCD ); Inline;
  2336. begin
  2337. BCDSubtract ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2338. end;
  2339. procedure BCDSubtract ( const Currin : currency;
  2340. const BCDIn : tBCD;
  2341. var BCDout : tBCD ); Inline;
  2342. begin
  2343. BCDSubtract ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2344. end;
  2345. {$ifdef comproutines}
  2346. procedure BCDSubtract ( const BCDIn : tBCD;
  2347. const Compin : Comp;
  2348. var BCDout : tBCD ); Inline;
  2349. begin
  2350. BCDSubtract ( BCDIn, CompToBCD ( Compin ), BCDout );
  2351. end;
  2352. procedure BCDSubtract ( const Compin : Comp;
  2353. const BCDIn : tBCD;
  2354. var BCDout : tBCD ); Inline;
  2355. begin
  2356. BCDSubtract ( CompToBCD ( Compin ), BCDIn, BCDout );
  2357. end;
  2358. {$endif}
  2359. procedure BCDSubtract ( const BCDIn : tBCD;
  2360. const StringIn : FmtBCDStringtype;
  2361. var BCDout : tBCD ); Inline;
  2362. begin
  2363. BCDSubtract ( BCDIn, StrToBCD ( StringIn ), BCDout );
  2364. end;
  2365. procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
  2366. const BCDIn : tBCD;
  2367. var BCDout : tBCD ); Inline;
  2368. begin
  2369. BCDSubtract ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2370. end;
  2371. procedure BCDSubtract ( const StringIn1,
  2372. StringIn2 : FmtBCDStringtype;
  2373. var BCDout : tBCD ); Inline;
  2374. begin
  2375. BCDSubtract ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
  2376. end;
  2377. procedure BCDMultiply ( const BCDIn : tBCD;
  2378. const IntIn : myInttype;
  2379. var BCDout : tBCD );
  2380. var
  2381. bh : tBCD_helper;
  2382. bhr : tBCD_helper;
  2383. bhrr : tBCD_helper_big;
  2384. int : {$ifopt r+} 0..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
  2385. i1 : {$ifopt r+} low ( bh.Singles )..high ( bh.Singles ) {$else} Integer {$endif};
  2386. i3 : {$ifopt r+} low ( bhr.Singles )..high ( bhr.Singles ) {$else} Integer {$endif};
  2387. 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};
  2388. ue : {$ifopt r+} 1 * ( low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10 ) DIV 10
  2389. ..( high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 ) DIV 10 {$else} Integer {$endif};
  2390. begin
  2391. if IntIn = 0
  2392. then begin
  2393. BCDout := NullBCD;
  2394. EXIT;
  2395. end;
  2396. if IntIn = 1
  2397. then begin
  2398. BCDout := BCDIn;
  2399. EXIT;
  2400. end;
  2401. if IntIn = -1
  2402. then begin
  2403. BCDout := BCDIn;
  2404. BCDNegate ( BCDout );
  2405. EXIT;
  2406. end;
  2407. if IntIn = low ( myInttype )
  2408. then begin
  2409. {$if declared ( myMinIntBCD ) }
  2410. BCDMultiply ( BCDIn, myMinIntBCD, BCDout );
  2411. EXIT;
  2412. {$else}
  2413. RAISE eBCDOverflowException.create ( 'in BCDmultiply' );
  2414. {$endif}
  2415. end;
  2416. if Abs ( IntIn ) > low ( bhrr.Singles[0] ) DIV 10
  2417. then begin
  2418. BCDMultiply ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
  2419. EXIT;
  2420. end;
  2421. unpack_BCD ( BCDIn, bh );
  2422. if bh.Prec = 0
  2423. then begin
  2424. BCDout := NullBCD;
  2425. EXIT;
  2426. end;
  2427. bhr := null_.bh;
  2428. bhrr := null_.bhb;
  2429. int := Abs ( IntIn );
  2430. WITH bhrr do
  2431. begin
  2432. Neg := bh.Neg XOR ( IntIn < 0 );
  2433. FDig := bh.FDig;
  2434. LDig := bh.LDig;
  2435. for i1 := bh.FDig TO bh.LDig do
  2436. Singles[i1] := bh.Singles[i1] * int;
  2437. {
  2438. for i3 := fdig to ldig do
  2439. write ( ' ', singles[i3] );
  2440. writeln;
  2441. }
  2442. ue := 0;
  2443. for i3 := LDig DOWNTO FDig do
  2444. begin
  2445. v := Singles[i3] + ue;
  2446. ue := v DIV 10;
  2447. v := v MOD 10;
  2448. bhr.Singles[i3] := v;
  2449. end;
  2450. while ue <> 0 do
  2451. begin
  2452. Dec ( FDig );
  2453. if FDig < low ( bhr.Singles )
  2454. then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2455. bhr.Singles[FDig] := ue MOD 10;
  2456. ue := ue DIV 10;
  2457. end;
  2458. bhr.Plac := LDig;
  2459. bhr.FDig := FDig;
  2460. if LDig > high ( bhr.Singles )
  2461. then bhr.LDig := high ( bhr.Singles )
  2462. else bhr.LDig := LDig;
  2463. end;
  2464. if NOT pack_BCD ( bhr, BCDout )
  2465. then begin
  2466. RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
  2467. end;
  2468. end;
  2469. procedure BCDMultiply ( const IntIn : myInttype;
  2470. const BCDIn : tBCD;
  2471. var BCDout : tBCD ); Inline;
  2472. begin
  2473. BCDMultiply ( BCDIn, IntIn, BCDout );
  2474. end;
  2475. procedure BCDMultiply ( const DoubleIn : myRealtype;
  2476. const BCDIn : tBCD;
  2477. var BCDout : tBCD ); Inline;
  2478. begin
  2479. BCDMultiply ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
  2480. end;
  2481. procedure BCDMultiply ( const BCDIn : tBCD;
  2482. const Currin : currency;
  2483. var BCDout : tBCD ); Inline;
  2484. begin
  2485. BCDMultiply ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2486. end;
  2487. procedure BCDMultiply ( const Currin : currency;
  2488. const BCDIn : tBCD;
  2489. var BCDout : tBCD ); Inline;
  2490. begin
  2491. BCDMultiply ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2492. end;
  2493. {$ifdef comproutines}
  2494. procedure BCDMultiply ( const BCDIn : tBCD;
  2495. const Compin : Comp;
  2496. var BCDout : tBCD ); Inline;
  2497. begin
  2498. BCDMultiply ( BCDIn, CompToBCD ( Compin ), BCDout );
  2499. end;
  2500. procedure BCDMultiply ( const Compin : Comp;
  2501. const BCDIn : tBCD;
  2502. var BCDout : tBCD ); Inline;
  2503. begin
  2504. BCDMultiply ( CompToBCD ( Compin ), BCDIn, BCDout );
  2505. end;
  2506. {$endif}
  2507. procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
  2508. const BCDIn : tBCD;
  2509. var BCDout : tBCD ); Inline;
  2510. begin
  2511. BCDMultiply ( StrToBCD ( StringIn ), BCDIn, BCDout );
  2512. end;
  2513. procedure BCDDivide ( const Dividend : tBCD;
  2514. const Divisor : myInttype;
  2515. var BCDout : tBCD ); Inline;
  2516. begin
  2517. BCDDivide ( Dividend, IntegerToBCD ( Divisor ), BCDout );
  2518. end;
  2519. procedure BCDDivide ( const Dividend : myInttype;
  2520. const Divisor : tBCD;
  2521. var BCDout : tBCD ); Inline;
  2522. begin
  2523. BCDDivide ( IntegerToBCD ( Dividend ), Divisor, BCDout );
  2524. end;
  2525. procedure BCDDivide ( const Dividend : myRealtype;
  2526. const Divisor : tBCD;
  2527. var BCDout : tBCD ); Inline;
  2528. begin
  2529. BCDDivide ( DoubleToBCD ( Dividend ), Divisor, BCDout );
  2530. end;
  2531. procedure BCDDivide ( const BCDIn : tBCD;
  2532. const Currin : currency;
  2533. var BCDout : tBCD ); Inline;
  2534. begin
  2535. BCDDivide ( BCDIn, CurrToBCD ( Currin ), BCDout );
  2536. end;
  2537. procedure BCDDivide ( const Currin : currency;
  2538. const BCDIn : tBCD;
  2539. var BCDout : tBCD ); Inline;
  2540. begin
  2541. BCDDivide ( CurrToBCD ( Currin ), BCDIn, BCDout );
  2542. end;
  2543. {$ifdef comproutines}
  2544. procedure BCDDivide ( const BCDIn : tBCD;
  2545. const Compin : Comp;
  2546. var BCDout : tBCD ); Inline;
  2547. begin
  2548. BCDDivide ( BCDIn, CompToBCD ( Compin ), BCDout );
  2549. end;
  2550. procedure BCDDivide ( const Compin : Comp;
  2551. const BCDIn : tBCD;
  2552. var BCDout : tBCD ); Inline;
  2553. begin
  2554. BCDDivide ( CompToBCD ( Compin ), BCDIn, BCDout );
  2555. end;
  2556. {$endif}
  2557. procedure BCDDivide ( const Dividend : FmtBCDStringtype;
  2558. const Divisor : tBCD;
  2559. var BCDout : tBCD ); Inline;
  2560. begin
  2561. BCDDivide ( StrToBCD ( Dividend ), Divisor, BCDout );
  2562. end;
  2563. operator = ( const BCD1,
  2564. BCD2 : tBCD ) z : Boolean; Inline;
  2565. begin
  2566. z := BCDCompare ( BCD1, BCD2 ) = 0;
  2567. end;
  2568. operator < ( const BCD1,
  2569. BCD2 : tBCD ) z : Boolean; Inline;
  2570. begin
  2571. z := BCDCompare ( BCD1, BCD2 ) < 0;
  2572. end;
  2573. operator > ( const BCD1,
  2574. BCD2 : tBCD ) z : Boolean; Inline;
  2575. begin
  2576. z := BCDCompare ( BCD1, BCD2 ) > 0;
  2577. end;
  2578. operator <= ( const BCD1,
  2579. BCD2 : tBCD ) z : Boolean; Inline;
  2580. begin
  2581. z := BCDCompare ( BCD1, BCD2 ) <= 0;
  2582. end;
  2583. operator >= ( const BCD1,
  2584. BCD2 : tBCD ) z : Boolean; Inline;
  2585. begin
  2586. z := BCDCompare ( BCD1, BCD2 ) >= 0;
  2587. end;
  2588. (* ######################## not allowed: why?
  2589. operator + ( const BCD : tBCD ) z : tBCD; Inline;
  2590. begin
  2591. z := bcd;
  2592. end;
  2593. ##################################################### *)
  2594. operator - ( const BCD : tBCD ) z : tBCD; Inline;
  2595. begin
  2596. z := BCD;
  2597. BCDNegate ( z );
  2598. end;
  2599. operator + ( const BCD1,
  2600. BCD2 : tBCD ) z : tBCD; Inline;
  2601. begin
  2602. BCDAdd ( BCD1, BCD2, z );
  2603. end;
  2604. operator + ( const BCD : tBCD;
  2605. const i : myInttype ) z : tBCD; Inline;
  2606. begin
  2607. BCDAdd ( BCD, i, z );
  2608. end;
  2609. operator + ( const i : myInttype;
  2610. const BCD : tBCD ) z : tBCD; Inline;
  2611. begin
  2612. BCDAdd ( i, BCD, z );
  2613. end;
  2614. operator + ( const BCD : tBCD;
  2615. const r : myRealtype ) z : tBCD; Inline;
  2616. begin
  2617. BCDAdd ( BCD, DoubleToBCD ( r ), z );
  2618. end;
  2619. operator + ( const r : myRealtype;
  2620. const BCD : tBCD ) z : tBCD; Inline;
  2621. begin
  2622. BCDAdd ( DoubleToBCD ( r ), BCD, z );
  2623. end;
  2624. operator + ( const BCD : tBCD;
  2625. const c : currency ) z : tBCD; Inline;
  2626. begin
  2627. BCDAdd ( BCD, CurrToBCD ( c ), z );
  2628. end;
  2629. operator + ( const c : currency;
  2630. const BCD : tBCD ) z : tBCD; Inline;
  2631. begin
  2632. BCDAdd ( CurrToBCD ( c ), BCD, z );
  2633. end;
  2634. {$ifdef comproutines}
  2635. operator + ( const BCD : tBCD;
  2636. const c : Comp ) z : tBCD; Inline;
  2637. begin
  2638. BCDAdd ( BCD, CompToBCD ( c ), z );
  2639. end;
  2640. operator + ( const c : Comp;
  2641. const BCD : tBCD ) z : tBCD; Inline;
  2642. begin
  2643. BCDAdd ( CompToBCD ( c ), BCD, z );
  2644. end;
  2645. {$endif}
  2646. operator + ( const BCD : tBCD;
  2647. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2648. begin
  2649. BCDAdd ( BCD, StrToBCD ( s ), z );
  2650. end;
  2651. operator + ( const s : FmtBCDStringtype;
  2652. const BCD : tBCD ) z : tBCD; Inline;
  2653. begin
  2654. BCDAdd ( StrToBCD ( s ), BCD, z );
  2655. end;
  2656. operator - ( const BCD1,
  2657. BCD2 : tBCD ) z : tBCD; Inline;
  2658. begin
  2659. BCDSubtract ( BCD1, BCD2, z );
  2660. end;
  2661. operator - ( const BCD : tBCD;
  2662. const i : myInttype ) z : tBCD; Inline;
  2663. begin
  2664. BCDSubtract ( BCD, i, z );
  2665. end;
  2666. operator - ( const i : myInttype;
  2667. const BCD : tBCD ) z : tBCD; Inline;
  2668. begin
  2669. BCDSubtract ( BCD, i, z );
  2670. BCDNegate ( z );
  2671. end;
  2672. operator - ( const BCD : tBCD;
  2673. const r : myRealtype ) z : tBCD; Inline;
  2674. begin
  2675. BCDSubtract ( BCD, DoubleToBCD ( r ), z );
  2676. end;
  2677. operator - ( const r : myRealtype;
  2678. const BCD : tBCD ) z : tBCD; Inline;
  2679. begin
  2680. BCDSubtract ( DoubleToBCD ( r ), BCD, z );
  2681. end;
  2682. operator - ( const BCD : tBCD;
  2683. const c : currency ) z : tBCD; Inline;
  2684. begin
  2685. BCDSubtract ( BCD, CurrToBCD ( c ), z );
  2686. end;
  2687. operator - ( const c : currency;
  2688. const BCD : tBCD ) z : tBCD; Inline;
  2689. begin
  2690. BCDSubtract ( CurrToBCD ( c ), BCD, z );
  2691. end;
  2692. {$ifdef comproutines}
  2693. operator - ( const BCD : tBCD;
  2694. const c : Comp ) z : tBCD; Inline;
  2695. begin
  2696. BCDSubtract ( BCD, CompToBCD ( c ), z );
  2697. end;
  2698. operator - ( const c : Comp;
  2699. const BCD : tBCD ) z : tBCD; Inline;
  2700. begin
  2701. BCDSubtract ( CompToBCD ( c ), BCD, z );
  2702. end;
  2703. {$endif}
  2704. operator - ( const BCD : tBCD;
  2705. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2706. begin
  2707. BCDSubtract ( BCD, StrToBCD ( s ), z );
  2708. end;
  2709. operator - ( const s : FmtBCDStringtype;
  2710. const BCD : tBCD ) z : tBCD; Inline;
  2711. begin
  2712. BCDSubtract ( StrToBCD ( s ), BCD, z );
  2713. end;
  2714. operator * ( const BCD1,
  2715. BCD2 : tBCD ) z : tBCD; Inline;
  2716. begin
  2717. BCDMultiply ( BCD1, BCD2, z );
  2718. end;
  2719. operator * ( const BCD : tBCD;
  2720. const i : myInttype ) z : tBCD; Inline;
  2721. begin
  2722. BCDMultiply ( BCD, i, z );
  2723. end;
  2724. operator * ( const i : myInttype;
  2725. const BCD : tBCD ) z : tBCD; Inline;
  2726. begin
  2727. BCDMultiply ( BCD, i, z );
  2728. end;
  2729. operator * ( const BCD : tBCD;
  2730. const r : myRealtype ) z : tBCD; Inline;
  2731. begin
  2732. BCDMultiply ( BCD, DoubleToBCD ( r ), z );
  2733. end;
  2734. operator * ( const r : myRealtype;
  2735. const BCD : tBCD ) z : tBCD; Inline;
  2736. begin
  2737. BCDMultiply ( DoubleToBCD ( r ), BCD, z );
  2738. end;
  2739. operator * ( const BCD : tBCD;
  2740. const c : currency ) z : tBCD; Inline;
  2741. begin
  2742. BCDMultiply ( BCD, CurrToBCD ( c ), z );
  2743. end;
  2744. operator * ( const c : currency;
  2745. const BCD : tBCD ) z : tBCD; Inline;
  2746. begin
  2747. BCDMultiply ( CurrToBCD ( c ), BCD, z );
  2748. end;
  2749. {$ifdef comproutines}
  2750. operator * ( const BCD : tBCD;
  2751. const c : Comp ) z : tBCD; Inline;
  2752. begin
  2753. BCDMultiply ( BCD, CompToBCD ( c ), z );
  2754. end;
  2755. operator * ( const c : Comp;
  2756. const BCD : tBCD ) z : tBCD; Inline;
  2757. begin
  2758. BCDMultiply ( CompToBCD ( c ), BCD, z );
  2759. end;
  2760. {$endif}
  2761. operator * ( const BCD : tBCD;
  2762. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2763. begin
  2764. BCDMultiply ( BCD, StrToBCD ( s ), z );
  2765. end;
  2766. operator * ( const s : FmtBCDStringtype;
  2767. const BCD : tBCD ) z : tBCD; Inline;
  2768. begin
  2769. BCDMultiply ( StrToBCD ( s ), BCD, z );
  2770. end;
  2771. operator / ( const BCD1,
  2772. BCD2 : tBCD ) z : tBCD; Inline;
  2773. begin
  2774. BCDDivide ( BCD1, BCD2, z );
  2775. end;
  2776. operator / ( const BCD : tBCD;
  2777. const i : myInttype ) z : tBCD; Inline;
  2778. begin
  2779. BCDDivide ( BCD, i, z );
  2780. end;
  2781. operator / ( const i : myInttype;
  2782. const BCD : tBCD ) z : tBCD; Inline;
  2783. begin
  2784. BCDDivide ( IntegerToBCD ( i ), BCD, z );
  2785. end;
  2786. operator / ( const BCD : tBCD;
  2787. const r : myRealtype ) z : tBCD; Inline;
  2788. begin
  2789. BCDDivide ( BCD, DoubleToBCD ( r ), z );
  2790. end;
  2791. operator / ( const r : myRealtype;
  2792. const BCD : tBCD ) z : tBCD; Inline;
  2793. begin
  2794. BCDDivide ( DoubleToBCD ( r ), BCD, z );
  2795. end;
  2796. operator / ( const BCD : tBCD;
  2797. const c : currency ) z : tBCD; Inline;
  2798. begin
  2799. BCDDivide ( BCD, CurrToBCD ( c ), z );
  2800. end;
  2801. operator / ( const c : currency;
  2802. const BCD : tBCD ) z : tBCD; Inline;
  2803. begin
  2804. BCDDivide ( CurrToBCD ( c ), BCD, z );
  2805. end;
  2806. {$ifdef comproutines}
  2807. operator / ( const BCD : tBCD;
  2808. const c : Comp ) z : tBCD; Inline;
  2809. begin
  2810. BCDDivide ( BCD, CompToBCD ( c ), z );
  2811. end;
  2812. operator / ( const c : Comp;
  2813. const BCD : tBCD ) z : tBCD; Inline;
  2814. begin
  2815. BCDDivide ( CompToBCD ( c ), BCD, z );
  2816. end;
  2817. {$endif}
  2818. operator / ( const BCD : tBCD;
  2819. const s : FmtBCDStringtype ) z : tBCD; Inline;
  2820. begin
  2821. BCDDivide ( BCD, StrToBCD ( s ), z );
  2822. end;
  2823. operator / ( const s : FmtBCDStringtype;
  2824. const BCD : tBCD ) z : tBCD; Inline;
  2825. begin
  2826. BCDDivide ( StrToBCD ( s ), BCD, z );
  2827. end;
  2828. operator := ( const i : Byte ) z : tBCD; Inline;
  2829. begin
  2830. z := IntegerToBCD ( myInttype ( i ) );
  2831. end;
  2832. operator := ( const BCD : tBCD ) z : Byte; Inline;
  2833. begin
  2834. z := BCDToInteger ( BCD );
  2835. end;
  2836. operator := ( const i : Word ) z : tBCD; Inline;
  2837. begin
  2838. z := IntegerToBCD ( myInttype ( i ) );
  2839. end;
  2840. operator := ( const BCD : tBCD ) z : Word; Inline;
  2841. begin
  2842. z := BCDToInteger ( BCD );
  2843. end;
  2844. operator := ( const i : longword ) z : tBCD; Inline;
  2845. begin
  2846. z := IntegerToBCD ( myInttype ( i ) );
  2847. end;
  2848. operator := ( const BCD : tBCD ) z : longword; Inline;
  2849. begin
  2850. z := BCDToInteger ( BCD );
  2851. end;
  2852. {$if declared ( qword ) }
  2853. operator := ( const i : qword ) z : tBCD; Inline;
  2854. begin
  2855. z := IntegerToBCD ( myInttype ( i ) );
  2856. end;
  2857. operator := ( const BCD : tBCD ) z : qword; Inline;
  2858. begin
  2859. z := BCDToInteger ( BCD );
  2860. end;
  2861. {$endif}
  2862. operator := ( const i : ShortInt ) z : tBCD; Inline;
  2863. begin
  2864. z := IntegerToBCD ( myInttype ( i ) );
  2865. end;
  2866. operator := ( const BCD : tBCD ) z : ShortInt; Inline;
  2867. begin
  2868. z := BCDToInteger ( BCD );
  2869. end;
  2870. operator := ( const i : smallint ) z : tBCD; Inline;
  2871. begin
  2872. z := IntegerToBCD ( myInttype ( i ) );
  2873. end;
  2874. operator := ( const BCD : tBCD ) z : smallint; Inline;
  2875. begin
  2876. z := BCDToInteger ( BCD );
  2877. end;
  2878. operator := ( const i : LongInt ) z : tBCD; Inline;
  2879. begin
  2880. z := IntegerToBCD ( myInttype ( i ) );
  2881. end;
  2882. operator := ( const BCD : tBCD ) z : LongInt; Inline;
  2883. begin
  2884. z := BCDToInteger ( BCD );
  2885. end;
  2886. {$if declared ( int64 ) }
  2887. operator := ( const i : int64 ) z : tBCD; Inline;
  2888. begin
  2889. z := IntegerToBCD ( myInttype ( i ) );
  2890. end;
  2891. operator := ( const BCD : tBCD ) z : int64; Inline;
  2892. begin
  2893. z := BCDToInteger ( BCD );
  2894. end;
  2895. {$endif}
  2896. operator := ( const r : Single ) z : tBCD; Inline;
  2897. begin
  2898. z := DoubleToBCD ( myRealtype ( r ) );
  2899. end;
  2900. operator := ( const BCD : tBCD ) z : Single; Inline;
  2901. begin
  2902. z := BCDToDouble ( BCD );
  2903. end;
  2904. operator := ( const r : Double ) z : tBCD; Inline;
  2905. begin
  2906. z := DoubleToBCD ( myRealtype ( r ) );
  2907. end;
  2908. operator := ( const BCD : tBCD ) z : Double; Inline;
  2909. begin
  2910. z := BCDToDouble ( BCD );
  2911. end;
  2912. {$if sizeof ( extended ) <> sizeof ( double )}
  2913. operator := ( const r : Extended ) z : tBCD; Inline;
  2914. begin
  2915. z := DoubleToBCD ( {myRealtype (} r {)} );
  2916. end;
  2917. operator := ( const BCD : tBCD ) z : Extended; Inline;
  2918. begin
  2919. z := BCDToDouble ( BCD );
  2920. end;
  2921. {$endif}
  2922. operator := ( const c : currency ) z : tBCD; Inline;
  2923. begin
  2924. CurrToBCD ( c, z );
  2925. end;
  2926. operator := ( const BCD : tBCD ) z : currency; Inline;
  2927. begin
  2928. BCDToCurr ( BCD, z );
  2929. end;
  2930. {$ifdef comproutines}
  2931. {$undef makedirect}
  2932. {$ifdef makedirect}
  2933. operator := ( const c : Comp ) z : tBCD; Inline;
  2934. var
  2935. cc : int64 absolute c;
  2936. begin
  2937. z := IntegerToBCD ( cc );
  2938. end;
  2939. { $define version1} { only one of these may be defined! }
  2940. { $define version2} { version 1 produces a compiler error (with INLINE only!)}
  2941. {$define version3} { I wasn't able to reduce the problem, sorry }
  2942. {$ifdef version1}
  2943. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2944. var
  2945. zz : Comp absolute z;
  2946. begin
  2947. zz := BCDToInteger ( BCD );
  2948. end;
  2949. {$endif}
  2950. {$ifdef version2}
  2951. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2952. var
  2953. zz : int64;
  2954. zzz : Comp absolute zz;
  2955. begin
  2956. zz := BCDToInteger ( BCD );
  2957. z := zzz;
  2958. end;
  2959. {$endif}
  2960. {$ifdef version3}
  2961. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2962. var
  2963. zz : record
  2964. case Boolean of
  2965. False: ( i : int64 );
  2966. True: ( c : Comp );
  2967. end;
  2968. begin
  2969. zz.i := BCDToInteger ( BCD );
  2970. z := zz.c;
  2971. end;
  2972. {$endif}
  2973. {$else}
  2974. operator := ( const c : Comp ) z : tBCD; Inline;
  2975. begin
  2976. z := CompToBCD ( c );
  2977. end;
  2978. operator := ( const BCD : tBCD ) z : Comp; Inline;
  2979. begin
  2980. z := BCDToComp ( BCD );
  2981. end;
  2982. {$endif}
  2983. {$endif}
  2984. operator := ( const s : string ) z : tBCD; Inline;
  2985. begin
  2986. z := StrToBCD ( s );
  2987. end;
  2988. operator := ( const BCD : tBCD ) z : string; Inline;
  2989. begin
  2990. z := BCDToStr ( BCD );
  2991. end;
  2992. operator := ( const s : AnsiString ) z : tBCD; Inline;
  2993. begin
  2994. z := StrToBCD ( s );
  2995. end;
  2996. operator := ( const BCD : tBCD ) z : AnsiString; Inline;
  2997. begin
  2998. z := BCDToStr ( BCD );
  2999. end;
  3000. {$endif}
  3001. constructor TFMTBcdVarData.create;
  3002. begin
  3003. inherited create;
  3004. FBcd:=NullBCD;
  3005. end;
  3006. constructor TFMTBcdVarData.create(const BCD : tBCD);
  3007. begin
  3008. inherited create;
  3009. FBcd:=BCD;
  3010. end;
  3011. function TFMTBcdFactory.GetInstance(const v : TVarData): tObject;
  3012. begin
  3013. result:=tObject(v.VPointer);
  3014. end;
  3015. procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
  3016. begin
  3017. case Operation of
  3018. opAdd:
  3019. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD+TFMTBcdVarData(Right.VPointer).BCD;
  3020. opSubtract:
  3021. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD-TFMTBcdVarData(Right.VPointer).BCD;
  3022. opMultiply:
  3023. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD*TFMTBcdVarData(Right.VPointer).BCD;
  3024. opDivide:
  3025. TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD/TFMTBcdVarData(Right.VPointer).BCD;
  3026. else
  3027. RaiseInvalidOp;
  3028. end;
  3029. end;
  3030. procedure TFMTBcdFactory.Clear(var V: TVarData);
  3031. begin
  3032. FreeAndNil(tObject(V.VPointer));
  3033. V.VType:=varEmpty;
  3034. end;
  3035. procedure TFMTBcdFactory.Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean);
  3036. begin
  3037. if Indirect then
  3038. Dest.VPointer:=Source.VPointer
  3039. else
  3040. Dest.VPointer:=TFMTBcdVarData.Create(TFMTBcdVarData(Source.VPointer).BCD);
  3041. Dest.VType:=Vartype;
  3042. end;
  3043. {$if declared ( myMinIntBCD ) }
  3044. (*
  3045. {$if sizeof ( integer ) = 2 }
  3046. {$ifdef BCDgr4 }
  3047. const
  3048. myMinIntBCDValue : packed array [ 1..3 ] of Char = #$32#$76#$80;
  3049. {$endif}
  3050. {$else}
  3051. {$if sizeof ( integer ) = 4 }
  3052. *)
  3053. {$ifdef BCDgr9 }
  3054. const
  3055. myMinIntBCDValue : packed array [ 1..10 ] of Char = #$21#$47#$48#$36#$48;
  3056. {$endif}
  3057. (*
  3058. {$else}
  3059. {$if sizeof ( integer ) = 8 }
  3060. {$ifdef BCDgr18 }
  3061. const
  3062. myMinIntBCDValue : packed array [ 1..19 ] of Char = #$92#$23#$37#$20#$36#$85#$47#$75#$80#$80;
  3063. {$endif}
  3064. {$else}
  3065. {$fatal You have an interesting integer type! Sorry, not supported}
  3066. {$endif}
  3067. {$endif}
  3068. {$endif}
  3069. *)
  3070. {$endif}
  3071. initialization
  3072. FillChar ( null_, SizeOf ( null_ ), #0 );
  3073. FillChar ( NullBCD_, SizeOf ( NullBCD_ ), #0 );
  3074. FillChar ( OneBCD_, SizeOf ( OneBCD_ ), #0 );
  3075. OneBCD_.Precision := 1;
  3076. OneBCD_.Fraction[low ( OneBCD_.Fraction )] := $10;
  3077. {$if declared ( myMinIntBCD ) }
  3078. FillChar ( myMinIntBCD, SizeOf ( myMinIntBCD ), #0 );
  3079. {$ifndef bigger_BCD}
  3080. myMinIntBCD.SignSpecialPlaces := NegBit;
  3081. {$else}
  3082. myMinIntBCD.Negativ := True;
  3083. {$endif}
  3084. {$if sizeof ( integer ) = 2 }
  3085. {$ifdef BCDgr4 }
  3086. myMinIntBCD.Precision := 5;
  3087. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3088. {$endif}
  3089. {$else}
  3090. {$if sizeof ( integer ) = 4 }
  3091. {$ifdef BCDgr9 }
  3092. myMinIntBCD.Precision := 10;
  3093. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3094. {$endif}
  3095. {$else}
  3096. {$if sizeof ( integer ) = 8 }
  3097. {$ifdef BCDgr18 }
  3098. myMinIntBCD.Precision := 19;
  3099. Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
  3100. {$endif}
  3101. {$else}
  3102. {$fatal You have an interesting integer type! Sorry, not supported}
  3103. {$endif}
  3104. {$endif}
  3105. {$endif}
  3106. {$endif}
  3107. FMTBcdFactory:=TFMTBcdFactory.create;
  3108. finalization
  3109. FreeAndNil(FMTBcdFactory)
  3110. end.