带整数和浮点数的Rust中的泛型函数存在问题。在Rust中完成计算机程序的结构和解释

带整数和浮点数的Rust中的泛型函数存在问题。在Rust中完成计算机程序的结构和解释,rust,sicp,Rust,Sicp,到目前为止,我有这个。我找不到一种方法来处理与足够好的比较。我正在研究计算机程序的结构和解释,我希望尽我所能符合本书中的实践。这个计划很简单。我明白了。只是需要帮助使我的防锈代码更通用 游乐场: 完成的操场:fn足够好(猜:T,x:T)->bool{ abs(square(guess)-x

到目前为止,我有这个。我找不到一种方法来处理与
足够好的比较。我正在研究计算机程序的结构和解释,我希望尽我所能符合本书中的实践。这个计划很简单。我明白了。只是需要帮助使我的防锈代码更通用

游乐场:

完成的操场:

fn足够好(猜:T,x:T)->bool{
abs(square(guess)-x
您需要将
0.0001
转换为
T
,因为
T
仅实现。

或者您可以设置
T:PartialOrd
,但这会使函数无法接受整数类型。

就是这样。我想这就是我在喷气式飞机之外努力工作的原因。我开始在家里输入你的答案,它开始弹出我之前在REPL.it上输入的内容,但我猜我输入了一个拼写错误,没有找到它。希望它能帮助别人。
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 {}", average(4, 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`.
use std::cmp::PartialOrd;
use std::convert::Into;
use std::ops::{Add, Div, Mul, 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 {}", 4, 2, average(4, 2));
    println!("Square of {} is {}", average(4, 2), square(average(4, 2)));
    println!("Absolute Value of {} is {}", -4.5, abs(-4.5));
    println!("Test of good_enough: {}", good_enough(3, 9));
    println!("See improve in action guess: {} x:{} outcome:{}", 2, 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