1

I have a very large (and old and ugly structured, ported from Fortran to C#) mathematical library, where all calculations are now done using doubles (as there were inaccuracies). However, to compare the results with the results from the old implementation, we sometimes have to switch back to floats - to check that the ported code is correct.

My idea would be to somehow generate a second library from the first one, where all variables and called methods are replaced by the corresponding float implementation. Is there any automatic solution to allow switching between floats and doubles without manually implementing all the code twice? I am talking about more than 30000 lines of code.

LionAM
  • 1,271
  • 10
  • 28
  • I can see 3 things you can do. 1: `Rewrite the whole library in C# and replace float by double` 2: `Use your existing library and cast from double to float on all calls, pretty pointless and I would suggest just using float if you go with this, unless there could be miscalculations` 3: `Use your existing library and call with float` – Bauss Aug 21 '15 at 12:34
  • I must agree with Bauss, there's no way to make the library generic and restrict it to types of either float or double. However, if you really need to do this, you could just implement every method with an overload that accepts either a double or a float. – GeorgDangl Aug 21 '15 at 12:38
  • Even using a generic does not help you ( so you could do something like `Add(0.3,0.4)`. See http://stackoverflow.com/questions/1348594/is-there-a-c-sharp-generic-constraint-for-real-number-types – Sascha Aug 21 '15 at 12:38
  • @Sascha: Indeed this would be possible. See http://stackoverflow.com/questions/600978/how-to-do-template-specialization-in-c-sharp/29379250#29379250 However, its not a task which I would like to do manually on this code base. – LionAM Aug 21 '15 at 12:49
  • @LionAM nice finding – Sascha Aug 21 '15 at 12:52

1 Answers1

3

Code generation is probably your best option - using the same source codes twice, but replacing all float operations with double with some global replace in the other set automatically.

If you want to keep to pure C# source codes, you could abuse the using directive. Have two separate projects - one for float, another for double. Have both of them use the same source code files (one will have them physically, the other will only have links). One of the projects will have a conditional compilation constant FLOAT. Instead of using float or double in your source code, you will use e.g. Number, defined as

#if FLOAT
using Number = float;
#else
using Number = double;
#endif

The proper type for Number will be substituted at compile-time, so everything like type inference etc. works properly. Visual Studio 2015 will even warn you if there's issues in the source that only impact one of the projects using the same linked source code file.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • @dman2306 Well, if there's no clean and idiomatic option, dirty isn't *really* dirty - it may still be the optimal solution. The annoying part is that you can't keep both implementations in the same project, because there's no way to e.g. have one compiler constant apply for one namespace, and another for another. – Luaan Aug 21 '15 at 12:57