Thread: issue with math functions

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    issue with math functions

    i have the following
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double power( double, int );
    
    int main()
    {
        //declare variables
        double iSum = 0, iTotalNum = 0, iMod = 10, iTmp = 0;
        int iPower = 10;
    
        /* on the basis that 2^15 == (2^3)^5 ie 3 * 5 = 15  */
    
        //calculate 2^10
        iTotalNum = power( 2 , iPower );
    
        //calculate (2^10)^10 ie 2^100
        iTotalNum = power( iTotalNum, iPower );
    
        //calculate ((2^10)^10)^10 ie 2^1000
        iTotalNum = power( iTotalNum, iPower );
    
        iTmp = fmod( 10, 3);
       
        printf("%lf\n\n\n\n%e\n", iTmp, iTotalNum);
    
        return 0;
    }
    
    double power( double iBase, int iExponent )
    {
        //break out clause
        if ( iExponent == 1 )
        {
            //end of line so return 1
            return iBase;
        }
    
        return iBase * power( iBase, --iExponent );
    }
    when i try and build/compile it i get |29|undefined reference to `fmod'|
    ||error: ld returned 1 exit status|
    ||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

    this happens with pow and sqrt but with them if i write in the numbers manualy rather than using assigned variables they work ie
    Code:
    pow( x, y) //this fails
    pow( 4, 2 )//this works

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    You most probably need to link against the math library - libm.

    For GCC you need the '-lm' compile option.

    The GCC compiler will optimize / inline some simple functions to improve performance rather than calling the library function, and pow(x,y) is one of them.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    how do i do that in codeblocks

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks 2 am and im getting square eyes never mind thinking for myself

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks that worked

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok here is the final program. code blocks now compiles it with out any errors or warnings.
    Code:
    /*
    2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
    What is the sum of the digits of the number 2^1000?
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double power( double, int );
    
    int main()
    {
        //declare variables
        double iTotalNum = 0, iMod = 10, iSum = 0;
        int iPower = 10;
    
        /* on the basis that 2^15 == (2^3)^5 ie 3 * 5 = 15  */
    
        //calculate 2^10
        iTotalNum = power( 2 , iPower );
    
        //calculate (2^10)^10 ie 2^100
        iTotalNum = power( iTotalNum, iPower );
    
        //calculate ((2^10)^10)^10 ie 2^1000
        iTotalNum = power( iTotalNum, iPower );
    
        while ( iTotalNum > 1 )
        {
            iSum += fmod( iTotalNum, iMod );
            iTotalNum /= iMod;
        }
    
        //iTmp = iTotalNum - iTmp;
    
        printf("%lf\n\n%lf\n", iSum, iTotalNum);
    
        return 0;
    }
    
    double power( double iBase, int iExponent )
    {
        //break out clause
        if ( iExponent == 1 )
        {
            //end of line so return 1
            return iBase;
        }
    
        return iBase * power( iBase, --iExponent );
    }
    however i don't get the result i expected (surprise surprise). i get 1195.955762 i understand that doubles are large floats but soon as it has got rid of the e notation (looped 301 times) it starts adding decimals

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    sorry meant to add that 2^1000 is 1.07150860719e+301 at least thats the number i am working with

  9. #9
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by cooper1200 View Post
    sorry meant to add that 2^1000 is 1.07150860719e+301 at least thats the number i am working with
    2^1000 will be about a 300 digit number, and is far more than any standard data type can hold.

    You should investigate using the "double dabble" algorithm.

    Double dabble - Wikipedia

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by hamster_nz View Post
    2^1000 will be about a 300 digit number, and is far more than any standard data type can hold.
    double can hold up to 2^1023.

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I would try using pow() again instead of your power() function. It could have been the missing -lm that caused it not to work with variables for you (the compiler probably replaced pow(constant, constant) with a constant value without a call to pow())

  12. #12
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by christop View Post
    I would try using pow() again instead of your power() function. It could have been the missing -lm that caused it not to work with variables for you (the compiler probably replaced pow(constant, constant) with a constant value without a call to pow())
    that was the issue the missing -lm was causing all sorts of problems i think

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struggling with c++ math related issue
    By jpappas in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2022, 09:25 AM
  2. New to Programming - Simple Math Program Issue
    By Cameron Taylor in forum C Programming
    Replies: 4
    Last Post: 06-10-2013, 09:32 PM
  3. Issue with math
    By Paul Omans in forum C Programming
    Replies: 8
    Last Post: 05-22-2013, 10:38 PM
  4. Issue with pointer math
    By Kudose in forum C Programming
    Replies: 6
    Last Post: 03-18-2008, 04:25 AM
  5. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM

Tags for this Thread