MathUtils.xtend 778 B

123456789101112131415161718192021222324252627
  1. package ua.ansymo.hintco
  2. import java.util.HashMap
  3. import org.eclipse.core.runtime.Assert
  4. class MathUtils {
  5. def static isApproximatelyEqual(double d1, double d2, double delta) {
  6. return d1 == d2 || delta > Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2));
  7. }
  8. def static converged(HashMap<OutputPortInstance, Double> newOuts, HashMap<OutputPortInstance, Double> oldOuts, double tol) {
  9. Assert.isTrue(newOuts !== null || oldOuts !== null)
  10. if (newOuts === null || oldOuts === null) {
  11. return false
  12. }
  13. for (outP : newOuts.keySet){
  14. Assert.isTrue(oldOuts.containsKey(outP))
  15. val nV = newOuts.get(outP)
  16. val oV = oldOuts.get(outP)
  17. if (! MathUtils.isApproximatelyEqual(oV, nV, tol)){
  18. return false
  19. }
  20. }
  21. return true
  22. }
  23. }