c++ - 当浮点变量除以c ++中的整数变量时的结果类型
问题描述
我在 geeksforgeeks.com 上用 c++ 解决了一个问题,我在其中确定了操作后变量的结果类型。
输入:1 1 2 3 5 gfgc
输出:4 8 4 8 32 1
Example:
Testcase 1:
b/c = 2/3 =>sizeof(2/3)=>float size is 4 bytes
"b/a = 2/1 =>sizeof(2/1)=>double size is 8 bytes"
c/a = 3/1 =>sizeof(3/1)=>integer size is 4 bytes
(c/a)+l =3+5= 8 =>sizeof(8)=>long long size is 8 bytes
sizeof(gfgc) = 32 => It is not 4 because of the reason listed here
sizeof(c) = 1 as it is just a character.
有人可以解释引号中显示的输出背后的原因吗?
解决方案
给定float a
and int b
(反之亦然)和表达式auto c = a/b
,两者a
和b
都被强制转换为float
s 以进行除法,并且c
是 afloat
。
更一般地,如果任一操作数是浮点类型,则生成的类型将在此列表中的第一个表达式中找到:long double
、double
或float
; 另一个操作数转换为该类型。如果两个操作数都是整数,则适用类似的规则。
这些是“通常的算术转换”。请参阅[expr.arith.conv]
。
推荐阅读
- css - 使按钮粘在页面底部*但*尊重滚动
- angular - Angular 6 中的多 ./styles.css 中的错误
- php - 在 Woocommerce 中获取具有特定产品属性的产品购买总数
- javascript - Meteor 应用程序上 React JS 中的访问控制允许来源
- javascript - 如何使用react js过滤html表的所有列?
- angular - 如何在按钮单击时添加新功能
- angular - 如何在 Angular 材料 6 表中使用 colspan 和 ngFor?
- elasticsearch - 使用java客户端Api时如何将_delete_by_query限制为单一类型
- django - 在 Eclipse 中运行 django 服务器时出错
- c++ - 将 c++ 结构转换为 c# 结构。错误输出