<commit_before>package me.pietrzak.aprcalc.root;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.function.Function;

public class NewtonsMethod {
    private static final int maximumNumberOfIterations = 5000;
    private static final int internalComputationsScale = 20;

    private static final BigDecimal maximumRange = BigDecimal.TEN.pow(322);
    private static final BigDecimal epsilon = BigDecimal.valueOf(1E-10);

    public static Optional<BigDecimal> findZero(Function<BigDecimal, BigDecimal> function, Function<BigDecimal, BigDecimal> derivative) {
        BigDecimal x = BigDecimal.ZERO;
        int iterationsCounter = 0;
        do {
            if (derivative.apply(x).equals(BigDecimal.ZERO)) {
                return Optional.empty();
            }
            if (x.compareTo(BigDecimal.ZERO)<0) {
                x = BigDecimal.ZERO;
            }
            if (iterationsCounter++ > maximumNumberOfIterations) {
                return Optional.empty();
            }
            if (x.compareTo(maximumRange)>0) {
                return Optional.empty();
            }
            x = x.subtract(function.apply(x).divide(derivative.apply(x), internalComputationsScale, BigDecimal.ROUND_HALF_UP));
        } while (errorIsLargerThanExpected(function, x, epsilon));
        return Optional.of(x);
    }

    private static boolean errorIsLargerThanExpected(Function<BigDecimal, BigDecimal> function, BigDecimal x, BigDecimal epsilon) {
        return function.apply(x).abs().compareTo(epsilon) >= 0;
    }
}<commit_msg>compareTo instead of equals<commit_after>package me.pietrzak.aprcalc.root;

import java.math.BigDecimal;
import java.util.Optional;
import java.util.function.Function;

public class NewtonsMethod {
    private static final int maximumNumberOfIterations = 5000;
    private static final int internalComputationsScale = 20;

    private static final BigDecimal maximumRange = BigDecimal.TEN.pow(322);
    private static final BigDecimal epsilon = BigDecimal.valueOf(1E-10);

    public static Optional<BigDecimal> findZero(Function<BigDecimal, BigDecimal> function, Function<BigDecimal, BigDecimal> derivative) {
        BigDecimal x = BigDecimal.ZERO;
        int iterationsCounter = 0;
        do {
            BigDecimal derivativeAtX = derivative.apply(x);
            if (derivativeAtX.compareTo(BigDecimal.ZERO) == 0) {
                return Optional.empty();
            }
            if (x.compareTo(BigDecimal.ZERO)<0) {
                x = BigDecimal.ZERO;
            }
            if (iterationsCounter++ > maximumNumberOfIterations) {
                return Optional.empty();
            }
            if (x.compareTo(maximumRange)>0) {
                return Optional.empty();
            }
            x = x.subtract(function.apply(x).divide(derivativeAtX, internalComputationsScale, BigDecimal.ROUND_HALF_UP));
        } while (errorIsLargerThanExpected(function, x, epsilon));
        return Optional.of(x);
    }

    private static boolean errorIsLargerThanExpected(Function<BigDecimal, BigDecimal> function, BigDecimal x, BigDecimal epsilon) {
        return function.apply(x).abs().compareTo(epsilon) >= 0;
    }
}
{'fraction_non_alphanumeric': 0.09662288930581614, 'fraction_numerical': 0.010318949343339587, 'mean_word_length': 2.8311377245508984, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '674889', 'n_tokens_mistral': 906, 'n_tokens_neox': 861, 'n_words': 189}