Pular para o conteúdo principal

Postagens

Mostrando postagens de abril, 2024

General: Prevent arbitrary precision arithmetic

Demand Some simple decimal calculations like 4.6 * 100 may lead in irregularities when dealing with numbers. Description During arithmetic operations like addition, subtraction, multiplication, and division, the numbers are manipulated according to the rules defined by the IEEE 754 standard. However, due to the finite precision of the representation, rounding errors may occur, leading to small discrepancies between the expected and actual results of computations.  To prevent it we may use some libraries to prevent the problem. In Javascript and Typescript we have the BigNumbers library (and others). PHP has a built-in library for arbitrary-precision arithmetic called BCMath (Binary Calculator). Wich I reccomend. It provides functions to perform mathematical operations on numbers with arbitrary precision, including addition, subtraction, multiplication, division, and more. Examples PHP Sample 1: php > var_dump(floor((10*0.91597) * 1000000)/1000000); 2: float(9.159699) 3: php ...