0 purchases
tailrec
Tailrec (anyone-dart-tailrec) #
This package help you to convert recursive function into the while-loop codes. so that program won't meet out of memory that caused by stack overflow. inspired by kotlin tailrec keywords. (https://kotlinlang.org/docs/functions.html#tail-recursive-functions)
If you like my module, please buy me a coffee.
More and more tiny and useful GitHub action modules are on the way. Please donate to me. I accept a part-time job contract. if you need, please contact me: [email protected]
How to use #
Math concepts 高中数学中的函数"不动点" #
Calculate fixed point for cos(x) #
import 'dart:core';
import 'package:tailrec/tailrec.dart';
import 'dart:math';
final eps = 1E-10; //precision
void main() {
final firstValue = 1;
var tailrec = Tailrec(
conditionCallback: <double>(dynamic x) {
final y = cos(x);
final temp = (x - y).abs();
return temp < eps;
},
recurringParamComputeCallback: <double>(dynamic x) {
return cos(x) as double;
},
resultCallback: <double>(dynamic x) {
return x;
},
);
final fixedPoint = tailrec.apply(firstValue);
print(fixedPoint);
}
copied to clipboard
Conclusion #
cos(x) fixed point approximately equal to 0.7390851332151607
cos(0.7390851332151607) ≈ 0.7390851332151607
Donation #
PalPal: https://paypal.me/nzhang4
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.