Illegal use of floating point...(In C)

This is a discussion on Illegal use of floating point...(In C) within the C Programming forums, part of the General Programming Boards category; Hello, I have just got into learing to program (In C), and I'm having a problem with floating point. It's ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    25

    Illegal use of floating point...(In C)

    Hello, I have just got into learing to program (In C), and I'm having a problem with floating point.

    It's a simple get the volume of a circle if given a radius problem.

    #include <stdio.h>

    char user[100];
    float radius;
    const float PI = 3.1415;
    float volume;

    int main()
    {
    printf("Enter radius of circle: ");
    fgets(user, sizeof(user), stdin);
    sscanf(user, "%f", &radius);

    volume = ((4.0 / 3.0 * PI) * radius) ^ 3.0;

    printf("The volume of the circle is %f\n", volume);
    return (0);
    }

    Now I don't know if this message board has problems with posts having source code in them, but it's a short program, so don't kill me.

    What I want to know from anyone that knows C, is to tell me if I did anything wrong, because when i compile the code, it brings up a error message saying illegal use of float point.

    I belive the problem lies in with the squaring the problem by 3. Maybe you can't square floating point numbers? Or a special function or key to do it?

    I dont know, please help.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    25
    Thank you, that fixed the problem. Seems this book I'm learning from decided that learning ^ is integer exclusive isn't that important.

    And no, I did not read that post. Thanks for the info though.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by OmnipotentCow
    Thank you, that fixed the problem. Seems this book I'm learning from decided that learning ^ is integer exclusive isn't that important.

    And no, I did not read that post. Thanks for the info though.
    *evil grin* Use a union!
    Code:
    union foobar
    {
        unsigned int foo;
        float bar;
    };
    
    union foobar badThingsTM;
    
    badThingsTM.bar = 123.456;
    printf("bar = %f\n", badThingsTM.bar );
    
    badThingsTM.foo ^= 123456;
    printf("bar = %f\n", badThingsTM.bar );
    My output is:

    bar = 123.456001
    bar = 123.521919

    As you can see, floating point numbers themselves just plain suck for precision. That aside, you can see that XOR does have some effect, but not what you'd hope. Still, it's an amusing exercise.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by OmnipotentCow
    Thank you, that fixed the problem. Seems this book I'm learning from decided that learning ^ is integer exclusive isn't that important.
    It's not that ^ is integer specific, it's that ^ is not to the power of. Looks like you learned Basic first where 3 ^ 2 means 3 squared.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    It's not that ^ is integer specific, it's that ^ is not to the power of. Looks like you learned Basic first where 3 ^ 2 means 3 squared.
    It is integer specific. It can only be used on integeral data types. (IE: int, long, char) It cannot be used on floating point or decimal values.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I think that he meant that it wasn't the main issue here. The issue was confusing ^ and pow( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21