Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implicit conversion from float (number) to int loses precision

i used to use this formula before php 8.1

<?php
$number = 0;
echo log10(abs($number)) / 3 | 0;

echo PHP_EOL;

$number = 100;
echo log10(abs($number)) / 3 | 0;

echo PHP_EOL;
    
$number = 1100;
echo log10(abs($number)) / 3 | 0;

echo PHP_EOL;
    
$number = 10000000;
echo log10(abs($number)) / 3 | 0;
?>

and it worked fine but now i keep getting these errors from them after upgrading

Deprecated: Implicit conversion from float -INF to int loses precision

Deprecated: Implicit conversion from float 0.6666666666666666 to int loses precision

Deprecated: Implicit conversion from float 1.0137975617194084 to int loses precision

Deprecated: Implicit conversion from float 2.3333333333333335 to int loses precision

and i can not find or understand why it is happening now from the 8.1 docs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re getting an implicit conversion to integer when you perform the bitwise OR operation via the | operator. It’s an… odd… way to convert to integer. To avoid the warning, just explicitly convert instead.

Implicit:

echo log10(abs($number)) / 3 | 0;

Explicit via function:

echo intval(log10(abs($number)) / 3);

Or via cast:

echo (int) (log10(abs($number)) / 3);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading