This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] 编译器/TMS320F2.8335万:分数'1/2'分配到浮点变量时,导线为'0&#39

Guru**** 2334730 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/583089/compiler-tms320f28335-fraction-1-2-leads-to-0-when-assigned-to-a-float-variable

部件号:TMS320F2.8335万

工具/软件:TI C/C++编译器

大家好,

我尝试将一个分数'1/2'分配给一个浮点变量,当我在监视表达式中观察变量时,结果是'0'而不是0.5 '。

此外,当我添加 一个带有'1/2'的变量时,如果不能反映,当我添加0.5 '时,它会反映在变量中。

请帮助我消除这种模棱两可的情况。  

我也尝试过更改库,但问题仍然存在。

谢谢你。

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好,

    我相信这是预期的行为。 1和2是整数,因此编译器将在将结果转换为浮点值之前进行整除(1/2=0)。 如果您要写入1.0 / 2.0 或(FLOW)1/(FLOW)2,它将执行浮点除法,并为您提供0.5。

    Whitney