There is a of lot room for improvements in your code so let us start straight ahead with your class
###functions
functions
Based on the .NET Naming Guidelines classes should be named using
PascalCase
casing which isn't the only problem here. Naming things is hard but its much harder to grasp at first glance what a class, method or field is used for if one uses names likefunction
,MyFun
orfun1
..fun3
. If you come back in a few weeks/months to fix a bug or to add a feature you still need to understand quickly what the code is doing which will be harder if you keep these names.Math.Pow(x,2)
should always be replaced byx * x
which will be faster
###Bisection
Bisection
You are executing
fun(start)
at least one time but up to four times. By storing the result of the method call inside a variable you code will be easier to read and also be faster. The same applies tofun(end)
.double avg,tolerance,sign;
don't do this. Its to hard to read. Always declare one variable per line.omitting braces
{}
is valid in C# but it is dangerous as well because it can lead to hidden and therefor hard to find bugs. But if you decide for yourself to take the risk you should at least be consequent with your style. Right now you are sometimes using braces and sometimes you don't.If a value of a method parameter doesn't fit in the range of an expected value one should throw an
ArgumentOutOfRangeException
or if the value is plainly wrong one should throw anArgumentException
instead of writing to the console and returning a magic number (-1
).Numbers used like
tolerance > 0.0001
should be stored in a meaningful named constant so every reader of the code exactly knows what0.0001
represents.
There is a of lot room for improvements in your code so let us start straight ahead with your class
###functions
Based on the .NET Naming Guidelines classes should be named using
PascalCase
casing which isn't the only problem here. Naming things is hard but its much harder to grasp at first glance what a class, method or field is used for if one uses names likefunction
,MyFun
orfun1
..fun3
. If you come back in a few weeks/months to fix a bug or to add a feature you still need to understand quickly what the code is doing which will be harder if you keep these names.Math.Pow(x,2)
should always be replaced byx * x
which will be faster
###Bisection
You are executing
fun(start)
at least one time but up to four times. By storing the result of the method call inside a variable you code will be easier to read and also be faster. The same applies tofun(end)
.double avg,tolerance,sign;
don't do this. Its to hard to read. Always declare one variable per line.omitting braces
{}
is valid in C# but it is dangerous as well because it can lead to hidden and therefor hard to find bugs. But if you decide for yourself to take the risk you should at least be consequent with your style. Right now you are sometimes using braces and sometimes you don't.If a value of a method parameter doesn't fit in the range of an expected value one should throw an
ArgumentOutOfRangeException
or if the value is plainly wrong one should throw anArgumentException
instead of writing to the console and returning a magic number (-1
).Numbers used like
tolerance > 0.0001
should be stored in a meaningful named constant so every reader of the code exactly knows what0.0001
represents.
There is a of lot room for improvements in your code so let us start straight ahead with your class
functions
Based on the .NET Naming Guidelines classes should be named using
PascalCase
casing which isn't the only problem here. Naming things is hard but its much harder to grasp at first glance what a class, method or field is used for if one uses names likefunction
,MyFun
orfun1
..fun3
. If you come back in a few weeks/months to fix a bug or to add a feature you still need to understand quickly what the code is doing which will be harder if you keep these names.Math.Pow(x,2)
should always be replaced byx * x
which will be faster
Bisection
You are executing
fun(start)
at least one time but up to four times. By storing the result of the method call inside a variable you code will be easier to read and also be faster. The same applies tofun(end)
.double avg,tolerance,sign;
don't do this. Its to hard to read. Always declare one variable per line.omitting braces
{}
is valid in C# but it is dangerous as well because it can lead to hidden and therefor hard to find bugs. But if you decide for yourself to take the risk you should at least be consequent with your style. Right now you are sometimes using braces and sometimes you don't.If a value of a method parameter doesn't fit in the range of an expected value one should throw an
ArgumentOutOfRangeException
or if the value is plainly wrong one should throw anArgumentException
instead of writing to the console and returning a magic number (-1
).Numbers used like
tolerance > 0.0001
should be stored in a meaningful named constant so every reader of the code exactly knows what0.0001
represents.
There is a of lot room for improvements in your code so let us start straight ahead with your class
###functions
Based on the .NET Naming Guidelines classes should be named using
PascalCase
casing which isn't the only problem here. Naming things is hard but its much harder to grasp at first glance what a class, method or field is used for if one uses names likefunction
,MyFun
orfun1
..fun3
. If you come back in a few weeks/months to fix a bug or to add a feature you still need to understand quickly what the code is doing which will be harder if you keep these names.Math.Pow(x,2)
should always be replaced byx * x
which will be faster
###Bisection
You are executing
fun(start)
at least one time but up to four times. By storing the result of the method call inside a variable you code will be easier to read and also be faster. The same applies tofun(end)
.double avg,tolerance,sign;
don't do this. Its to hard to read. Always declare one variable per line.omitting braces
{}
is valid in C# but it is dangerous as well because it can lead to hidden and therefor hard to find bugs. But if you decide for yourself to take the risk you should at least be consequent with your style. Right now you are sometimes using braces and sometimes you don't.If a value of a method parameter doesn't fit in the range of an expected value one should throw an
ArgumentOutOfRangeException
or if the value is plainly wrong one should throw anArgumentException
instead of writing to the console and returning a magic number (-1
).Numbers used like
tolerance > 0.0001
should be stored in a meaningful named constant so every reader of the code exactly knows what0.0001
represents.