Rust中具有整数和浮点数的泛型函数出现问题通过Rust进行计算机程序的结构和解释

问题描述

到目前为止,我有这个。而且我找不到与good_enough进行比较的方法。我正在研究“计算机程序的结构和解释”,我希望使自己尽可能地适应本书中的实践。计划很容易。我知道了只需要帮助使我的rust代码更通用即可。

游乐场:Rust Playground

use num_traits::cast::FromPrimitive;
use std::cmp::PartialOrd;
use std::ops::{Add,Div,Mul,Sub};

fn square<T: Mul<Output = T> + Copy>(x: T) -> T {
    x * x
}

fn average<T: Add<Output = T> + Div<Output = T> + FromPrimitive>(x: T,y: T) -> T {
    (x + y) / FromPrimitive::from_usize(2).unwrap()
}

fn abs<T: PartialOrd + Mul<Output = T> + FromPrimitive>(x: T) -> T {
    if x < FromPrimitive::from_usize(0).unwrap() {
        x * FromPrimitive::from_isize(-1).unwrap()
    } else {
        x
    }
}

fn good_enough<T: Copy + Sub<Output = T> + Mul<Output = T> + FromPrimitive + PartialOrd>(guess: T,x: T) -> bool {
    abs(square(guess) - x) < 0.0001
}

fn main() {
    println!("Average or {} and {} is {}",4,2,average(4,2));
    println!("Square of {} is {}",2),square(average(4,2)));
    println!("Absolute Value of {} is {}",-4.5,abs(-4.5));
    println!("Test of good_enough: {}",good_enough(3,9));
}

错误:

error[E0308]: mismatched types
  --> src/main.rs:22:30
   |
21 | fn good_enough<T: Copy + Sub<Output = T> + Mul<Output = T> + FromPrimitive + PartialOrd>(guess: T,x: T) -> bool {
   |                - this type parameter
22 |     abs(square(guess) - x) < 0.0001
   |                              ^^^^^^ expected type parameter `T`,found floating-point number
   |
   = note: expected type parameter `T`
                        found type `{float}`
   = help: type parameters must be constrained to match other types
   = note: for more information,visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to previous error

For more information about this error,try `rustc --explain E0308`.
error: could not compile `sqrt`.

为那些对计算机程序的结构和解释感兴趣的人完成了sqrt项目(第001章)!

use std::cmp::PartialOrd;
use std::convert::Into;
use std::ops::{Add,Sub};

use num_traits::cast::FromPrimitive;

fn square<T>(x: T) -> T
    where T: Mul<Output=T> + Copy
{
    x * x
}

fn average<T>(x: T,y: T) -> T
    where T: Add<Output=T> + Div<Output=T> + FromPrimitive
{
    (x + y) / FromPrimitive::from_usize(2).unwrap()
}

fn abs<T>(x: T) -> T
    where T: PartialOrd + Mul<Output=T> + FromPrimitive
{
    if x < FromPrimitive::from_usize(0).unwrap() {
        x * FromPrimitive::from_isize(-1).unwrap()
    } else {
        x
    }
}

fn improve<T>(guess: T,x: T) -> T
    where T: Add<Output=T> + Div<Output=T> + FromPrimitive + Copy
{
    average(guess,x / guess)
}

fn good_enough<T:>(guess: T,x: T) -> bool
    where T: Copy + Sub<Output=T> + Mul<Output=T> + FromPrimitive + PartialOrd + Into<f64>
{
    let new_guess = guess.into();
    let y = x.into();
    abs(square(new_guess) - y) <= FromPrimitive::from_f64(0.00000000000001).unwrap()
}

fn sqrt_iter<T>(guess: T,x: T) -> f64
    where T: Copy + Add<Output=T> + Div<Output=T> + Mul<Output=T> + Sub<Output=T> + PartialOrd + FromPrimitive + Into<f64>
{
    let mut updated_guess = guess.into();
    // loop only because I had some stack overflows during testing
    let y = x.into();
    loop {
        if good_enough(updated_guess,y) {
            return updated_guess;
        } else {
            updated_guess = improve(updated_guess,y);
        }
    }
}

fn my_sqrt<T>(x: T) -> f64
    where T: Copy + Add<Output=T> + Div<Output=T> + Mul<Output=T> + Sub<Output=T> + PartialOrd + FromPrimitive + Into<f64>
{
    sqrt_iter(FromPrimitive::from_f64(1.0).unwrap(),x)
}

fn main() {
    println!("Average or {} and {} is {}",9));
    println!("See improve in action guess: {} x:{} outcome:{}",9,improve(2,9));
    println!("Sqrt of {} is {}",7921,my_sqrt(7921));
}

输出:

Average or 4 and 2 is 3
Square of 3 is 9
Absolute Value of -4.5 is 4.5
Test of good_enough: true
See improve in action guess: 2 x:9 outcome:3
Sqrt of 7921 is 89

成品游乐场:Final Playground!

解决方法

fn good_enough<T: Copy + Sub<Output = T> + Mul<Output = T> + FromPrimitive + PartialOrd>(guess: T,x: T) -> bool {
    abs(square(guess) - x) < FromPrimitive::from_f64(0.0001).unwrap()
}

您需要将0.0001转换为T,因为T仅实现PartialOrd
或者,您可以使T: PartialOrd<f64>但使该函数不能接受整数类型。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...