This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: audioop.c: fbound() casts double to int for its return value
Type: Stage: resolved
Components: Extension Modules Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2018-06-06 10:23 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7447 merged vstinner, 2018-06-06 10:48
PR 7450 merged miss-islington, 2018-06-06 14:17
PR 7451 merged miss-islington, 2018-06-06 14:18
PR 7452 merged vstinner, 2018-06-06 14:40
PR 7453 merged vstinner, 2018-06-06 15:17
Messages (12)
msg318808 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 10:23
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]
msg318811 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 11:02
Oh, fbound() cast to (int) has been done by... myself :-D

commit f2b9a340ef143099726fb642951410e4ad793c7f
Author: Victor Stinner <victor.stinner@gmail.com>
Date:   Tue May 7 23:49:15 2013 +0200

    audioop: explicit cast to fix a compiler warning

diff --git a/Modules/audioop.c b/Modules/audioop.c
index 7e40bbddc6..4f2b25f33a 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -37,7 +37,7 @@ fbound(double val, double minval, double maxval)
         val = maxval;
     else if (val < minval + 1)
         val = minval;
-    return val;
+    return (int)val;
 }


Stupid me!
msg318824 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 13:50
New changeset 45e4efba7fa2abe61d25e4f8b5bf482e19ff1280 by Victor Stinner in branch 'master':
bpo-33781: audioop: enhance rounding double as int (GH-7447)
https://github.com/python/cpython/commit/45e4efba7fa2abe61d25e4f8b5bf482e19ff1280
msg318826 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-06 14:18
It was my mistake. fbound() should be return double.

But since it is always used with floor(), it is better to move floor() inside it.
msg318828 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 14:23
> But since it is always used with floor(), it is better to move floor() inside it.

Yeah, that's what I did ;-) I will backport the change to 3.7, 3.6 and 2.7. My final goal is just to fix a compiler warning in 2.7 :-D
msg318829 - (view) Author: miss-islington (miss-islington) Date: 2018-06-06 14:33
New changeset 5c022f13ab6db8929e092ad035b3dc61701e3198 by Miss Islington (bot) in branch '3.7':
bpo-33781: audioop: enhance rounding double as int (GH-7447)
https://github.com/python/cpython/commit/5c022f13ab6db8929e092ad035b3dc61701e3198
msg318830 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 14:41
Note: I chose to not add a NEWS entry, because even if my change might enhance the rounding, I don't think that it has any effect in practice :-)
msg318831 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 15:12
New changeset b17d409bc0458e3981987c2358da661f228f5891 by Victor Stinner in branch '2.7':
bpo-33781: audioop: enhance rounding double as int (GH-7447) (GH-7452)
https://github.com/python/cpython/commit/b17d409bc0458e3981987c2358da661f228f5891
msg318832 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 15:14
> New changeset b17d409bc0458e3981987c2358da661f228f5891 by Victor Stinner in branch '2.7':
> bpo-33781: audioop: enhance rounding double as int (GH-7447) (GH-7452)

I checked AppVeyor logs: this change fixed the audioop.c warning that I wanted to fix ;-)
msg318833 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 15:18
New changeset 2bc1946fb02e140f5f5ac21a1afa2763615ad16b by Victor Stinner (Miss Islington (bot)) in branch '3.6':
bpo-33781: audioop: enhance rounding double as int (GH-7447) (GH-7451)
https://github.com/python/cpython/commit/2bc1946fb02e140f5f5ac21a1afa2763615ad16b
msg318837 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 15:51
New changeset e5b79c546370521764457ea2ec809303e580f5ea by Victor Stinner in branch '2.7':
bpo-19418: audioop.c: Fix warnings on -0x80000000 (GH-7453)
https://github.com/python/cpython/commit/e5b79c546370521764457ea2ec809303e580f5ea
msg318848 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-06-06 17:04
Ok, fbound() is now better in all branches. I also fixed all compiler warnings in 2.7. I close the issue.
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77962
2018-06-06 17:04:01vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg318848

stage: patch review -> resolved
2018-06-06 15:51:10vstinnersetmessages: + msg318837
2018-06-06 15:18:30vstinnersetmessages: + msg318833
2018-06-06 15:17:49vstinnersetpull_requests: + pull_request7076
2018-06-06 15:14:11vstinnersetmessages: + msg318832
2018-06-06 15:12:43vstinnersetmessages: + msg318831
2018-06-06 14:41:36vstinnersetmessages: + msg318830
2018-06-06 14:40:41vstinnersetpull_requests: + pull_request7074
2018-06-06 14:33:08miss-islingtonsetnosy: + miss-islington
messages: + msg318829
2018-06-06 14:23:20vstinnersetmessages: + msg318828
2018-06-06 14:18:13miss-islingtonsetpull_requests: + pull_request7073
2018-06-06 14:18:13serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg318826
2018-06-06 14:17:16miss-islingtonsetpull_requests: + pull_request7072
2018-06-06 13:50:52vstinnersetmessages: + msg318824
2018-06-06 11:02:16vstinnersetmessages: + msg318811
2018-06-06 10:48:42vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request7070
2018-06-06 10:23:23vstinnercreate