以下代码:
uint8_t num_0 = 50;
uint16_t num_1 = 50;
float num_2 = 50.0;
#define NUM_DIV 100
uint8_t val_0 = 1320;
uint8_t val_1 = 1310;
uint8_t res0[3] = 0;
uint8_t res1[3] = 0;
res0[0] = (uint16_t)((val_0 * num_0) / NUM_DIV);
res0[1] = (uint16_t)((val_0 * num_1) / NUM_DIV);
res0[2] = (uint16_t)((val_0 * num_2) / NUM_DIV);
res1[0] = (uint16_t)((val_1 * num_0) / NUM_DIV);
res1[1] = (uint16_t)((val_1 * num_1) / NUM_DIV);
res1[2] = (uint16_t)((val_1 * num_2) / NUM_DIV);
在codeWarrior(execute on MC9SG128 chip)中,执行结果:
res0[0] = 4;
res0[1] = 4;
res0[2] = 660;
res1[0] = 655;
res1[1] = 655;
res1[2] = 655;
在eclipse(execute on PC)中,执行结果:
res0[0] = 660;
res0[1] = 660;
res0[2] = 660;
res1[0] = 655;
res1[1] = 655;
res1[2] = 655;
假设,uint32_t num_3 = 50;
则,num_3参与运算后结果与num_2一致,因为其数据长度都足以存放乘法运算后超过65535的值。