将整数添加到浮点时为什么会出错? [英] Why do I get an error when adding an integer to a floating point?

查看:207
本文介绍了将整数添加到浮点时为什么会出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Rust。我试过这个程序:

I started learning Rust. I tried this program:

fn main() {
     let a = 5;
     let b = 5.5;
     let k = a + b;
     println!("{}", k);
}

并显示此错误:

error[E0277]: cannot add a float to an integer
 --> src/main.rs:4:16
  |
4 |      let k = a + b;
  |                ^ no implementation for `{integer} + {float}`
  |
  = help: the trait `std::ops::Add<{float}>` is not implemented for `{integer}`

代码是否错误?

推荐答案

技术上正确答案是:因为没有人写过 impl Add< f64>对于i32 {}

The technically correct answer is: because no one has written impl Add<f64> for i32 {}.

厚颜无耻的答案是:因为Rust不想让你自己开枪足球。

The cheeky answer is: because Rust doesn't want you to shoot yourself in the foot.

更长,可能更有用的答案是......

The longer, potentially more useful answer is...

在计算机,整数和浮动中点数都有一个有限的范围,最终由我们用来表示它们的位数驱动。在Rust中,没有受到约束的整数的默认类型是 i32 ,并且没有受到约束的浮点的默认类型是 f64

In computers, integers and floating point numbers both have a limited range, ultimately driven by the number of bits that we use to represent them. In Rust, the default type of an integer that isn't otherwise constrained is a i32, and the default type of a floating point that isn't otherwise constrained is a f64.

积分类型不允许你有小数部分,浮点类型他们可以完全代表有限数量的整数。如果Rust允许你添加这两种类型,那么就可以决定哪个数据不太重要,这实际上并不是你想要系统编程语言做的事情!

Integral types don't allow you to have a fractional part, and floating point types have a limited number of integers they can exactly represent. If Rust let you add these two types, it would be making a decision for you about which piece of data was less important, which is not really the kind of thing you want your systems programming language to do!

以下是我可以看到的选项:

Here are the options I can see:


  1. 提出错误,强制程序员选择他们需要的数据类型。

  2. 自动将两个数字转换为整数,丢弃任何潜在的小数值。

  3. 自动转换这两个数字到浮点,不正确地表示更大的积分值。

在这些选择中,只有错误是合理的。

Of those choices, only an error is reasonable.

还有引入一种可以精确处理任意精度的类型的潜力。不幸的是,这些类型不再是处理器操作的便宜,所以你必须权衡性能。

There's also the potential to introduce a type that can precisely handle arbitrary precision. Unfortunately, those types are no longer "cheap" for the processor to operate on, so you'd have to trade off performance.

如果程序员希望执行一些转换,然后您可以使用将值转换为

If the programmer wishes to perform some conversion, then you can cast the value using as:

a as f64 + b;





a + b as i32 






维德拉克补充


[这个答案给出] 0u32 + 0u64 应该有效的印象,但Rust不进行任何数字促销,即使促销无损。此外, i32 f64 是无损促销,因为 f64 有一个52位的尾数。

[this answer gives] the impression that 0u32 + 0u64 should work, but Rust doesn't do any numeric promotions, even if promotion would be lossless. Also, i32f64 is a lossless promotion, since f64 has a 52-bit mantissa.

虽然这些类型的扩展促销确实是无损的,但它们会隐含地增加你的内存需求。例如,过去仅使用32位的内容需要64位。除了内存要求之外,还有语义考虑因素。如果值应该只需要 u8 (0-255),那么将它增加一个可能的值是没有意义的超出这个范围。知道进行这种转换是恰当的,完全取决于程序员。

While these types of widening promotions would indeed be lossless, they would involve implicitly increasing your memory requirements. What used to only take 32 bits now takes 64 bits, for example. Beyond the memory requirements, there's also semantic considerations. If a value should only require a u8 (0-255), then it doesn't make sense to increase it by a value that might be beyond that range. Knowing that it is appropriate to do such a transformation falls solely on the programmer.

这篇关于将整数添加到浮点时为什么会出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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