We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I can also point to example code in https://www.gnu.org/software/libc/manual/html_node/Comparison-Functions.html which also uses the subtraction method (albeit in a somewhat questional way in my opinion). That said, this function guarantees that -1, 0, or 1 will be returned since it's finding the difference between 2 relational expressions.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cats and a Mouse
You are viewing a single comment's thread. Return to all comments →
Probably not. I assume most Java implementations are based on openJDK, and I looked through the openJDK sources. They use:
which came from whatever reference sources they started with (I'm guessing Sun's 1.2 implementation).
I can, however, show you glibc's strcmp() implementation which does implement compare using subtraction: https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strcmp.c;h=3485aa64c10072ed7ad52340c6690d0ca7602506;hb=1d714fd95da16f0d97c8c670a2c899f99c01eb45#l45
I can also point to example code in https://www.gnu.org/software/libc/manual/html_node/Comparison-Functions.html which also uses the subtraction method (albeit in a somewhat questional way in my opinion). That said, this function guarantees that -1, 0, or 1 will be returned since it's finding the difference between 2 relational expressions.