0

I'm working on a project in C# on .NET Framework 4.0, and the mere presence of NaN, -inf, and +inf in my code (see the sample below) causes a compiler error.

This project is based on the NotepadPlusPlusPluginPack.NET, and indeed the same error appears when I introduce those constants in the demo plugin that comes with that repo.

EDIT: It's definitely somehow linked to the plugin pack mentioned above (and speculatively its IlMerge dependency in particular), because I can use those constants in a new .NET 4.0 project with no problems.

For example, any of the first three lines in the block below will raise an error.

double nan = double.NaN;
double neginf = double.NegativeInfinity;
double inf = double.PositiveInfinity;
if (nan == inf || inf == neginf) { }

And no, I don't have to assign a variable to one of those three; just mentioning one of them will throw an error.

My project builds fine otherwise. I can replace every instance of -inf, NaN or +inf with 1d or some other double, and my project will build. And it's not like it builds but it's somehow horribly flawed; every other feature is fine as far as I can tell.

This problem occurs whether I use Visual Studio 2022 or Visual Studio 2019.

Here's a representative line of code and associated error message:

double inf = double.PositiveInfinity;
Severity    Code    Description Project File    Line    Suppression State
Error       
<source code filename>.cs(784) : error : syntax error at token 'inf' in:     IL_03db:  ldc.r8     inf   JsonToolsNppPlugin          

I have built previous projects using these constants in .NET 6.0 and I never had this problem. I just rebuilt another unrelated project in .NET 6.0 and it still works fine.

I can also try introducing these constants into other unrelated projects and they now error out.

molsonkiko
  • 31
  • 5
  • Which line of your code is actually giving you a problem? Perhaps more code context with the line causing errors with the compiler specified would help. – Ibrennan208 Aug 24 '22 at 20:59
  • I did some more digging and it looks like it might have something to do with ILASM, whatever that means: [https://github.com/3F/DllExport/issues/128](https://github.com/3F/DllExport/issues/128) and [https://stackoverflow.com/questions/66282361/ilasm-problem-when-dealing-with-double-nan-and-inf](https://stackoverflow.com/questions/66282361/ilasm-problem-when-dealing-with-double-nan-and-inf) – molsonkiko Aug 24 '22 at 21:08
  • lbrennan208, thanks for replying! I don't think that context actually helps in this case because the error *always* appears, no matter where those constants appear in my project. – molsonkiko Aug 24 '22 at 21:09
  • Are you creating a project, disassembling it, and then reassembling it with ILASM? Or is this just a "standard" c# project in visual studio? – Ibrennan208 Aug 24 '22 at 21:13
  • https://learn.microsoft.com/en-us/dotnet/framework/tools/ilasm-exe-il-assembler – Ibrennan208 Aug 24 '22 at 21:13
  • I didn't even know what ILASM was until I started googling search terms like `"double negativeinfinity raises syntax error c#"`. So I'm only guessing that because it seems to be a recurring term in the things I read about. – molsonkiko Aug 24 '22 at 21:19

1 Answers1

0

This is a hacky solution, but it's better than nothing: generate NaN, inf, and -inf at runtime with something that doesn't get pre-compiled.

using System;

namespace YourNameSpace
{
    public class NanInf
    {
        /// <summary>
        /// a/b<br></br>
        /// may be necessary to generate infinity or nan at runtime
        /// to avoid the compiler pre-computing things<br></br>
        /// since if the compiler sees something like literal 1d/0d in the code
        /// it just pre-computes it at compile time
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double Divide(double a, double b) { return a / b; }

        public static readonly double inf = Divide(1d, 0d);
        public static readonly double neginf = Divide(-1d, 0d);
        public static readonly double nan = Divide(0d, 0d);
    }
}

I tested this in the code where I was having trouble and it works fine.

molsonkiko
  • 31
  • 5