C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old February 9th, 2003, 02:57 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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;


}

Reply With Quote
  #2  
Old February 9th, 2003, 03:00 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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.
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old February 9th, 2003, 03:21 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
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.

Reply With Quote
  #4  
Old February 9th, 2003, 03:30 PM
Stargazer Stargazer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 8 Stargazer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #5  
Old February 9th, 2003, 03:45 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
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.

Reply With Quote
  #6  
Old February 9th, 2003, 03:45 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
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.

Reply With Quote
  #7  
Old February 9th, 2003, 04:00 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
"misleading"? Stargazer, was that you who put his functions into the header file for building a library?

Last edited by M.Hirsch : February 9th, 2003 at 04:03 PM.

Reply With Quote
  #8  
Old February 9th, 2003, 05:14 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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

Reply With Quote
  #9  
Old February 9th, 2003, 06:31 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
andy3109, that would probably be the way to go .

Reply With Quote
  #10  
Old February 9th, 2003, 08:35 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #11  
Old February 9th, 2003, 08:48 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
My book doesn't mention it..hmm..Is that ivors book good?

Reply With Quote
  #12  
Old February 9th, 2003, 10:08 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
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.

Reply With Quote
  #13  
Old February 11th, 2003, 08:22 PM
ahuimanu ahuimanu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Right Coast
Posts: 25 ahuimanu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 34 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Conv. From double to float?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump