Click to See Complete Forum and Search --> : repost: type conversion problem


Karsten Döring
April 16th, 1999, 07:13 AM
Hi,

here is a repost of my message:

I have a problem with following code:

void calculateamount( long* l_amount )
{
double amount;

// amount gets calculated here
// the result is: 19.67

amount *= 100;
*l_amount = amount;

// then l_amount = 1966 and not 1967. WHY ????
// how can i prevent it?

}

I use VC6, SP2. U can find an example project on http://www.dedenet.de/d_l_test.zip

Michael Decker
April 16th, 1999, 07:19 AM
When you convert a float to an int (or long) then the value is truncated and not rounded.

To round the value try:
*l_amount = (long)(amount + 0.5);

Michael Decker
April 16th, 1999, 07:20 AM
OOPs, sorry I misread your problem...

Karsten Döring
April 16th, 1999, 07:22 AM
I know that, but the value cannot be truncated because it's 1967 and not 196.7 or something else. i just want to convert
(double)1967 to (ulong)1967.

take a look at that example project, there u can see it.

do u have another solution or do u agree to me that there is a bug in vc6, sp2

Michael Decker
April 16th, 1999, 07:29 AM
I tested the problem that you show and I get the correct answer, assuming amount = 19.67.

Are you sure its exactly 19.67 and not 19.66xxxx?

Michael Decker
April 16th, 1999, 07:35 AM
I took a look at you example project, very odd behavior! I'll see what I can find out.

Karsten Döring
April 16th, 1999, 07:36 AM
have u downloaded that example project i made? there u can see that it is exact 19.67

M Grundberg
April 16th, 1999, 07:48 AM
Hi again Karsten,
I downloaded your project and tested it and it's not a truncation error like I thought yesterday. It has probably something to do with the rounding error that occurs when trying to represent either 3.2 or 16.47 in binary form. Not all decimal numbers can be represented exactly in a limited precision binary form, so they have to be rounded. Though VC6 debugger surely seems to be rounding it back correctly.
This is no bug in VC6, but rather a known limitation of the float value representation.

/Michael Grundberg
M.Sc. Computer Science
Visionova IT-System, Sweden
michael@itsystem.se