Technical article 100852

Implicit conversion sometimes produces unexpected results.

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

Sometimes an implicit conversion from a floating type to an integer type produces unexpected results. For example, the following two methods produce different answers:

double0 = -71.1;

long2 = (double0 * 1000000); /* This will appear as -71099999 */

double1 = (double0 * 1000000); long2 = double1; /* This will appear as -71100000 */

The following test case illustrates this issue:

/******************************/
          /* Compile with " cc test.c " */
int main()
     {
     double double_org=-71.1, double_temp=0;
     long long_cast=0, long_implicit=0, long_explicit=0;
printf("\nOriginal value for double: %f\n\n", double_org);

/* Do the basic math for comparison */ double_temp = (double_org * 1000000); printf("Double to double: %f\n", double_temp);

/* Cast this to a long */ long_cast = (long) double_temp; printf("Double to long, cast: %ld\n", long_cast);

/* Explicit conversion */ long_explicit = double_temp; printf("Double to long, explicit conversion: %ld\n", long_explicit);

/* Math and conversion in one step (implicit conversion) * This gives a different result */

long_implicit = (double_org * 1000000); printf("Double to long, implicit conversion: %ld\n\n", long_implicit);

/* Check the two values against each other to make sure the * difference is real and not a figment of printf */

if (long_implicit == long_explicit) printf("Real values are equal.\n"); else printf("Real values are different!\n\n");

exit(0);
}
/******************************/

Cause

It would appear that an explicit cast produces a different result than an implicit conversion. However, the following shows that cast is irrelevant. The example below saves the result of the multiplication in a long double. After:

long_double1 = (double_org * 1000000)
the assignments:
long_implicit = long_double1
and:
long_cast = (long)long_double1
produce identical results.
To understand the real reason for the difference it is necessary
to examine how the multiplication is carried out. Our compiler
does the multiplication in extended double precision in the
floating register stack.  This behavior is explicitly permitted
by the C standard.  This means that the actual operation looks
more accurately like this:
(long double)double0 * 1000000.0L

The incoming value for double0 is (more accurately):

-71.0999999999999943

and the result of the multiply is:

-71099999.9999999943
When this is rounded back to double for the assignment to double1,
it becomes - -71100000.000000000.  When this is assigned to long2,
the result is the expected -71100000.  On the other hand, if the
result of the multiply is directly assigned to long2, the result of
truncating -71099999.9999999943 is naturally -71099999.

Solution

Use the implicit conversion method for more accurate results:

long2 = (double0 * 1000000);

From the Xinuos technical article database, record 100852. Reproduced with permission. The original is at www.sco.com/ta/100852, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.