-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difference between java and python implementation: Spoiler, the problem is the round #99
Comments
For now, I'm working with this local solution to have equivalence: import me.xdrop.diffutils.DiffUtils;
import me.xdrop.fuzzywuzzy.ratios.SimpleRatio;
public class SimplePythonRatio extends SimpleRatio {
@Override
public int apply(String s1, String s2) {
double ratio = 100 * DiffUtils.getRatio(s1, s2);
double floored = Math.floor(ratio);
double pythonRatio = ratio - floored <= 0.5 ? floored : Math.round(ratio);
return (int) pythonRatio;
}
} new TokenSet().apply(s1, s2, new SimplePythonRatio()); What do you think of adding this to the java lib? |
@leafah warned about HALF_EVEN python round behaviour ( https://en.wikipedia.org/wiki/Rounding#Round_half_to_even ), so the import me.xdrop.diffutils.DiffUtils;
import me.xdrop.fuzzywuzzy.ratios.SimpleRatio;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SimplePythonRatio extends SimpleRatio {
@Override
public int apply(String s1, String s2) {
double ratio = 100 * DiffUtils.getRatio(s1, s2);
return (int) bankerRound(ratio);
}
// https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
double bankerRound(double value) {
return new BigDecimal(value)
.setScale(0, RoundingMode.HALF_EVEN)
.doubleValue();
}
} |
Honestly at this point I'm kinda confused regarding whats the deal here. I'm still interested in what @xdrop says about this, but I doubt it would be much different from this. |
Here is the example where I was stuck. The python implemention gets 22, and the java implementation gets 23:
Debugging both code I could find that the problem is when rounding the value: 22.5
Python code, located in
utils.py
:Java code, located in
SimpleRatio
class is:TLDR:
Java:
Math.round(22.5)
=> 23Python:
round(22.5)
=> 22Don't know which one is correct for this algorithm...
The text was updated successfully, but these errors were encountered: