I am writing a class that has a bunch of metric properties of ulong data type
class Metrics {
public ulong Memory
public ulong Handles
public ulong Calls
}
The reason I use ulong is because it's a fact that those values are unsigned and the capacity of signed will not be enough.
But I also initiate another instance of this class to store Deltas of those values, the change between the last check and the current.
The problem I am having is that sometimes the number can go down, thus the delta is a negative value, but I cannot store it on the ulong.
Even if I declare the Delta as a regular LONG, if number goes up to the ulong max from 0 it will not fit and will throw an error.
How could I accomplish storing the delta value of a ulong knowing this?
class myData {
public Metrics Deltas
public Metrics Data
ulong myValue = ulong.MaxValue;
Deltas.Calls = (myValue - Data.Calls);
Data.Calls = myValue;
// Delta will be +MaxValue
myValue = 0;
Deltas.Calls = (myValue - Data.Calls);
Data.Calls = myValue;
// Delta will be -MaxValue, unsigned, cannot store it
}
Adding/subtracting two 64-bit numbers produces a 65-bit result, so just use Int128 if you use .NET Core 7.0 Preview 5 or newer. And don't use
classif you just need to store data like this, astructorrecordwould be far better because they can live on stackIf you're on an old .NET framework then the most efficient solution is to implement your own 65-bit data type. It should be very simple because for printing and sorting purposes multiplication and division won't be needed. You just need to implement
+/-and comparison operators like this