0

这个问题被问了多次,但我无法让它工作......

我正在开发一个程序来计算两点之间的距离,并计算一个单位绕它走的时间......

这是我需要做的简化版本:

x = 24 
y = 23

Root of (24² + 23²) = 33.24 fields

33.24 x 30 = 997,2 minuts

997,2/60 = 16,62 hours
16: (60*0.62) = 16:37,2   
16:37: (60*0,2) = 16:37:12

所以我在 C# 中有这段代码,但它不起作用:

first_town = "35|629";
second_town = "59|606";
snelheidUnit = 30;

string[] firstTownSplit = first_town.Split('|');
string[] secondTownSplit = second_town.Split('|');
int firstTownPart1 = Convert.ToInt32(firstTownSplit[0]);
int firstTownPart2 = Convert.ToInt32(firstTownSplit[1]);
int secondTownPart1 = Convert.ToInt32(secondTownSplit[0]);
int secondTownPart2 = Convert.ToInt32(secondTownSplit[1]);

int verschilX = firstTownPart1 - secondTownPart1;
int verschilY = firstTownPart2 - secondTownPart2;

double aantalVelden = Math.Sqrt((Math.Pow(verschilX, 2) + Math.Pow(verschilY,2)));       // Fields
double aantalMinuten = aantalVelden * snelheidUnit;             // Minuts
double aantalUren = aantalMinuten / 60;                 // Hours

TimeSpan time = TimeSpan.FromHours(aantalUren);
string FinalTime = string.Format("{0:D2}d:{1:D2}h:{2:D2}m:{3:D2}s:{4:D3}ms", time.Days, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);      

预期的输出应该是:00d:16h:37m:12s:772ms

但是是:00d:16h:37m:14s:772ms

差异为 2 秒。瓶颈是什么?为什么预期输出和实际输出之间有 2 秒的差异?

4

3 回答 3

2

(24² + 23²) 的根 = 33.24 个字段

的平方根1105完全是 33.24; 只有33.24将平方根四舍五入才能得到。所以比较你的问题描述:

33.24 x 30 = 997,2 minuts

997,2/60 = 16,62 hours
16: (60*0.62) = 16:37,2   
16:37: (60*0,2) = 16:37:12

与更精确的平方根值相反:

33.241540277189322804630581542105 * 30 = 997.24620831567968413891744626316 minutes
less 16 hours (960 minutes) leaves 37.246208315679684138917446263157 minutes
less 37 minutes leaves 0.246208315679684138917446263157 minutes
which gives 14.772498940781048335046775789394 seconds

这与你得到的大约 2 秒不同

如果你想重现你原来的计算,你需要在之后做一些事情

double aantalVelden = Math.Sqrt((Math.Pow(verschilX, 2) + Math.Pow(verschilY,2)));       // Fields

以与原件相同的方式对其进行四舍五入;例如,

aantalVelden  = Math.Round(aantalVelden, 2);
于 2012-10-03T07:58:21.533 回答
1

似乎它应该像它一样工作。十进制值的差异很小,最终导致与您的预期结果有几秒钟的差异。这是我使用过的代码:

        double verschilX = 24;
        double verschilY = 23;
        double snelheidUnit = 30;

        double aantalVelden = Math.Sqrt((Math.Pow(verschilX, 2) + Math.Pow(verschilY, 2)));       // Fields 
        double aantalMinuten = aantalVelden * snelheidUnit;             // Minuts 
        double aantalUren = aantalMinuten / 60;                 // Hours 

        TimeSpan time = TimeSpan.FromHours(aantalUren);
        string FinalTime = string.Format("{0:D2}d:{1:D2}h:{2:D2}m:{3:D2}s:{4:D3}ms", time.Days, time.Hours, time.Minutes, time.Seconds, time.Milliseconds); 
于 2012-10-03T07:37:25.847 回答
0

我拿了你的代码,它没有编译和制作

        double verschilY = 24;
        double verschilX = 23;
        double aantalVelden = Math.Sqrt((Math.Pow(verschilX, 2) + Math.Pow(verschilY, 2)));       // Fields 
        double aantalMinuten = aantalVelden * 30;             // Minuts 
        double aantalUren = aantalMinuten / 60;                 // Hours 
        TimeSpan time = TimeSpan.FromHours(aantalUren); 
        string FinalTime = string.Format("{0:D2}d:{1:D2}h:{2:D2}m:{3:D2}s:{4:D3}ms", time.Days, time.Hours, time.Minutes, time.Seconds, time.Milliseconds);
        Console.WriteLine(FinalTime);

它带着正确的答案回来了。

于 2012-10-03T07:35:41.110 回答