Почему я получаю эти результаты с UInt64 в C #? - PullRequest
0 голосов
/ 07 июня 2019

Здесь у меня есть 2 массива UInt64:

UInt64[] debit = { 1227472838000, 239613677000, 1280855526000, 235973794000, 916401761000, 778857860000, 613860937000, 323570932000, 1379077243000, 748734870000, 319293438000, 266363662000, 962885150000, 1386950860000, 24497296322000, 1497287074000, 371579620000};

UInt64[] credit = { 1227452838000, 239613641000, 1235410585000, 235973795000, 916342194000, 778887860000, 613859937000, 323660931000, 1379080221000, 748734871000, 319293436000, 200648963000, 962885151000, 1430250860000, 16577296322000,1446583769000, 371579628000};

когда я запускаю этот код:

for (int i = 0; i < debit.Length; i++)
{
     Console.WriteLine($"{i+1}) Debit: {debit[i]} Credit: {credit[i]}");
     Console.WriteLine($"Debit - Credit = {debit[i] - credit[i]}");
     Console.WriteLine($"Credit - Debit = {credit[i] - debit[i]}");
     Console.WriteLine("==============================================");
}

Я получаю это:

1) Debit: 1227472838000 Credit: 1227452838000
   Debit - Credit = 20000000
   Credit - Debit = 18446744073689551616
==============================================
2) Debit: 239613677000 Credit: 239613641000
   Debit - Credit = 36000
   Credit - Debit = 18446744073709515616
==============================================
3) Debit: 1280855526000 Credit: 1235410585000
   Debit - Credit = 45444941000
   Credit - Debit = 18446744028264610616
==============================================
4) Debit: 235973794000 Credit: 235973795000
   Debit - Credit = 18446744073709550616
   Credit - Debit = 1000
==============================================
5) Debit: 916401761000 Credit: 916342194000
   Debit - Credit = 59567000
   Credit - Debit = 18446744073649984616
==============================================
6) Debit: 778857860000 Credit: 778887860000
   Debit - Credit = 18446744073679551616
   Credit - Debit = 30000000
==============================================
7) Debit: 613860937000 Credit: 613859937000
   Debit - Credit = 1000000
   Credit - Debit = 18446744073708551616
==============================================
8) Debit: 323570932000 Credit: 323660931000
   Debit - Credit = 18446744073619552616
   Credit - Debit = 89999000
==============================================
9) Debit: 1379077243000 Credit: 1379080221000
   Debit - Credit = 18446744073706573616
   Credit - Debit = 2978000
==============================================
10) Debit: 748734870000 Credit: 748734871000
   Debit - Credit = 18446744073709550616
   Credit - Debit = 1000
==============================================
11) Debit: 319293438000 Credit: 319293436000
   Debit - Credit = 2000
   Credit - Debit = 18446744073709549616
==============================================
12) Debit: 266363662000 Credit: 200648963000
   Debit - Credit = 65714699000
   Credit - Debit = 18446744007994852616
==============================================
13) Debit: 962885150000 Credit: 962885151000
   Debit - Credit = 18446744073709550616
   Credit - Debit = 1000
==============================================
14) Debit: 1386950860000 Credit: 1430250860000
   Debit - Credit = 18446744030409551616
   Credit - Debit = 43300000000
==============================================
15) Debit: 24497296322000 Credit: 16577296322000
   Debit - Credit = 7920000000000
   Credit - Debit = 18446736153709551616
==============================================
16) Debit: 1497287074000 Credit: 1446583769000
   Debit - Credit = 50703305000
   Credit - Debit = 18446744023006246616
==============================================
17) Debit: 371579620000 Credit: 371579628000
   Debit - Credit = 18446744073709543616
   Credit - Debit = 8000
==============================================

как исправить эти арифметические ошибки?

1 Ответ

0 голосов
/ 07 июня 2019

Измените часть декларации на

    Decimal[] debit = { 1227472838000, 239613677000, 1280855526000, 235973794000, 916401761000, 778857860000, 613860937000, 323570932000, 1379077243000, 748734870000, 319293438000, 266363662000, 962885150000, 1386950860000, 24497296322000, 1497287074000, 371579620000 };

    Decimal[] credit = { 1227452838000, 239613641000, 1235410585000, 235973795000, 916342194000, 778887860000, 613859937000, 323660931000, 1379080221000, 748734871000, 319293436000, 200648963000, 962885151000, 1430250860000, 16577296322000, 1446583769000, 371579628000 };
...