Reputation: 6653
This question is asked multiple time's, but i can't get it working...
I'm working on an program that calculates the distance between two points, and the calculates the time an unit walks about it...
This is the simplefied version of what i need to do:
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
So i've got this piece of code in C#, but it isn't working:
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);
The expected output should be: 00d:16h:37m:12s:772ms
But is: 00d:16h:37m:14s:772ms
The difrnce is 2 seconds. What is the bottleneck? Why is there 2 seconds difrnce between the expected output and the real output?
Upvotes: 0
Views: 792
Reputation: 63358
Root of (24² + 23²) = 33.24 fields
The square root of 1105
isn't exactly 33.24
; you only get 33.24
if you round the square root. So compare your problem description:
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
against what happens with a more precise value for the square root:
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
which is about 2 seconds different from what you are getting
If you want to reproduce your original calculation, you need to do something after
double aantalVelden = Math.Sqrt((Math.Pow(verschilX, 2) + Math.Pow(verschilY,2))); // Fields
to round that in the same way as your original; for example,
aantalVelden = Math.Round(aantalVelden, 2);
Upvotes: 2
Reputation: 6805
It seems it works like it should. There are just small differences in decimal values resulting in few seconds diff in the end from your expected result. Here is code that i have used:
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);
Upvotes: 1
Reputation: 17868
I took your code, which didnt compile and made
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);
And it came back with the right answer.
Upvotes: 0