小编qua*_*ore的帖子

GCC划分截断(舍入问题)

在Ubuntu Linux 10.04上使用GCC,我在分割后进行了不必要的舍入.

我试过了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp)
{
    double reading = temp / 100;
    printf("%f\n",reading);  /* displays 226.000000, was expecting 226.60 */
}

int main(void)
{
    FormatReading(22660);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人建议我试试:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp)
{
    long reading = temp ;
    reading = reading / 100;
    printf("%3.2ld\n",reading);  /* displays 226 */
}

int main(void)
{
    FormatReading(22660);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void FormatReading(int temp) …
Run Code Online (Sandbox Code Playgroud)

c floating-point gcc

0
推荐指数
1
解决办法
884
查看次数

标签 统计

c ×1

floating-point ×1

gcc ×1