打印功能返回(INT)和浮点数常数的乘法结果

发布于 2025-02-10 11:28:01 字数 415 浏览 0 评论 0原文

我遇到一个问题,即函数somme_diagonale在主机中返回正确的结果,但是一旦我将其乘以(2 /9)并打印结果,结果似乎为0。

功能:功能:

{
    int i, s = 0 ;
    for ( i = 0 ; i < n ; i++ )
    {
        s = s + mat [ i ] [ i ] ;
    }
    return s ;
}```

call : 


``` printf("resultat est : %f", ( float ) ( 2 / 9 ) * somme_diagonale ( F ) );```



i have tested that somme_diagonale ( F ) returns 165 ( int ).

can someone help me ?

i have a problem where the function somme_diagonale return the right result when called in main, but as soon as i multiply it to ( 2 / 9 ) and print the result it appears to be 0.

function :

{
    int i, s = 0 ;
    for ( i = 0 ; i < n ; i++ )
    {
        s = s + mat [ i ] [ i ] ;
    }
    return s ;
}```

call : 


``` printf("resultat est : %f", ( float ) ( 2 / 9 ) * somme_diagonale ( F ) );```



i have tested that somme_diagonale ( F ) returns 165 ( int ).

can someone help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-02-17 11:28:01

hi,为了拥有float值,必须施放2或9个。但是,这必须在执行划分之前发生(2/9)产生类型int的结果,然后然后将其施加到float
您应该做的是用这些选项替换该代码(对不起,我不是空间的粉丝):(

  • float)2 /9 * somme_diagonale(f)
  • 2 /(float)9 * somme_diagonale(f )
  • (float)2 /(float)9 * somme_diagonale(f)

Hi, in order to have a float value, either the 2 or the 9 must be cast. This, though, must happen before the division is performed. (2/9) produces a result of type int, which only then is cast to float.
What you should do is replace that piece of code with of these options (sorry in advance, I'm not a fan of spaces):

  • (float)2 / 9 * somme_diagonale(F)
  • 2 / (float)9 * somme_diagonale(F)
  • (float)2 / (float)9 * somme_diagonale(F)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文