Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Как известно программисты бывают 10 типов: те, кто знает двоичное исчисление и те, кто не знает ©
typedef enum {PLUS, MINUS} sign;
typedef struct bc_struct *bc_num;
typedef struct bc_struct
{
sign n_sign;
int n_len; /* The number of digits before the decimal point. */
int n_scale; /* The number of digits after the decimal point. */
int n_refs; /* The number of pointers to this number. */
bc_num n_next; /* Linked list for available list. */
char *n_ptr; /* The pointer to the actual storage.
If NULL, n_value points to the inside of
another number (bc_multiply...) and should
not be "freed." */
char *n_value; /* The number. Not zero char terminated.
May not point to the same place as n_ptr as
in the case of leading zeros generated. */
} bc_struct;0.1 + 0.1 + 0.1 - 0.3 = 5.6e-17 != 00.1 + 0.1 - 0.1 = 0.10000000000000001e = 0.000001 a = 0.1 + 0.1 + 0.1 - 0.3 if( abs( a ) < e ) ... a = 0.1 + 0.1 - 0.1 b = 0.1 if( abs(a - b) < e ) ...
C разностью тоже надо быть осторожным, я выше приводил пример:
0.1 + 0.1 + 0.1 — 0.3 = 5.6e-17 != 0
function round2($a) {
return round($a + 1e-8);
}
$value = 97.5;
var_dump($value, round2($value)); // float(97.5) float(98)
$value = (9.975-9)*100;
var_dump($value, round2($value)); // float(97.5) float(98)
round(0.49999999999) = 0
round2(0.49999999999) = 1смею предположить, что в PHP реализовано статистическое округление
Бубен в PHP