F#等价于C#代码输出不同,可能存在数学差异?
我开始喜欢C#和DOTNET编程,因为我已经涉足其中 我有一个有效的C代码: 我尽力把它翻译成F#:F#等价于C#代码输出不同,可能存在数学差异?,c#,.net,f#,C#,.net,F#,我开始喜欢C#和DOTNET编程,因为我已经涉足其中 我有一个有效的C代码: 我尽力把它翻译成F#: (* Name : paintRoomCalc in C# Author : Аїӡек Меѥҏ Version : b0.1 License : N/A *) open System; let getLayers (rW:float) (rD:float) (rH:float) (pT:float) : int = let paintThick = pT; let mu
(*
Name : paintRoomCalc in C#
Author : Аїӡек Меѥҏ
Version : b0.1
License : N/A
*)
open System;
let getLayers (rW:float) (rD:float) (rH:float) (pT:float) : int =
let paintThick = pT;
let mutable i = 0;
let mutable t = 0.0;
let mutable roomWidth = (float rW)*12.0;
let mutable roomDepth = (float rD)*12.0;
let mutable roomHeight = (float rH)*12.0;
let mutable edgeArea = 0.0;
let mutable edgeSideDepthArea = 0.0;
let mutable edgeSideDepthVol = 0.0;
let mutable edgeSideWidthArea = 0.0;
let mutable edgeSideWidthVol = 0.0;
let mutable edgeUpArea = 0.0;
let mutable edgeUpVol = 0.0;
let mutable edgeVol = 0.0;
let mutable roomVol = 0.0;
let mutable wallArea = 0.0;
let mutable wallFive = 0.0;
let mutable wallOne = 0.0;
let mutable wallThree = 0.0;
let mutable wallVol = 0.0;
let mutable roomVol = roomWidth * roomDepth * roomHeight;
while (float roomVol) >= 0.0 do
// account for walls
wallOne <- (float roomWidth) * (float roomHeight);
wallThree <- (float roomDepth) * (float roomHeight);
wallFive <- (float roomDepth) * (float roomWidth);
wallVol <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive)) * (float paintThick);
wallArea <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive));
roomWidth <- ((float roomWidth) - ((float paintThick) * 2.0));
roomDepth <- ((float roomDepth) - ((float paintThick) * 2.0));
roomHeight <- ((float roomHeight) - ((float paintThick) * 2.0));
// account for edges going up & down
edgeUpVol <- ((float roomHeight) * Math.Pow((float paintThick), 2.0));
edgeUpVol <- ((float edgeUpVol) * 2.0);
edgeUpArea <- ((float roomHeight) * (float paintThick));
edgeUpVol <- ((float edgeUpArea) * 2.0);
// account for edges going side to side {on ceiling}
//// account for edges going side to side {depth}
edgeSideDepthVol <- ((float roomDepth) * Math.Pow((float paintThick), 2.0));
edgeSideDepthVol <- ((float edgeSideDepthVol) * 2.0);
edgeSideDepthArea <- ((float roomDepth) * (float paintThick));
//// account for edges going side to side {width}
edgeSideWidthVol <- ((float roomWidth) * Math.Pow((float paintThick), 2.0));
edgeSideWidthVol <- ((float edgeSideWidthVol) * 2.0);
edgeSideWidthArea <- ((float roomWidth) * (float paintThick));
// add edges
edgeVol <- ((float edgeUpVol) + (float edgeSideWidthVol) + (float edgeSideDepthVol));
edgeArea <- ((float edgeUpArea) + (float edgeSideWidthArea) + (float edgeSideDepthArea));
// calculate final values
roomVol <- ((float roomVol) - (float wallVol) - (float edgeVol));
wallArea <- ((float wallArea) - (float edgeArea));
i <- i + 1;
t <- (float wallArea) + (float t);
Console.WriteLine((string i) + " layers to fill your room with paint!!");
Console.WriteLine("& " + (string (t / 4800.0)) + " gallons of paint");
0;
let input1:float = float(Console.ReadLine());
let input2:float = float(Console.ReadLine());
let input3:float = float(Console.ReadLine());
let input4:float = float(Console.ReadLine());
getLayers input1 input2 input3 input4;
在C#中,我得到:
我猜这与我的打字和大量的浮雕有关
TL;DR:我应该在F#程序中做些什么修改,使这两个程序在数学上是等价的,从而得到相同的结果?我忘了用输入4除以1000.0得到mil等价物。 它是按原样处理的。 简单的错误 我还删除了丑陋的c-like
返回0在函数getLayers
中选择code>,并将其替换为返回列表。这是从中的最终输出中返回的getLayers
读取的:
答案。[0]
和答案。[1]
完整代码(带奇偶校验):
(*
名称:paintRoomCalc in F#
作者:АїӡМѥҏ
版本:v1.0
许可证:不适用
*)
开放系统;
让getLayers(rW:float)(rD:float)(rH:float)(pT:float):浮点列表=
让paintThick=pT;
设可变i=0;
设可变t=0.0;
设可变房间宽度:float=(float rW)*12.0;
设可变roomDepth:float=(float-rD)*12.0;
让可变房间高度:浮动=(浮动相对湿度)*12.0;
设可变边ea:float=0.0;
设可变edgeSideDepthArea:float=0.0;
设可变edgeSideDepthVol:float=0.0;
设可变边宽度面积:float=0.0;
设可变边宽度vol:float=0.0;
设可变边parea:float=0.0;
设可变边值:float=0.0;
设可变edgeVol:float=0.0;
设可变roomVol:float=0.0;
设可变wallArea:float=0.0;
设可变墙5:float=0.0;
设可变wallOne:float=0.0;
设可变墙三:float=0.0;
设可变wallVol:float=0.0;
让可变roomVol:float=(float roomWidth)*(float roomDepth)*(float roomHeight);
而(float roomVol)>=0.0 do
//说明墙壁
wallOne我忘了用输入4除以1000.0得到mil当量。
它是按原样处理的。
简单的错误
我还删除了丑陋的c-like返回0在函数getLayers
中选择code>,并将其替换为返回列表。这是从中的最终输出中返回的getLayers
读取的:
答案。[0]
和答案。[1]
完整代码(带奇偶校验):
(*
名称:paintRoomCalc in F#
作者:АїӡМѥҏ
版本:v1.0
许可证:不适用
*)
开放系统;
让getLayers(rW:float)(rD:float)(rH:float)(pT:float):浮点列表=
让paintThick=pT;
设可变i=0;
设可变t=0.0;
设可变房间宽度:float=(float rW)*12.0;
设可变roomDepth:float=(float-rD)*12.0;
让可变房间高度:浮动=(浮动相对湿度)*12.0;
设可变边ea:float=0.0;
设可变edgeSideDepthArea:float=0.0;
设可变edgeSideDepthVol:float=0.0;
设可变边宽度面积:float=0.0;
设可变边宽度vol:float=0.0;
设可变边parea:float=0.0;
设可变边值:float=0.0;
设可变edgeVol:float=0.0;
设可变roomVol:float=0.0;
设可变wallArea:float=0.0;
设可变墙5:float=0.0;
设可变wallOne:float=0.0;
设可变墙三:float=0.0;
设可变wallVol:float=0.0;
让可变roomVol:float=(float roomWidth)*(float roomDepth)*(float roomHeight);
而(float roomVol)>=0.0 do
//说明墙壁
wallOne我应该在这两个程序中更改什么使它们等效取决于,哪一个是正确的?@đxěŕc#是正确的我应该在这两个程序中更改什么使它们等效取决于,哪一个是正确的?@đxěc#是正确的关于F#的一个好处是,它为您提供了避免此类错误的工具。看看测量语言的特性。F#的一个好处是它为您提供了避免此类错误的工具。查看测量语言功能。
(*
Name : paintRoomCalc in C#
Author : Аїӡек Меѥҏ
Version : b0.1
License : N/A
*)
open System;
let getLayers (rW:float) (rD:float) (rH:float) (pT:float) : int =
let paintThick = pT;
let mutable i = 0;
let mutable t = 0.0;
let mutable roomWidth = (float rW)*12.0;
let mutable roomDepth = (float rD)*12.0;
let mutable roomHeight = (float rH)*12.0;
let mutable edgeArea = 0.0;
let mutable edgeSideDepthArea = 0.0;
let mutable edgeSideDepthVol = 0.0;
let mutable edgeSideWidthArea = 0.0;
let mutable edgeSideWidthVol = 0.0;
let mutable edgeUpArea = 0.0;
let mutable edgeUpVol = 0.0;
let mutable edgeVol = 0.0;
let mutable roomVol = 0.0;
let mutable wallArea = 0.0;
let mutable wallFive = 0.0;
let mutable wallOne = 0.0;
let mutable wallThree = 0.0;
let mutable wallVol = 0.0;
let mutable roomVol = roomWidth * roomDepth * roomHeight;
while (float roomVol) >= 0.0 do
// account for walls
wallOne <- (float roomWidth) * (float roomHeight);
wallThree <- (float roomDepth) * (float roomHeight);
wallFive <- (float roomDepth) * (float roomWidth);
wallVol <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive)) * (float paintThick);
wallArea <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive));
roomWidth <- ((float roomWidth) - ((float paintThick) * 2.0));
roomDepth <- ((float roomDepth) - ((float paintThick) * 2.0));
roomHeight <- ((float roomHeight) - ((float paintThick) * 2.0));
// account for edges going up & down
edgeUpVol <- ((float roomHeight) * Math.Pow((float paintThick), 2.0));
edgeUpVol <- ((float edgeUpVol) * 2.0);
edgeUpArea <- ((float roomHeight) * (float paintThick));
edgeUpVol <- ((float edgeUpArea) * 2.0);
// account for edges going side to side {on ceiling}
//// account for edges going side to side {depth}
edgeSideDepthVol <- ((float roomDepth) * Math.Pow((float paintThick), 2.0));
edgeSideDepthVol <- ((float edgeSideDepthVol) * 2.0);
edgeSideDepthArea <- ((float roomDepth) * (float paintThick));
//// account for edges going side to side {width}
edgeSideWidthVol <- ((float roomWidth) * Math.Pow((float paintThick), 2.0));
edgeSideWidthVol <- ((float edgeSideWidthVol) * 2.0);
edgeSideWidthArea <- ((float roomWidth) * (float paintThick));
// add edges
edgeVol <- ((float edgeUpVol) + (float edgeSideWidthVol) + (float edgeSideDepthVol));
edgeArea <- ((float edgeUpArea) + (float edgeSideWidthArea) + (float edgeSideDepthArea));
// calculate final values
roomVol <- ((float roomVol) - (float wallVol) - (float edgeVol));
wallArea <- ((float wallArea) - (float edgeArea));
i <- i + 1;
t <- (float wallArea) + (float t);
Console.WriteLine((string i) + " layers to fill your room with paint!!");
Console.WriteLine("& " + (string (t / 4800.0)) + " gallons of paint");
0;
let input1:float = float(Console.ReadLine());
let input2:float = float(Console.ReadLine());
let input3:float = float(Console.ReadLine());
let input4:float = float(Console.ReadLine());
getLayers input1 input2 input3 input4;
5 layers to fill your room with paint!!
& 2.01875 gallons of paint
6668 layers to fill your room with paint!!
& 2078.3967309443156 gallons of paint
(*
Name : paintRoomCalc in F#
Author : Аїӡек Меѥҏ
Version : v1.0
License : N/A
*)
open System;
let getLayers (rW:float) (rD:float) (rH:float) (pT:float) : float list =
let paintThick = pT;
let mutable i = 0;
let mutable t = 0.0;
let mutable roomWidth:float = (float rW)*12.0;
let mutable roomDepth:float = (float rD)*12.0;
let mutable roomHeight:float = (float rH)*12.0;
let mutable edgeArea:float = 0.0;
let mutable edgeSideDepthArea:float = 0.0;
let mutable edgeSideDepthVol:float = 0.0;
let mutable edgeSideWidthArea:float = 0.0;
let mutable edgeSideWidthVol:float = 0.0;
let mutable edgeUpArea:float = 0.0;
let mutable edgeUpVol:float = 0.0;
let mutable edgeVol:float = 0.0;
let mutable roomVol:float = 0.0;
let mutable wallArea:float = 0.0;
let mutable wallFive:float = 0.0;
let mutable wallOne:float = 0.0;
let mutable wallThree:float = 0.0;
let mutable wallVol:float = 0.0;
let mutable roomVol:float = (float roomWidth) * (float roomDepth) * (float roomHeight);
while (float roomVol) >= 0.0 do
// account for walls
wallOne <- (float roomWidth) * (float roomHeight);
wallThree <- (float roomDepth) * (float roomHeight);
wallFive <- (float roomDepth) * (float roomWidth);
wallVol <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive)) * (float paintThick);
wallArea <- (((float wallOne) * 2.0) + ((float wallThree) * 2.0) + (float wallFive));
roomWidth <- ((float roomWidth) - ((float paintThick) * 2.0));
roomDepth <- ((float roomDepth) - ((float paintThick) * 2.0));
roomHeight <- ((float roomHeight) - ((float paintThick) * 2.0));
// account for edges going up & down
edgeUpVol <- ((float roomHeight) * Math.Pow((float paintThick), 2.0));
edgeUpVol <- ((float edgeUpVol) * 2.0);
edgeUpArea <- ((float roomHeight) * (float paintThick));
edgeUpVol <- ((float edgeUpArea) * 2.0);
// account for edges going side to side {on ceiling}
//// account for edges going side to side {depth}
edgeSideDepthVol <- ((float roomDepth) * Math.Pow((float paintThick), 2.0));
edgeSideDepthVol <- ((float edgeSideDepthVol) * 2.0);
edgeSideDepthArea <- ((float roomDepth) * (float paintThick));
//// account for edges going side to side {width}
edgeSideWidthVol <- ((float roomWidth) * Math.Pow((float paintThick), 2.0));
edgeSideWidthVol <- ((float edgeSideWidthVol) * 2.0);
edgeSideWidthArea <- ((float roomWidth) * (float paintThick));
// add edges
edgeVol <- ((float edgeUpVol) + (float edgeSideWidthVol) + (float edgeSideDepthVol));
edgeArea <- ((float edgeUpArea) + (float edgeSideWidthArea) + (float edgeSideDepthArea));
// calculate final values
roomVol <- ((float roomVol) - (float wallVol) - (float edgeVol));
wallArea <- ((float wallArea) - (float edgeArea));
i <- i + 1;
t <- (float wallArea) + (float t);
[float i; t];
Console.WriteLine("Paint Layer Calculator");
Console.WriteLine("by : Аїӡек Меѥҏ");
Console.WriteLine();
//get input for room dimentions
Console.Write("Room Width in Feet? ");
let roomWidthI:float = float(Console.ReadLine());
Console.Write("Room Depth in Feet? ");
let roomDepthI:float = float(Console.ReadLine());
Console.Write("Room Height in Feet? ");
let roomHeightI:float = float(Console.ReadLine());
Console.WriteLine();
Console.WriteLine("1 mil is 1/1000 of an inch");
Console.Write("Paint Thickness in Mils? ");
let mutable paintThickI:float = float(Console.ReadLine());
paintThickI <- float(paintThickI / 1000.0);
Console.WriteLine();
Console.WriteLine("Calculating...");
let answer = getLayers roomWidthI roomDepthI roomHeightI paintThickI;
Console.WriteLine((string answer.[0]) + " layers to fill your room with paint!!");
Console.WriteLine("& " + (string (answer.[1] / 4800.0)) + " gallons of paint");