#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    Conv. From double to float?


    When I don't stick that f at the end of '3.456' it gives me a warning saying, "conversion from 'double' to 'float', possible loss of data." But it has the same output as if I added the f. I don't understand why it thinks there is a double when I clearly defined the array 'num' as a float. Here is the code:

    #include <iostream.h>

    int main()
    {
    float num[5];

    for (int i=0; i<5; i++)
    num[i] = i * 3.456f; <----- There is the problem

    for (i=0; i<5; i++)
    cout << "num[" << i <<"] == " << num[i] << '\n';
    return 0;


    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    the default type for floating point numbers is "double", not "float". this is why you get a warning. You could just ignore it, but for good style you should put the "f" there.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    Actually it has to do with the implicit conversion that a c compiler does. Whenever it sees a number with a decimal point, like 3.145, it assumes that the number is of type double, for greater precision. Now if you multiply a double by an int variable, the compiler will first automatically convert the int to a double (again for precision reasons) and then do the multiplication. Hence the result will be a double. Now you're assigining this double to a float variable (which is less precise than a double) and thus the compiler has to downcast the double result to a float. Since a float is less precise than a double, you might lose some digits while downcasting it and that's what the compiler is complaining about (maybe not for this specific number, but for other numbers, it could happen).

    Now if you explictly cast a number as float by putting an f at the end (like 3.145f), the compiler will not assume it to be double. Now since you're multiplying a float and an int, the compiler will automatically convert the int as a float before doing the multiplication. Thus the result will be a float and can be assigned to another float variable without loss of precision.

    See http://www.eskimo.com/~scs/C-faq/s14.html for more on floating points (esp. sections 14.1 and 14.4). For conversion rules that a C compiler does automatically, see http://www.ssec.wisc.edu/~dglo/c_class/promo_conv.html
    Hope this helps! :)
    Last edited by Scorpions4ever; February 9th, 2003 at 03:24 PM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    8
    Rep Power
    0
    andy3109, these people have been misleading you. It looks like you're programming in C++ and they're giving you advice about C (If you actually are programming in C, ignore this)

    Just use typecasting. Instead of having this line
    num[i] = i * 3.456;

    Put this line:
    num[i] = i * (float)3.456;

    No errors, no warnings.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    Misleading?? Would you like to qualify that? FYI, adding a suffix of f converts the constant to a floating point in both C and C++. Just google for "Floating Point Constants C++" for proof of this. Incidentally this typecasting code that you showed also works in both C and C++.
    Last edited by Scorpions4ever; February 9th, 2003 at 03:48 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Scorpions4ever and M.Hirsh are correct, whether it be C or C++, in that a decimal number is by default interpreted as a double. The end result is that the number is being assigned to a float, which can result in loss of data/precision, not unlike storing an int into a short would do. Similarly, when operating on floating point number with integer an type, the integer is coerced to the respective floating point format before the operation by the compiler.

    Forcing the constant to a float, regardless how (via typecast or explicit statement with the 'f') will solve the problem, since the entire operation is being done with floats.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    "misleading"? Stargazer, was that you who put his functions into the header file for building a library? :rolleyes:
    Last edited by M.Hirsch; February 9th, 2003 at 04:03 PM.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Thanks everyone for your replies. I understand, but it sounds sort of stupid that when you type float, its default is a double. I might as well type double instead of float, right?

    -andy
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    andy3109, that would probably be the way to go :).
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    andy,

    One of the great things about C++ is that it's a very efficient language. You can always just use the greatest precision for any variable like long for integers and long double for floating point numbers, and forget about it, but then you're using more memory than you need. For a short program, it doesn't make much difference, but if you had a bunch of massive arrays of data you were dealing with, it would slow down your program and use lots of memory that may not be available causing your program to fail. Why not learn good coding style? If you only need a float, use a float. If you need greater precision, use a double or long double.

    C++ is a strongly typed language and you need to learn to assign the correct types to the correct variables, so that later when you get into pointers, classes, and functions you will have a feel for types.

    Stargazer,

    I've never studied C, only C++, and the default in C++ is for a floating point literals to be of type double. It is one of the basic lessons in my C++ book "Ivor Horton's Beginning C++".
    Last edited by 7stud; February 9th, 2003 at 08:39 PM.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    My book doesn't mention it..hmm..Is that ivors book good?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    andy,

    I started off with one of those Sam's books, which I quickly discovered was horrible. It doesn't explain things fully, so you don't understand the whys. I liked Ivor's book because it's very thorough and logical. However, it gets pretty complex at times especially in the later chapters, so I wished he had made some of the examples simpler. That's not to say, you won't be able turn back several pages or even a chapter and thoroughly re-read a couple of sections to figure out what's happening, unlike the Sam's book where what they were doing in an example wasn't explained anywhere. I just bought "Ivor Horton's Beginning Visual C++" to learn more about windows programming, and it has a shorter version of Beginning C++ in the first 11 chapters. I haven't looked at it too closely yet, but I wish I had started with that book.
    Last edited by 7stud; February 10th, 2003 at 04:30 AM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Right Coast
    Posts
    25
    Rep Power
    0

    stargazer wrong


    Most other advice about the use of the f suffix is correct.

    Things like I/O and the presence of classes and the STL are among the refinements of C++ built ON TOP OF C. However, the basic handling of things like the primitive types is not different between C and C++.

    So the whole

    float x = 4.5f;

    business is present in BOTH C and C++.

    7stud, Scorpions4Ever and M>Hirsch are correct in this matter.

    For what it is worth:

    float x = 4.5f;

    is less effort for the compiler than

    float x = (float)4.5;

    Then don't forget this awfulness from C++:

    float x = static_cast<float>(4.5);

    which ensures that your type conversion is kosher at compile time.

    J

IMN logo majestic logo threadwatch logo seochat tools logo