# datatype "Decimal" in VB.Net is 32-bit or 64-bit? • ### Question

• ```Module Module1

Sub Main()
Dim pi As Decimal = 3D

System.Console.WriteLine("Calculation in Progress...")
For i As Integer = 1 To 10000000
If i Mod 2 = 1 Then
pi = pi + (4 / ((2D * i) * ((2D * i) + 1) * ((2D * i) + 2)))
Else
pi = pi - (4 / ((2D * i) * ((2D * i) + 1) * ((2D * i) + 2)))
End If
Next
Console.WriteLine("Result is {0}", pi.ToString)
Console.WriteLine("Press any Key to Continue...")
End Sub

End Module```

code above is program to calculate number of Pi.

I know datatype of "Decimal" will is occupy 16 byte memory allocation. but I wish to know data will be store as 64-bit data in 64-bit program or 32-bit data in 64-bit program? or 16-bit instead?

Friday, February 14, 2020 4:08 PM

### All replies

• 16 bytes means 128 bits:

MsgBox(Marshal.SizeOf(Of Decimal) * 8) ' Shows 128

You can check it in various environment.

In Compile tab of Project Properties you can find the “Prefer 32-bit” option and perform experiments. This should not affect the size of Decimal.

• Edited by Friday, February 14, 2020 5:06 PM
Friday, February 14, 2020 5:05 PM
• Here is an article that discusses the Decimal data type.

https://www.w3computing.com/vb2008/vb-decimal-data-type/

The values are 16 bytes which, as Viorel said, is 128 bits.  The numbers can store up to 28 decimal digits, which means it's using about 91 bits for the mantissa.  (There are 3.232 bits per decimal digit.)

You need to understand that the Decimal data type is not directly supported in the hardware, like integers and floats.  All of the math operations are simulated in software.  That means math like you are doing is very slow.  Also, the algorithm you are using is itself very slow.  Even after 10,000,000 operations, like you have here, you will still only have about 20 digits of pi.  You can tell that because you are adding or subtracting basically 1/(n^3) during the nth iteration.  10,000,000 is 10^7, and 10^7 cubed is 10^21.  To get 28 decimal digits, you'd need to run at least 2,000,000,000 loops.

Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

Monday, February 17, 2020 12:36 AM
• Even after 10,000,000 operations, like you have here, you will still only have about 20 digits of pi.  You can tell that because you are adding or subtracting basically 1/(n^3) during the nth iteration.  10,000,000 is 10^7, and 10^7 cubed is 10^21.  To get 28 decimal digits, you'd need to run at least 2,000,000,000 loops.

Hmmm, math check requested. Actual output from the OP's code as posted:

Calculation in Progress...
Result is 3.1415926535897932384623932776

- Wayne

Monday, February 17, 2020 1:00 AM