February 9th, 2003, 02:57 PM
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:
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';
February 9th, 2003, 03:00 PM
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.
February 9th, 2003, 03:21 PM
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.
February 9th, 2003, 03:30 PM
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.
February 9th, 2003, 03:45 PM
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.
February 9th, 2003, 03:45 PM
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.
February 9th, 2003, 04:00 PM
"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.
February 9th, 2003, 05:14 PM
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?
February 9th, 2003, 06:31 PM
andy3109, that would probably be the way to go :).
February 9th, 2003, 08:35 PM
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.
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.
February 9th, 2003, 08:48 PM
My book doesn't mention it..hmm..Is that ivors book good?
February 9th, 2003, 10:08 PM
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.
February 11th, 2003, 08:22 PM
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.