Thread: Float doesn't seem to be working?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Float doesn't seem to be working?

    Hello everyone, I have a few problems with float not working like I think it should...? Using TCC to compile.

    Basically, I'm trying to implement functions to calculate radians, degrees and powers.

    Code:
    #include <stdio.h>
    #define PI 3.14159265
    
    int main(void){
        double rads = myrad(90);        // Should be 1.570796
        double degs = mydeg(rads); // Should be 90.000000
        double pows = mypow(2, 4);        // Should be 16.000000
        
        printf("90 degrees to radians = %lf\n", rads);
        printf("1.570796 radians to degrees = %lf\n", degs);
        printf("2^4 = %lf\n", pows);
        return 0;
    }
    
    int mypow(double x, double y){
        double i = 0;
        double power = 1;
        while(i < y){
            power *= x;
            i++;
        }
        return power;
    }
    
    int myrad(double x){
        double radians = x * PI / 180;
        return radians;
    }
    
    int mydeg(double x){
        double degrees = x / PI * 180;
        return degrees;
    }
    The output is:
    Code:
    90 degrees to radians = 0.000000
    1.570796 radians to degrees = 0.000000
    2^4 = 0.000000
    I can't change the "int"s to "double"s because TCC throws a
    Code:
    main.c:*insert_line*: error: incompatible types for redefinition of '*insert_function*'
    at me.
    Can anyone please help me with this, as I'm trying to teach myself with some books.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You should prototype all your functions before you use them.
    Otherwise, the compiler automatically creates a declaration for them - based on the first use, which it then finds is incompatible with the later actual definition.

    Put these 3 lines above main.
    Code:
    int mypow(double x, double y);
    int myrad(double x);
    int mydeg(double x);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    56
    Try:

    Code:
    #include <stdio.h>
    #define PI 3.14159265
    
    double mypow(double x, double y){
        double i = 0;
        double power = 1;
        while(i < y){
            power *= x;
            i++;
        }
        return power;
    }
     
    double myrad(double x){
        double radians = x * PI / 180;
        return radians;
    }
     
    double mydeg(double x){
        double degrees = x / PI * 180;
        return degrees;
    }
    
    int main(void){
        double rads = myrad(90);        // Should be 1.570796
        double degs = mydeg(rads);      // Should be 90.000000
        double pows = mypow(2, 4);      // Should be 16.000000
         
        printf("90 degrees to radians = %lf\n", rads);
        printf("1.570796 radians to degrees = %lf\n", degs);
        printf("2^4 = %lf\n", pows);
        return 0;
    }
    Code:
    C:\Users\Michael\Desktop\test>g++ test.c -o test.exe
    
    C:\Users\Michael\Desktop\test>test
    90 degrees to radians = 1.570796
    1.570796 radians to degrees = 90.000000
    2^4 = 16.000000

  4. #4
    Banned
    Join Date
    Oct 2014
    Location
    Home
    Posts
    135
    Yeah. &nbsp;I would put the functions first instead of after the main. &nbsp;That way you don't need the prototype to specifically preface it. &nbsp;It looks like it is fine from inspection except put all your functions before the main. &nbsp;The prototype is supposed to explicitly make it clear to the compiler what to expect but in my two years of hardcore tcc programming it should only be used if you are going to be calling an external function like an assembly language module, etc.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by Tien Nguyen View Post
    Yeah. ..I would put the functions first instead of after the main. ..That way you don't need the prototype to specifically preface it. ..It looks like it is fine from inspection except put all your functions before the main. ..The prototype is supposed to explicitly make it clear to the compiler what to expect but in my two years of hardcore tcc programming it should only be used if you are going to be calling an external function like an assembly language module, etc.
    Actually prototypes should be used in all programs for many reasons.

    1) If you define the functions above main() without prototypes, you could easily get into a "Chicken and the Egg" syndrome, where function A may call function B, and function B might call function A. In what order would you define them??? Either order would not work!!

    2) In the real world, many if not most programs might start out as a one file program, but then expand into a multi-source file application with possibly multiple header files. It would be easier to expand the program if you have prototypes in place from the start.

    3+) I'm sure there are many more reasons, but with only one cup of coffee in me, I will stop here!

    @SoLDMG:

    You should think about moving to a more modern compiler for Windows. Turbo C is non-standard, and too old to be able to use C99 and C11 C Programming Language Standards. Turbo C 1.0 is completely useless, and should be thrown away!

    This code will NOT execute correctly in TC 1.0!!! Not sure if or when they corrected this bug!
    Code:
    int main(void)
    {
       float value = -123.45;
    
       printf("value == %.2f\n", -value);
    
       return 0;
    }
    Last edited by rstanley; 07-03-2016 at 08:36 AM. Reason: Wrong version of the code!

  6. #6
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    With TCC I meant Tiny C Compiler, haha. My bad. Thank you for the code though.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    56
    OK, point taken.
    Code:
    #include <stdio.h>
    #define PI 3.14159265
    
    double mypow(double x, double y);
    double myrad(double x);
    double mydeg(double x);
    
    int main(void){
        double rads = myrad(90);        // Should be 1.570796
        double degs = mydeg(rads);      // Should be 90.000000
        double pows = mypow(2, 4);      // Should be 16.000000
         
        printf("90 degrees to radians = %lf\n", rads);
        printf("%lf radians to degrees = %lf\n", rads, degs);
        printf("2^4 = %lf\n", pows);
        return 0;
    }
    
    double mypow(double x, double y){
        double i = 0;
        double power = 1;
        while(i < y){
            power *= x;
            i++;
        }
        return power;
    }
     
    double myrad(double x){
        double radians = x * PI / 180;
        return radians;
    }
     
    double mydeg(double x){
        double degrees = x / PI * 180;
        return degrees;
    }

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    I think using const double PI = 3.14159265 is better than using a macro.

    One benefit I like is being able to see the value of the variable when you are debugging.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Float variable not working!
    By Furugoori_K1204 in forum C Programming
    Replies: 5
    Last Post: 12-12-2013, 01:19 PM
  2. Why 1 of them is working and other one doesn't??
    By chottachatri in forum C Programming
    Replies: 4
    Last Post: 10-23-2008, 10:48 AM
  3. Why doesn't working?
    By ch4 in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2008, 12:07 PM
  4. Supposed to return float but doesn't.
    By bliss in forum C++ Programming
    Replies: 6
    Last Post: 05-18-2005, 12:49 PM
  5. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM

Tags for this Thread