I have to convert some C code (that ran in a Linux machine) to .Net, and found some operations with long double. They are not special operations, just some multiplications and divisions that end with a lot of decimals, it seems the type was used just because of its size.
What would you do in order to convert the code? I'm thinking of just multiply the values and divide back at the end to see if it helps since the code is anyway returning short values at the end, plus I don't know if the original binary was indeed using all the size a long double can provide or if it was just using a common 64-bit double under the hood.
If you don't think this is a good idea, do you know of any class or structure that can be u开发者_StackOverflowsed to wrap and simulate a long double? Making a wrapper in C/C++ and call it from .Net is "not possible".
It's most likely that the C long double actually mapped to a double and so without knowing more about the calculations I'd assume you were fine to use double in C#.
In my experience it's very rare to find a scientific or engineering calculation for which 64 bit double is not sufficient.
The only way to be 100% certain that double is sufficient would be to study the algorithm so this advice is based on instinct and experience alone.
In .net, you have decimal which is highest precision real type available as of now. Decimal is mostly used for financial calculations as it's precise and it has larger range. However for scientific calculations it is not recommended as it is slow compared to double.
You might try System.Decimal
(aka decimal
in C#) - this is a 96-bit number, with base-10 arithmetic (rounding etc), making it particularly suitable for scenarios involving things like currency. It doesn't map to IEEE, but depending on the C compiler, neither does long double
;p Note that it is implemented in software.
精彩评论