Why does the `int` overload of `abs` get called in my code?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







8















I've had a super frustrating bug in my code for the past few weeks. My code would work exactly as expected on my computer, but as soon as I farmed it out to the HPC server, it would produce weird results.



I've boiled it down to this: on my computer (iMac) the function abs() works with floating point numbers, but on the server abs() truncates it to an integer.



Example:



Server



abs(-1.1341234) = 1


My Mac



abs(-1.1341234) = 1.1341234


Now I know I can fix this by using fabs(), that's not the question. I looked at the versions of gcc on either machine and this is the output:



Server



g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/software/GCCcore/5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-gold=default --enable-plugins --enable-ld --with-plugin-ld=ld.gold --prefix=/apps/software/GCCcore/5.4.0 --with-local-prefix=/apps/software/GCCcore/5.4.0 --enable-bootstrap --with-isl=/dev/shm/GCCcore/5.4.0/dummy-/gcc-5.4.0/stage2_stuff
Thread model: posix
gcc version 5.4.0 (GCC)


My Mac



g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


So my question, why does abs() produce different results between gcc and clang? This issue literally cost me 3 weeks of progress, so as you can imagine I'm a little salty just now...










share|improve this question




















  • 3





    so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

    – Scheff
    42 mins ago








  • 1





    @Scheff Oh man, I certainly won't ever forget this lesson...

    – Phill
    27 mins ago











  • @Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

    – Scheff
    26 mins ago


















8















I've had a super frustrating bug in my code for the past few weeks. My code would work exactly as expected on my computer, but as soon as I farmed it out to the HPC server, it would produce weird results.



I've boiled it down to this: on my computer (iMac) the function abs() works with floating point numbers, but on the server abs() truncates it to an integer.



Example:



Server



abs(-1.1341234) = 1


My Mac



abs(-1.1341234) = 1.1341234


Now I know I can fix this by using fabs(), that's not the question. I looked at the versions of gcc on either machine and this is the output:



Server



g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/software/GCCcore/5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-gold=default --enable-plugins --enable-ld --with-plugin-ld=ld.gold --prefix=/apps/software/GCCcore/5.4.0 --with-local-prefix=/apps/software/GCCcore/5.4.0 --enable-bootstrap --with-isl=/dev/shm/GCCcore/5.4.0/dummy-/gcc-5.4.0/stage2_stuff
Thread model: posix
gcc version 5.4.0 (GCC)


My Mac



g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


So my question, why does abs() produce different results between gcc and clang? This issue literally cost me 3 weeks of progress, so as you can imagine I'm a little salty just now...










share|improve this question




















  • 3





    so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

    – Scheff
    42 mins ago








  • 1





    @Scheff Oh man, I certainly won't ever forget this lesson...

    – Phill
    27 mins ago











  • @Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

    – Scheff
    26 mins ago














8












8








8


1






I've had a super frustrating bug in my code for the past few weeks. My code would work exactly as expected on my computer, but as soon as I farmed it out to the HPC server, it would produce weird results.



I've boiled it down to this: on my computer (iMac) the function abs() works with floating point numbers, but on the server abs() truncates it to an integer.



Example:



Server



abs(-1.1341234) = 1


My Mac



abs(-1.1341234) = 1.1341234


Now I know I can fix this by using fabs(), that's not the question. I looked at the versions of gcc on either machine and this is the output:



Server



g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/software/GCCcore/5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-gold=default --enable-plugins --enable-ld --with-plugin-ld=ld.gold --prefix=/apps/software/GCCcore/5.4.0 --with-local-prefix=/apps/software/GCCcore/5.4.0 --enable-bootstrap --with-isl=/dev/shm/GCCcore/5.4.0/dummy-/gcc-5.4.0/stage2_stuff
Thread model: posix
gcc version 5.4.0 (GCC)


My Mac



g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


So my question, why does abs() produce different results between gcc and clang? This issue literally cost me 3 weeks of progress, so as you can imagine I'm a little salty just now...










share|improve this question
















I've had a super frustrating bug in my code for the past few weeks. My code would work exactly as expected on my computer, but as soon as I farmed it out to the HPC server, it would produce weird results.



I've boiled it down to this: on my computer (iMac) the function abs() works with floating point numbers, but on the server abs() truncates it to an integer.



Example:



Server



abs(-1.1341234) = 1


My Mac



abs(-1.1341234) = 1.1341234


Now I know I can fix this by using fabs(), that's not the question. I looked at the versions of gcc on either machine and this is the output:



Server



