The domain of a function in javaScript
When getting into writing functions in javaScript there are the things that have to do with how functions work in javaScript, but then there are all kinds of things that have to do with functions in general. That is things that do not just apply to functions in javaScript, but any language for that matter. With that said todays post is on the subject of the domain of a function in javaScript.
A function domain as it some times might be called is the full range of arguments that are possible for a given function. So say I have a function that accepts a single argument that represents a singe side of a six sided die, in that case the range for the one argument would be the whole numbers 1 threw 6. However many functions will have a very wide range for an argument, and on top of that float numbers can be used. ALso often a function will have more than one argument, on top of having a wide range for one or more arguments. This can result in w very wide range of possibles for the domain of a function, making it hard to create a way to graph all possibles, or run threw all possible combinations of calls to make sure the function will always work as expected for all possible input values.
1 - Basics of function domain in javaScript
In this section I will just be going over a few simple examples of function domain in javaScript. These will include examples that involve creating the whole range of possible input values for a function when it is possible to do so without eating up to much overhead. In other cases some other means will have to be used to run threw just a fraction of the function domain because there are just way to many possible input values.
1.1 - Creating a function domain
Lets say I just have a very simple function that will just take the numbers 0 there 10 as the first and only argument. On top of that the numbers that are expected are whole numbers so that means that the full function domain can just be a small array of numbers. So for this example it is possible to have a create domain function that will create an array of all possible values for the function. I can then use the array map method and call the function for each value in the domain array to get all possible return values for the domain also while I am at it.
|
|
1.2 - Example using Math.sin
The full scope of the function domain for the Math.sin method might be to high to create an array for, because in a way it is any number between negative and positive infinity for the range of javaScript numbers. However there is not always creating the full domain for a function, just a given range that reflects some kind of typical use case. When it comes to using the Math.sin function I often one use case is to use it to get the y axis value for a point relative to a given origin. So I might not need to do the full possible domain as that would be way to large, but I can do it for say, just 8 directions.
|
|
2 - Conclusion
The domain of a function is something that I want to run threw when it comes to testing a function that I wrote to make sure that I get experted results for all possible calls. However some times it might not be possible to do so, or it would require a whole lot of processing power and time to do so at least. So some times I test out a function by just going threw part of a range, or by a certain stepping value.
Often it might be a good idea to graph output, or create some kind of visual aid as a way to make sure that a function is working the way that it should for at least some kind of domain. Typically there is creating a simple graph using a canvas element for example.