4

I'm attempting to write an interval arithmetic library in C# .NET, but in order to do this accurately I need to be able to control the rounding mode of floating point operations.

After a bit of research, it has become apparent that in order to do this I will need to call some unmanaged C/C++ code from C#, as per this forum. I know very little C++, so I'm not sure which function I actaully need to call: Change floating point rounding mode seems to suggest using fegetround. My question is, is this the right function to go about calling from C# (and if so how should I go about calling it - using DllImport as per this question?), and will this change the rounding mode for the managed C# code?

Community
  • 1
  • 1
acernine
  • 729
  • 2
  • 6
  • 16
  • 1
    Google "c# _controlfp" for hits. Do beware that this is completely illegal, the CLR assumes you never do this. You'll get away with it when you restore it again immediately after the math. But surely there is a solution that doesn't require using a sledgehammer. You are not helping us to help you. – Hans Passant Nov 06 '16 at 13:12
  • Are you building your project as Debug or Release? Debug is dong tghe FPU in software which is very slow. Release uses the MPU FPU which is much faster. – jdweng Nov 06 '16 at 13:23
  • @jdweng I wonder where you got that nonsense from. Both modes use hardware, just check the assembly output. – Lucas Trzesniewski Nov 06 '16 at 13:25
  • @HansPassant Thanks for the replies guys/girls. I do need to ensure that all floating point calculations in a portion of my code round up (or down - as long as it is consistent). As for changing the FPU that is the only solution I have come across while searching, but if there as another, safer / easier solution I would certainly appreciate hearing it! If you need any more details just let me know. – acernine Nov 06 '16 at 13:32
  • I have seen differences in speed and output between the two methods. Microsoft probably has made changes in VS/Net between the version you are using and the one I have been using. – jdweng Nov 06 '16 at 13:32

0 Answers0