【发布时间】:2017-11-28 03:22:44
【问题描述】:
我想问一下.Net 中是否有一个内置函数可以从方程中获取最小和最大输出。例如:
float a,b,c,d,e,f,g;
float result() {
return a*b/c + d/(e-d) + f/g;
} //how to know the possible min and max value of result()?
我知道域(a、b、c、d、e、f、g 的最小值和最大值;其中 c、(e-d)、g 不为 0),如何从 result() 获取输出范围? 到目前为止,我尝试手动运行原始嵌套循环,例如: figure: Output range of a function (
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEST {
class Program {
static void Main(string[] args) {
ThreatRange();
Console.ReadLine();
}
static void ThreatRange() {
int a0 = 1, a1 = 10; // I know the min and max value for each variable
int b0 = 1, b1 = 10;
int c0 = 1, c1 = 10;
int d0 = 1, d1 = 10;
int e0 = 1, e1 = 10;
int f0 = 1, f1 = 10;
int g0 = 1, g1 = 10;
float threat = 0, threatmin = 9999, threatmax = 0;
for (int a = a0; a <= a1; a++) {
for (int b = b0; b <= b1; b++) {
for (int c = c0; c <= c1; c++) {
for (int d = d0; d <= d1; d++) {
for (int e = e0; e <= e1; e++) {
for (int f = f0; f <= f1; f++) {
for (int g = g0; g <= g1; g++) {
threat = a * b / (float)c + d / (float)Math.Max(1, Math.Abs(e - d)) + (d / (float)Math.Max(1, Math.Abs(e - d))) * (f / g);
// I want to know the min and max output from the equation above
if (threat < threatmin) threatmin = threat;
if (threat > threatmax) threatmax = threat;
Console.WriteLine(a + "," + b + "," + c + "," + d + "," + e + "," + f + "," + g);
Console.WriteLine("Min: " + threatmin + ". Max: " + threatmax);
}
}
}
}
}
}
}
}
}
}
如果我问错了关键字,请原谅我,我不是以英语为母语的人,我的数学也不好。 :( 如果您知道任何可以为我提供除 c# 之外的解决方案的工具,请告诉我。
【问题讨论】:
-
我不明白你的问题和你的代码。但我相信你可以使用
List<int>或array -
@MohitShrivastava 假设我想知道具有许多参数的方程的最小和最大输出值。有没有比使用许多嵌套循环更好的方法?
-
是的 .. 如果将输入放入列表或数组中,您可以使用 LINQ ... 而不是嵌套循环 ..。相信我,它会是一两个班轮。
-
如果没有exception,比如除以0,那么可能的最小值是
Single.MinValue,可能的最大值是Single.MaxValue。 -
谢谢,伙计们。我现在要浏览一下。