Floating point division

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Floating point division

跳至解决方案
805 次查看
woohoo
Contributor I

Hi,

 

I am using a GT16A microcontroller and am having issues when dividing 2 numbers that would form a floating point number. There is no problem with addition, subtraction and multiplication.

 

For example, 20/8 gives me a value of 2.0 and not the expected 2.5

 

I have tried increasing the stack size and I still haven't managed to figure out what is causing the problem. Any ideas?

标签 (1)
标记 (3)
0 项奖励
回复
1 解答
605 次查看
kef
Specialist I

float f:

f = 20/8;

20 and 8 both are integers, division produces integer result, and then you request to convert integer 2 to float. To get 2.5 at least one of dividers has to be FP.

在原帖中查看解决方案

0 项奖励
回复
2 回复数
605 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport

1. you need have floating lib support. when creates new project, choose floating library support for your project.

2. do like this:

float f;

f= (float)20/(float)8;

0 项奖励
回复
606 次查看
kef
Specialist I

float f:

f = 20/8;

20 and 8 both are integers, division produces integer result, and then you request to convert integer 2 to float. To get 2.5 at least one of dividers has to be FP.

0 项奖励
回复