什么是默认的浮点类型? [英] What's the default floating point type?

查看:287
本文介绍了什么是默认的浮点类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在变量 a 上指定浮点类型,则可以使用 abs 函数。以下示例正在运行:

  fn main(){
let a = -1.0f64;
println!({:?},a.abs());

$ / code>

打印 1 如预期。但是,如果省略 f64 ,那么在编译过程中会抛出一个错误,如下例所示:

  fn main(){
let a = -1.0;
println!({:?},a.abs());





此版本提供以下失败:

 编译操作区v0.1.0(file:/// C:/ git / Rust / playground)
src\main.rs:3:24:3 :29错误:在当前作用域中找不到类型为_的`abs`方法
src\main.rs:3 println!({:?},a.abs());
^ ~~~~
注意:在扩展format_args!
< std宏>:2:25:2:56注意:扩展站点
< std宏>:1:1:2:62注意:在扩展打印中!
< std宏>:3:1:3:54注意:扩展站点
< std宏>:1:1:3:58注意:在扩展println!
src\main.rs:3:5:3:31注意:扩展站点
src\main.rs:3:24:3:29帮助:来自traits的项只能用于if特点在于范围;下面的特性被实现,但不在范围内,可能为它添加一个`use`:
src\main.rs:3:24:3:29 help:candidate#1:use`core :: num: :浮点数
错误:由于前面的错误而中止
无法编译`playground`。

要了解更多信息,请使用--verbose再次运行该命令。

这条消息表示 a 的类型是 _ 。我猜是不能使用 abs 函数,因为不清楚 a 的具体类型是什么。这是否意味着在编译时没有定义类型?如果在Rust中没有声明特定的浮点类型,那么使用的是什么类型的?

解决方案

RFC 212 说:


类型为无约束的整数文字将默认为 i32 [...]浮点文字默认为<$ c $然而,在大多数情况下,有些东西会把推断的类型限制到一个具体的类型,例如通过将它传递给一个方法或将其放入一个结构中。


这是否表示在编译时未定义类型时间?

在实际写出代码之前,定义总是。然而,整数或浮点文字的具体类型在类型的量子叠加中徘徊,直到某种方式迫使它以某种方式或另一种方式。如果没有什么强制它,那么它会回落到默认值。



这是最终允许这样的代码工作:

 使用std :: {f32,f64}; 

fn main(){
let a = -1.0;
让b = -1.0;

println!({:?},f32 :: abs(a));
println!({:?},f64 :: abs(b));



$ b $ p
$ b

如果变量是 f32 f64 ,然后继续选择一个。我不知道编译器内部能够具体回答,但似乎默认类型后备来保存你的代码太迟了。当方法查找发生时,它想知道变量的类型以找到有效的匹配,但是它还不可用。

If the floating point type is specified on variable a, the abs function can be used. The following example is running:

fn main() {
    let a = -1.0f64;
    println!("{:?}", a.abs());
}

It prints 1 as expected. But if the f64 is omitted an error is thrown during compilation as in following example:

fn main() {
    let a = -1.0;
    println!("{:?}", a.abs());
}

This version gives following failure:

   Compiling playground v0.1.0 (file:///C:/git/Rust/playground)
src\main.rs:3:24: 3:29 error: no method named `abs` found for type `_` in the current scope
src\main.rs:3     println!("{:?}", a.abs());
                                     ^~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
src\main.rs:3:5: 3:31 note: expansion site
src\main.rs:3:24: 3:29 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
src\main.rs:3:24: 3:29 help: candidate #1: use `core::num::Float`
error: aborting due to previous error
Could not compile `playground`.

To learn more, run the command again with --verbose.

This message says the type of a is _. I guess the abs function cannot be used, because it is not clear, what the specific type of a is. Does it mean that the type is not defined at compile time? What is the type used, if no specific float type is declared in Rust?

解决方案

RFC 212 says:

Integer literals whose type is unconstrained will default to i32 [...] Floating point literals will default to f64.

However, in most cases something is going to restrict the inferred type to a concrete type, such as by passing it to a method or putting it in a struct.

Does it mean that the type is not defined at compile time?

The type will always be defined before code is actually written out. However, the concrete type of an integer or floating-point literal hovers in a quantum superposition of types until something forces it one way or the other. If nothing forces it, then it would fall back to the default.

That's what ultimately allows code like this to work:

use std::{f32, f64};

fn main() {
    let a = -1.0;
    let b = -1.0;

    println!("{:?}", f32::abs(a));
    println!("{:?}", f64::abs(b));
}

It might be expected that if the variable is either a f32 or an f64, then go ahead and pick one. I don't know the compiler internals to be able to answer concretely, but it appears that the default type fallback comes into play too late to save your code. When method lookup is occurring, it wants to know the type of the variable to find valid matches, but it's not available yet.

这篇关于什么是默认的浮点类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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