Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

type conversion

Posted on 2004-09-03
7
Medium Priority
?
245 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

610 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