Converting long double in .NET

你说的曾经没有我的故事 提交于 2019-12-13 18:35:17

问题


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 used to wrap and simulate a long double? Making a wrapper in C/C++ and call it from .Net is "not possible".


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/6992628/converting-long-double-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!