- Home /
High precision numbers
I have numbers that need precision of at least 10 decimals, while they are as big as 10^8.
Is this possible??
Answer by duck · May 22, 2010 at 01:02 PM
Yes this is possible.
Because Unity runs Mono, you have access to all the standard .net base types. There are a number of different base types of varying precision which you can use to hold numeric values:
Name Type Signed Bytes Values
sbyte System.Sbyte Yes 1 -128 to 127 short System.Int16 Yes 2 -32768 to 32767 int System.Int32 Yes 4 -2147483648 to 2147483647 long System.Int64 Yes 8 -9223372036854775808 to 9223372036854775807 byte System.Byte No 1 0 to 255 ushort System.Uint16 No 2 0 to 65535 uint System.UInt32 No 4 0 to 4294967295 ulong System.Uint64 No 8 0 to 18446744073709551615
float System.Single Yes 4 Approximately 1.5 x 10-45 to 3.4 x 1038 with 7 significant figures
double System.Double Yes 8 Approximately 5.0 x 10-324 to 1.7 x 10308 with 15 or 16 significant figures
decimal System.Decimal Yes 12 Approximately 1.0 x 10-28 to 7.9 x 1028 with 28 or 29 significant figures
In your case, it sounds like you'd want to use either double or decimal.
double has a lower precision, but a wider range of potential exponents, and - like float - is not "decimally accurate" becuase it stores values as a binary floating point type.
decimal has much higher precision, and is "decimally accurate" because it stores values as a decimal floating point type. For this reason, it's generally used for "naturally exact decimal values", such as financial calculations.
I can't really test it, because System.Decimal vars don't show up in Inspector panel... :( How can I view them??
@Duck have you actually tried these data types? Even using System.Decimal it seems Unity implicitly typecasts it to just float precision (i.e. 6 decimal digits)
Answer by adrianseeley · May 17, 2015 at 02:56 PM
If you came here looking for how to use UInt64
or UInt32
in Unity - most likely as a result of getting a compiler error when using one; Then simply include:
using System;
At the top of your script and the errors should go away.