Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit e852c58

Browse files
committed
make nan, +/-inf at runtime, avoid compiler errors
Fix issue #93 Add simple test (that also functions as a warning to devs) in Demo.cs
1 parent b588f00 commit e852c58

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Demo Plugin/NppManagedPluginDemo/Demo.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ static internal void CommandMenuInit()
136136
PluginBase.SetCommand(16, "---", null);
137137

138138
PluginBase.SetCommand(17, "Print Scroll and Row Information", PrintScrollInformation);
139+
140+
PluginBase.SetCommand(18, "Use NanInf class for -inf, inf, nan!!", PrintNanInf);
139141
}
140142

141143
/// <summary>
@@ -443,6 +445,21 @@ static void DockableDlgDemo()
443445
}
444446
frmGoToLine.textBox1.Focus();
445447
}
448+
449+
static void PrintNanInf()
450+
{
451+
bool neginf_correct = double.IsNegativeInfinity(NanInf.neginf);
452+
bool inf_correct = double.IsPositiveInfinity(NanInf.inf);
453+
bool nan_correct = double.IsNaN(NanInf.nan);
454+
string naninf = $@"-infinity == NanInf.neginf: {neginf_correct}
455+
infinity == NanInf.inf: {inf_correct}
456+
NaN == NanInf.nan: {nan_correct}
457+
If you want these constants in your plugin, you can find them in the NanInf class in PluginInfrastructure.
458+
DO NOT USE double.PositiveInfinity, double.NegativeInfinity, or double.NaN.
459+
You will get a compiler error if you do.";
460+
notepad.FileNew();
461+
editor.AppendTextAndMoveCursor(naninf);
462+
}
446463
#endregion
447464
}
448465
}

Demo Plugin/NppManagedPluginDemo/NppManagedPluginDemo.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\UnmanagedExports.cs">
115115
<Link>PluginInfrastructure\UnmanagedExports.cs</Link>
116116
</Compile>
117+
<Compile Include="..\..\Visual Studio Project Template C#\PluginInfrastructure\NanInf.cs">
118+
<Link>PluginInfrastructure\NanInf.cs</Link>
119+
</Compile>
117120
<Compile Include="Forms\frmGoToLine.cs">
118121
<SubType>Form</SubType>
119122
</Compile>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Kbg.NppPluginNET.PluginInfrastructure
2+
{
3+
/// <summary>
4+
/// holds NaN, Infinity, and -Infinity as things generated at runtime<br></br>
5+
/// because the compiler freaks out if it sees any of<br></br>
6+
/// double.NegativeInfinity, double.PositiveInfinity, or double.NaN<br></br>
7+
/// or any statically analyzed function of two constants that makes one of those constants<br></br>
8+
/// like 1d/0d, 0d/0d, or -1d/0d.
9+
/// </summary>
10+
public class NanInf
11+
{
12+
/// <summary>
13+
/// a/b<br></br>
14+
/// may be necessary to generate infinity or nan at runtime
15+
/// to avoid the compiler pre-computing things<br></br>
16+
/// since if the compiler sees literal 1d/0d in the code
17+
/// it just pre-computes it at compile time
18+
/// </summary>
19+
/// <param name="a"></param>
20+
/// <param name="b"></param>
21+
/// <returns></returns>
22+
public static double Divide(double a, double b) { return a / b; }
23+
24+
public static readonly double inf = Divide(1d, 0d);
25+
public static readonly double neginf = Divide(-1d, 0d);
26+
public static readonly double nan = Divide(0d, 0d);
27+
}
28+
}

0 commit comments

Comments
 (0)