Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Stupid big numbers....

  • 28-11-2004 9:19pm
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    Error message:
    warning: integer constant is too large for "long" type

    How do i fix it, i presume i have to use the "double" type for my values, but i couldn't get that to work. Here's me code. At first i used #define for my constants (the ones in capitals :p) but that threw up the same errors, so i tried floating em, and then specifying their values, which didn't help either.
    /***************************/
    /* Preprocessor Directives */
    /***************************/
    #include <stdio.h>
    
    /*****************/
    /* Begin Program */
    /*****************/
    int main()
    {
    
      /********************/
      /* Define Variables */
      /********************/
      float velocity, frequency_recieved;
      float TRANSMITTED_FREQUENCY;
      float SPEED_LIGHT;
    
      TRANSMITTED_FREQUENCY = 5500000000;
      SPEED_LIGHT = 300000000;
    
      /****************/
      /* Calculations */
      /****************/
    
      for(velocity = 10; velocity <=100; velocity = velocity + 10)
        {
          frequency_recieved = (float)(((2*velocity*TRANSMITTED_FREQUENCY)/SPEED_LIGHT)+ TRANSMITTED_FREQUENCY);
          printf("\nThe recieved frequency at %dm/s is %d", velocity, frequency_recieved);
        }
      return 0;
    }
    


Comments

  • Closed Accounts Posts: 82 ✭✭cyberbob


    double should work fine.
    i cut n pasted it and it ran with doubles ok for me


  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    What Compiler are you using and which standard is it (ANSI C89/C99)? ANSI C99 gives you a long long which you might want to look into.


    [EDIT1] your also using a Floating point number in the loop guard, you might want to change that to an int.

    [EDIT2] Check formatt in printf


  • Registered Users, Registered Users 2 Posts: 205 ✭✭Stugots


    Your program compiles with one warning under Microsoft Visual C which reports that the line

    TRANSMITTED_FREQUENCY = 5500000000;

    results in a truncation from type const int64 to type float, so it appears that in VC, constant values are 64 bits. There may be a command line option with your compiler to set the default int size to 64 bits, but it sounds like the intent of the exercise is to get you to use the float type.

    As an aside, you are attempting to type cast constants by declaring an equivalent variable of the type to which you want to cast it. It would be simpler and better to just type cast a constant definition instead, e.g.

    #define TRANSMITTED_FREQUENCY ((double)5500000000)


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Stugots wrote:
    you are attempting to type cast constants by declaring an equivalent variable of the type to which you want to cast it. It would be simpler and better to just type cast a constant definition instead, e.g.
    There is just one response to that sentance... :eek: :eek: :eek: :eek:

    I know what typecasting is, but thats just confusing :p I'll work on it now, and see if i can fix my borked-ness and get this working now.

    No go, i still can't get it to compile. I'm using CYGWin and gcc to compile, and it keeps giving this error:
    a.c: In function `main':
    a.c:27: warning: integer constant is too large for "long" type
    a.c:27: warning: integer constant is too large for "long" type

    This is the current code i was trying...
    /***************************/
    /* Preprocessor Directives */
    /***************************/
    #include <stdio.h>
    #define TRANSMITTED_FREQUENCY ((double)5500000000)
    
    /*****************/
    /* Begin Program */
    /*****************/
    int main()
    {
    
      /********************/
      /* Define Variables */
      /********************/
      double velocity, frequency_recieved;
      double SPEED_LIGHT;
    
      SPEED_LIGHT = 300000000;
    
      /****************/
      /* Calculations */
      /****************/
    
      for(velocity = 10; velocity <=100; velocity = velocity + 10)
        {
          frequency_recieved = ((double)(((2*velocity*TRANSMITTED_FREQUENCY)/SPEED_LIGHT)+ TRANSMITTED_FREQUENCY));
          printf("\nThe recieved frequency at %dm/s is %d", velocity, frequency_recieved);
        }
      return 0;
    }
    


  • Registered Users, Registered Users 2 Posts: 205 ✭✭Stugots


    It appears that the intermediate calculations on line 27 are assumed to result in a long int value, so you are overflowing this by multiplying/dividing floats. This may be happening because you have an integer in the calculation, i.e. 2. You could try type casting the 2 to a float, i.e. ((float)2).

    Another possibility is that the purpose of the exercise is to make you use a different library for multiplication and division of long ints. I think I recall in some older compiler that I had to use library calls for multiplication/division of longs to avoid the overflow, eg. c = long_multiply(a,b); instead of c = a*b; Perhaps you even have to write the long_multiply() and long_divide() functions yourself.

    It may help you debug if you split the calculation that is reporting the error onto several lines, e.g. instead of
    c = 2*a/b;
    try
    c = a/b;
    c *= 2;


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 950 ✭✭✭jessy


    You still havent checked the printf(theres an error there aswell).


  • Registered Users, Registered Users 2 Posts: 205 ✭✭Stugots


    'i' before 'e' except after 'c'???? :D

    ..or are you just being picky about how the last line of the output looks?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    jessy wrote:
    You still havent checked the printf(theres an error there aswell).
    I think he was pointing out that i used %d's instead of %f's. The thing is originally i used int's instead of floats/doubles, so i had the %d's there. But when that didn't compile due to long numbers, i just changed the ints to floats/doubles without changing the rest of my code in order to see if that would work.

    Anyway, it turns out that it compiles perfectly with "double" only if i add .0 to the end of each of my "doubled" numbers... i.e. 5500000000.0 I assume thats because "doubles" are big floats, and floats have to be written with a decimal point otherwise they explode.

    If only i knew that beforehand... :p

    Thanks for all yer help


Advertisement