为什么当转换为int的float由C以下四舍五入? [英] Why does a float when converted to an int be rounded off below in C?

查看:139
本文介绍了为什么当转换为int的float由C以下四舍五入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在接受记者采访时,有人问我,我想下面的code的内容:

In an interview, I was asked what do I think about the following code:

#include <stdio.h>

int main()
{
    float f = 10.7;
    int a;
    a = f;
    printf ("%d\n", a);
}

我回答:


  • 编译器会发出如要更改浮动 INT 没有投一个警告

INT 你不是使用强制将有垃圾的价值。

The int will have garbage value as you are not using a cast.

然后,他们让我在网上编译运行程序。我不知所措。我的两个假设是错误的。编译器没有发出任何警告,而 INT 的值为10,甚至当我变了,浮动来像10.9或10.3的数值,得到的答案是一样的。即使把一个并没有改变结果。

Then, they allowed me to run the program on a online compiler. I was overwhelmed. Both my assumptions were wrong. The compiler did not emit any warnings, and the int had the value 10. Even when I changed, the float to a value like 10.9, or 10.3, the answer was the same. Even putting a cast did not change the result.

谁能告诉我为什么发生这种情况以及在什么情况下,将其结果是不同的。

Can someone tell me why this happens and in what cases will the result be different.

注意:在编译时,面试官告诉我,没有添加 GCC 标记

NOTE: While compiling, the interviewer told me to add no gcc flags.

编辑:现在我已经明白,浮子没有得到四舍五入,答案将是10.但有人可以解释我这是为什么这样设计?为什么当转换为int任何浮动,以下四舍五入?是否有一个具体的理由?

Now I have understood, that the float does not get rounded off, and the answer will be 10. But can someone explain me why is this designed like this? Why does any float when converted to an int, be rounded below? Is there a specific reason?

推荐答案

这是什么标准6.3.1.4说:

This is what the standard 6.3.1.4 says:

当真实浮动型的有限值被转换为整数
  比_Bool其他类型,小数部分被丢弃(即,
  价值向零截断)。如果积分部分的值
  不能由整数类型psented重新$ P $,其行为是不确定的。

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

未定义行为的一部分是关于整数的大小和符号性。如果你选择了一个整数,它是太小,无法包含的结果,它调用未定义的行为。

The undefined behavior part is regarding size and signedness of the integer. If you picked an integer which is too small to contain the result, it invokes undefined behavior.

你不是使用强制的int将垃圾值

The int will have garbage value as you are not using a cast

的int的可以的有溢出的情况下,垃圾的价值,但presence或缺乏投无关,用它做。

The int may have a garbage value in case of overflow, but the presence or lack of cast has nothing to do with it.

一个编译器从来没有要求出示警告,警告是不是由C标准,只谈到的诊断指定的东西的(即某种讯息,称之为错误或警告)。因此,你永远无法可移植性认为每一个编译器会给出任何形式的警告。

A compiler is never required to show a "warning", warnings are not something specified by the C standard, which only speaks of diagnostics (that is, some kind of message, call it error or warning). Therefore you can never portably assume that every compiler will give a warning of any kind.

在这种情况下,隐含的float到INT转换,并不需要编译器在所有显示任何形式的诊断。良好的编译器会,但是。

In this case of implicit float-to-int conversion, the compiler is not required to display any form of diagnostic at all. Good compilers will, however.

在GCC的情况下,这是相当草率约这样的警告,你必须告诉它明确将给予警告 -Wconversion -Wfloat转换。这是在暗示额外的标志。

In the case of GCC, it is rather sloppy about such warnings, you have to tell it explicitly to give the warnings by adding -Wconversion or -Wfloat-conversion. These are the extra flags hinted at.

这篇关于为什么当转换为int的float由C以下四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