Author vstinner
Recipients vstinner
Date 2018-06-06.10:23:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1528280604.0.0.592728768989.issue33781@psf.upfronthosting.co.za>
In-reply-to
Content
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]
History
Date User Action Args
2018-06-06 10:23:24vstinnersetrecipients: + vstinner
2018-06-06 10:23:23vstinnersetmessageid: <1528280604.0.0.592728768989.issue33781@psf.upfronthosting.co.za>
2018-06-06 10:23:23vstinnerlinkissue33781 messages
2018-06-06 10:23:23vstinnercreate