473,238 Members | 1,791 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,238 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 21797
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);
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.