Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorException: bcmul(): bcmath function argument is not well-formed #159

Closed
ryanrapini opened this issue Jan 15, 2021 · 6 comments · Fixed by #163
Closed

ErrorException: bcmul(): bcmath function argument is not well-formed #159

ryanrapini opened this issue Jan 15, 2021 · 6 comments · Fixed by #163
Assignees

Comments

@ryanrapini
Copy link

In PHP 7.4 I believe this becomes a warning.

https://www.php.net/manual/en/migration74.incompatible.php

BCMath Arbitrary Precision Mathematics ¶

BCMath functions will now warn if a non well-formed number is passed, such as "32foo". The argument will be interpreted as zero, as before.

Code to reproduce:

$converter = UnitConverter::binary();
$converted_size = $converter->convert('1000')->from('mg')->to('g');

Result:

ErrorException: bcmul(): bcmath function argument is not well-formed

/vendor/jordanbrauer/unit-converter/src/Calculator/BinaryCalculator.php:59
/vendor/jordanbrauer/unit-converter/src/Calculator/Formula/UnitConversionFormula.php:35
/vendor/jordanbrauer/unit-converter/src/Calculator/AbstractCalculator.php:162
/vendor/jordanbrauer/unit-converter/src/UnitConverter.php:359
/vendor/jordanbrauer/unit-converter/src/UnitConverter.php:314
/src/Units.php:15
/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
/tests/Unit/Units.php:236

Additionally, I tried using the standard UnitConverter::default() but I get weird things like:

$converted_size = $converter->convert('1')->from('mg')->to('g');
echo($converted_size);
0.0

Any advice?

@jordanbrauer
Copy link
Owner

jordanbrauer commented Jan 15, 2021

In PHP 7.4 I believe this becomes a warning.

https://www.php.net/manual/en/migration74.incompatible.php
...

@ryanrapini interesting, thanks for the heads up on this; I will take a peak at this later today and see what I can do!

Additionally, I tried using the standard UnitConverter::default() but I get weird things like:

This will depend on what you're converting. Since you are converting a smaller unit to a larger unit, you will wind up with a decimal, so you will to need to pass the convert method a precision value for rounding. In your case, if you were to pass it a 3 you would get a 0.001 as your result (1 gram is 1,000 milligrams), like so

$converted_size = $converter->convert('1', 3)->from('mg')->to('g');
echo($converted_size);
0.001

Hopefully that helps! 🙂

P.S. FWIW, the default precision is 2 (see the AbstractCalculator::DEFAULT_PRECISION constant)

image

@jordanbrauer jordanbrauer self-assigned this Jan 15, 2021
@ryanrapini
Copy link
Author

Thanks, this is great help. I dug in a little deeper to the issue before I had to stop working on it - the issue is coming because mul() is being passed these arguments:

  0 => "1"
  1 => "1.0E-6"

the 1.0E-6 is the issue, of course.

I believe you can force php to output "0.000006" (which is what that function wants) with, for example,

echo sprintf('%.8f', floatval('-1.0E-5'));//force 8 decimal places

(from here)

but I'm not sure this is the right way to go about this,

Hope this helps.

@ryanrapini
Copy link
Author

P.S: your documentation appears to have weird compile errors, FYI

https://jordanbrauer.github.io/unit-converter/classes/UnitConverter.UnitConverterInterface.html

@jordanbrauer
Copy link
Owner

jordanbrauer commented Jan 15, 2021

Thanks, this is great help.

Yeah, not a problem! Thanks for the heads up RE: documentation being out of whack.

mul() is being passed these arguments:

0 => "1"
1 => "1.0E-6"

What conversion are you performing when this happens? E.g., value and to/from units

UnitConverter::default()
    ->convert($n)
    ->from($x)
    ->to($y);

You can also get a small debug log that might show more of what's going on with the numbers/formula

$converter->getConversionLog();

jordanbrauer added a commit that referenced this issue Feb 12, 2021
* denormalize scientific notation numbers

* testing on PHP 7.4

* better scientific notation expanding

* ensure that results are always valid numbers

* ensure we trim only when a decimal is present
@jordanbrauer
Copy link
Owner

@ryanrapini The issue should be rectified in #163 . If you would like to take advantage of it right away, you can target dev-master in your composer.json, otherwise it will be in the next release!

Here is some samples to show new behaviour working as expected.

$bc = new BinaryCalculator(13); // high precision binary calculator

dump(
    $bc->add('2.3562e-007', '1.8281e-009'), # (string) 0.0000002374481
    $bc->mul('1', '1.0E-6'),                # (string) 0.000001
);

@ryanrapini
Copy link
Author

Thanks for your hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants