Thursday, June 15, 2023

Doing something

 Kind of realized that some times, doing something can distract you from getting something done.

I had decided that I needed an interface then found the INumber generic interface. However, I still hadn't done the work to decide what I needed or how I was going to make it work.

So, lets do that now.

What is a number. It is a value, a quantity. Mathematicians have determined that these values have certain properties, main among them, that they can be discussed in a meaningful way separate from the object that they are describing the quantity of. Mathematically 19 oranges, is the same as 19 inches, is the same as 19 days, which is the same as 2013 math nerds (2013 is 19 expressed in base 3, will get to bases later) (and you can find 19 math nerds in the same place, though it usually a math class, or a comic con).

So, what does a number data type need. INumber says 50 some functions, and it is right to some degree. However, those functions tell us attributes of the number or defines ways to interact with other numbers, but the most important thing is the value. It is a data type and we need to have the data. What is the data with a number, well the value. So, how do you store the value? Well, that was some of what I wanted to explore with this project. With the OutrageousInt I started with, the value is an int. And yes, I'm wrapping an int in an unnecessary class, but this whole thing is in and of itself, unnecessary. I am doing it to try and learn something, and experiment with algorithms and maybe help someone else.

Now, does the value have any properties, or need any properties to ease calculation or interaction with other classes implementing INumber? Well, that is the question, but perhaps I need to start with a naïve implementation. Currently, I believe that my int version is finished or close to it, so maybe I need to create a unit testing project to be used on the OutrageousNumbers, and perhaps some sort of simple app to play with the numbers. 

Monday, June 12, 2023

Implementing INumber

 I thought it might be useful for some to describe my tool set. I am currently using Visual Studio Community 2022, I typically keep it up to date. I am using .NET 7.0 and C# 11.0. I use Git for version control. I'm using GitHub for storage of the repository and management of the project. I am also currently experimenting with GitHub Copilot for code suggestions and assistance.

My intention is to create a branch for each blog post or perhaps group of blog posts, depending on how tasks, or ideas get broken up. I will then try to check in every major step of the way. The banch for today is, ImplementINumber.

First I will add a Value. because a number has a value. This is not part of the interface, but may be used by the interface. I am using a property because it is a practice that I am used to and is common in the offices that I have worked in. Specifically using what is know as an auto property. Technically any data stored in a class is a field, a property is a set of methods that set the data and get the data. Using methods for access allows for validation and alteration as needed either on setting or getting the data. A standard property is written - 

private int _value;
private int Value
{
  get { return _value; }
  set { _value = value; }
}

I realize that I am explaining things in a haphazard manor. If you feel that I'm being too detailed feel free to skip any description, paragraph, post or the whole blog. If you feel that there is something that you need explained further, please feel free to ask, if I cannot answer I may be able to help you find the answer.

Returning to properties, the previous code snippet is a fairly standard property/field code snippet. If only there were a simpler way to write it that functionality in .NET would be able to replace with the structure above in the background. Well, there is -

private int Value { get; set; }

The backing field is assumed, as is the code for the get and the set. This format does allow for the declaration of the get or set, if it is needed. This is what is known as an auto property.

At this point Quick Actions can be used to create all the interface functions. It seems that there were some limitations on the Quick Actions. But there are other Quick Actions to add more for the interface. I may want to make value protected and not private. I may even end up making my own interface to add properties. But I will finish making the int number first.

I feel like I've lost the plot. I need to reset.

Saturday, June 10, 2023

Interface, IReallyBigNumber Or Something

In the last entry I talked about designing an interface for a number. So, what is an interface? I always struggle with how deep to go in answering some questions. I currently work in C#. C# is an object-oriented language, meaning that data is structured into classes. A class is a collection of data about a specific thing, and occasionally functions related to that class or contained data. Now one of the concept of object-oriented programming (oop) is polymorphism. Polymorphism is the idea that objects of different classes can be sent to a function of a specific class. One of the ways of supporting this is called an interface. It is like a class, but only has function declarations. Any function call that requires that interface will accept an object of a class that implements that interface.

So, what does a number need for it's implementation? One camp of oop says that all data fields should only be accessible/alterable through functions and methods. This ensures that all operations and changes to the data follow the rules of the class, this is especially important when creating several classes that are supposed to react is similar manners and be processable from the same functions. The argument being that none of the external services need to know what the internal data is or how it is processed.

So, bare minimum for a number is equals, add, subtract, multiply, divide, and compare (greater than, less than). There may be others that I may want / need to keep the coding internally. For the algorithms I am thinking I want to attempt, the basic parts may be enough to get it all working.

I had created a repository to hold the code I am trying, but decided that I had not started it right, so I dumped it and started a new repository. I will try and keep commit labeled in such a way to indicate what is being done and which blog entries they belong to. The current repository is OutrageousNumbers. I found a website that will check some of the most popular repositories for open source code for a name, after trying a couple I got frustrated and tried OutrageousNumbers.

While I was doing other stuff I decided to see if the "primitive" types had an interface, despite the fact that this whole project is about doing work that has already been done, I didn't feel like the interface should be repeated if an acceptable one was in use in the main parts of .Net. I found out that they don't seem to, but that in .Net 7 some Generic Math libraries were added including an INumber<Tself> interface. So, I do not need to create the interface and it already is more detailed than I was expecting to start at.

I think I will start by creating an Outrageous version of the int, not a large version, just to explore the interface and work with the many parts of that. I will check in the initial creation of the project and the class, and we will do more from there in the next installment.

Tuesday, June 6, 2023

Design Document

 When I can I try and do some programming for myself. It is not as often as I would like, as I have a family and can never keep a computer in good shape, but I do try it from time to time.

Sometimes I try and put forward a challenge. One I did previously came out of a project for work (a previous job). That team was big on testing, so I created a tool that could take two unspecified inputs and determine if they are equal. I created it in C# and .Net, and use that languages reflection capability to determine the parts of an object and compare them to the same part of the other if it exists. It is called GenericDeepEquality, the code is on GitHub and I have published a NuGet package, located here.

The current challenge I am undertaking is creating a library for Math with Big Numbers. Also, maybe, designing it to work for any base number system. This isn't to make the next great Math library, it's just to figure out how to do it and play with algorithms for mathematical functions.

So, now it time to work through some ideas to determine where I am going to start.

The main issue of a big number is the big number and how to handle it. In C# an integer is represented by an int. An int has the ability to represent a number between -2,147,483,648 to 2,147,483,647, That's a lot of number space, but it's not enough for the federal budget, for example, and there are applications that require much larger numbers, like cryptography.

Additionally, there are technically three main parts of a number variable, assigning the number a value, storing that value and displaying that value. Some might ask about manipulating the value, but with math, the operations usually produce new values, so that is different, also, that is a different part of the processes in general.

Terminology: As I am using it, not necessarily completely accurate to math.

  • digit - one "character" of a number. 19 has the digits of 1 and 9. Digit may seem to imply base 10, but I am using it in a more generic manner.
  • significant digit - a representation of the position of a digit in a number. Standard terminology has the "least significant digit" being the digit representing the zeroth power of the base in the number and the "most significant digit" being the digit representing the highest power of the base in the number. In 19, the most significant digit is 1 and the least significant digit is 9. 
  • base - The number of values available for a single digit. For example base 2 have 0 & 1, where as base 10 has 0, 1, 2, 3, 4, 5, 6, 7, 8 & 9.  A number can be written as the sum of a series of single digit numbers times a power of the base representing it's location in then number. 19 can be written as 1*101 + 9*100

Before we can do anything else we need to determine how we are going to store the value. Now, much like you can write a number larger than 9 by adding another digit to the left (10), we can create an array of ints, such that the following ints would represent the next significant "digit" in the number. So, an array of integer values (int being one possible type). Of course, now as I'm thinking about it, I should do them all.

So, what I'm thinking now is I need to create an interface, so that all of the different versions can be used in the same algorithms. Now I need to figure out what I need for a number interface.

I'm going to put this out there and come back to designing the interface.