|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Value > Long Data TypeI'm erring because the whole number cumulative total is greater than
the long data type. Any suggestions on how to handle in VB? David wrote:
> I'm erring because the whole number cumulative total is greater than Decimal> the long data type. > > Any suggestions on how to handle in VB? Currency Double Single are options. Look in online help for characteristics (and limitations) of each. W/o more on the problem to be solved, more specifics would be guessing at best. --
Show quote
Hide quote
"dpb" <n***@non.net> wrote in message news:gpn7ii$pqr$1@aioe.org... I use currency because it is integer internally. The value displays 4 > David wrote: >> I'm erring because the whole number cumulative total is greater than >> the long data type. >> >> Any suggestions on how to handle in VB? > > Decimal > Currency > Double > Single > > are options. Look in online help for characteristics (and limitations) of > each. W/o more on the problem to be solved, more specifics would be > guessing at best. > > -- digits, but it is what is called a scaled integer divided by 10,000. The integer overflows at 2^63-1 instead of 2^31-1. Check help for details. > I use currency because it is integer internally. The value displays 4 Actually, Currency values overflow at just over 2^49.71228762045054> digits, but it is what is called a scaled integer divided by 10,000. The > integer overflows at 2^63-1 instead of 2^31-1. -- Rick (MVP - Excel) "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message The maximm value should be 922,337,203,685,477.5807. Internally, this is the news:Oj5XszvpJHA.996@TK2MSFTNGP03.phx.gbl... >> I use currency because it is integer internally. The value displays 4 >> digits, but it is what is called a scaled integer divided by 10,000. The >> integer overflows at 2^63-1 instead of 2^31-1. > > Actually, Currency values overflow at just over 2^49.71228762045054 > > -- > Rick (MVP - Excel) integer 9,223,372,036,854,775,807 which is 2^63-1, but the value is "scaled" by dividing by 10,000. >>> I use currency because it is integer internally. The value displays 4 Ah, yes, but of course... you meant the unscaled value, not the scaled one.>>> digits, but it is what is called a scaled integer divided by 10,000. The >>> integer overflows at 2^63-1 instead of 2^31-1. >> >> Actually, Currency values overflow at just over 2^49.71228762045054 >> >> -- >> Rick (MVP - Excel) > > The maximm value should be 922,337,203,685,477.5807. Internally, this is > the integer 9,223,372,036,854,775,807 which is 2^63-1, but the value is > "scaled" by dividing by 10,000. -- Rick (MVP - Excel) A Long value has a maximum of 2,147,483,647.
Currency goes up to 922,337,203,477.5807 - and no one enforces you not to have no decimals. And if that all is not enough for you, you could create your own huge type variable based on a class rather than a type definition. Have a look at Variant Decimal
Dim v As Variant ' not assigned Debug.Print VarType(v) Debug.Print TypeName(v) v = 0 Debug.Print VarType(v) Debug.Print TypeName(v) v = CDec(0) Debug.Print VarType(v) Debug.Print TypeName(v) v = CDec(1000) Debug.Print VarType(v) Debug.Print TypeName(v) v = v + 1 Debug.Print VarType(v) Debug.Print TypeName(v) Show quoteHide quote "David" wrote: > I'm erring because the whole number cumulative total is greater than > the long data type. > > Any suggestions on how to handle in VB? > > > Thanks All, I guess Currency is it.
------------------------------------ Claus Centrino Out of "Learning" How would you go about creating a class that could handle a type larger than Long or Currency for that matter. Only thing I could think of is to divided the number by some value (say 1000 or 10000, etc) and then add them. But how would the method return the value since the return type would need to be defined as some type. Show quoteHide quote > "David" wrote: > >> I'm erring because the whole number cumulative total is greater than >> the long data type. >> >> Any suggestions on how to handle in VB? >> >> >> > How would you go about creating a class that could handle Well, I can't you give a readily coded class (there migh > a type larger than > Long or Currency for that matter. already exist one in the community, don't know) but I can outline the steps: Just like an integer variable consists of two bytes and a long variable consists of four bytes you could think about a huge variable consisting of eight bytes. Create a class clsHuge which holds two long public variables, say HI and LO, which represent the high and low part of a huge number. Then you can say Set H = new clsHuge and assign values to the new huge variable by H.LO = xxx, H.HI = yyy. You can combine that in an assign method to shorten code to "H.LetValue xxx,yyy" and for retrieving values you can have "H.GetValue Hi, Lo". Implementing addition and subtraction is easy, just write a new method "Add(Hi as long, Lo as long)" and "Subtract(Hi as long,Lo as Long)". The only thing is you must handle overflows by your own. If this is done you even can make it more comfortable using methods having class memebrs as parameters, something like "HugeVar1.AddAnotherHugeOne HugeVar2", or "HugeVar1.Clone HugeVar2" Implementing multiplication and division is a little bit more difficult. But what would live be without little difficulties :-) ? And maybe you don't need it at all if your huge data only comes from creating subtotals. Additionaly you'll have to build a string output function to return the string value of HI * maxLO + LO. Once having coded these basic functions you'll see that working with such huge variables is almost easy, especially as you probably don't need huge variables everywhere but only in some special situations. As adding and subtracting will be done by integer calculation this won't take a long time. Mr Centrino:
Thanks for the input. Will have to think on this one for a bit or byte (pun intended). Where I have a problem with the class concept is what to do with the return value as some point (unless I'm misunderstanding) you would need to put your high and low values back togerher. My thoughts are it may be easier to divide the number by some factor (say 1000, or 10000) and then deal with the number as a single or double. This should maintain the relationship between numbers while keeping it within a normal defined type value. David Show quoteHide quote "Claus Centrino" <c***@sofort-mail.de> wrote in message news:gptu2e$l6q$1@online.de... > >> How would you go about creating a class that could handle a type larger >> than >> Long or Currency for that matter. > > Well, I can't you give a readily coded class (there migh already exist one > in the community, don't know) but I can outline the steps: > > Just like an integer variable consists of two bytes and a long variable > consists of four bytes you could think about a huge variable consisting of > eight bytes. > > Create a class clsHuge which holds two long public variables, say HI and > LO, which represent the high and low part of a huge number. > > Then you can say Set H = new clsHuge and assign values to the new huge > variable by H.LO = xxx, H.HI = yyy. > > You can combine that in an assign method to shorten code to "H.LetValue > xxx,yyy" and for retrieving values you can have "H.GetValue Hi, Lo". > > Implementing addition and subtraction is easy, just write a new method > "Add(Hi as long, Lo as long)" and "Subtract(Hi as long,Lo as Long)". The > only thing is you must handle overflows by your own. > > If this is done you even can make it more comfortable using methods having > class memebrs as parameters, something like "HugeVar1.AddAnotherHugeOne > HugeVar2", or "HugeVar1.Clone HugeVar2" > > Implementing multiplication and division is a little bit more difficult. > But what would live be without little difficulties :-) ? And maybe you > don't need it at all if your huge data only comes from creating subtotals. > > Additionaly you'll have to build a string output function to return the > string value of HI * maxLO + LO. > > Once having coded these basic functions you'll see that working with such > huge variables is almost easy, especially as you probably don't need huge > variables everywhere but only in some special situations. > > As adding and subtracting will be done by integer calculation this won't > take a long time. > > David wrote:
> Certainly not as a Single, which has less precision than a Long (but> My thoughts are it may be easier to divide the number by some > factor (say 1000, or 10000) and then deal with the number as a > single or double. This should maintain the relationship between > numbers while keeping it within a normal defined type value. more range). As for dividing by 10,000 -- you're describing Currency. There should be no need to do anything with the 'return' value.
By creating it as a class you have created a numeric type that you can use like you would use any other type - increment it or do other math, compare to other variables of the same type, display it, print it, save it to file, etc. The only thing is that any particular operation you use it for will have to be created as a method or property of the class. Rather than using two smaller numeric variables and scaling one of them, it may be easier to use something like a byte array with one or two digits per byte, or even as a string with one digit per character. The low level routines can be somewhat complex, but once you have the basics in place the higher level routines are implemented using the methods of the class. I know there used to be routines around for a BCD numeric class of arbitrary size in VB, but I can't find any references now. There's some discussion here: http://en.wikipedia.org/wiki/Arbitrary_precision_arithmetic If the Java implementations are available as source, conversion is reasonably straightforward. Show quoteHide quote "David" <dw85745***@earthlink.net> wrote in message news:ugmmXGXqJHA.996@TK2MSFTNGP03.phx.gbl... > Mr Centrino: > > Thanks for the input. > Will have to think on this one for a bit or byte (pun intended). > Where I have a problem with the class concept is what to > do with the return value as some point (unless I'm misunderstanding) > you would need to put your high and low values back togerher. > On Sat, 21 Mar 2009 16:27:40 +1100, in
microsoft.public.vb.general.discussion you wrote: 8< SNIP >8 >Rather than using two smaller numeric variables and scaling one of them, it Hmmm ... a VB6 based BigNum library. Any takers? God knows I've tried,>may be easier to use something like a byte array with one or two digits per >byte, or even as a string with one digit per character. The low level >routines can be somewhat complex, but once you have the basics in place the >higher level routines are implemented using the methods of the class. I >know there used to be routines around for a BCD numeric class of arbitrary >size in VB, but I can't find any references now. There's some discussion >here: >http://en.wikipedia.org/wiki/Arbitrary_precision_arithmetic but it was primitive, and division was a #^?@. Every level of mathematics being based on the level prior to it, it's possible, but the implementation gets clunky without sufficient knowledge. Does Knuth's "Numerical recipes in C" contain any applicable formulae? (In reviewing your response, I realized that you mentioned the recursive nature of mathematics, but I'll leave the above paragraphs intact for posterity <g>) >If the Java implementations are available as source, conversion is I would expect a lot of unsigned bit-shifting, so analogous VB code>reasonably straightforward. would need to be implemented, but I don't see why it couldn't be done. J. Jeremiah D. Seitz Omega Techware http://www.omegatechware.net What is wrong with the Decimal data type?
+/-79,228,162,514,264,337,593,543,950,335 /Henning Show quoteHide quote "David" <dw85745***@earthlink.net> skrev i meddelandet news:OeDp8L1pJHA.5832@TK2MSFTNGP06.phx.gbl... > Thanks All, I guess Currency is it. > > ------------------------------------ > > Claus Centrino > > Out of "Learning" > > How would you go about creating a class that could handle a type larger > than Long or Currency for that matter. > > Only thing I could think of is to divided the number by some value > (say 1000 or 10000, etc) and then add them. But how would the method > return the value since the return type would need to be defined as some > type. > > > >> "David" wrote: >> >>> I'm erring because the whole number cumulative total is greater than >>> the long data type. >>> >>> Any suggestions on how to handle in VB? >>> >>> >>> > > "Henning" <computer_h***@coldmail.com> wrote in message Nothing, except it does not exist in VB6news:49c3c180$0$16212$57c3e1d3@news3.bahnhof.se... > What is wrong with the Decimal data type? > +/-79,228,162,514,264,337,593,543,950,335 > > /Henning Dave O. "Dave O." <nob***@nowhere.com> wrote in message It does, but only inside a variant. See "Decimal data type" in MSDN Library. news:%23SKgmnXqJHA.3840@TK2MSFTNGP03.phx.gbl... > > "Henning" <computer_h***@coldmail.com> wrote in message > news:49c3c180$0$16212$57c3e1d3@news3.bahnhof.se... >> What is wrong with the Decimal data type? >> +/-79,228,162,514,264,337,593,543,950,335 >> >> /Henning > > Nothing, except it does not exist in VB6 You have to use CDec() when you first initialize it, such as with "d = CDec(0)". Dave O. wrote:
> "Henning" <computer_h***@coldmail.com> wrote in message Really? This doesn't work in your copy of VB6?> news:49c3c180$0$16212$57c3e1d3@news3.bahnhof.se... >> What is wrong with the Decimal data type? >> +/-79,228,162,514,264,337,593,543,950,335 >> >> /Henning > > Nothing, except it does not exist in VB6 Dim Big As Variant Big = CDec(892281625142643@) Big = Big + Big + Big Debug.Print Format$(Big, "#,#") Sorry, I should have said "as a native data type" I avoid Variants where
ever possible as in the vast majority of cases they are not necessary. In fact I cannot think of a single instance where I have used a Variant by choice. Dave O. Show quoteHide quote "Jim Mack" <jmack@mdxi.nospam.com> wrote in message news:OLkCl1XqJHA.1252@TK2MSFTNGP03.phx.gbl... > Dave O. wrote: >> "Henning" <computer_h***@coldmail.com> wrote in message >> news:49c3c180$0$16212$57c3e1d3@news3.bahnhof.se... >>> What is wrong with the Decimal data type? >>> +/-79,228,162,514,264,337,593,543,950,335 >>> >>> /Henning >> >> Nothing, except it does not exist in VB6 > > Really? This doesn't work in your copy of VB6? > > Dim Big As Variant > > Big = CDec(892281625142643@) > > Big = Big + Big + Big > > Debug.Print Format$(Big, "#,#") > > -- > Jim Mack > Twisted tees at http://www.cafepress.com/2050inc > "We sew confusion" "David" <dw85745***@earthlink.net> wrote in message Depending on the application. I would use Currency, Decimal, or Double, but news:OeDp8L1pJHA.5832@TK2MSFTNGP06.phx.gbl... > a type larger than Long or Currency for that matter. if this is not enough, check this VB6 calculator(including source) that handles unlimited digits: http://www.karenware.com/powertools/ptcalc.asp
How to make this call from form to control
Differences between VB amd VBA and VBA Education Application.Quit but Word remains open in Task Manager VB App fails when log reaches 65536 HOW IS Memory Used by a VB App vb.net executing on Help with Binary Compatibility Fonts Internal String Visibility NET Required ??? |
|||||||||||||||||||||||