Solved

type conversion

Posted on 2004-09-03
7
236 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 72
IdTCPClient1->Disconnect(); not working 3 66
thread-safe code in c++ 2 100
Best book to learn C++ 4 78
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 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