g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/apps/software/GCCcore/5.4.0/libexec/gcc/x86_64-unknown-linux-gnu/5.4.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran --enable-lto --enable-checking=release --disable-multilib --enable-shared=yes --enable-static=yes --enable-threads=posix --enable-gold=default --enable-plugins --enable-ld --with-plugin-ld=ld.gold --prefix=/apps/software/GCCcore/5.4.0 --with-local-prefix=/apps/software/GCCcore/5.4.0 --enable-bootstrap --with-isl=/dev/shm/GCCcore/5.4.0/dummy-/gcc-5.4.0/stage2_stuff
Thread model: posix
gcc version 5.4.0 (GCC)


My Mac



g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


So my question, why does abs() produce different results between gcc and clang? This issue literally cost me 3 weeks of progress, so as you can imagine I'm a little salty just now...







c++ gcc compilation clang






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 mins ago









J. Antonio Perez

3,533926




3,533926










asked 1 hour ago









PhillPhill

21415




21415








  • 3





    so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

    – Scheff
    42 mins ago








  • 1





    @Scheff Oh man, I certainly won't ever forget this lesson...

    – Phill
    27 mins ago











  • @Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

    – Scheff
    26 mins ago














  • 3





    so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

    – Scheff
    42 mins ago








  • 1





    @Scheff Oh man, I certainly won't ever forget this lesson...

    – Phill
    27 mins ago











  • @Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

    – Scheff
    26 mins ago








3




3





so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

– Scheff
42 mins ago







so as you can imagine I'm a little salty just now... Annoying but worth the lecture... This will make you very sensitive about these things. ;-) (I don't mean this offensive but rather as a "heads up". Every really annoying bug might at least produce a valuable experience. So, take it positively...)

– Scheff
42 mins ago






1




1





@Scheff Oh man, I certainly won't ever forget this lesson...

– Phill
27 mins ago





@Scheff Oh man, I certainly won't ever forget this lesson...

– Phill
27 mins ago













@Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

– Scheff
26 mins ago





@Phill I have collected some of them in my mind ...and sometimes tell my colleagues my stories of old about this... ;-)

– Scheff
26 mins ago












1 Answer
1






active

oldest

votes


















14














You must use std::abs, which has overloads for the primitive types.



You are falling through to the C++ version in one case, and the old C version (which converts its parameters to integral types) in the other.



Things to avoid: (1) using namespace std; (here is why) and (2) not having the appropriate #includes to bring in the functionality you need. Don't rely on C++ standard library implementations to include files for you implicitly.



Some compilers will warn you of a "lossy" conversion if you set the warning level appropriately.






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55916514%2fwhy-does-the-int-overload-of-abs-get-called-in-my-code%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    14














    You must use std::abs, which has overloads for the primitive types.



    You are falling through to the C++ version in one case, and the old C version (which converts its parameters to integral types) in the other.



    Things to avoid: (1) using namespace std; (here is why) and (2) not having the appropriate #includes to bring in the functionality you need. Don't rely on C++ standard library implementations to include files for you implicitly.



    Some compilers will warn you of a "lossy" conversion if you set the warning level appropriately.






    share|improve this answer






























      14














      You must use std::abs, which has overloads for the primitive types.



      You are falling through to the C++ version in one case, and the old C version (which converts its parameters to integral types) in the other.



      Things to avoid: (1) using namespace std; (here is why) and (2) not having the appropriate #includes to bring in the functionality you need. Don't rely on C++ standard library implementations to include files for you implicitly.



      Some compilers will warn you of a "lossy" conversion if you set the warning level appropriately.






      share|improve this answer




























        14












        14








        14







        You must use std::abs, which has overloads for the primitive types.



        You are falling through to the C++ version in one case, and the old C version (which converts its parameters to integral types) in the other.



        Things to avoid: (1) using namespace std; (here is why) and (2) not having the appropriate #includes to bring in the functionality you need. Don't rely on C++ standard library implementations to include files for you implicitly.



        Some compilers will warn you of a "lossy" conversion if you set the warning level appropriately.






        share|improve this answer















        You must use std::abs, which has overloads for the primitive types.



        You are falling through to the C++ version in one case, and the old C version (which converts its parameters to integral types) in the other.



        Things to avoid: (1) using namespace std; (here is why) and (2) not having the appropriate #includes to bring in the functionality you need. Don't rely on C++ standard library implementations to include files for you implicitly.



        Some compilers will warn you of a "lossy" conversion if you set the warning level appropriately.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 5 mins ago

























        answered 59 mins ago









        BathshebaBathsheba

        183k27258386




        183k27258386
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55916514%2fwhy-does-the-int-overload-of-abs-get-called-in-my-code%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Last logged in always never, not logging

            Iĥnotaksono

            Colouring column values based on a specific condition. How could I do this?