F#移植C#墙漆层数计算器结果不一致问题排查
C#与F#墙漆层数计算器输出差异排查
接触C#与.NET技术栈后逐渐产生了开发兴趣,手头有一份可正常运行的C#版本墙漆层数计算代码,功能为根据房间尺寸、漆层厚度计算所需涂刷层数与总漆量,代码如下:
/* Name : paintRoomCalc in C# Author : Аїӡек Меѥҏ Version : v1.0 License : N/A */ using System; class MainClass { static public void getLayers(float roomWidth, float roomDepth, float roomHeight, float paintThick) { float edgeArea, edgeSideDepthArea, edgeSideDepthVol, edgeSideWidthArea, edgeSideWidthVol, edgeUpArea, edgeUpVol, edgeVol, roomVol, wallArea, wallFive, wallOne, wallThree, wallVol; double i = 0; double t = 0; roomWidth = roomWidth * 12; roomDepth = roomDepth * 12; roomHeight = roomHeight * 12; roomVol = roomWidth * roomDepth * roomHeight; while (roomVol >= 0) { // account for walls wallOne = (float)(roomWidth * roomHeight); wallThree = (float)(roomDepth * roomHeight); wallFive = (float)(roomDepth * roomWidth); wallVol = (float)(((wallOne * 2) + (wallThree * 2) + wallFive) * paintThick); wallArea = (float)((wallOne * 2) + (wallThree * 2) + wallFive); roomWidth = (float)(roomWidth - (paintThick * 2)); roomDepth = (float)(roomDepth - (paintThick * 2)); roomHeight = (float)(roomHeight - (paintThick * 2)); // account for edges going up & down edgeUpVol = (float)(roomHeight * Math.Pow(paintThick, 2)); edgeUpVol = (float)(edgeUpVol * 2); edgeUpArea = (float)(roomHeight * paintThick); edgeUpVol = (float)(edgeUpArea * 2); // account for edges going side to side {on ceiling} //// account for edges going side to side {depth} edgeSideDepthVol = (float)(roomDepth * Math.Pow(paintThick, 2)); edgeSideDepthVol = (float)(edgeSideDepthVol * 2); edgeSideDepthArea = (float)(roomDepth * paintThick); //// account for edges going side to side {width} edgeSideWidthVol = (float)(roomWidth * Math.Pow(paintThick, 2)); edgeSideWidthVol = (float)(edgeSideWidthVol * 2); edgeSideWidthArea = (float)(roomWidth * paintThick); // add edges edgeVol = (float)(edgeUpVol + edgeSideWidthVol + edgeSideDepthVol); edgeArea = (float)(edgeUpArea + edgeSideWidthArea + edgeSideDepthArea); // calculate final values roomVol = (float)(roomVol - wallVol - edgeVol); wallArea = (float)(wallArea - edgeArea); i += 1; t += wallArea; } Console.WriteLine(i + " layers to fill your room with paint!!"); Console.WriteLine("& " + t / 4800 + " gallons of paint"); } static public void Main(string[] args) { float roomWidth, roomDepth, roomHeight, paintThickMil; float paintThick; Console.WriteLine("Paint Layer Calculator"); Console.WriteLine("by : Аїӡек Меѥҏ"); Console.WriteLine(); //get input for room dimentions Console.Write("Room Width in Feet? "); roomWidth = float.Parse(Console.ReadLine()); Console.Write("Room Depth in Feet? "); roomDepth = float.Parse(Console.ReadLine()); Console.Write("Room Height in Feet? "); roomHeight = float.Parse(Console.ReadLine()); Console.WriteLine(); Console.WriteLine("1 mil is 1/1000 of an inch"); Console.Write("Paint Thickness in Mils? "); paintThickMil = float.Parse(Console.ReadLine()); paintThick = paintThickMil / 1000; Console.WriteLine(); Console.WriteLine("Calculating..."); getLayers(roomWidth, roomDepth, roomHeight, paintThick); Console.ReadKey(); } }
基于对上述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 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;
F#版本相关说明:
- 代码中的输入变量后续会进行重命名,待整体数学逻辑校准完成后也会做对应的一致性调整
- 由于刚接触F#和C#不久,当前代码实现仍有较大优化空间
按顺序输入参数1、2、3、1进行测试时,两个版本的输出结果差异极大:
- F#版本输出:
5 layers to fill your room with paint!! & 2.01875 gallons of paint - C#版本输出:
6668 layers to fill your room with paint!! & 2078.3967309443156 gallons of paint
问题分析与核心诉求:
- 初步推测:输出差异可能与代码中大量冗余的float类型强转操作有关
- 核心诉求:调整F#程序的实现,使两个程序的数学逻辑完全等价,最终输出一致的计算结果
内容的提问来源于stack exchange,提问作者izder456 dev
相关产品推荐
相关产品推荐

