[New-bugs-announce] [issue33781] audioop.c: fbound() casts double to int for its return value

STINNER Victor report at bugs.python.org
Wed Jun 6 06:23:24 EDT 2018


New submission from STINNER Victor <vstinner at redhat.com>:

Extract of Python 2.7, Modules/audioop.c:

static int
fbound(double val, double minval, double maxval)
{
    if (val > maxval)
        val = maxval;
    else if (val < minval + 1)
        val = minval;
    return val;
}

Example of usage:

    double factor, fval, maxval, minval;
    int len, size, val = 0;

        fval = (double)val*factor;
        val = (int)floor(fbound(fval, minval, maxval));

It seems wrong to me to call floor() with an integer. Why fbound() doesn't return a double?

fbound() has been modified to explicitly cast its result to an int in the master branch:

static int
fbound(double val, double minval, double maxval)
{
    if (val > maxval)
        val = maxval;
    else if (val < minval + 1)
        val = minval;
    return (int)val;
}

But master still uses something like:

        val = floor(fbound(val, minval, maxval));


It seems like fbound() result is always passed into floor(). Maybe floor() should be moved into fbound()?

Attached PR changes fbound() to round correctly: call floor() into fbound().

--

I looked at the code because of the following compiler warning on Python 2.7:

[00:02:21] ..\Modules\audioop.c(38): warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data [C:\projects\cpython\PCbuild\pythoncore.vcxproj]

----------
components: Extension Modules
messages: 318808
nosy: vstinner
priority: normal
severity: normal
status: open
title: audioop.c: fbound() casts double to int for its return value
versions: Python 2.7, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33781>
_______________________________________


More information about the New-bugs-announce mailing list