//共用体其实有时也会用到,特别是RAM不够的时候,我们可以定义一个union,一会把它当做这种类型的变量用,一会又作另外的变量用,当然要前面一个变量不用才能当另外类型变量用,
As an alternative, you may use the following union to store floating-point values.
union f {
float f; /* Floating-point value */
unsigned long ul; /* Unsigned long value */
};
This union contains a float and an unsigned long in order to perform floating-point math operations and to respond to the IEEE error states.
For example:
#define NaN 0xFFFFFFFF /* Not a number (error) */
#define plusINF 0x7F800000 /* Positive overflow */
#define minusINF 0xFF800000 /* Negative overflow */
union f {
float f; /* Floating-point value */
unsigned long ul; /* Unsigned long value */
};
void main (void) {
float a, b;
union f x;
x.f = a * b;
if (x.ul == NaN || x.ul == plusINF || x.ul == minusINF) {
/* handle the error */
}
else {
/* result is correct */
}
}
Copyright (c) Keil Software, Inc. and Keil Elektronik GmbH. All rights reserved.
温馨提示:答案为网友推荐,仅供参考