Thread: Illegal use of floating point

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    26

    Illegal use of floating point

    hi!
    When I try to run the program below, I get the following error:
    Illegal use of floating point.
    Can anyone tell me what's wrong?!?!

    Code:
    #include <stdio.h>
    #include <math.h>
     int main ()
     {
    float i, x, c;
    while ( i <=1000) {
    x = pow(2,i);
    c= x % i;
    if ( c == 2)
    printf ("%f\n",i);
    i++;
     }
     return 0;
     }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The modulus (%) operator can only be used on integer types. Try the fmod function in math.h.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also you never initialized i. Initialize i to some value:
    Code:
    float x, c;
    float i = 0.0;
    while ( i <=1000) {

  4. #4
    Unregistered
    Guest
    i've made some changes to the code, but when i run, i get pow: OVERFLOW error. plz help!!
    Code:
    #include <stdio.h>
    #include <math.h>
     int main ()
     {
    double i=1, x, c;
    while ( i <=10000) {
    x = pow(2,i);
    c= fmod(x, i);
    if ( c == 2)
    printf ("%lf\n",i);
    i += 2;
     }
     return 0;
     }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > while ( i <=10000)
    Make this loop smaller - much smaller

    If you really need to go this high, you need another plan - the standard numeric types are not sufficient for very large numbers.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i get pow: OVERFLOW error.
    >x = pow(2,i);
    You honestly think that a double can hold 2^10000? Do you have any idea how big of a number that is? On my system a double can hold the result of pow ( 2, 1023 ). Any more than that and it overflows. You want your program to calculate almost ten times that. I recommend either another approach or another language with no limits on the size of values.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why illegal use of floating point?
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2008, 07:00 AM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. strtol with floating point
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 07-26-2004, 06:02 AM
  5. floating point variables in edittext controls
    By dootickle in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2004, 11:15 AM