Post
Topic
Board Bitcoin Technical Support
Re: Why is this code yielding something like this?
by
NotATether
on 19/06/2022, 04:12:32 UTC
Why is this happening?

Warning
Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

You can always use floating-point precision if possible by rounding to 6 digits (most people will never need more than 6 digits of precision) using a neat scaling-up trick I devised:

Code:
$num = $1.00000001
$scaling_factor = $1000000
$num = round($num * $scaling_factor) / $scaling_factor;
echo $num; # 1.000000