1

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".

Neverbirth
  • 1,044
  • 8
  • 20
  • 2
    `long double` is very vaguely defined - what are the exact requirements here (in numerical terms)? – Marc Gravell Aug 09 '11 at 07:21
  • I say use double and see if it really matters. 99% it's long double for no real reason. – Petr Abdulin Aug 09 '11 at 07:24
  • 2
    @Petr indeed, depending on the C compiler, `long double` can simply be an alias for... `double`. – Marc Gravell Aug 09 '11 at 07:26
  • Yep, I'm aware. I don't know what to reply exactly about the exact requirements tho, sorry. I don't have any output value sample, but looking at the constants and some of the operations used, it seems to need a lot of decimals in place. Will get some sample values and write back. – Neverbirth Aug 09 '11 at 07:35
  • I know in a lot of cases long double is just an alias for double, specially in Windows environments AFAIK, that's why I pointed it was used on a Linux machine (although it doesn't have to mean anything). I've already asked for some values, but while waiting for them I'd like to know which are the best ways to solve this problem if double doesn't fit. – Neverbirth Aug 09 '11 at 07:41

3 Answers3

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

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.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • I know, but decimal doesn't suit my needs. This is indeed for scientific calculations. Double would be more suited, but I don't know if it would be good enough. – Neverbirth Aug 09 '11 at 07:26
0

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • It seems I've been somewhat fooled heh. I've talked with the author of the code, he says he changed the code from double to long double a week before the final release, and isn't even sure if it's really needed, but if it is, I should see strange results. Hope you don't mind I mark David's answer as the correct one. I'm against using decimal in this case, but your previous comments were also right, so voted them. – Neverbirth Aug 09 '11 at 08:28