ARM Technical Support Knowledge Articles

CONVERTING FLOATING-POINT NUMBER TO INTEGER

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

I would like to convert a floating-point number to an integer. Is there a standard math library routine to accomplish this?

ANSWER

No. The C Programming Language does not require library routines to perform simple type conversions. Types are converted using implicit type casting. For example:

long l;
float f;

l = f;      // f is converted to a long value
            // the result is stored in l

In the above example, the value of the floating-point variable f is converted to a long value and is assigned to l. If the value of f is outside the range of values for a long type, the maximum positive or negative long value is assigned to l. The value ranges for the following integer and floating point types are shown below;

Data Type        Bits  Bytes    Value Range
--------------------------------------------------------------
signed int       16    2        -32768 to 32767
unsigned int     16    2        0 to 65535
signed long      32    4        -2147483648 to +2147483647
unsigned long    32    4        0 to 4294967295
float (IEEE-754) 32    4        ±1.175494E-38 to ±3.402823E+38

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-07-15 10:22:42

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