Math implementation
This commit is contained in:
parent
43d38645a5
commit
d83106358f
11
build_c.sh
11
build_c.sh
@ -17,14 +17,19 @@ LINKER_ARGS="$(pkg-config --libs glib-2.0) -lm"
|
|||||||
rm -rf out bin
|
rm -rf out bin
|
||||||
mkdir out bin
|
mkdir out bin
|
||||||
|
|
||||||
args="-l c -D C -o "$OUT_FILE" $SRC_FILES"
|
|
||||||
echo "------------[fut]------------"
|
echo "------------[fut]------------"
|
||||||
|
|
||||||
|
args="-l c -D C -o "$OUT_FILE" $SRC_FILES"
|
||||||
|
if [[ $1 == '--implement-math-functions' || $2 == '--implement-math-functions' ]]; then
|
||||||
|
args="$args -D IMPLEMENT_MATH_FUNCTIONS"
|
||||||
|
fi
|
||||||
|
|
||||||
echo fut $args
|
echo fut $args
|
||||||
fut $args
|
fut $args
|
||||||
|
|
||||||
if [[ $1 != '--translate-only' ]]; then
|
if [[ $1 != '--translate-only' && $2 != '--translate-only' ]]; then
|
||||||
echo "------------[gcc]------------"
|
echo "------------[gcc]------------"
|
||||||
args="$WARNINGS $COMPILER_ARGS "$OUT_FILE" -o "$BIN_FILE" $INCLUDES $LINKER_ARGS"
|
args="$WARNINGS $COMPILER_ARGS "$OUT_FILE" -o "$BIN_FILE" $INCLUDES $LINKER_ARGS"
|
||||||
echo gcc $args
|
echo gcc $args
|
||||||
gcc $args
|
gcc $args
|
||||||
fi
|
fi
|
||||||
@ -12,12 +12,17 @@ done
|
|||||||
for src_file in $SRC_FILES; do
|
for src_file in $SRC_FILES; do
|
||||||
echo "---------[$src_file]---------"
|
echo "---------[$src_file]---------"
|
||||||
out_file="out/$(basename $src_file .fu).cs"
|
out_file="out/$(basename $src_file .fu).cs"
|
||||||
|
|
||||||
args="-l cs -D CS -n FusionCalculator $INCLUDES -o $out_file $src_file"
|
args="-l cs -D CS -n FusionCalculator $INCLUDES -o $out_file $src_file"
|
||||||
|
if [[ $1 == '--implement-math-functions' || $2 == '--implement-math-functions' ]]; then
|
||||||
|
args="$args -D IMPLEMENT_MATH_FUNCTIONS"
|
||||||
|
fi
|
||||||
|
|
||||||
echo fut $args
|
echo fut $args
|
||||||
fut $args
|
fut $args
|
||||||
done
|
done
|
||||||
|
|
||||||
if [[ $1 != '--translate-only' ]]; then
|
if [[ $1 != '--translate-only' && $2 != '--translate-only' ]]; then
|
||||||
echo "---------[FusionCalculator.csproj]---------"
|
echo "---------[FusionCalculator.csproj]---------"
|
||||||
args="build -c Release FusionCalculator.exe.csproj -o bin"
|
args="build -c Release FusionCalculator.exe.csproj -o bin"
|
||||||
echo dotnet $args
|
echo dotnet $args
|
||||||
|
|||||||
@ -11,16 +11,43 @@ abstract class FunctionCallExpression : IExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FunctionCallExpressionSin : FunctionCallExpression {
|
class FunctionCallExpressionSin : FunctionCallExpression {
|
||||||
internal override double FunctionImplementation(double x) => Math.Sin(x);
|
internal override double FunctionImplementation(double x) {
|
||||||
|
#if IMPLEMENT_MATH_FUNCTIONS
|
||||||
|
return MyMath.Sin(x);
|
||||||
|
#else
|
||||||
|
return Math.Sin(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FunctionCallExpressionCos : FunctionCallExpression {
|
class FunctionCallExpressionCos : FunctionCallExpression {
|
||||||
internal override double FunctionImplementation(double x) => Math.Cos(x);
|
internal override double FunctionImplementation(double x) {
|
||||||
|
#if IMPLEMENT_MATH_FUNCTIONS
|
||||||
|
return MyMath.Cos(x);
|
||||||
|
#else
|
||||||
|
return Math.Cos(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FunctionCallExpressionTg : FunctionCallExpression {
|
class FunctionCallExpressionTg : FunctionCallExpression {
|
||||||
internal override double FunctionImplementation(double x) => Math.Tan(x);
|
internal override double FunctionImplementation(double x){
|
||||||
|
#if IMPLEMENT_MATH_FUNCTIONS
|
||||||
|
return MyMath.Tg(x);
|
||||||
|
#else
|
||||||
|
return Math.Tan(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FunctionCallExpressionCtg : FunctionCallExpression {
|
class FunctionCallExpressionCtg : FunctionCallExpression {
|
||||||
internal override double FunctionImplementation(double x) => 1 / Math.Tan(x);
|
internal override double FunctionImplementation(double x) {
|
||||||
|
#if IMPLEMENT_MATH_FUNCTIONS
|
||||||
|
return MyMath.Ctg(x);
|
||||||
|
#else
|
||||||
|
return 1 / Math.Tan(x);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FunctionCallExpressionAsin : FunctionCallExpression {
|
class FunctionCallExpressionAsin : FunctionCallExpression {
|
||||||
|
|||||||
50
src/Expressions/MyMath.fu
Normal file
50
src/Expressions/MyMath.fu
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
//
|
||||||
|
// My implementation of math functions using Taylor (Maclaurin) series
|
||||||
|
// https://en.wikipedia.org/wiki/Taylor_series#List_of_Maclaurin_series_of_some_common_functions
|
||||||
|
//
|
||||||
|
public class MyMath {
|
||||||
|
/// TODO: fix fail on tg(pi/2), tg(pi*), ctg(pi*2)
|
||||||
|
static double ClampRadians(double x){
|
||||||
|
int quotient = 0;
|
||||||
|
double pi2 = 2*Math.PI;
|
||||||
|
native {
|
||||||
|
quotient = (int)(x / pi2);
|
||||||
|
}
|
||||||
|
x -= pi2 * quotient;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double Sin(double x){
|
||||||
|
x = ClampRadians(x);
|
||||||
|
int iters = 16;
|
||||||
|
double pow = x;
|
||||||
|
double fact = 1;
|
||||||
|
double result = x;
|
||||||
|
for(int i = 3; i <= (2*iters+1); i+=2){
|
||||||
|
pow *= x*x; // x power +2
|
||||||
|
fact *= i * (i-1); // making i! from (i-2)!
|
||||||
|
fact *= -1; // change sign every iteration
|
||||||
|
result += pow/fact;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double Cos(double x){
|
||||||
|
x = ClampRadians(x);
|
||||||
|
int iters = 16;
|
||||||
|
double pow = 1;
|
||||||
|
double fact = 1;
|
||||||
|
double result = 1;
|
||||||
|
for(int i = 2; i <= (2*iters); i+=2){
|
||||||
|
pow *= x*x; // x power +2
|
||||||
|
fact *= i * (i-1); // making i! from (i-2)!
|
||||||
|
fact *= -1; // change sign every iteration
|
||||||
|
result += pow/fact;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double Tg(double x) => Sin(x)/Cos(x);
|
||||||
|
|
||||||
|
public static double Ctg(double x) => Cos(x)/Sin(x);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user