Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. sqrt() pow() fabs() do not work

sqrt() pow() fabs() do not work

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
9 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 3375334
    wrote on last edited by
    #1

    Hello everyone, i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like: error C2668: 'fabs' : ambiguous call to overloaded function same for the rest of the functions i have this included: #include "stdafx.h" #include "math.h" i tried including but still same errors. Does anyone know why they are not being recognized? my file is .cpp not .c but it is an MFC project. thanks

    C 1 Reply Last reply
    0
    • M Member 3375334

      Hello everyone, i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like: error C2668: 'fabs' : ambiguous call to overloaded function same for the rest of the functions i have this included: #include "stdafx.h" #include "math.h" i tried including but still same errors. Does anyone know why they are not being recognized? my file is .cpp not .c but it is an MFC project. thanks

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (multiply what you pass in by 1.0), this should fix the problem. int result = (int)fabs(value*1.0);

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      N 1 Reply Last reply
      0
      • C Cedric Moonen

        It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (multiply what you pass in by 1.0), this should fix the problem. int result = (int)fabs(value*1.0);

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        Cedric Moonen wrote:

        int result = (int)fabs(value*1.0);

        Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?

        - ns ami -

        C D C 3 Replies Last reply
        0
        • N Nishad S

          Cedric Moonen wrote:

          int result = (int)fabs(value*1.0);

          Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?

          - ns ami -

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Yes you're right. It's just because I am used to multiply by 1.0 because in general I use that in calculation (where I don't want to put a cast inside because it looks ugly) :)

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • N Nishad S

            Cedric Moonen wrote:

            int result = (int)fabs(value*1.0);

            Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?

            - ns ami -

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            ns ami wrote:

            Am I right?

            Only if the code were in a huge loop would the difference in time even be noticeable.

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            C 1 Reply Last reply
            0
            • N Nishad S

              Cedric Moonen wrote:

              int result = (int)fabs(value*1.0);

              Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?

              - ns ami -

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              the compiler will optimize that multiplication away.

              int value = rand();
              int result1 = (int)fabs(value*1.0);
              printf("%d, %d\n", result1, result1);

              VC6, release mode gives the following assembly:

              ; 10 : int value = rand();

              call	\_rand
              

              ; 11 : int result1 = (int)fabs(value*1.0);

              mov	DWORD PTR -4+\[esp+4\], eax
              fild	DWORD PTR -4+\[esp+4\]
              fabs
              call	\_\_ftol
              

              ; 12 : printf("%d, %d\n", result1, result1);

              push	eax
              push	eax
              push	OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string'
              call	\_printf
              

              no fmul. if you do this:

              int value = rand();
              int result1 = (int)fabs((double)value);
              printf("%d, %d\n", result1, result1);

              you get this:

              ; 10 : int value = rand();

              call	\_rand
              

              ; 11 : int result1 = (int)fabs((double)value);

              mov	DWORD PTR -4+\[esp+4\], eax
              fild	DWORD PTR -4+\[esp+4\]
              fabs
              call	\_\_ftol
              

              ; 12 : printf("%d, %d\n", result1, result1);

              push	eax
              push	eax
              push	OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string'
              call	\_printf
              

              exactly the same.

              batch image processing

              N 1 Reply Last reply
              0
              • D David Crow

                ns ami wrote:

                Am I right?

                Only if the code were in a huge loop would the difference in time even be noticeable.

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.

                batch image processing

                A 1 Reply Last reply
                0
                • C Chris Losinger

                  it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.

                  batch image processing

                  A Offline
                  A Offline
                  Alexander M
                  wrote on last edited by
                  #8

                  nevertheless it's a bad style as many people won't understand it.

                  Don't try it, just do it! ;-)

                  1 Reply Last reply
                  0
                  • C Chris Losinger

                    the compiler will optimize that multiplication away.

                    int value = rand();
                    int result1 = (int)fabs(value*1.0);
                    printf("%d, %d\n", result1, result1);

                    VC6, release mode gives the following assembly:

                    ; 10 : int value = rand();

                    call	\_rand
                    

                    ; 11 : int result1 = (int)fabs(value*1.0);

                    mov	DWORD PTR -4+\[esp+4\], eax
                    fild	DWORD PTR -4+\[esp+4\]
                    fabs
                    call	\_\_ftol
                    

                    ; 12 : printf("%d, %d\n", result1, result1);

                    push	eax
                    push	eax
                    push	OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string'
                    call	\_printf
                    

                    no fmul. if you do this:

                    int value = rand();
                    int result1 = (int)fabs((double)value);
                    printf("%d, %d\n", result1, result1);

                    you get this:

                    ; 10 : int value = rand();

                    call	\_rand
                    

                    ; 11 : int result1 = (int)fabs((double)value);

                    mov	DWORD PTR -4+\[esp+4\], eax
                    fild	DWORD PTR -4+\[esp+4\]
                    fabs
                    call	\_\_ftol
                    

                    ; 12 : printf("%d, %d\n", result1, result1);

                    push	eax
                    push	eax
                    push	OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string'
                    call	\_printf
                    

                    exactly the same.

                    batch image processing

                    N Offline
                    N Offline
                    Nishad S
                    wrote on last edited by
                    #9

                    Chris Losinger wrote:

                    the compiler will optimize that multiplication away.

                    Thank you for pointing out that... :)

                    - ns ami -

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups