vak: (Знайка)
Serge Vakulenko ([personal profile] vak) wrote2023-01-14 02:45 pm

За что математики не любят компьютерную арифметику

#include <stdio.h>
int main ()
{
    if (-5.2 + 4.9 != -0.3) {
        printf("Oops!\n");
    }
    if (4.8 - 6.1 != -1.3) {
        printf("Oops!\n");
    }
    if (4.3 - 3.6 != 0.7) {
        printf("Oops!\n");
    }
}
Это мне надо было простенький тестик сварганить по работе, суммировать плавающие числа, а оно вон как боком выскочило.
dennisgorelik: 2020-06-13 in my home office (Default)

Re: C#

[personal profile] dennisgorelik 2023-01-15 04:01 pm (UTC)(link)
Is there a convenient way to use more precise representation?

ircicq: (Default)

Re: C#

[personal profile] ircicq 2023-01-15 07:15 pm (UTC)(link)
Type `decimal` in many programming languages including C#
Although it is slower

Edited 2023-01-15 19:15 (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)

Re: C#

[personal profile] dennisgorelik 2023-01-15 08:40 pm (UTC)(link)
Oh.
[personal profile] vak meant "data representation in computer memory" (floating-point double vs decimal).
I thought he meant representation/conversion of data to decimal string.

In any case, the primary way to deal with imprecision is not increasing precision, but replacing number equality comparison with [epsilon] range comparison.
Edited 2023-01-15 20:41 (UTC)
dennisgorelik: 2020-06-13 in my home office (Default)

[personal profile] dennisgorelik 2023-01-16 05:50 am (UTC)(link)
> Hex literals work fine.

Hex interface is a good workaround.
But it may be hard to convince end users to use hex interface, right?