ARM Technical Support Knowledge Articles

Why does double-to-integer conversion give incorrect result?

Applies to: RealView Development Suite (RVDS)

Scenario

When a double variable d=2.26*100 is converted to unsigned long, the value became 225. If d=226, then the converted unsigned long is 226. Is this expected behaviour? For example,

#include <stdio.h>
double dvar;
unsigned long lvar;
int main()
{
  dvar = 2.26 * 100;
  lvar = (unsigned long)dvar;
  printf ("%f %d\n", dvar, lvar);
  return 0;
}

Answer

Yes, this is expected behaviour. Note that decimals like 2.26 are not represented exactly in binary, and that floating-to-integer conversion rounds down.

Both Visual C++ and GCC also generate the same result as armcc.

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential