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

Implicit conversion from float #154

Open
rlzdesenv opened this issue May 31, 2022 · 4 comments
Open

Implicit conversion from float #154

rlzdesenv opened this issue May 31, 2022 · 4 comments

Comments

@rlzdesenv
Copy link

I've been getting a lot of alerts like

Implicit conversion from float 23.5 to int loses precision in /var/www/vendor/milon/barcode/src/Milon/Barcode/QRcode.php on line 878

@lehnen
Copy link

lehnen commented Mar 1, 2023

@rlzdesenv Since this repo is not realy up to date, a quick workarroud is this:

Line 870:
$col = (int)($this->count / $this->blocks);

Line 877:
$col = (int)(($this->count - $this->dataLength) / $this->blocks);

@emmadonjo
Copy link

I'm having the log file loaded up with this error on lines 874 and 878.

@lehnen, directly updating this might be rendered useless when an update or a new installation command is run. Can a pull request be made on this?

@Kaczorex
Copy link

I got the same issue. When it will be fixed? 2 years delay is nice. When can we expect a fix?

@vdussan
Copy link

vdussan commented Jun 1, 2024

The error "Deprecated: Implicit conversion from float 0.5 to int loses precision" suggests that there is an implicit conversion from a floating-point number (float) to an integer (int) in your code, which is losing precision. This usually happens when a non-integer division is being used, and the result is used as an index in an array or similar.

In PHP, the division of two integers can produce a floating-point result. To resolve this issue, you should ensure that any division used as an index is explicitly converted to an integer. Here’s how you can adjust your getCode function to solve this problem:

I change de the function:

public function getCode() {
    if ($this->count < $this->dataLength) {
        $row = $this->count % $this->blocks;
        $col = $this->count / $this->blocks;
        if ($col >= $this->rsblocks[0]['dataLength']) {
            $row += $this->b1;
        }
        $ret = $this->rsblocks[$row]['data'][$col];
    } elseif ($this->count < $this->dataLength + $this->eccLength) {
        $row = ($this->count - $this->dataLength) % $this->blocks;
        $col = ($this->count - $this->dataLength) / $this->blocks;
        $ret = $this->rsblocks[$row]['ecc'][$col];
    } else {
        return 0;
    }
    $this->count++;
    return $ret;
}

To:

public function getCode() {
    if ($this->count < $this->dataLength) {
        $row = $this->count % $this->blocks;
        $col = intdiv($this->count, $this->blocks); // Asegurando división entera
        if ($col >= $this->rsblocks[0]['dataLength']) {
            $row += $this->b1;
        }
        $ret = $this->rsblocks[$row]['data'][$col];
    } elseif ($this->count < $this->dataLength + $this->eccLength) {
        $row = ($this->count - $this->dataLength) % $this->blocks;
        $col = intdiv(($this->count - $this->dataLength), $this->blocks); // Asegurando división entera
        $ret = $this->rsblocks[$row]['ecc'][$col];
    } else {
        return 0;
    }
    $this->count++;
    return $ret;
}

The intdiv() function in PHP performs an integer division, ensuring that the result is an integer. This should solve the problem of the implicit conversion from float to int that was causing the deprecation message.

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

No branches or pull requests

5 participants