Thread: Float problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    69

    Float problem

    Hey guys! I think I must be going crazy...

    Code:
    int main()
    {
      float test = 48 / 512;
      printf("%f", test);
      return (0);
    }
    gives me 0.0000. Why? How can I get the answer?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    48 / 512 is less than 0. Both 48 and 512 are integers so the whole division is done on integers and the precision is lopped off leaving you with 0. Change one or both of the numbers to float and it'll work.
    Code:
    int main()
    {
      float test = 48.0f / 512.0f;
      printf("%f", test);
      return (0);
    }

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Banana Man View Post
    48 / 512 is less than 0. Both 48 and 512 are integers so the whole division is done on integers and the precision is lopped off leaving you with 0. Change one or both of the numbers to float and it'll work.
    Code:
    int main()
    {
      float test = 48.0f / 512.0f;
      printf("%f", test);
      return (0);
    }
    You don't need to add the f at the end of the value, a decimal point is enough to make
    it work. Infact if you miss out the decimal point it won't compile on my machine!!



    Code:
    
    int main()
    {
     float test = 48f/512f;
      printf("%f", test);
      return (0);
    }
    Code:
    invalid sufix 'f' on integer constant,
    So I am not sure what the f does,

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    1.0 is a double. 1.0f is a float.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    1.0 is a double. 1.0f is a float.
    The result is a float though - well it works anyway, so what difference does it make?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    You don't need to add the f at the end of the value, a decimal point is enough to make
    it work. Infact if you miss out the decimal point it won't compile on my machine!!
    Leaving it out will do integer division, hence giving you 0. That is no error or warning.
    If you add decimals, you get a double, but some compilers won't warn about that.

    So I am not sure what the f does,
    So again, as others stated, it makes the number a float. It needs to be 1.0f, not 1f.

    And for once, you used "int main" and "return 0". Congratulations! Must have been hard on you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    Leaving it out will do integer division, hence giving you 0. That is no error or warning.
    Not if you add a decimal point it won't.

    Quote Originally Posted by Elysia View Post
    If you add decimals, you get a double, but some compilers won't warn about that.
    So again, as others stated, it makes the number a float. It needs to be 1.0f, not 1f.
    It only make it a float if it has a decimal point, otherwise it just gives you a compiler error.
    Quote Originally Posted by Elysia View Post
    And for once, you used "int main" and "return 0". Congratulations! Must have been hard on you.
    Not really, I am quite good at cut and paste.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by esbo View Post
    Not if you add a decimal point it won't.
    But that was beyond the point. The point was that if you didn't do it, it would be integer division.

    It only make it a float if it has a decimal point, otherwise it just gives you a compiler error.
    Of course. A float is a floating point number, after all.

    Not really, I am quite good at cut and paste.
    Yes, but you could have removed them, to better suit your "tastes."
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Elysia View Post
    But that was beyond the point. The point was that if you didn't do it, it would be integer division.


    Of course. A float is a floating point number, after all.


    Yes, but you could have removed them, to better suit your "tastes."
    The f changes a floating point number to a float, it won't change an integer to a float.
    A double is a floating point number.
    My 'taste' is just to copy what is already there, I won't change something if it compiles
    and works.
    I don't really see the point in having an f there at all, it seems rather pointless.
    Last edited by esbo; 02-27-2008 at 06:43 PM.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The f changes a floating point number to a float, it won't change an integer to a float.
    A double is a floating point number.
    My 'taste' is just to copy what is already there, I won't change something if it compiles
    and works.
    I don't really see the point in having an f there at all, it seems rather pointless.
    Because it is a very bad habit to get into. If you wish to use float then specify the f otherwise do not specify the f. It also informs other programmers that you are using float's and not doubles. There is a big difference between a double and a float. Just because 'it works' does not mean it's correct.

    This:

    Code:
    float x = 4f;
    This means nothing to me except that the person who programmed it either was very tired that day or doesn't understand floating point values. Did they want 4.0f or is it a typo?

    This:

    Code:
    float x = 4.0f;
    double y = 4.0;
    Means that x is a float and y is a double.

    So you ask why use the f? Here is an example:

    Code:
    float x = some_angle * 180.0 / 3.14159f
    What do you suppose happens in this assignment?
    Last edited by VirtualAce; 02-27-2008 at 07:10 PM.

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Bubba View Post
    Because it is a very bad habit to get into. If you wish to use float then specify the f otherwise do not specify the f. It also informs other programmers that you are using float's and not doubles. There is a big difference between a double and a float. Just because 'it works' does not mean it's correct.

    This:

    Code:
    float x = 4f;
    This means nothing to me except that the person who programmed it either was very tired that day or doesn't understand floating point values. Did they want 4.0f or is it a typo?

    This:

    Code:
    float x = 4.0f;
    double y = 4.0;
    Means that x is a float and y is a double.

    So you ask why use the f? Here is an example:

    Code:
    float x = some_angle * 180.0 / 3.14159f
    What do you suppose happens in this assignment?
    I don't care, I would not have used the f so it's not really my problem.
    What do you think happens?

    Also

    Code:
    float x = 4.0;
    double y = 4.0;
    Means that x is a float and y is a double.
    Last edited by esbo; 02-27-2008 at 07:57 PM.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> Means that x is a float and y is a double.

    Maybe, but it's an issue in precision: in Bubba's example, we're using a floating-point constant. Had he left off the trailing f, we would have done double floating point division, and the answer would have been rounded to fit in a float variable.

    As you should know, the quality in answers is quite different when finer precision matters. So, it's at least fairly important to avoid conversion issues while you're using constants like pi, or some other number.

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by citizen View Post
    >> Means that x is a float and y is a double.

    Maybe, but it's an issue in precision: in Bubba's example, we're using a floating-point constant. Had he left off the trailing f, we would have done double floating point division, and the answer would have been rounded to fit in a float variable.
    And exactly what is wrong with that?

    Quote Originally Posted by citizen View Post
    As you should know, the quality in answers is quite different when finer precision matters. So, it's at least fairly important to avoid conversion issues while you're using constants like pi, or some other number.
    I am not really sure what you are saying here, could you be more specific about what point
    it is your are trying to make?

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Also I have been getting into bad habits doing this.
    In 'real life' I would never write:-


    Code:
    float x = 4.0;
    double y = 4.0;
    I would write:-
    Code:
    float x = 4;
    double y = 4;
    Means that x is a float, y is a double

    And never in a million years (I hope) would I write

    Code:
    float x = 4.0f;
    double y = 4.0;
    Last edited by esbo; 02-27-2008 at 08:58 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    float x = 4;
    double y = 4;

    Nothing wrong with promotion (as long as it is compatible). But when you convert to a lesser data type, you might lose your data.

    >> And exactly what is wrong with [rounding]?
    >> what point it is your are trying to make?

    It is completely reasonable to avoid possible loss of data.
    Last edited by whiteflags; 02-27-2008 at 09:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  5. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM