473,385 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

conversion from 'int' to 'float', possible loss of data ??

Expand|Select|Wrap|Line Numbers
  1. int a=10;
  2. int b=3;
  3.  
  4. float c;
  5. c=a/b;
  6.  
  7. cout << c << endl;
if, in my program, the int type for a and b is a must (according to the return), performing operation on a & b results in float type of c.

From msvc++, a message "warning C4244: '=' : conversion from 'int' to 'float', possible loss of data" is resulted. how to solve? the c variable printed out to the screen is 3, but not 3.3333333333333............. !
Apr 6 '08 #1
2 21874
Laharl
849 Expert 512MB
Because neither a nor b is a float or double, but are instead both integers, integer division is done, so the remainder is jettisoned and the whole number is all that can be stored. Either declare one of a or b as float/double or use a cast, eg ((float) a)/b. Don't try to cast the result, eg (float)(a/b), since that will just cast 3 to a float, 3.0.
Apr 6 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
[quote=Laharl]
Because neither a nor b is a float or double, but are instead both integers, integer division is done, so the remainder is jettisoned and the whole number is all that can be stored. Either declare one of a or b as float/double or use a cast, eg ((float) a)/b. Don't try to cast the result, eg (float)(a/b), since that will just cast 3 to a float, 3.0.
[quote]

Not entirely correct.

a and b are int and c is a float. c/a requires an automatic conversion. In this case, the int a is converted to a temporary float, the division is done using float and the result is a float. See K&R page 198 A6.5.

The warning is due to the fact that a float has only six significant figures whereas an int can have more. If it does, then accuracy is lost.

In general, you cannot convert an integer to floating point without possibke losing data. Also, you cannot convert from floating point back to integer without losing the deceimal places, so you get a warning again.

My advice is to stick with either all integer or all floating point. If you decide on floating point, then use double throughout unless you can write diwn on paper the precise reason you can't do this. In this respect your advice is correct.
Apr 7 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
4
by: Simon Cheng | last post by:
Hi, Using VS.NET 2003, the following code fails compilation (as expected): ------------------------------------------- using System; class App { static public int I { get { return 1.0f; } }...
1
by: Josué Andrade Gomes | last post by:
Take this code: 1: int main() 2: { 3: unsigned short x = { 1, 2, 3, 4, 5 }; 4: unsigned short sum = 0; 5: 6: for (int i = 0; i < 5; ++i) { 7: sum += x; 8: } 9: }
15
by: k3n3dy | last post by:
Hello everybody, I would like to ask anybody to help me understand what should be done exactly with this Problem. I have provided what I have up to now, although it is not quite accurate. Create...
24
by: Rajesh S R | last post by:
Isn't this code violation of C99 standard? #include <stdio.h> int main( void ) { float a = 0.7; if(a < 0.7) printf("Wrong"); else
0
by: ronysk | last post by:
Hi, I am posting here to seek for help on type conversion between Python (Numeric Python) and C. Attachment A is a math function written in C, which is called by a Python program. I had...
2
by: curious2007 | last post by:
Hello, The following code: inline float SIGN(const double &a, const float &b) {return b>=0 ? (a>=0 ? a:-a) : (a>=0 ? -a:a);} gives me the warning:
8
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val =...
3
by: yxxxxy | last post by:
Hi, this is a part of my program code. i want to ask two questions. int time; float rate; float salary; printf("Enter # of hours worked (-1 to end):"); scanf("%d",&time);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.