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
Solved

type conversion

Posted on 2004-09-03
7
237 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

840 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