1
0
mirror of synced 2024-11-27 17:00:50 +01:00

Fix broken complex number conversion cases

This commit is contained in:
0auBSQ 2024-08-17 10:54:58 +09:00
parent 8c65243b1a
commit c05bbf2627

View File

@ -174,11 +174,14 @@ namespace System {
// Split the input into real and imaginary parts
string[] parts = input.Split(new[] { '+', '-' }, StringSplitOptions.RemoveEmptyEntries);
// If starts by - (fix for -1+3i, etc)
double factor = input.StartsWith("-") ? -1 : 1;
if (input.Contains("+")) {
real = double.Parse(parts[0], CultureInfo.InvariantCulture);
imaginary = double.Parse(parts[1], CultureInfo.InvariantCulture);
} else if (input.LastIndexOf('-') > 0) // handling cases like "1-2i"
{
{
real = double.Parse(parts[0], CultureInfo.InvariantCulture);
imaginary = -double.Parse(parts[1], CultureInfo.InvariantCulture);
} else if (input.StartsWith("-")) {
@ -186,6 +189,8 @@ namespace System {
} else {
imaginary = double.Parse(parts[0], CultureInfo.InvariantCulture);
}
real *= factor;
}
}
} else {