[AccessD] Decimal Data Type

jwcolby jwcolby at colbyconsulting.com
Thu May 15 08:51:11 CDT 2008


sng and dbl are computed inside of the processor as binary digits in a 
floating point processor portion of the cpu.  IIRC there are 80 bits in 
the intel processor register.  Of these, some number are used to store 
numbers to the left of the mantissa, and the remainder to store numbers 
to the right of the mantissa.  The number to the left and the right of 
the mantissa vary depending on whether you are using sng or dbl.

Again IIRC the difference is whether you can compute very large numbers 
with not so large accuracy (to the right of the mantissa) or a smaller 
range of numbers but with a higher degree of accuracy (to the right of 
the mantissa).

IIRC decimal was actually a math program (not calculated in the math 
processor) and could calculate and STORE DECIMAL numbers precisely, 
whereas some decimal numbers can only be approximated when stored in binary.

Bear in mind that all of this is very old memory from "back in the day". 
    Such memory is very suspect due to encroaching age, chemical and 
physical damage inflicted on the memory storage unit etc.

John W. Colby
www.ColbyConsulting.com


Gustav Brock wrote:
> Hi Tony (iggy?)
> 
> As for the difference, look up the on-line help for range etc.
> 
> Why it was added I don't know, but I guess it was to have a data type compatible with Decimal of SQL Server.
> 
> I haven't used it myself in a table. William, I think, posted some years ago some nasty examples on errors which the use of the data type could cause.
> 
> In VBA it can be necessary for avoiding bit errors when handling very large numbers. I use it in our rounding function posted several times.
> 
> Here is another example: Create the Fibonacci sequence of numbers. If Long was used only 46 elements could be calculated:
> 
> <code>
> Public Function FibonacciSequenceDec( _
>   ByVal lngElements As Long) _
>   As Variant
> 
> ' Build and return array with Fibonacci sequence of numbers.
> ' Count of elements is determined by lngElements.
> ' Max. number returned in array:
> '   50095301248058391139327916261
> '
> ' 2004-10-03. Gustav Brock, Cactus Data, CPH.
> 
>   ' Min. index of sequence per definition.
>   Const clngIndexMin  As Long = 0
>   ' Max. possible index of sequence for datatype Decimal.
>   Const clngIndexMax  As Long = 139
>   
>   Dim adecSeq()       As Variant
>   Dim lngIndex        As Long
>   
>   If lngElements < clngIndexMin Or lngElements > clngIndexMax Then
>     ' Not a valid input.
>   Else
>     ' Build and fill array with the Fibonacci sequence of numbers.
>     ReDim adecSeq(clngIndexMin To lngElements)
>     For lngIndex = clngIndexMin To lngElements
>       If lngIndex < 2 Then
>         ' Values of the first two elements are 0 and 1 per definition.
>         adecSeq(lngIndex) = CDec(lngIndex)
>       Else
>         ' Value is the sum of the two preceding numbers.
>         adecSeq(lngIndex) = CDec(adecSeq(lngIndex - 2)) + CDec(adecSeq(lngIndex - 1))
>       End If
> ''      Debug.Print adecSeq(lngIndex);
>     Next
>   End If
>   
>   FibonacciSequenceDec = adecSeq()
> 
> End Function
> 
> Public Function FibonacciElementDec( _
>   ByVal lngElements As Long) _
>   As Variant
> 
> ' Returns the value of element lngElements in the
> ' Fibonacci sequence of numbers.
> ' Max. number returned:
> '   50095301248058391139327916261
> '
> ' 2004-10-03. Gustav Brock, Cactus Data, CPH.
> 
>   Const clngIndexMin  As Long = 0
>   Const clngIndexMax  As Long = 139
>   
>   Dim decValue        As Variant
>   
>   If lngElements < clngIndexMin Or lngElements > clngIndexMax Then
>     ' Not a valid input. Return error.
>     decValue = CDec(-1)
>   Else
>     ' Return last number in sequence in array.
>     decValue = FibonacciSequenceDec(lngElements)(lngElements)
>   End If
>   
>   FibonacciElementDec = decValue
> 
> End Function
> </code>
> 
> /gustav
> 
>>>> iggy at nanaimo.ark.com 15-05-2008 14:37 >>>
> Hey Gustav
> How does Decimal differ from Double or Currency and do you know why it 
> was added as a data type?  As Edward has confirmed when Access does the 
> conversion "you have to hunt through the app and fix things by hand".
> 
> That said, Decimal should be avoided as data type in tables as it is known to be buggy. In VBA, however, it can be quite useful.
> 
> Just out of curiosity (because I don't know what it is supposed to be 
> used for) where would you use it in VBA?
> 
> Thanks
> 
> 



More information about the AccessD mailing list