?
Solved

type conversion

Posted on 2004-09-03
7
Medium Priority
?
243 Views
Last Modified: 2013-12-03
Hi,
I was recently debugging a program, when I noticed something funny going on with the maths - with some playing, I managed to determine that this was to do with type conversions - trouble is, I can't figure out exactly *why* these 'problems' are there! Could somebody please explain this to me? I'm using VC++ 5.0. Take for example the following situation:

float a=4.7, b=50;

float c=a*b;                         //c=235, correct.
int d=a*b;                           //d=234!
int e=(float)4.7*(float)50;     //e=235 :S

What is causing this?

Thanks alot,
Jonathan
0
Comment
Question by:jonathandodd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 11978198
you have initialised b with an integer not a float number you have to do

b=50.0
0
 

Author Comment

by:jonathandodd
ID: 11978212
that won't make any difference...if it's a float variable it'll be stored as a float, extra .00 s  or not :p Anyhow, that was just a setup scenario to demonstrate the problem...
0
 
LVL 11

Accepted Solution

by:
avizit earned 340 total points
ID: 11978282
the reason i can think of is

in

int d=a*b;  

teh product is getting calculated to   234.99999999999999 ( something like that ) and the
conversion to int is making it 234

while in

int e=(float)4.7*(float)50;
the product is getting calclated to

235 or maybe 235.00000000000000000001 ..

also check http://www2.hursley.ibm.com/decimal/decifaq1.html#inexact

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:grg99
ID: 11978285
what's happening is normal floating point roundoff.

Apparently 4.7 times 50.000 is a few bits shy of 235.

If you print out the number with lots of decimal places you'll see something like 234.999998.

Storing the result into an integer results in truncation to the next lower integer.

0
 
LVL 3

Expert Comment

by:teratoma
ID: 11978332
The real question question is why (float)4.7*(float)50 is different from a*b when a and b have type float.  The real answer is that in the case of (float)4.7*(float)50, the compiler is doing the math at compile time, and in the case of a*b, the code generated by the compiler is doing the math at runtime.  Your compiler seems to use higher-precision math than float while preprocessing literals, and it's allowed to do that.  It's not allowed to inject code to do the same thing into the final executable because the programmer specified float*float and nothing else.  Note also that the preprocessor probably ignores your casts, so 4.7*50 might work the same way as (float)4.7*(float)50.
0
 

Author Comment

by:jonathandodd
ID: 11979358
wow thanks - I've been working with C++ for years and never realised floating point calculations worked like that! Why is it that floating point calculations are sometimes worked out to be 0.000000001 or something off? Surely, if I put in 4.7, it stores it as 4.7, not 4.699999999999999 or 4.7000000000000001 (or to however many places)?
0
 

Author Comment

by:jonathandodd
ID: 11980069
OK I've done some reading - I geddit now :D

thanks again!
Jonathan
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question