user10846907
user10846907

Reputation:

why does bccomp require a scale to work properly?

php > var_dump(bccomp('-10.00001', '-10.0'));
int(0)

php > var_dump(bccomp('-10.00001', '-10.0', 17));
int(-1);

I don't get this at all. Isn't the entire point of the bcmath functions to allow you to do comparisons/arithmatic on floating point values as strings to avoid floating point issues?

Why even have a scale, shouldn't this just work properly every single time? What possible reason could someone want for having two unequal values be returned as equal?

Upvotes: 2

Views: 413

Answers (2)

olidem
olidem

Reputation: 2121

By setting bcscale you can set the scale globally.

php > var_dump(bccomp('-10.00001', '-10.0'));
int(0)

php > bcscale(5);
php > var_dump(bccomp('-10.00001', '-10.0'));
int(-1)

Upvotes: 0

Jay Blanchard
Jay Blanchard

Reputation: 34426

It does not require a scale. From the docs:

"The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison."

There are some cases in which you would want a number with a certain number of decimal places to be equal to another number with a different number of decimal places, for a rather simplistic example:

10.44 = 10.4390

Upvotes: 1

Related Questions