在Convert.toSingle,Float32,Float之间感到困惑 [英] Confused between Convert.toSingle, Float32, Float

查看:193
本文介绍了在Convert.toSingle,Float32,Float之间感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let rec fun1(a : float, b : float, c : float) =
    if a > 0.0 then
        let b = fun1(a-b, b, c)+fun1(a-c, b, c)
        b
    else
        0.001


    for i in 1 .. 10 do
        for j in 1 .. 10 do
            let i2 : float = i/10.0
            let j2 : float = j/10.0
            Console.WriteLine(fun1(1.0, i2, j2))

无法编译此程序

推荐答案

              let i2 : float = i/10.0
  -------------------------------^^^^

stdin(35,32):错误FS0001:类型"float"与类型"int32"不匹配

stdin(35,32): error FS0001: The type 'float' does not match the type 'int32'

错误消息告诉您不能将整数除以浮点数. F#不会自动为您转换内容.您需要先将i和j转换为浮点数.

The error message is telling you that you can't divide an integer by a float. F# does not auto-convert things for you. You need to convert i and j into floats before dividing them.

    for i in 1 .. 10 do
        for j in 1 .. 10 do
            let i2 = float i / 10.0
            let j2 = float j / 10.0
            Console.WriteLine(fun1(1.0, i2, j2))



这篇关于在Convert.toSingle,Float32,Float之间感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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