Switch function from runtime to compile time

Michelle Long HappyDance321 at gmail.com
Fri Mar 15 03:05:43 UTC 2019


On Thursday, 14 March 2019 at 11:38:44 UTC, alex1974 wrote:
> 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?

Just store them to a file and read the file at compile time if it 
exists... quite simple.

if (exists(file)) import(file);
elseif (optimal) write(file);

If you need to retrain just remove the file or use something like 
version.





More information about the Digitalmars-d-learn mailing list