Switch function from runtime to compile time

alex1974 alex.leisser at gmail.com
Thu Mar 14 11:38:44 UTC 2019


I have several geometric shapes (triangle, trapezoid, gauss, ...) 
forming the membership functions of a fuzzy set.
For example the shape of the triangle is defined by the variables 
a, b and c. The function calculating membership looks like:

real triangle (real a, real b, real c, real value) {
   if (value <= a || value >= c) return 0.0;
   else if (value <= b) return (x-a)/(b-a);
   else return (c-x)/(c-b);
}

Intuitiv I packed this in a class:

class Triangle {
   real a,b,c;
   real getValue (real value) {
     ... // math as above
   }
}

My question is if this is the best practice. During the learning 
process of the fuzzy logic the shape of the triangle will change.
But once I found the optimal shape the triangle will be fixed and 
the program could be recompiled with the optimal shapes. The 
compiler could then perform optimization of the code at 
compile-time. Look at the term (b-a) and (c-b) which are then 
known at compile-time.
  How can I achieve this without writing duplicate code for 
runtime and compile-time?



More information about the Digitalmars-d-learn mailing list