问题描述
我正在研究Rust手册的第二版,并决定尝试制作经典的摄氏转华氏转换器:
I'm working through the second edition of the Rust handbook, and decided to try and make the classic Celsius-to-Fahrenheit converter:
fn c_to_f(c: f32) -> f32 {
return ( c * ( 9/5 ) ) + 32;
}
使用cargo build
进行编译将产生编译时错误:
Compiling this with cargo build
will yield the compile-time error:
error[E0277]: the trait bound `f32: std::ops::Mul<{integer}>` is not satisfied
--> src/main.rs:2:12
|
2 | return (c * (9 / 5)) + 32;
| ^^^^^^^^^^^^^ the trait `std::ops::Mul<{integer}>` is not implemented for `f32`
|
= note: no implementation for `f32 * {integer}`
作为新的Rust程序员,我的解释是我不能将float和integer类型相乘.我通过使所有常量成为浮点数来解决了这个问题:
As a new Rust programmer, my interpretation is that I cannot multiply float and integer types together. I solved this by making all of my constants floating points:
fn c_to_f(c: f32) -> f32 {
return ( c * ( 9.0/5.0 ) ) + 32.0;
}
这让我有所保留.来自C/C ++/Java/Python,令人惊讶的是得知您不能简单地对不同类型的数字执行算术运算.像我在这里所做的那样,将它们简单地转换为相同类型是正确的选择吗?
This leaves me with reservations. Coming from C/C++/Java/Python, it was surprising to learn that you cannot simply perform arithmetic on numbers of different types. Is the right thing to simply convert them to the same type, as I did here?
推荐答案
TL; DR :as
是在原始数字类型之间进行转换的最常见方法,但是使用它需要思考. /p>
TL;DR: as
is the most common way to convert between the primitive numeric types but using it requires thinking.
fn c_to_f(c: f32) -> f32 {
(c * (9 as f32 / 5 as f32)) + 32 as f32
}
不过在此示例中,使用浮点字面量以以下内容开头更合理:
In this example though, it's more reasonable to just use floating point literals to start with:
fn c_to_f(c: f32) -> f32 {
(c * (9. / 5.)) + 32.
}
真实问题是,进行混合类型算术有点复杂.
The real problem is that doing mixed type arithmetic is a bit complicated.
如果将 a T
乘以T
,则通常希望至少使用基本类型得到类型为T
的结果.
If you are multiplying a T
by a T
, you generally expect to get a result of type T
, at least with the basic types.
但是,在混合类型时,存在一些困难:
When mixing types, however, there are some difficulties:
- 混合签名,
- 混合精度.
例如,i8 * u32
的理想结果是什么?可以包含所有i8
和u32
值的完整集合的最小类型是i64
.那应该是结果吗?
So, for example, what is the ideal result of i8 * u32
? The smallest type that can encompass the full set of all i8
and u32
values is a i64
. Should that be the result?
作为另一个示例,f32 * i32
的理想结果是什么?可以包含所有f32
和i32
值的完整集合的最小类型是f64
.那应该是结果吗?
As another example, what is the ideal result of f32 * i32
? The smallest type that can encompass the full set of all f32
and i32
values is a f64
. Should that be the result?
我发现进行这样的扩大的想法相当混乱.它也会对性能产生影响(一旦向量化,对f32
的操作可能比对f64
的操作要快得多).
I find the idea of having a such widening rather confusing. It also has performance impacts (operations on f32
can be much speedier than operations on f64
, once vectorized).
由于这些问题,Rust现在要求您明确:您希望将计算携带到哪种类型?哪种类型适合您的特定情况?
Due to those issues, Rust for now requires you to be explicit: which type do you want the computation to be carried in? Which type makes sense for your particular situation?
然后使用as
进行适当的转换,并考虑要应用哪种舍入模式(当从浮点变为整数时,是.round()
,.ceil()
,.floor()
或.trunc()
).
And then cast appropriately, using as
, and do think about which rounding mode to apply (.round()
, .ceil()
, .floor()
or .trunc()
when going from floating point to integral).
以类似方式进行加,减和除工作.
这篇关于如何乘/除/加/减不同类型的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!