12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842 |
- {
- This file is part of the Free Pascal run time library.
- Copyright (c) 2005-2006 by the Free Pascal development team
- and Gehard Scholz
- It contains the Free Pascal BCD implementation
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE.
- **********************************************************************}
- { "Programming is the time between two bugs" }
- { (last words of the unknown programmer) }
- (* this program was a good test for the compiler: some bugs have been found.
- 1. WITH in inline funcs produces a compiler error AFTER producing an .exe file
- (was already known; I didn't see it in the bug list)
- 2. macro names were checked for being a keyword, even when starting with
- an '_' (produces range check when compiler is compiled with { $r+ }-mode
- fixed.
- 3. { $define program } was not possible in { $macro on } mode
- (keywords not allowed: doesn't make sense here)
- fixed.
- 4. the Inc/Dec ( unsigned, signed ) problem (has been similar in the
- bug list already)
- 5. when the result of an overloaded (inline) operator is ABSOLUTEd:
- compiler error 200110205
- happens only when operator is defined in a unit.
- 6. two range check errors in scanner.pas
- a) array subscripting
- b) value out ouf range
- *)
- { $define debug_version}
- {$r+,q+,s+}
- { $r-,q-,s-}
- {$mode objfpc}
- {$h-}
- {$inline on}
- {$macro on}
- {$define BCDMaxDigits := 64 } { should be even }
- { the next defines must be defined by hand,
- unless someone shows me a way how to to it with macros }
- {$define BCDgr4} { define this if MCDMaxDigits is greater 4, else undefine! }
- {$define BCDgr9} { define this if MCDMaxDigits is greater 9, else undefine! }
- {$define BCDgr18} { define this if MCDMaxDigits is greater 18, else undefine! }
- { $define BCDgr64} { define this if MCDMaxDigits is greater 64, else undefine! }
- { $define BCDgr180} { define this if MCDMaxDigits is greater 180, else undefine! }
- {$ifdef BCDgr4}
- {$hint BCD Digits > 4}
- {$endif}
- {$ifdef BCDgr9}
- {$hint BCD Digits > 9}
- {$endif}
- {$ifdef BCDgr18}
- {$hint BCD Digits > 18}
- {$endif}
- {$ifdef BCDgr64}
- {$hint BCD Digits > 64}
- {$endif}
- {$ifdef BCDgr180}
- {$hint BCD Digits > 180}
- {$endif}
- {$ifndef NO_SMART_LINK}
- { $smartlink on}
- {$endif}
- {$define some_packed} { enable this to keep some local structures PACKED }
- { $define as_object} { to define the tBCD record as object instead;
- fields then are private }
- { not done yet! }
- {$define additional_routines} { to create additional routines and operators }
- (* only define one of them! *)
- { $define integ32}
- {$define integ64}
- (* only define one of them! *)
- { $define real8}
- {$define real10}
- {check}
- {$ifndef integ32}
- {$ifndef integ64}
- {$define integ64}
- {$endif}
- {$endif}
- {$ifdef integ32}
- {$ifdef integ64}
- {$undef integ32}
- {$endif}
- {$endif}
- {check}
- {$ifndef real8}
- {$ifndef real10}
- {$define real8}
- {$endif}
- {$endif}
- {$ifdef real8}
- {$ifdef real10}
- {$undef real10}
- {$endif}
- {$endif}
- {$ifdef some_packed}
- {$define maybe_packed := packed}
- {$else}
- {$define maybe_packed := (**)}
- {$endif}
- UNIT FmtBCD;
- INTERFACE
- USES
- SysUtils,
- { dateutils,}
- Variants;
- const
- MaxStringDigits = 100; { not used ! }
- _NoDecimal = -255; { not used ! }
- _DefaultDecimals = 10; { not used ! }
- { From DB.pas }
- { Max supported by Midas } { must be EVEN }
- MaxFmtBCDFractionSize = BCDMaxDigits + Ord ( Odd ( BCDMaxDigits ) );
- { Max supported by Midas }
- MaxFmtBCDDigits = 32; { not used ! }
- DefaultFmtBCDScale = 6; { not used ! }
- MaxBCDPrecision = 18; { not used ! }
- MaxBCDScale = 4; { not used ! }
- {$ifdef BCDgr64}
- { $fatal big 1}
- {$define bigger_BCD} { must be defined
- if MaxFmtBCDFractionSize > 64 }
- { not usable in the moment }
- {$endif}
- {$ifdef BCDgr180}
- { $fatal big 2}
- type
- FmtBCDStringtype = AnsiString;
- {$define use_Ansistring}
- {$else}
- type
- FmtBCDStringtype = string [ 255 ];
- {$undef use_Ansistring}
- {$endif}
- {$ifdef use_ansistring}
- {$hint ansi}
- {$else}
- {$hint -ansi}
- {$endif}
- {$ifdef integ32}
- {$define myInttype := LongInt}
- {$endif}
- {$ifdef integ64}
- {$define myInttype := int64}
- {$endif}
- {$ifndef FPUNONE}
- {$ifdef real8}
- {$define myRealtype := double}
- {$endif}
- {$ifdef real10}
- {$define myRealtype := extended}
- {$endif}
- {$endif}
- {$ifdef SUPPORT_COMP}
- {$define comproutines}
- {$endif SUPPORT_COMP}
- {$define __low_Fraction := 0 }
- {$define __high_Fraction := ( ( MaxFmtBCDFractionSize DIV 2 ) - 1 ) }
- type
- pBCD = ^ tBCD;
- tBCD = packed {$ifdef as_object} OBJECT {$else} record {$endif}
- {$ifdef as_object} PRIVATE {$endif}
- Precision : 0..maxfmtbcdfractionsize; { 1 (joke?)..64 }
- {$ifndef bigger_BCD}
- SignSpecialPlaces : Byte; { Sign:1, Special:1, Places:6 }
- {$else}
- Negativ : Boolean;
- {
- Special : Boolean;
- }
- Places : 0..maxfmtbcdfractionsize - 1;
- {$endif}
- Fraction : packed array [ __low_Fraction..__high_Fraction ] of Byte;
- { BCD Nibbles, 00..99 per Byte, high Nibble 1st }
- end;
- type
- tDecimalPoint = ( DecimalPoint_is_Point, DecimalPoint_is_Comma, DecimalPoint_is_System );
- { Exception classes }
- type
- eBCDException = CLASS ( Exception );
- eBCDOverflowException = CLASS ( eBCDException );
- eBCDNotImplementedException = CLASS ( eBCDException );
- var
- DecimalPoint : tDecimalPoint = DecimalPoint_is_Point;
- { Utility functions for TBCD access }
- function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
- function BCDScale ( const BCD : tBCD ) : Word; Inline;
- function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
- { BCD Arithmetic}
- procedure BCDNegate ( var BCD : tBCD ); Inline;
- { !!!!!!!!!! most routines are intentionally NOT inline !!!!!!!!!! }
- { Returns True if successful, False if Int Digits needed to be truncated }
- function NormalizeBCD ( const InBCD : tBCD;
- var OutBCD : tBCD;
- const Prec,
- Scale : Word ) : Boolean;
- procedure BCDAdd ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- procedure BCDSubtract ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- procedure BCDMultiply ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- {$ifndef FPUNONE}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- { !!! params changed to const, shouldn't give a problem }
- procedure BCDMultiply ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- procedure BCDDivide ( const Dividend,
- Divisor : tBCD;
- var BCDout : tBCD );
- {$ifndef FPUNONE}
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : myRealtype;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- { !!! params changed to const, shouldn't give a problem }
- procedure BCDDivide ( const Dividend,
- Divisor : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- { TBCD variant creation utils }
- procedure VarFmtBCDCreate ( var aDest : Variant;
- const aBCD : tBCD );
- function VarFmtBCDCreate : Variant;
- function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
- Precision,
- Scale : Word ) : Variant;
- {$ifndef FPUNONE}
- function VarFmtBCDCreate ( const aValue : myRealtype;
- Precision : Word = 18;
- Scale : Word = 4 ) : Variant;
- {$endif}
- function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
- function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
- function VarFmtBCD : TVartype;
- { Convert string/Double/Integer to BCD struct }
- function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
- function TryStrToBCD ( const aValue : FmtBCDStringtype;
- var BCD : tBCD ) : Boolean;
- {$ifndef FPUNONE}
- function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
- procedure DoubleToBCD ( const aValue : myRealtype;
- var BCD : tBCD );
- {$endif}
- function IntegerToBCD ( const aValue : myInttype ) : tBCD;
- function VarToBCD ( const aValue : Variant ) : tBCD;
- { From DB.pas }
- function CurrToBCD ( const Curr : currency;
- var BCD : tBCD;
- Precision : Integer = 32;
- Decimals : Integer = 4 ) : Boolean;
- { Convert BCD struct to string/Double/Integer }
- function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
- {$ifndef FPUNONE}
- function BCDToDouble ( const BCD : tBCD ) : myRealtype;
- {$endif}
- function BCDToInteger ( const BCD : tBCD;
- Truncate : Boolean = False ) : myInttype;
- { From DB.pas }
- function BCDToCurr ( const BCD : tBCD;
- var Curr : currency ) : Boolean;
- { Formatting BCD as string }
- function BCDToStrF ( const BCD : tBCD;
- Format : TFloatFormat;
- const Precision,
- Digits : Integer ) : FmtBCDStringtype;
- function FormatBCD ( const Format : string;
- BCD : tBCD ) : FmtBCDStringtype;
- { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
- function BCDCompare ( const BCD1,
- BCD2 : tBCD ) : Integer;
- {$ifdef additional_routines}
- function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
- {$ifdef comproutines}
- function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
- function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
- {$endif}
- procedure BCDAdd ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- procedure BCDAdd ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifndef FPUNONE}
- procedure BCDAdd ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- procedure BCDAdd ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDAdd ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- procedure BCDAdd ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifdef comproutines}
- procedure BCDAdd ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- procedure BCDAdd ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDAdd ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- procedure BCDAdd ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- procedure BCDAdd ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- procedure BCDSubtract ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifndef FPUNONE}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifdef comproutines}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- procedure BCDSubtract ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- procedure BCDMultiply ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- procedure BCDMultiply ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifndef FPUNONE}
- procedure BCDMultiply ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- procedure BCDMultiply ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifdef comproutines}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- procedure BCDMultiply ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : myInttype;
- var BCDout : tBCD ); Inline;
- procedure BCDDivide ( const Dividend : myInttype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifndef FPUNONE}
- procedure BCDDivide ( const Dividend : myRealtype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDDivide ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- procedure BCDDivide ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$ifdef comproutines}
- procedure BCDDivide ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- procedure BCDDivide ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- {$endif}
- procedure BCDDivide ( const Dividend : FmtBCDStringtype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- operator = ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- operator < ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- operator > ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- operator <= ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- operator >= ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- (* ######################## not allowed: why?
- operator + ( const BCD : tBCD ) z : tBCD; make_Inline
- ##################################################### *)
- operator - ( const BCD : tBCD ) z : tBCD; Inline;
- operator + ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- operator + ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- operator + ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifndef FPUNONE}
- operator + ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- operator + ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator + ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- operator + ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifdef comproutines}
- operator + ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- operator + ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator + ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- operator + ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- operator - ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- operator - ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- operator - ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifndef FPUNONE}
- operator - ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- operator - ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator - ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- operator - ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifdef comproutines}
- operator - ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- operator - ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator - ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- operator - ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- operator * ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- operator * ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- operator * ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifndef FPUNONE}
- operator * ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- operator * ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator * ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- operator * ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifdef comproutines}
- operator * ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- operator * ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator * ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- operator * ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- operator / ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- operator / ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- operator / ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifndef FPUNONE}
- operator / ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- operator / ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator / ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- operator / ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- {$ifdef comproutines}
- operator / ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- operator / ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- {$endif}
- operator / ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- operator / ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- operator := ( const i : Byte ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Byte; Inline;
- operator := ( const i : Word ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Word; Inline;
- operator := ( const i : longword ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : longword; Inline;
- {$if declared ( qword ) }
- operator := ( const i : qword ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : qword; Inline;
- {$endif}
- operator := ( const i : ShortInt ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : ShortInt; Inline;
- operator := ( const i : smallint ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : smallint; Inline;
- operator := ( const i : LongInt ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : LongInt; Inline;
- {$if declared ( int64 ) }
- operator := ( const i : int64 ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : int64; Inline;
- {$endif}
- {$ifndef FPUNONE}
- operator := ( const r : Single ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Single; Inline;
- operator := ( const r : Double ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Double; Inline;
- {$if sizeof ( extended ) <> sizeof ( double )}
- operator := ( const r : Extended ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Extended; Inline;
- {$endif}
- {$endif}
- operator := ( const c : currency ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : currency; Inline;
- {$ifdef comproutines}
- operator := ( const c : Comp ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : Comp; Inline;
- {$endif}
- operator := ( const s : string ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : string; Inline;
- operator := ( const s : AnsiString ) z : tBCD; Inline;
- operator := ( const BCD : tBCD ) z : AnsiString; Inline;
- {$endif}
- function __get_null : tBCD; Inline;
- function __get_one : tBCD; Inline;
- PROPERTY
- NullBCD : tBCD Read __get_null;
- OneBCD : tBCD Read __get_one;
- //{$define __lo_bh := 1 * ( -( MaxFmtBCDFractionSize * 1 + 2 ) ) }
- //{$define __hi_bh := 1 * ( MaxFmtBCDFractionSize * 1 + 1 ) }
- {$define helper_declarations :=
- const
- __lo_bh = -( MaxFmtBCDFractionSize + 2 );
- __hi_bh = ( MaxFmtBCDFractionSize + 1 );
- type
- tBCD_helper = Maybe_Packed record
- Prec : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
- Plac : {$ifopt r+} 0..( __hi_bh - __lo_bh + 1 ) {$else} Integer {$endif};
- FDig,
- LDig : {$ifopt r+} __lo_bh..__hi_bh {$else} Integer {$endif};
- Singles : Maybe_packed array [ __lo_bh..__hi_bh ]
- of {$ifopt r+} 0..9 {$else} Byte {$endif};
- Neg : Boolean;
- end;
- { in the tBCD_helper the bcd is stored for computations,
- shifted to the right position }
- // {$define __lo_bhb := 1 * ( __lo_bh + __lo_bh ) }
- // {$define __hi_bhb := 1 * ( __hi_bh + __hi_bh + 1 ) }
- const
- __lo_bhb = __lo_bh + __lo_bh - 1;
- __hi_bhb = __hi_bh + __hi_bh;
- type
- tBCD_helper_big = Maybe_Packed record
- Prec : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
- Plac : {$ifopt r+} 0.. ( __hi_bhb - __lo_bhb + 1 ) {$else} Integer {$endif};
- FDig,
- LDig : {$ifopt r+} __lo_bhb..__hi_bhb {$else} Integer {$endif};
- Singles : Maybe_packed array [ __lo_bhb..__hi_bhb ]
- of {$ifopt r+} 0 * 0..9 * 9 * Pred ( MaxFmtBCDDigits ) {$else} Integer {$endif};
- Neg : Boolean;
- end;
- }
- {$ifdef debug_version}
- helper_declarations
- procedure unpack_BCD ( const BCD : tBCD;
- var bh : tBCD_helper );
- function pack_BCD ( var bh : tBCD_helper;
- var BCD : tBCD ) : Boolean;
- procedure dumpBCD ( const v : tBCD );
- {$endif}
- IMPLEMENTATION
- USES
- classes {$ifopt r+}, sysconst {$endif};
- type
- TFMTBcdFactory = CLASS(TPublishableVarianttype)
- PROTECTED
- function GetInstance(const v : TVarData): tObject; OVERRIDE;
- PUBLIC
- procedure BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp); override;
- procedure Clear(var V: TVarData); override;
- procedure Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean); override;
- end;
- TFMTBcdVarData = CLASS(TPersistent)
- PRIVATE
- FBcd : tBCD;
- PUBLIC
- constructor create;
- constructor create(const BCD : tBCD);
- PROPERTY BCD : tBCD Read FBcd Write FBcd;
- end;
- var
- NullBCD_ : tBCD;
- OneBCD_ : tBCD;
- function __get_null : tBCD; Inline;
- begin
- __get_null := NullBCD_;
- end;
- function __get_one : tBCD; Inline;
- begin
- __get_one := OneBCD_;
- end;
- type
- range_digits = 1..maxfmtbcdfractionsize;
- range_digits0 = 0..maxfmtbcdfractionsize;
- range_fracdigits = 0..pred ( MaxFmtBCDFractionSize );
- {$ifopt r+}
- procedure RangeError;
- begin
- raise ERangeError.Create(SRangeError);
- end;
- {$endif}
- {$ifndef debug_version}
- helper_declarations
- {$endif}
- var
- null_ : record
- case Boolean of
- False: ( bh : tBCD_helper );
- True: ( bhb : tBCD_helper_big );
- end;
- FMTBcdFactory : TFMTBcdFactory = NIL;
- {$ifndef bigger_BCD}
- const
- NegBit = 1 SHL 7;
- SpecialBit = 1 SHL 6;
- PlacesMask = $ff XOR ( NegBit OR SpecialBit );
- {$endif}
- {$define _select := {$define _when := if {$define _when := end else if } }
- {$define _then := then begin }
- {$define _whenother := end else begin }
- {$define _endselect := end } }
- {$ifdef debug_version}
- procedure dumpBCD ( const v : tBCD );
- var
- i,
- j : Integer;
- const
- ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
- begin
- {$ifndef bigger_BCD}
- Write ( 'Prec:', v.Precision, ' ',
- 'Neg:', ft[( v.SignSpecialPlaces AND NegBit ) <> 0], ' ',
- 'Special:', ft[( v.SignSpecialPlaces AND SpecialBit ) <> 0], ' ',
- 'Places:', v.SignSpecialPlaces AND PlacesMask, ' ' );
- {$else}
- Write ( 'Prec:', v.Precision, ' ',
- 'Neg:', ft[v.Negativ], ' ',
- 'Places:', v.Places, ' ' );
- {$endif}
- j := 0;
- for i := 1 TO v.Precision do
- if Odd ( i )
- then Write ( ( v.Fraction[j] AND $f0 ) SHR 4 )
- else begin
- Write ( v.Fraction[j] AND $0f );
- Inc ( j );
- end;
- WriteLn;
- end;
- procedure dumpbh ( const v : tBCD_helper );
- var
- i : Integer;
- const
- ft : ARRAY [ Boolean ] of Char = ( 'f', 't' );
- begin
- Write ( 'Prec:', v.Prec, ' ',
- 'Neg:', ft[v.Neg], ' ',
- 'Places:', v.Plac, ' ',
- 'FDig:', v.FDig, ' ',
- 'LDig:', v.LDig, ' ',
- 'Digits:', v.LDig - v.FDig + 1, ' ' );
- for i := v.FDig TO v.LDig do
- Write ( v.Singles[i] );
- WriteLn;
- end;
- {$endif}
- {$if sizeof ( integer ) = 2 }
- {$ifdef BCDgr4 }
- var
- myMinIntBCD : tBCD;
- {$endif}
- {$else}
- {$if sizeof ( integer ) = 4 }
- {$ifdef BCDgr9 }
- var
- myMinIntBCD : tBCD;
- {$endif}
- {$else}
- {$if sizeof ( integer ) = 8 }
- {$ifdef BCDgr18 }
- var
- myMinIntBCD : tBCD;
- {$endif}
- {$else}
- {$fatal You have an interesting integer type! Sorry, not supported}
- {$endif}
- {$endif}
- {$endif}
- procedure not_implemented;
- begin
- RAISE eBCDNotImplementedException.create ( 'not implemented' );
- end;
- procedure unpack_BCD ( const BCD : tBCD;
- var bh : tBCD_helper );
- var
- i : {$ifopt r+} __lo_bh + 1 ..__hi_bh {$else} Integer {$endif};
- j : {$ifopt r+} -1..__high_fraction {$else} Integer {$endif};
- vv : {$ifopt r+} $00..$99 {$else} Integer {$endif};
- begin
- bh := null_.bh;
- WITH bh,
- BCD do
- begin
- Prec := Precision;
- if Prec > 0
- then begin
- {$ifndef bigger_BCD}
- Plac := SignSpecialPlaces AND PlacesMask;
- Neg := ( SignSpecialPlaces AND NegBit ) <> 0;
- {$else}
- Plac := Places;
- Neg := Negativ;
- {$endif}
- LDig := Plac;
- FDig := LDig - Prec + 1;
- j := -1;
- i := FDig;
- while i <= LDig do
- begin
- Inc ( j );
- vv := Fraction[j];
- Singles[i] := ( vv {AND $f0} ) SHR 4;
- if i < LDig
- then Singles[i+1] := vv AND $0f;
- Inc ( i, 2 );
- end;
- end;
- end;
- end;
- function pack_BCD ( var bh : tBCD_helper;
- var BCD : tBCD ) : Boolean;
- { return TRUE if successful (BCD valid) }
- var
- pre : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
- fra : {$ifopt r+} -1 * ( __hi_bh - __lo_bh + 1 )..__hi_bh - __lo_bh + 1 {$else} Integer {$endif};
- tm : {$ifopt r+} 0..__hi_bh - __lo_bh + 1 - Pred ( MaxFmtBCDFractionSize ) {$else} Integer {$endif};
- i : {$ifopt r+} low ( bh.FDig ) - 1..high ( bh.LDig ) {$else} Integer {$endif};
- rp : {$ifopt r+} low ( BCD.Fraction )..high ( BCD.Fraction ) + 1 {$else} Integer {$endif};
- ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
- v : {$ifopt r+} 0..10 {$else} Integer {$endif};
- lnz : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
- doround,
- lnzf : Boolean;
- begin
- pack_BCD := False;
- BCD := NullBCD;
- WITH BCD,
- bh do
- begin
- lnzf := FDig < 0;
- while lnzf do
- if Singles[FDig] = 0
- then begin
- Inc ( FDig );
- if FDig = 0
- then lnzf := False;
- end
- else lnzf := False;
- pre := LDig - FDig + 1;
- fra := Plac;
- doround := False;
- if fra >= MaxFmtBCDFractionSize
- then begin
- doround := True;
- tm := fra - Pred ( MaxFmtBCDFractionSize );
- { dec ( pre, tm ); Dec/Inc error? }
- pre := pre - tm;
- { Dec ( fra, tm ); Dec/Inc error? }
- fra := fra - tm;
- { Dec ( LDig, tm ); Dec/Inc error? }
- LDig := LDig - tm;
- end;
- if pre > MaxFmtBCDFractionSize
- then begin
- doround := True;
- tm := pre - MaxFmtBCDFractionSize;
- { Dec ( pre, tm ); Dec/Inc error? }
- pre := pre - tm;
- { Dec ( fra, tm ); Dec/Inc error? }
- fra := fra - tm;
- { Dec ( LDig, tm ); Dec/Inc error? }
- LDig := LDig - tm;
- end;
- if fra < 0
- then EXIT;
- if doround
- then begin
- v := Singles[fra + 1];
- if v > 4
- then begin
- ue := 1;
- i := LDig;
- while ( i >= FDig ) AND ( ue <> 0 ) do
- begin
- v := Singles[i] + ue;
- ue := v DIV 10;
- Singles[i] := v MOD 10;
- Dec ( i );
- end;
- if ue <> 0
- then begin
- Dec ( FDig );
- Singles[FDig] := ue;
- Dec ( LDig );
- Dec ( fra );
- if fra < 0
- then EXIT;
- end;
- end;
- end;
- lnzf := False;
- i := LDig;
- while ( i >= FDig ) AND ( NOT lnzf ) do
- begin
- if Singles[i] <> 0
- then begin
- lnz := i;
- lnzf := True;
- end;
- Dec ( i );
- end;
- if lnzf
- then begin
- tm := LDig - lnz;
- if tm <> 0
- then begin
- { Dec ( pre, tm ); Dec/Inc error? }
- pre := pre - tm;
- { Dec ( fra, tm ); Dec/Inc error? }
- fra := fra - tm;
- { Dec ( LDig, tm ); Dec/Inc error? }
- LDig := LDig - tm;
- if fra < 0
- then begin
- { Dec ( pre, fra ); Dec/Inc error? }
- pre := pre - fra;
- { Dec ( LDig, fra ); Dec/Inc error? }
- LDig := LDig - fra;
- fra := 0;
- end;
- end;
- end
- else begin
- LDig := FDig;
- fra := 0;
- pre := 0;
- Neg := False;
- end;
- if pre <> 0
- then begin
- Precision := pre;
- rp := 0;
- i := FDig;
- while i <= LDig do
- begin
- if i < LDig
- then Fraction[rp] := ( Singles[i] SHL 4 ) OR Singles[i + 1]
- else Fraction[rp] := Singles[i] SHL 4;
- Inc ( rp );
- Inc ( i, 2 );
- end;
- {$ifndef bigger_BCD}
- if Neg
- then SignSpecialPlaces := NegBit;
- SignSpecialPlaces := SignSpecialPlaces OR fra;
- {$else}
- Negativ := Neg;
- Places := fra;
- {$endif}
- end;
- end;
- pack_BCD := True;
- end;
- procedure SetDecimals ( var dp,
- dc : Char );
- begin
- case DecimalPoint of
- DecimalPoint_is_Point: begin
- dp := '.';
- dc := ',';
- end;
- DecimalPoint_is_Comma: begin
- dp := ',';
- dc := '.';
- end;
- { find out language-specific ? }
- DecimalPoint_is_System: begin
- dp := DecimalSeparator;
- dc := ThousandSeparator;
- end;
- end;
- end;
- function BCDPrecision ( const BCD : tBCD ) : Word; Inline;
- begin
- BCDPrecision := BCD.Precision;
- end;
- function BCDScale ( const BCD : tBCD ) : Word; Inline;
- begin
- {$ifndef bigger_BCD}
- BCDScale := BCD.SignSpecialPlaces AND PlacesMask;
- {$else}
- BCDScale := BCD.Places;
- {$endif}
- end;
- function IsBCDNegative ( const BCD : tBCD ) : Boolean; Inline;
- begin
- {$ifndef bigger_BCD}
- IsBCDNegative := ( BCD.SignSpecialPlaces AND NegBit ) <> 0;
- {$else}
- IsBCDNegative := BCD.Negativ;
- {$endif}
- end;
- { BCD Arithmetic}
- procedure BCDNegate ( var BCD : tBCD ); Inline;
- begin
- { with-statement geht nicht !!
- with bcd do
- if precision <> 0
- then signspecialplaces := signspecialplaces xor negbit;
- }
- if BCD.Precision <> 0
- then
- {$ifndef bigger_BCD}
- BCD.SignSpecialPlaces := BCD.SignSpecialPlaces XOR NegBit;
- {$else}
- BCD.Negativ := NOT BCD.Negativ;
- {$endif}
- end;
- { returns -1 if BCD1 < BCD2, 0 if BCD1 = BCD2, 1 if BCD1 > BCD2 }
- function BCDCompare ( const BCD1,
- BCD2 : tBCD ) : Integer;
- var
- pl1 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
- pl2 : {$ifopt r+} 0..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
- pr1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
- pr2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
- pr : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
- idig1 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
- idig2 : {$ifopt r+} 0..maxfmtbcdfractionsize {$else} Integer {$endif};
- i : {$ifopt r+} __low_Fraction..__high_Fraction + 1 {$else} Integer {$endif};
- f1 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
- f2 : {$ifopt r+} $00..$99 {$else} Integer {$endif};
- res : {$ifopt r+} -1..1 {$else} Integer {$endif};
- neg1,
- neg2 : Boolean;
- begin
- {$ifndef bigger_BCD}
- neg1 := ( BCD1.SignSpecialPlaces AND NegBit ) <> 0;
- neg2 := ( BCD2.SignSpecialPlaces AND NegBit ) <> 0;
- {$else}
- neg1 := BCD1.Negativ;
- neg2 := BCD2.Negativ;
- {$endif}
- _SELECT
- _WHEN neg1 AND ( NOT neg2 )
- _THEN result := -1;
- _WHEN ( NOT neg1 ) AND neg2
- _THEN result := +1;
- _WHENOTHER
- pr1 := BCD1.Precision;
- pr2 := BCD2.Precision;
- {$ifndef bigger_BCD}
- pl1 := BCD1.SignSpecialPlaces AND PlacesMask;
- pl2 := BCD2.SignSpecialPlaces AND PlacesMask;
- {$else}
- pl1 := BCD1.Places;
- pl2 := BCD2.Places;
- {$endif}
- idig1 := pr1 - pl1;
- idig2 := pr2 - pl2;
- if idig1 <> idig2
- then begin
- if ( idig1 > idig2 ) = neg1
- then result := -1
- else result := +1;
- end
- else begin
- if pr1 < pr2
- then pr := pr1
- else pr := pr2;
- res := 0;
- i := __low_Fraction;
- while ( res = 0 ) AND ( i < ( __low_Fraction + ( pr DIV 2 ) ) ) do
- begin
- {
- if BCD1.Fraction[i] < BCD2.Fraction[i]
- then res := -1
- else
- if BCD1.Fraction[i] > BCD2.Fraction[i]
- then res := +1;
- }
- _SELECT
- _WHEN BCD1.Fraction[i] < BCD2.Fraction[i]
- _THEN res := -1
- _WHEN BCD1.Fraction[i] > BCD2.Fraction[i]
- _THEN res := +1;
- _WHENOTHER
- _endSELECT;
- Inc ( i );
- end;
- if res = 0
- then begin
- if Odd ( pr )
- then begin
- f1 := BCD1.Fraction[i] AND $f0;
- f2 := BCD2.Fraction[i] AND $f0;
- {
- if f1 < f2
- then res := -1
- else
- if f1 > f2
- then res := +1;
- }
- _SELECT
- _WHEN f1 < f2
- _THEN res := -1
- _WHEN f1 > f2
- _THEN res := +1;
- _endSELECT;
- end;
- end;
- if neg1
- then result := 0 - res
- else result := res;
- end;
- _endSELECT
- end;
- { Convert string/Double/Integer to BCD struct }
- function TryStrToBCD ( const aValue : FmtBCDStringtype;
- var BCD : tBCD ) : Boolean;
- { shall this return TRUE when error and FALSE when o.k. or the other way round ? }
- var
- {$ifndef use_ansistring}
- lav : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- i : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- {$else}
- lav : {$ifopt r+} longword {$else} longword {$endif};
- i : {$ifopt r+} longword {$else} longword {$endif};
- {$endif}
- ch : Char;
- dp,
- dc : Char;
- type
- ife = ( inint, infrac, inexp );
- {$define max_exp_scanned := 9999 }
- var
- inife : ife;
- lvars : record
- fp,
- lp : ARRAY [ ife ]
- {$ifndef use_ansistring}
- of {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- pfnb : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- ps : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- pse : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- errp : {$ifopt r+} 0..high ( aValue ) {$else} Integer {$endif};
- {$else}
- of {$ifopt r+} longword {$else} longword {$endif};
- pfnb : {$ifopt r+} longword {$else} longword {$endif};
- ps : {$ifopt r+} longword {$else} longword {$endif};
- pse : {$ifopt r+} longword {$else} longword {$endif};
- errp : {$ifopt r+} longword {$else} longword {$endif};
- {$endif}
- exp : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
- p : {$ifopt r+} -max_exp_scanned..max_exp_scanned {$else} Integer {$endif};
- bh : tBCD_helper;
- nbf : Boolean;
- end;
- begin
- result := False;
- FillChar ( lvars, SizeOf ( lvars ), #0 );
- BCD := NullBCD;
- lav := Length ( aValue );
- if lav <> 0
- then
- WITH lvars,
- bh do
- begin
- SetDecimals ( dp, dc );
- while ( pfnb < lav ) AND ( NOT nbf ) do
- begin
- Inc ( pfnb );
- nbf := aValue[pfnb] <> ' ';
- end;
- if nbf
- then begin
- if aValue[pfnb] IN [ '+', '-' ]
- then begin
- ps := pfnb;
- Inc ( pfnb );
- end;
- inife := low ( inife );
- for i := pfnb TO lav do
- begin
- ch := aValue[i];
- case ch of
- '0'..'9': begin
- case inife of
- inint,
- inexp: if fp[inife] = 0
- then begin
- if ch <> '0'
- then begin
- fp[inife] := i;
- lp[inife] := i;
- end;
- end
- else lp[inife] := i;
- infrac: begin
- if fp[infrac] = 0
- then fp[infrac] := i;
- if ch <> '0'
- then lp[infrac] := i;
- end;
- end;
- end;
- ',',
- '.': if ch = dp
- then begin
- if inife <> inint
- then result := True
- else inife := infrac;
- end;
- 'e',
- 'E': if inife = inexp
- then result := True
- else inife := inexp;
- '+',
- '-': if ( inife = inexp ) AND ( fp[inexp] = 0 )
- then pse := i
- else result := True;
- else begin
- result := True;
- errp := i;
- end;
- end;
- end;
- if result
- then begin
- result := False;
- for i := errp TO lav do
- if aValue[i] <> ' '
- then result := True;
- end;
- if result
- then EXIT;
- if ps <> 0
- then Neg := aValue[ps] = '-';
- if lp[infrac] = 0
- then fp[infrac] := 0;
- if fp[inexp] <> 0
- then begin
- exp := 0;
- for i := fp[inexp] TO lp[inexp] do
- if NOT result
- then
- if aValue[i] <> dc
- then begin
- exp := exp * 10 + ( Ord ( aValue[i] ) - Ord ( '0' ) );
- if exp > 999
- then result := True;
- end;
- if result
- then EXIT;
- if pse <> 0
- then
- if aValue[pse] = '-'
- then exp := -exp;
- end;
- p := -exp;
- if fp[infrac] <> 0
- then begin
- for i := fp[infrac] TO lp[infrac] do
- if aValue[i] <> dc
- then begin
- if p < ( MaxFmtBCDFractionSize + 2 )
- then begin
- Inc ( p );
- Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
- end;
- end;
- end;
- LDig := p;
- p := 1 - exp;
- if fp[inint] <> 0
- then
- for i := lp[inint] DOWNTO fp[inint] do
- if aValue[i] <> dc
- then begin
- if p > - ( MaxFmtBCDFractionSize + 2 )
- then begin
- Dec ( p );
- Singles[p] := Ord ( aValue[i] ) - Ord ( '0' );
- end
- else result := True;
- end;
- if result
- then EXIT;
- FDig := p;
- if LDig < 0
- then LDig := 0;
- Plac := LDig;
- result := NOT pack_BCD ( bh, BCD );
- end;
- end;
- end;
- function StrToBCD ( const aValue : FmtBCDStringtype ) : tBCD;
- var
- BCD : tBCD;
- begin
- if TryStrToBCD ( aValue, BCD )
- then begin
- RAISE eBCDOverflowException.create ( 'in StrToBCD' );
- end
- else StrToBCD := BCD;
- end;
- {$ifndef FPUNONE}
- procedure DoubleToBCD ( const aValue : myRealtype;
- var BCD : tBCD );
- var
- s : string [ 30 ];
- dp : tDecimalPoint;
- begin
- Str ( aValue : 25, s );
- dp := DecimalPoint;
- DecimalPoint := DecimalPoint_is_Point;
- BCD := StrToBCD ( s );
- DecimalPoint := dp;
- end;
- function DoubleToBCD ( const aValue : myRealtype ) : tBCD; Inline;
- begin
- DoubleToBCD ( aValue, result );
- end;
- {$endif}
- function IntegerToBCD ( const aValue : myInttype ) : tBCD;
- var
- bh : tBCD_helper;
- v : {$ifopt r+} 0..high ( myInttype ) {$else} Integer {$endif};
- p : {$ifopt r+} low ( bh.Singles ) - 1..0 {$else} Integer {$endif};
- exitloop : Boolean;
- begin
- _SELECT
- _WHEN aValue = 0
- _THEN result := NullBCD;
- _WHEN aValue = 1
- _THEN result := OneBCD;
- _WHEN aValue = low ( myInttype )
- _THEN
- {$if declared ( myMinIntBCD ) }
- result := myMinIntBCD;
- {$else}
- RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
- {$endif}
- _WHENOTHER
- bh := null_.bh;
- WITH bh do
- begin
- Neg := aValue < 0;
- if Neg
- then v := -aValue
- else v := +aValue;
- LDig := 0;
- p := 0;
- REPEAT
- Singles[p] := v MOD 10;
- v := v DIV 10;
- exitloop := v = 0;
- Dec ( p );
- if p < low ( Singles )
- then begin
- exitloop := True;
- (* what to do if error occured? *)
- RAISE eBCDOverflowException.create ( 'in IntegerToBCD' );
- end;
- UNTIL exitloop;
- FDig := p + 1;
- end;
- pack_BCD ( bh, result );
- _endSELECT;
- end;
- {$warnings off}
- function VarToBCD ( const aValue : Variant ) : tBCD;
- begin
- not_implemented;
- end;
- {$warnings on}
- function CurrToBCD ( const Curr : currency;
- var BCD : tBCD;
- Precision : Integer = 32;
- Decimals : Integer = 4 ) : Boolean;
- {
- this works under the assumption that a currency is an int64,
- except for scale of 10000
- }
- var
- i : int64 absolute Curr;
- begin
- BCD := IntegerToBCD ( i );
- {$ifndef bigger_BCD}
- BCD.SignSpecialPlaces := 4 OR ( BCD.SignSpecialPlaces AND NegBit );
- {$else}
- BCD.Places := 4;
- {$endif}
- if Decimals <> 4 then
- Result := NormalizeBCD ( BCD, BCD, Precision, Decimals )
- else
- CurrToBCD := True;
- end;
- {$ifdef comproutines}
- function CompToBCD ( const Curr : Comp ) : tBCD; Inline;
- var
- cc : int64 absolute Curr;
- begin
- result := IntegerToBCD ( cc );
- end;
- function BCDToComp ( const BCD : tBCD ) : Comp; Inline;
- var
- zz : record
- case Boolean of
- False: ( i : int64 );
- True: ( c : Comp );
- end;
- begin
- zz.i := BCDToInteger ( BCD );
- BCDToComp := zz.c;
- end;
- {$endif}
- { Convert BCD struct to string/Double/Integer }
- function BCDToStr ( const BCD : tBCD ) : FmtBCDStringtype;
- var
- bh : tBCD_helper;
- l : {$ifopt r+} 0..maxfmtbcdfractionsize + 1 + 1 {$else} Integer {$endif};
- i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
- pp : {$ifopt r+} low ( bh.FDig ) - 1..1 {$else} Integer {$endif};
- begin
- {$ifdef use_ansistring}
- result := '';
- {$endif}
- unpack_BCD ( BCD, bh );
- WITH bh do
- begin
- l := 0;
- if Neg
- then begin
- {$ifndef use_ansistring}
- Inc ( l );
- result[1] := '-';
- {$else}
- result := result + '-';
- {$endif}
- end;
- if Prec = Plac
- then begin
- {$ifndef use_ansistring}
- Inc ( l );
- result[1] := '0';
- {$else}
- result := result + '0';
- {$endif}
- end;
- if Prec > 0
- then begin
- pp := low ( bh.FDig ) - 1;
- if Plac > 0
- then pp := 1;
- for i := FDig TO LDig do
- begin
- if i = pp
- then begin
- {$ifndef use_ansistring}
- Inc ( l );
- result[l] := '.';
- {$else}
- result := result + '.';
- {$endif}
- end;
- {$ifndef use_ansistring}
- Inc ( l );
- result[l] := Chr ( Singles[i] + Ord ( '0' ) );
- {$else}
- result := result + Chr ( Singles[i] + Ord ( '0' ) );
- {$endif}
- end;
- end;
- end;
- {$ifndef use_ansistring}
- result[0] := Chr ( l );
- {$endif}
- end;
- {$ifndef FPUNONE}
- function BCDToDouble ( const BCD : tBCD ) : myRealtype;
- var
- bh : tBCD_helper;
- i : {$ifopt r+} low ( bh.FDig )..high ( bh.LDig ) {$else} Integer {$endif};
- r,
- e : myRealtype;
- begin
- unpack_BCD ( BCD, bh );
- WITH bh do
- begin
- r := 0;
- e := 1;
- for i := 0 DOWNTO FDig do
- begin
- r := r + Singles[i] * e;
- e := e * 10;
- end;
- e := 1;
- for i := 1 TO LDig do
- begin
- e := e / 10;
- r := r + Singles[i] * e;
- end;
- if Neg
- then BCDToDouble := -r
- else BCDToDouble := +r;
- end;
- end;
- {$endif}
- function BCDToInteger ( const BCD : tBCD;
- Truncate : Boolean = False ) : myInttype;
- var
- bh : tBCD_helper;
- res : myInttype;
- i : {$ifopt r+} low ( bh.FDig )..0 {$else} Integer {$endif};
- {
- unclear: behaviour if overflow: abort? return 0? return something?
- so: checks are missing yet
- }
- begin
- unpack_BCD ( BCD, bh );
- res := 0;
- WITH bh do
- begin
- for i := FDig TO 0 do
- res := res * 10 - Singles[i];
- if NOT Truncate
- then
- if Plac > 0
- then
- if Singles[1] > 4
- then Dec ( res );
- if Neg
- then BCDToInteger := +res
- else BCDToInteger := -res;
- end;
- end;
- { From DB.pas }
- function BCDToCurr ( const BCD : tBCD;
- var Curr : currency ) : Boolean;
- var
- bh : tBCD_helper;
- res : int64;
- c : currency absolute res;
- i : {$ifopt r+} low ( bh.FDig )..4 {$else} Integer {$endif};
- {
- unclear: behaviour if overflow: abort? return 0? return something?
- }
- begin
- BCDToCurr := True;
- unpack_BCD ( BCD, bh );
- res := 0;
- WITH bh do
- begin
- for i := FDig TO 4 do
- res := res * 10 + Singles[i];
- if Plac > 4
- then
- if Singles[5] > 4
- then Inc ( res );
- if Neg
- then Curr := -c
- else Curr := +c;
- end;
- end;
- procedure BCDAdd ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- var
- bhr,
- bh1,
- bh2 : tBCD_helper;
- ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
- i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
- v : {$ifopt r+} 0..9 + 9 + 1 {$else} Integer {$endif};
- BCD : tBCD;
- negate : Boolean;
- begin
- negate := IsBCDNegative ( BCDin1 );
- if negate <> IsBCDNegative ( BCDin2 )
- then begin
- if negate
- then begin
- BCD := BCDin1;
- BCDNegate ( BCD );
- BCDSubtract ( BCDin2, BCD, BCDout );
- EXIT;
- end;
- BCD := BCDin2;
- BCDNegate ( BCD );
- BCDSubtract ( BCDin1, BCD, BCDout );
- EXIT;
- end;
- bhr := null_.bh;
- WITH bhr do
- begin
- unpack_BCD ( BCDin1, bh1 );
- unpack_BCD ( BCDin2, bh2 );
- if bh1.FDig < bh2.FDig
- then FDig := bh1.FDig
- else FDig := bh2.FDig;
- if bh1.LDig > bh2.LDig
- then LDig := bh1.LDig
- else LDig := bh2.LDig;
- Plac := LDig;
- ue := 0;
- for i := LDig DOWNTO FDig do
- begin
- v := bh1.Singles[i] + bh2.Singles[i] + ue;
- ue := v DIV 10;
- Singles[i] := v MOD 10;
- end;
- if ue <> 0
- then begin
- Dec ( FDig );
- Singles[FDig] := ue;
- end;
- Neg := negate;
- end;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDAdd' );
- end;
- end;
- procedure BCDSubtract ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- var
- bhr,
- bh1,
- bh2 : tBCD_helper;
- cmp : {$ifopt r+} -1..1 {$else} Integer {$endif};
- ue : {$ifopt r+} 0..1 {$else} Integer {$endif};
- i : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
- v : {$ifopt r+} 0 - 9 - 1..9 - 0 - 0 {$else} Integer {$endif};
- negate : Boolean;
- BCD : tBCD;
- begin
- negate := IsBCDNegative ( BCDin1 );
- if negate <> IsBCDNegative ( BCDin2 )
- then begin
- if negate
- then begin
- BCD := BCDin1;
- BCDNegate ( BCD );
- BCDAdd ( BCDin2, BCD, BCDout );
- BCDNegate ( BCDout );
- EXIT;
- end;
- BCD := BCDin2;
- BCDNegate ( BCD );
- BCDAdd ( BCDin1, BCD, BCDout );
- EXIT;
- end;
- cmp := BCDCompare ( BCDin1, BCDin2 );
- if cmp = 0
- then begin
- BCDout := NullBCD;
- EXIT;
- end;
- bhr := null_.bh; { n n }
- WITH bhr do { > < > < }
- begin { }
- if ( cmp > 0 ) = negate { +123 +12 -12 -123 }
- then begin { - +12 - +123 - -123 - -12 }
- unpack_BCD ( BCDin1, bh2 ); { x x }
- unpack_BCD ( BCDin2, bh1 ); { s s s s }
- negate := NOT negate; { nn n nn n }
- end
- else begin
- unpack_BCD ( BCDin1, bh1 );
- unpack_BCD ( BCDin2, bh2 );
- end;
- if bh1.FDig < bh2.FDig
- then FDig := bh1.FDig
- else FDig := bh2.FDig;
- if bh1.LDig > bh2.LDig
- then LDig := bh1.LDig
- else LDig := bh2.LDig;
- Plac := LDig;
- ue := 0;
- for i := LDig DOWNTO FDig do
- begin
- v := Integer ( bh1.Singles[i] ) - bh2.Singles[i] - ue;
- ue := 0;
- if v < 0
- then begin
- ue := 1;
- Inc ( v, 10 );
- end;
- Singles[i] := v;
- end;
- Neg := negate;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- {should never occur!}
- RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
- end;
- end;
- end;
- { Returns True if successful, False if Int Digits needed to be truncated }
- function NormalizeBCD ( const InBCD : tBCD;
- var OutBCD : tBCD;
- const Prec,
- Scale : Word ) : Boolean;
- var
- bh : tBCD_helper;
- tm : {$ifopt r+} 1..maxfmtbcdfractionsize - 1 {$else} Integer {$endif};
- begin
- NormalizeBCD := True;
- {$ifopt r+}
- if ( Prec < 0 ) OR ( Prec > MaxFmtBCDFractionSize ) then RangeError;
- if ( Scale < 0 ) OR ( Prec >= MaxFmtBCDFractionSize ) then RangeError;
- {$endif}
- if BCDScale ( InBCD ) > Scale
- then begin
- unpack_BCD ( InBCD, bh );
- WITH bh do
- begin
- tm := Plac - Scale;
- Plac := Scale;
- { dec ( prec, tm ); Dec/Inc error? }
- Prec := Prec - tm;
- { dec ( ldig, tm ); Dec/Inc error? }
- LDig := LDig - tm;
- NormalizeBCD := False;
- end;
- if NOT pack_BCD ( bh, OutBCD )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDAdd' );
- end;
- end;
- end;
- procedure BCDMultiply ( const BCDin1,
- BCDin2 : tBCD;
- var BCDout : tBCD );
- var
- bh1,
- bh2,
- bhr : tBCD_helper;
- bhrr : tBCD_helper_big;
- i1 : {$ifopt r+} low ( bh1.FDig )..high ( bh1.LDig ) {$else} Integer {$endif};
- i2 : {$ifopt r+} low ( bh2.FDig )..high ( bh2.LDig ) {$else} Integer {$endif};
- i3 : {$ifopt r+} low ( bhrr.FDig )..high ( bhrr.LDig ) {$else} Integer {$endif};
- v : {$ifopt r+} low ( bhrr.Singles[0] )..high ( bhrr.Singles[0] ) {$else} Integer {$endif};
- ue : {$ifopt r+} low ( bhrr.Singles[0] ) DIV 10..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
- begin
- unpack_BCD ( BCDin1, bh1 );
- unpack_BCD ( BCDin2, bh2 );
- if ( bh1.Prec = 0 ) OR ( bh2.Prec = 0 )
- then begin
- BCDout := NullBCD;
- EXIT;
- end;
- bhr := null_.bh;
- bhrr := null_.bhb;
- WITH bhrr do
- begin
- Neg := bh1.Neg XOR bh2.Neg;
- {
- writeln ( __lo_bhb, ' ', __hi_bhb, ' ', bh1.fdig, ' ', bh2.fdig, ' ', low ( fdig ), ' ', low ( ldig ) );
- }
- FDig := bh1.FDig + bh2.FDig;
- LDig := bh1.LDig + bh2.LDig;
- for i1 := bh1.FDig TO bh1.LDig do
- for i2 := bh2.FDig TO bh2.LDig do
- begin
- Inc ( Singles[i1 + i2],
- bh1.Singles[i1]
- * bh2.Singles[i2] );
- {
- write ( Singles[i1 + i2], ' ', bh1.Singles[i1], ' ', bh2.Singles[i2], ' : ' );
- writeln ( Singles[i1 + i2] + bh1.Singles[i1] + bh2.Singles[i2] );
- }
- {
- Singles[i1 + i2] := Singles[i1 + i2]
- + bh1.Singles[i1]
- * bh2.Singles[i2];
- }
- end;
- {
- for i3 := fdig to ldig do
- write ( ' ', singles[i3] );
- writeln;
- }
- if FDig < low ( bhr.Singles )
- then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
- ue := 0;
- for i3 := LDig DOWNTO FDig do
- begin
- v := Singles[i3] + ue;
- ue := v DIV 10;
- v := v MOD 10;
- bhr.Singles[i3] := v;
- end;
- while ue <> 0 do
- begin
- Dec ( FDig );
- if FDig < low ( bhr.Singles )
- then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
- bhr.Singles[FDig] := ue MOD 10;
- ue := ue DIV 10;
- end;
- bhr.Plac := LDig;
- bhr.FDig := FDig;
- if LDig > high ( bhr.Singles )
- then bhr.LDig := high ( bhr.Singles )
- else bhr.LDig := LDig;
- end;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
- end;
- end;
- {$ifndef FPUNONE}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
- end;
- {$endif}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( BCDIn, StrToBCD ( StringIn ), BCDout );
- end;
- procedure BCDMultiply ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
- end;
- procedure BCDDivide ( const Dividend,
- Divisor : tBCD;
- var BCDout : tBCD );
- var
- bh1 : ARRAY [ Boolean ] of tBCD_helper;
- bh2,
- bh : tBCD_helper;
- p : {$ifopt r+} low ( bh.FDig ) - high ( bh.FDig )..high ( bh.FDig ) - low ( bh.FDig ) {$else} Integer {$endif};
- v1 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
- v2 : {$ifopt r+} low ( bh.Singles[0] )..high ( bh.Singles[0] ) {$else} Integer {$endif};
- lFDig : {$ifopt r+} low ( bh.FDig )..high ( bh.FDig ) {$else} Integer {$endif};
- d1 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
- d2 : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
- d : {$ifopt r+} low ( bh.LDig ) - high ( bh.FDig )..high ( bh.LDig ) - low ( bh.FDig ) {$else} Integer {$endif};
- lLdig : {$ifopt r+} low ( lFDig ) + low ( d )..high ( lFDig ) + high ( d ) {$else} Integer {$endif};
- tm : {$ifopt r+} low ( lLdig ) - high ( bh2.Singles )..high ( lLdig ) - high ( bh2.Singles ) {$else} Integer {$endif};
- i2 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
- i3 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
- ie : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
- i4 : {$ifopt r+} low ( lFDig )..high ( lLdig ) {$else} Integer {$endif};
- nFDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
- nLDig : {$ifopt r+} low ( i2 )..high ( i2 ) {$else} Integer {$endif};
- dd : {$ifopt r+} 0..9 {$else} Integer {$endif};
- Add : {$ifopt r+} 0..99 {$else} Integer {$endif};
- ue : {$ifopt r+} 0..99 {$else} Integer {$endif};
- 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};
- v4 : {$ifopt r+} low ( bh.Singles[0] ) + low ( add )..high ( bh.Singles[0] ) + high ( add ) {$else} Integer {$endif};
- FlipFlop,
- nz,
- sf,
- sh,
- fdset : Boolean;
- {
- bh1p : ARRAY [ Boolean ] of ^ tBCD_helper;
- }
- begin
- { test:
- bh1p[false] := @ bh1[false];
- bh1p[true] := @ bh1[true];
- v := bh1[false].singles[0];
- v := bh1[true].singles[0];
- v := bh1p[false]^.singles[0];
- v := bh1p[true]^.singles[0];
- v := bh1[nz].singles[0];
- v := bh1p[nz]^.singles[0];
- }
- unpack_BCD ( Divisor, bh2 );
- unpack_BCD ( Dividend, bh1[False] );
- p := bh1[False].FDig - bh2.FDig;
- _SELECT
- _WHEN bh2.Prec = 0
- _THEN RAISE eBCDException.create ( 'Division by zero' );
- _WHEN bh1[False].Prec = 0
- _THEN BCDout := NullBCD;
- _WHEN p < low ( bh2.Singles )
- _THEN RAISE eBCDOverflowException.create ( 'in BCDDivide' );
- _WHENOTHER
- bh := null_.bh;
- bh.Neg := bh1[False].Neg XOR bh2.Neg;
- if p <= high ( bh.Singles )
- then begin
- bh1[True] := null_.bh;
- FlipFlop := False;
- fdset := p > 0;
- if fdset
- then bh.FDig := 0;
- add := 0;
- nz := True;
- while nz do
- WITH bh1[FlipFlop] do
- begin
- {
- WriteLn('#####');
- dumpbh ( bh1[flipflop] );
- dumpbh ( bh2 );
- dumpbh ( bh );
- }
- if ( Singles[FDig] + bh2.Singles[bh2.FDig] ) = 0
- then begin
- if ( FDig >= LDig )
- OR ( bh2.FDig >= bh2.LDig )
- then nz := False
- else begin
- Inc ( FDig );
- Inc ( bh2.FDig );
- end;
- end
- else begin
- v1 := Singles[FDig];
- v2 := bh2.Singles[bh2.FDig];
- sh := v1 < v2;
- if ( v1 = v2 )
- then begin
- nz := False;
- i3 := Succ ( FDig );
- ie := LDig;
- while ( i3 <= ie ) AND ( NOT nz ) AND ( NOT sh ) do
- begin
- v1 := Singles[i3];
- v2 := bh2.Singles[i3 - p];
- if v1 <> v2
- then begin
- nz := True;
- if v1 < v2
- then sh := True;
- end;
- Inc ( i3 );
- end;
- end;
- if NOT nz
- then Add := 1
- else begin
- if sh
- then begin
- Inc ( p );
- {
- if p > 3 then halt;
- }
- if p > high ( bh.Singles )
- then nz := False
- else Dec ( bh2.FDig );
- end
- else begin
- lFDig := FDig;
- d1 := LDig - FDig;
- d2 := bh2.LDig - bh2.FDig;
- if d1 > d2
- then d := d1
- else d := d2;
- lLdig := lFDig + d;
- if lLdig > high ( bh2.Singles )
- then begin
- tm := ( lLdig ) - high ( bh2.Singles );
- d := d - tm;
- lLdig := lLdig - tm;
- {runden?}
- end;
- sf := True;
- Add := 0;
- nFDig := 0;
- nLDig := 0;
- ue := 0;
- dd := Singles[lFDig] DIV ( bh2.Singles[lFDig - p] + 1 );
- {
- dd := 1;
- }
- if dd < 1
- then dd := 1;
- {
- writeln ( 'p=', p, ' dd=', dd, ' lFdig=', lfdig, ' lldig=', lldig );
- }
- for i2 := lLdig DOWNTO lFDig do
- begin
- v3 := Singles[i2] - bh2.Singles[i2 - p] * dd - ue;
- ue := 0;
- while v3 < 0 do
- begin
- Inc ( ue );;
- v3 := v3 + 10;
- end;
- {
- if v3 <> 0
- then begin
- }
- bh1[NOT FlipFlop].Singles[i2] := v3;
- {
- nFDig := i2;
- if sf
- then begin
- nLDig := i2;
- sf := False;
- end;
- end;
- }
- end;
- sf := False;
- nfdig := lfdig;
- nldig := lldig;
- Inc ( Add, dd );
- if NOT fdset
- then begin
- bh.FDig := p;
- fdset := True;
- end;
- if bh.LDig < p
- then begin
- bh.LDig := p;
- if ( bh.LDig - bh.FDig ) > Succ ( MaxFmtBCDFractionSize )
- then nz := False;
- end;
- if sf
- then nz := False
- else begin
- FillChar ( bh1[FlipFlop], SizeOf ( bh1[FlipFlop] ), #0 );
- FlipFlop := NOT FlipFlop;
- WITH bh1[FlipFlop] do
- begin
- FDig := nFDig;
- LDig := nLDig;
- end;
- end;
- end;
- end;
- if Add <> 0
- then begin
- i4 := p;
- while ( Add <> 0 ) AND ( i4 >= bh.FDig ) do
- begin
- {
- writeln ( '> ', i4, ' ', bh.Singles[i4], ' ', Add );
- }
- v4 := bh.Singles[i4] + Add;
- Add := v4 DIV 10;
- bh.Singles[i4] := v4 MOD 10;
- Dec ( i4 );
- end;
- if Add <> 0
- then begin
- Dec ( bh.FDig );
- bh.Singles[bh.FDig] := Add;
- Add := 0;
- end;
- end;
- end;
- end;
- end;
- WITH bh do
- begin
- if LDig < 0
- then LDig := 0;
- if LDig > 0
- then Plac := LDig
- else Plac := 0;
- end;
- if NOT pack_BCD ( bh, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDDivide' );
- end;
- _endSELECT
- end;
- procedure BCDDivide ( const Dividend,
- Divisor : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( StrToBCD ( Dividend ), StrToBCD ( Divisor ), BCDout );
- end;
- {$ifndef FPUNONE}
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : myRealtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( Dividend, DoubleToBCD ( Divisor ), BCDout );
- end;
- {$endif}
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( Dividend, StrToBCD ( Divisor ), BCDout );
- end;
- { TBCD variant creation utils }
- procedure VarFmtBCDCreate ( var aDest : Variant;
- const aBCD : tBCD );
- begin
- VarClear(aDest);
- TVarData(aDest).Vtype:=FMTBcdFactory.Vartype;
- TVarData(aDest).VPointer:=TFMTBcdVarData.create(aBCD);
- end;
- function VarFmtBCDCreate : Variant;
- begin
- VarFmtBCDCreate ( result, NullBCD );
- end;
- function VarFmtBCDCreate ( const aValue : FmtBCDStringtype;
- Precision,
- Scale : Word ) : Variant;
- begin
- VarFmtBCDCreate ( result, StrToBCD ( aValue ) );
- end;
- {$ifndef FPUNONE}
- function VarFmtBCDCreate ( const aValue : myRealtype;
- Precision : Word = 18;
- Scale : Word = 4 ) : Variant;
- begin
- VarFmtBCDCreate ( result, DoubleToBCD ( aValue ) );
- end;
- {$endif}
- function VarFmtBCDCreate ( const aBCD : tBCD ) : Variant;
- begin
- VarFmtBCDCreate ( result, aBCD );
- end;
- function VarIsFmtBCD ( const aValue : Variant ) : Boolean;
- begin
- result:=false;
- not_implemented;
- end;
- function VarFmtBCD : TVartype;
- begin
- result:=0;
- not_implemented;
- end;
- { Formatting BCD as string }
- function BCDToStrF ( const BCD : tBCD;
- Format : TFloatFormat;
- const Precision,
- Digits : Integer ) : FmtBCDStringtype;
- begin
- not_implemented;
- result:='';
- end;
- function FormatBCD ( const Format : string;
- BCD : tBCD ) : FmtBCDStringtype;
- begin
- not_implemented;
- result:='';
- end;
- {$ifdef additional_routines}
- function CurrToBCD ( const Curr : currency ) : tBCD; Inline;
- begin
- CurrToBCD ( Curr, result );
- end;
- procedure BCDAdd ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- var
- BCD : tBCD;
- bhr : tBCD_helper;
- p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
- ue : {$ifopt r+} 0..high ( IntIn ) - 9 {$else} Integer {$endif};
- v : {$ifopt r+} 0..{high ( ue ) + 9} high ( IntIn ) {$else} Integer {$endif};
- nz : Boolean;
- begin
- if IntIn = 0
- then begin
- BCDout := BCDIn;
- EXIT;
- end;
- if IntIn = low ( myInttype )
- then begin
- {$if declared ( myMinIntBCD ) }
- BCDAdd ( BCDIn, myMinIntBCD, BCDout );
- EXIT;
- {$else}
- RAISE eBCDOverflowException.create ( 'in BCDAdd' );
- {$endif}
- end;
- if IsBCDNegative ( BCDIn )
- then begin
- BCD := BCDIn;
- BCDNegate ( BCD );
- if IntIn < 0
- then BCDAdd ( BCD, -IntIn, BCDout )
- else BCDSubtract ( BCD, IntIn, BCDout );
- BCDNegate ( BCDout );
- EXIT;
- end;
- if IntIn < 0
- then begin
- BCDSubtract ( BCDIn, -IntIn, BCDout );
- EXIT;
- end;
- if IntIn > ( high ( IntIn ) - 9 )
- then begin
- BCDAdd ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
- EXIT;
- end;
- unpack_BCD ( BCDIn, bhr );
- p := 0;
- nz := True;
- ue := IntIn;
- while nz do
- begin
- v := bhr.Singles[p] + ue;
- bhr.Singles[p] := v MOD 10;
- ue := v DIV 10;
- if ue = 0
- then nz := False
- else Dec ( p );
- end;
- if p < bhr.FDig
- then begin
- bhr.FDig := p;
- bhr.Prec := bhr.Prec + ( bhr.FDig - p );
- end;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDAdd' );
- end;
- end;
- procedure BCDSubtract ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- {}
- var
- BCD : tBCD;
- bhr : tBCD_helper;
- p : {$ifopt r+} low ( bhr.FDig ) - 1..0 {$else} Integer {$endif};
- ue : {$ifopt r+} 0..pred ( 100000000 ) {$else} Integer {$endif};
- v : {$ifopt r+} -9..9 {$else} Integer {$endif};
- direct : Boolean;
- {}
- begin
- if IntIn = 0
- then begin
- BCDout := BCDIn;
- EXIT;
- end;
- if IntIn = low ( myInttype )
- then begin
- {$if declared ( myMinIntBCD ) }
- BCDSubtract ( BCDIn, myMinIntBCD, BCDout );
- EXIT;
- {$else}
- RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
- {$endif}
- end;
- if IsBCDNegative ( BCDIn )
- then begin
- BCD := BCDIn;
- BCDNegate ( BCD );
- if IntIn < 0
- then BCDSubtract ( BCD, -IntIn, BCDout )
- else BCDAdd ( BCD, IntIn, BCDout );
- BCDNegate ( BCDout );
- EXIT;
- end;
- if IntIn < 0
- then begin
- BCDAdd ( BCDIn, -IntIn, BCDout );
- EXIT;
- end;
- direct := False;
- case BCDIn.Precision
- -
- {$ifndef bigger_BCD}
- ( BCDIn.SignSpecialPlaces AND PlacesMask )
- {$else}
- BCDIn.Places
- {$endif}
- of
- 2: direct := IntIn < 10;
- 3: direct := IntIn < 100;
- 4: direct := IntIn < 1000;
- 5: direct := IntIn < 10000;
- 6: direct := IntIn < 100000;
- 7: direct := IntIn < 1000000;
- 8: direct := IntIn < 10000000;
- 9: direct := IntIn < 100000000;
- end;
- {
- write(direct);dumpbcd(bcdin);write('[',intin,']');
- }
- if direct
- then begin
- unpack_BCD ( BCDIn, bhr );
- WITH bhr do
- begin
- p := 0;
- ue := IntIn;
- while p >= FDig do
- begin
- v := Singles[p] - ue MOD 10;
- ue := ue DIV 10;
- if v < 0
- then begin
- v := v + 10;
- ue := ue + 1;
- end;
- Singles[p] := v;
- Dec ( p );
- end;
- end;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDSubtract' );
- end;
- end
- else
- {}
- BCDSubtract ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
- end;
- procedure BCDAdd ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( BCDIn, IntIn, BCDout );
- end;
- {$ifndef FPUNONE}
- procedure BCDAdd ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
- end;
- procedure BCDAdd ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDAdd ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( BCDIn, CurrToBCD ( Currin ), BCDout );
- end;
- procedure BCDAdd ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( CurrToBCD ( Currin ), BCDIn, BCDout );
- end;
- {$ifdef comproutines}
- procedure BCDAdd ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( BCDIn, CompToBCD ( Compin ), BCDout );
- end;
- procedure BCDAdd ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( CompToBCD ( Compin ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDAdd ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( BCDIn, StrToBCD ( StringIn ), BCDout );
- end;
- procedure BCDAdd ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( StrToBCD ( StringIn ), BCDIn, BCDout );
- end;
- procedure BCDAdd ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDAdd ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
- end;
- procedure BCDSubtract ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( BCDIn, IntIn, BCDout );
- BCDNegate ( BCDout );
- end;
- {$ifndef FPUNONE}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const DoubleIn : myRealtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( BCDIn, DoubleToBCD ( DoubleIn ), BCDout );
- end;
- procedure BCDSubtract ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( BCDIn, CurrToBCD ( Currin ), BCDout );
- end;
- procedure BCDSubtract ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( CurrToBCD ( Currin ), BCDIn, BCDout );
- end;
- {$ifdef comproutines}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( BCDIn, CompToBCD ( Compin ), BCDout );
- end;
- procedure BCDSubtract ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( CompToBCD ( Compin ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDSubtract ( const BCDIn : tBCD;
- const StringIn : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( BCDIn, StrToBCD ( StringIn ), BCDout );
- end;
- procedure BCDSubtract ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( StrToBCD ( StringIn ), BCDIn, BCDout );
- end;
- procedure BCDSubtract ( const StringIn1,
- StringIn2 : FmtBCDStringtype;
- var BCDout : tBCD ); Inline;
- begin
- BCDSubtract ( StrToBCD ( StringIn1 ), StrToBCD ( StringIn2 ), BCDout );
- end;
- procedure BCDMultiply ( const BCDIn : tBCD;
- const IntIn : myInttype;
- var BCDout : tBCD );
- var
- bh : tBCD_helper;
- bhr : tBCD_helper;
- bhrr : tBCD_helper_big;
- int : {$ifopt r+} 0..high ( bhrr.Singles[0] ) DIV 10 {$else} Integer {$endif};
- i1 : {$ifopt r+} low ( bh.Singles )..high ( bh.Singles ) {$else} Integer {$endif};
- i3 : {$ifopt r+} low ( bhr.Singles )..high ( bhr.Singles ) {$else} Integer {$endif};
- 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};
- ue : {$ifopt r+} 1 * ( low ( bhrr.Singles[0] ) + low ( bhrr.Singles[0] ) DIV 10 ) DIV 10
- ..( high ( bhrr.Singles[0] ) + high ( bhrr.Singles[0] ) DIV 10 ) DIV 10 {$else} Integer {$endif};
- begin
- if IntIn = 0
- then begin
- BCDout := NullBCD;
- EXIT;
- end;
- if IntIn = 1
- then begin
- BCDout := BCDIn;
- EXIT;
- end;
- if IntIn = -1
- then begin
- BCDout := BCDIn;
- BCDNegate ( BCDout );
- EXIT;
- end;
- if IntIn = low ( myInttype )
- then begin
- {$if declared ( myMinIntBCD ) }
- BCDMultiply ( BCDIn, myMinIntBCD, BCDout );
- EXIT;
- {$else}
- RAISE eBCDOverflowException.create ( 'in BCDmultiply' );
- {$endif}
- end;
- if Abs ( IntIn ) > low ( bhrr.Singles[0] ) DIV 10
- then begin
- BCDMultiply ( BCDIn, IntegerToBCD ( IntIn ), BCDout );
- EXIT;
- end;
- unpack_BCD ( BCDIn, bh );
- if bh.Prec = 0
- then begin
- BCDout := NullBCD;
- EXIT;
- end;
- bhr := null_.bh;
- bhrr := null_.bhb;
- int := Abs ( IntIn );
- WITH bhrr do
- begin
- Neg := bh.Neg XOR ( IntIn < 0 );
- FDig := bh.FDig;
- LDig := bh.LDig;
- for i1 := bh.FDig TO bh.LDig do
- Singles[i1] := bh.Singles[i1] * int;
- {
- for i3 := fdig to ldig do
- write ( ' ', singles[i3] );
- writeln;
- }
- ue := 0;
- for i3 := LDig DOWNTO FDig do
- begin
- v := Singles[i3] + ue;
- ue := v DIV 10;
- v := v MOD 10;
- bhr.Singles[i3] := v;
- end;
- while ue <> 0 do
- begin
- Dec ( FDig );
- if FDig < low ( bhr.Singles )
- then RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
- bhr.Singles[FDig] := ue MOD 10;
- ue := ue DIV 10;
- end;
- bhr.Plac := LDig;
- bhr.FDig := FDig;
- if LDig > high ( bhr.Singles )
- then bhr.LDig := high ( bhr.Singles )
- else bhr.LDig := LDig;
- end;
- if NOT pack_BCD ( bhr, BCDout )
- then begin
- RAISE eBCDOverflowException.create ( 'in BCDMultiply' );
- end;
- end;
- procedure BCDMultiply ( const IntIn : myInttype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( BCDIn, IntIn, BCDout );
- end;
- {$ifndef FPUNONE}
- procedure BCDMultiply ( const DoubleIn : myRealtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( DoubleToBCD ( DoubleIn ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( BCDIn, CurrToBCD ( Currin ), BCDout );
- end;
- procedure BCDMultiply ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( CurrToBCD ( Currin ), BCDIn, BCDout );
- end;
- {$ifdef comproutines}
- procedure BCDMultiply ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( BCDIn, CompToBCD ( Compin ), BCDout );
- end;
- procedure BCDMultiply ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( CompToBCD ( Compin ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDMultiply ( const StringIn : FmtBCDStringtype;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDMultiply ( StrToBCD ( StringIn ), BCDIn, BCDout );
- end;
- procedure BCDDivide ( const Dividend : tBCD;
- const Divisor : myInttype;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( Dividend, IntegerToBCD ( Divisor ), BCDout );
- end;
- procedure BCDDivide ( const Dividend : myInttype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( IntegerToBCD ( Dividend ), Divisor, BCDout );
- end;
- {$ifndef FPUNONE}
- procedure BCDDivide ( const Dividend : myRealtype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( DoubleToBCD ( Dividend ), Divisor, BCDout );
- end;
- {$endif}
- procedure BCDDivide ( const BCDIn : tBCD;
- const Currin : currency;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( BCDIn, CurrToBCD ( Currin ), BCDout );
- end;
- procedure BCDDivide ( const Currin : currency;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( CurrToBCD ( Currin ), BCDIn, BCDout );
- end;
- {$ifdef comproutines}
- procedure BCDDivide ( const BCDIn : tBCD;
- const Compin : Comp;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( BCDIn, CompToBCD ( Compin ), BCDout );
- end;
- procedure BCDDivide ( const Compin : Comp;
- const BCDIn : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( CompToBCD ( Compin ), BCDIn, BCDout );
- end;
- {$endif}
- procedure BCDDivide ( const Dividend : FmtBCDStringtype;
- const Divisor : tBCD;
- var BCDout : tBCD ); Inline;
- begin
- BCDDivide ( StrToBCD ( Dividend ), Divisor, BCDout );
- end;
- operator = ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- begin
- z := BCDCompare ( BCD1, BCD2 ) = 0;
- end;
- operator < ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- begin
- z := BCDCompare ( BCD1, BCD2 ) < 0;
- end;
- operator > ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- begin
- z := BCDCompare ( BCD1, BCD2 ) > 0;
- end;
- operator <= ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- begin
- z := BCDCompare ( BCD1, BCD2 ) <= 0;
- end;
- operator >= ( const BCD1,
- BCD2 : tBCD ) z : Boolean; Inline;
- begin
- z := BCDCompare ( BCD1, BCD2 ) >= 0;
- end;
- (* ######################## not allowed: why?
- operator + ( const BCD : tBCD ) z : tBCD; Inline;
- begin
- z := bcd;
- end;
- ##################################################### *)
- operator - ( const BCD : tBCD ) z : tBCD; Inline;
- begin
- z := BCD;
- BCDNegate ( z );
- end;
- operator + ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD1, BCD2, z );
- end;
- operator + ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD, i, z );
- end;
- operator + ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( i, BCD, z );
- end;
- {$ifndef FPUNONE}
- operator + ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD, DoubleToBCD ( r ), z );
- end;
- operator + ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( DoubleToBCD ( r ), BCD, z );
- end;
- {$endif}
- operator + ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD, CurrToBCD ( c ), z );
- end;
- operator + ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( CurrToBCD ( c ), BCD, z );
- end;
- {$ifdef comproutines}
- operator + ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD, CompToBCD ( c ), z );
- end;
- operator + ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( CompToBCD ( c ), BCD, z );
- end;
- {$endif}
- operator + ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- begin
- BCDAdd ( BCD, StrToBCD ( s ), z );
- end;
- operator + ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDAdd ( StrToBCD ( s ), BCD, z );
- end;
- operator - ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD1, BCD2, z );
- end;
- operator - ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, i, z );
- end;
- operator - ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, i, z );
- BCDNegate ( z );
- end;
- {$ifndef FPUNONE}
- operator - ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, DoubleToBCD ( r ), z );
- end;
- operator - ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( DoubleToBCD ( r ), BCD, z );
- end;
- {$endif}
- operator - ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, CurrToBCD ( c ), z );
- end;
- operator - ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( CurrToBCD ( c ), BCD, z );
- end;
- {$ifdef comproutines}
- operator - ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, CompToBCD ( c ), z );
- end;
- operator - ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( CompToBCD ( c ), BCD, z );
- end;
- {$endif}
- operator - ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- begin
- BCDSubtract ( BCD, StrToBCD ( s ), z );
- end;
- operator - ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDSubtract ( StrToBCD ( s ), BCD, z );
- end;
- operator * ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD1, BCD2, z );
- end;
- operator * ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, i, z );
- end;
- operator * ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, i, z );
- end;
- {$ifndef FPUNONE}
- operator * ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, DoubleToBCD ( r ), z );
- end;
- operator * ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( DoubleToBCD ( r ), BCD, z );
- end;
- {$endif}
- operator * ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, CurrToBCD ( c ), z );
- end;
- operator * ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( CurrToBCD ( c ), BCD, z );
- end;
- {$ifdef comproutines}
- operator * ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, CompToBCD ( c ), z );
- end;
- operator * ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( CompToBCD ( c ), BCD, z );
- end;
- {$endif}
- operator * ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- begin
- BCDMultiply ( BCD, StrToBCD ( s ), z );
- end;
- operator * ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDMultiply ( StrToBCD ( s ), BCD, z );
- end;
- operator / ( const BCD1,
- BCD2 : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD1, BCD2, z );
- end;
- operator / ( const BCD : tBCD;
- const i : myInttype ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD, i, z );
- end;
- operator / ( const i : myInttype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( IntegerToBCD ( i ), BCD, z );
- end;
- {$ifndef FPUNONE}
- operator / ( const BCD : tBCD;
- const r : myRealtype ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD, DoubleToBCD ( r ), z );
- end;
- operator / ( const r : myRealtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( DoubleToBCD ( r ), BCD, z );
- end;
- {$endif}
- operator / ( const BCD : tBCD;
- const c : currency ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD, CurrToBCD ( c ), z );
- end;
- operator / ( const c : currency;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( CurrToBCD ( c ), BCD, z );
- end;
- {$ifdef comproutines}
- operator / ( const BCD : tBCD;
- const c : Comp ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD, CompToBCD ( c ), z );
- end;
- operator / ( const c : Comp;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( CompToBCD ( c ), BCD, z );
- end;
- {$endif}
- operator / ( const BCD : tBCD;
- const s : FmtBCDStringtype ) z : tBCD; Inline;
- begin
- BCDDivide ( BCD, StrToBCD ( s ), z );
- end;
- operator / ( const s : FmtBCDStringtype;
- const BCD : tBCD ) z : tBCD; Inline;
- begin
- BCDDivide ( StrToBCD ( s ), BCD, z );
- end;
- operator := ( const i : Byte ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : Byte; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- operator := ( const i : Word ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : Word; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- operator := ( const i : longword ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : longword; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- {$if declared ( qword ) }
- operator := ( const i : qword ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : qword; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- {$endif}
- operator := ( const i : ShortInt ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : ShortInt; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- operator := ( const i : smallint ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : smallint; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- operator := ( const i : LongInt ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : LongInt; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- {$if declared ( int64 ) }
- operator := ( const i : int64 ) z : tBCD; Inline;
- begin
- z := IntegerToBCD ( myInttype ( i ) );
- end;
- operator := ( const BCD : tBCD ) z : int64; Inline;
- begin
- z := BCDToInteger ( BCD );
- end;
- {$endif}
- {$ifndef FPUNONE}
- operator := ( const r : Single ) z : tBCD; Inline;
- begin
- z := DoubleToBCD ( myRealtype ( r ) );
- end;
- operator := ( const BCD : tBCD ) z : Single; Inline;
- begin
- z := BCDToDouble ( BCD );
- end;
- operator := ( const r : Double ) z : tBCD; Inline;
- begin
- z := DoubleToBCD ( myRealtype ( r ) );
- end;
- operator := ( const BCD : tBCD ) z : Double; Inline;
- begin
- z := BCDToDouble ( BCD );
- end;
- {$if sizeof ( extended ) <> sizeof ( double )}
- operator := ( const r : Extended ) z : tBCD; Inline;
- begin
- z := DoubleToBCD ( {myRealtype (} r {)} );
- end;
- operator := ( const BCD : tBCD ) z : Extended; Inline;
- begin
- z := BCDToDouble ( BCD );
- end;
- {$endif}
- {$endif}
- operator := ( const c : currency ) z : tBCD; Inline;
- begin
- CurrToBCD ( c, z );
- end;
- operator := ( const BCD : tBCD ) z : currency; Inline;
- begin
- BCDToCurr ( BCD, z );
- end;
- {$ifdef comproutines}
- {$undef makedirect}
- {$ifdef makedirect}
- operator := ( const c : Comp ) z : tBCD; Inline;
- var
- cc : int64 absolute c;
- begin
- z := IntegerToBCD ( cc );
- end;
- { $define version1} { only one of these may be defined! }
- { $define version2} { version 1 produces a compiler error (with INLINE only!)}
- {$define version3} { I wasn't able to reduce the problem, sorry }
- {$ifdef version1}
- operator := ( const BCD : tBCD ) z : Comp; Inline;
- var
- zz : Comp absolute z;
- begin
- zz := BCDToInteger ( BCD );
- end;
- {$endif}
- {$ifdef version2}
- operator := ( const BCD : tBCD ) z : Comp; Inline;
- var
- zz : int64;
- zzz : Comp absolute zz;
- begin
- zz := BCDToInteger ( BCD );
- z := zzz;
- end;
- {$endif}
- {$ifdef version3}
- operator := ( const BCD : tBCD ) z : Comp; Inline;
- var
- zz : record
- case Boolean of
- False: ( i : int64 );
- True: ( c : Comp );
- end;
- begin
- zz.i := BCDToInteger ( BCD );
- z := zz.c;
- end;
- {$endif}
- {$else}
- operator := ( const c : Comp ) z : tBCD; Inline;
- begin
- z := CompToBCD ( c );
- end;
- operator := ( const BCD : tBCD ) z : Comp; Inline;
- begin
- z := BCDToComp ( BCD );
- end;
- {$endif}
- {$endif}
- operator := ( const s : string ) z : tBCD; Inline;
- begin
- z := StrToBCD ( s );
- end;
- operator := ( const BCD : tBCD ) z : string; Inline;
- begin
- z := BCDToStr ( BCD );
- end;
- operator := ( const s : AnsiString ) z : tBCD; Inline;
- begin
- z := StrToBCD ( s );
- end;
- operator := ( const BCD : tBCD ) z : AnsiString; Inline;
- begin
- z := BCDToStr ( BCD );
- end;
- {$endif}
- constructor TFMTBcdVarData.create;
- begin
- inherited create;
- FBcd:=NullBCD;
- end;
- constructor TFMTBcdVarData.create(const BCD : tBCD);
- begin
- inherited create;
- FBcd:=BCD;
- end;
- function TFMTBcdFactory.GetInstance(const v : TVarData): tObject;
- begin
- result:=tObject(v.VPointer);
- end;
- procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);
- begin
- case Operation of
- opAdd:
- TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD+TFMTBcdVarData(Right.VPointer).BCD;
- opSubtract:
- TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD-TFMTBcdVarData(Right.VPointer).BCD;
- opMultiply:
- TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD*TFMTBcdVarData(Right.VPointer).BCD;
- opDivide:
- TFMTBcdVarData(Left.VPointer).BCD:=TFMTBcdVarData(Left.VPointer).BCD/TFMTBcdVarData(Right.VPointer).BCD;
- else
- RaiseInvalidOp;
- end;
- end;
-
- procedure TFMTBcdFactory.Clear(var V: TVarData);
- begin
- FreeAndNil(tObject(V.VPointer));
- V.VType:=varEmpty;
- end;
- procedure TFMTBcdFactory.Copy(var Dest: TVarData; const Source: TVarData; const Indirect: Boolean);
- begin
- if Indirect then
- Dest.VPointer:=Source.VPointer
- else
- Dest.VPointer:=TFMTBcdVarData.Create(TFMTBcdVarData(Source.VPointer).BCD);
- Dest.VType:=Vartype;
- end;
- {$if declared ( myMinIntBCD ) }
- (*
- {$if sizeof ( integer ) = 2 }
- {$ifdef BCDgr4 }
- const
- myMinIntBCDValue : packed array [ 1..3 ] of Char = #$32#$76#$80;
- {$endif}
- {$else}
- {$if sizeof ( integer ) = 4 }
- *)
- {$ifdef BCDgr9 }
- const
- myMinIntBCDValue : packed array [ 1..10 ] of Char = #$21#$47#$48#$36#$48;
- {$endif}
- (*
- {$else}
- {$if sizeof ( integer ) = 8 }
- {$ifdef BCDgr18 }
- const
- myMinIntBCDValue : packed array [ 1..19 ] of Char = #$92#$23#$37#$20#$36#$85#$47#$75#$80#$80;
- {$endif}
- {$else}
- {$fatal You have an interesting integer type! Sorry, not supported}
- {$endif}
- {$endif}
- {$endif}
- *)
- {$endif}
- initialization
- FillChar ( null_, SizeOf ( null_ ), #0 );
- FillChar ( NullBCD_, SizeOf ( NullBCD_ ), #0 );
- FillChar ( OneBCD_, SizeOf ( OneBCD_ ), #0 );
- OneBCD_.Precision := 1;
- OneBCD_.Fraction[low ( OneBCD_.Fraction )] := $10;
- {$if declared ( myMinIntBCD ) }
- FillChar ( myMinIntBCD, SizeOf ( myMinIntBCD ), #0 );
- {$ifndef bigger_BCD}
- myMinIntBCD.SignSpecialPlaces := NegBit;
- {$else}
- myMinIntBCD.Negativ := True;
- {$endif}
- {$if sizeof ( integer ) = 2 }
- {$ifdef BCDgr4 }
- myMinIntBCD.Precision := 5;
- Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
- {$endif}
- {$else}
- {$if sizeof ( integer ) = 4 }
- {$ifdef BCDgr9 }
- myMinIntBCD.Precision := 10;
- Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
- {$endif}
- {$else}
- {$if sizeof ( integer ) = 8 }
- {$ifdef BCDgr18 }
- myMinIntBCD.Precision := 19;
- Move ( myMinIntBCDValue, myMinIntBCD.Fraction, SizeOf ( myMinIntBCDValue ) );
- {$endif}
- {$else}
- {$fatal You have an interesting integer type! Sorry, not supported}
- {$endif}
- {$endif}
- {$endif}
- {$endif}
- FMTBcdFactory:=TFMTBcdFactory.create;
- finalization
- FreeAndNil(FMTBcdFactory)
- end.
|