What common Math errors should I be aware of when using .NET on x32 and x64 platforms?
-
28-09-2019 - |
Pergunta
What should I keep in mind when performing calculations using .NET?
For example, I know a little about floating point errors, but am unfamilliar with this forum post on CodeProject. What do I need to know to complete my knowledge of .NET-based math so that I can advise how to work with parameters and results of varying bit-sizes.
Solução
Always remember that floating point stores binary fractions, i.e. your number will be represented with a summation series of 2^(-n) where n is any integer from 1 to (mantissa size) raised to some exponent N. Use decimal
for financial calculations.
Also remember that 64-bit word read/writes on a 32-bit processor are not atomic and thus are not thread-safe. More generally, try your best to not expect that shared mutable state is always safe to read.
Outras dicas
You need to remember that different numeric types have different ranges: take a look at the static MinValue
and MaxValue
properties. If you exceed these ranges (by addition, multiplication or whatever) then the answers won't be what you think they should be. (Numbers wrap around from the maximum back to the minimum.)