Floating point division

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Floating point division

ソリューションへジャンプ
1,232件の閲覧回数
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 解決策
1,032件の閲覧回数
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 返答(返信)
1,032件の閲覧回数
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 件の賞賛
返信
1,033件の閲覧回数
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 件の賞賛
返信