Solved

type conversion

Posted on 2004-09-03
7
241 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 85 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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