Jonathan H
2016-Oct-21 12:33 UTC
[asterisk-users] Problem with REMAINDER? 957%60 be 15 remainder 57 not 15 remainder -3 ?
I'm not mathematically gifted, but shouldn't 957%60 be 15 remainder 57? Google and my desktop calculator certainly think so. So where am I going wrong here? The following code exten => 7,1,Verbose(Context: ${CONTEXT} Exten:${EXTEN}) same => n,Set(myNum=957) same => n,Set(sec=$[REMAINDER(${myNum},60)]) same => n,Set(sec=$[ABS(${sec})]) same => n,Set(sec=$[${MATH(${sec}+0,i)}]) same => n,Verbose(1,${myNum},${sec}) gives me -- Executing [7 at fromvoipfone201:1] Verbose("PJSIP/6001-00000007", "Context: fromvoipfone201 Exten:7") in new stack Context: fromvoipfone201 Exten:7 -- Executing [7 at fromvoipfone201:2] Set("PJSIP/6001-00000007", "myNum=957") in new stack -- Executing [7 at fromvoipfone201:3] Set("PJSIP/6001-00000007", "sec=-3") in new stack -- Executing [7 at fromvoipfone201:4] Set("PJSIP/6001-00000007", "sec=3") in new stack -- Executing [7 at fromvoipfone201:5] Set("PJSIP/6001-00000007", "sec=3") in new stack -- Executing [7 at fromvoipfone201:6] Verbose("PJSIP/6001-00000007", "1,957,3") in new stack Where is -3 coming from?!? I can flip it around by using ABS to stop people freaking out when they hear a track is "15 minutes minus 3 seconds long", but it's still incorrect, isn't it? (I add the second MATH function to everything I do now to make it an integer as it seems you never can tell with Asterisk, just ignore it!) Any ideas? Thanks.
James Thomas
2016-Oct-21 15:27 UTC
[asterisk-users] Problem with REMAINDER? 957%60 be 15 remainder 57 not 15 remainder -3 ?
All I can tell you is where -3 comes from.>From http://www.voip-info.org/wiki/view/Asterisk+Expressions :REMAINDER(x,y) computes the remainder of dividing x by y. The return value is x - n*y, where n is the value x/y, rounded to the nearest integer. If this quotient is 1/2, it is rounded to the nearest even number. -3 comes from: n = x/y = 957/60 = 15.95 which rounds to 16 n*y = 16*60 = 960 x - 960 = 957-960 = -3 I'm not mathematically gifted either but I think the n is the problem. it shouldn't be the rounded result it should be the integer part of x/y (n=15) Can you just use modulo instead: ${MATH(${myNum}%60,int)} -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.digium.com/pipermail/asterisk-users/attachments/20161021/1ba0f24e/attachment.html>
Jonathan H
2016-Oct-21 16:17 UTC
[asterisk-users] Problem with REMAINDER? 957%60 be 15 remainder 57 not 15 remainder -3 ?
Yes! That's the one. Thank you. That's a good workaround. The following test dialplan shows the bug (feature?) exten => 7,1,Verbose(Context: ${CONTEXT} Exten:${EXTEN}) same => n,Set(seconds=57) same => n,While($[${seconds} <= 400]); same => n,Set(minutes=$[FLOOR(${seconds} / 60)]) same => n,Set(myRemainderSec=$[REMAINDER(${seconds},60)]) same => n,SET(myModSec=${MATH(${seconds}%60,int)}) same => n,Verbose(1,Seconds:${seconds} = Minutes:${minutes} Remainder Seconds:${myRemainderSec} modulo seconds:${myModSec}) same => n,Set(seconds=$[${seconds}+3]) same => n,EndWhile() This is the output: Seconds:57 = Minutes:0 Remainder Seconds:-3 modulo seconds:57 Seconds:60 = Minutes:1 Remainder Seconds:0 modulo seconds:0 Seconds:63 = Minutes:1 Remainder Seconds:3 modulo seconds:3 Seconds:66 = Minutes:1 Remainder Seconds:6 modulo seconds:6 Seconds:69 = Minutes:1 Remainder Seconds:9 modulo seconds:9 Seconds:72 = Minutes:1 Remainder Seconds:12 modulo seconds:12 Seconds:75 = Minutes:1 Remainder Seconds:15 modulo seconds:15 Seconds:78 = Minutes:1 Remainder Seconds:18 modulo seconds:18 Seconds:81 = Minutes:1 Remainder Seconds:21 modulo seconds:21 Seconds:84 = Minutes:1 Remainder Seconds:24 modulo seconds:24 Seconds:87 = Minutes:1 Remainder Seconds:27 modulo seconds:27 Seconds:90 = Minutes:1 Remainder Seconds:-30 modulo seconds:30 Seconds:93 = Minutes:1 Remainder Seconds:-27 modulo seconds:33 Seconds:96 = Minutes:1 Remainder Seconds:-24 modulo seconds:36 Seconds:99 = Minutes:1 Remainder Seconds:-21 modulo seconds:39 Seconds:102 = Minutes:1 Remainder Seconds:-18 modulo seconds:42 Seconds:105 = Minutes:1 Remainder Seconds:-15 modulo seconds:45 Seconds:108 = Minutes:1 Remainder Seconds:-12 modulo seconds:48 Seconds:111 = Minutes:1 Remainder Seconds:-9 modulo seconds:51 Seconds:114 = Minutes:1 Remainder Seconds:-6 modulo seconds:54 Seconds:117 = Minutes:1 Remainder Seconds:-3 modulo seconds:57 Seconds:120 = Minutes:2 Remainder Seconds:0 modulo seconds:0 Issue filed at https://issues.asterisk.org/jira/browse/ASTERISK-26493 On 21 October 2016 at 16:27, James Thomas <jthomasdpu at gmail.com> wrote:> All I can tell you is where -3 comes from. > From http://www.voip-info.org/wiki/view/Asterisk+Expressions : > REMAINDER(x,y) computes the remainder of dividing x by y. The return value > is x - n*y, where n is the value x/y, rounded to the nearest integer. If > this quotient is 1/2, it is rounded to the nearest even number. > > -3 comes from: > n = x/y = 957/60 = 15.95 which rounds to 16 > n*y = 16*60 = 960 > x - 960 = 957-960 = -3 > > I'm not mathematically gifted either but I think the n is the problem. it > shouldn't be the rounded result it should be the integer part of x/y (n=15) > > Can you just use modulo instead: ${MATH(${myNum}%60,int)} > > -- > _____________________________________________________________________ > -- Bandwidth and Colocation Provided by http://www.api-digital.com -- > > Join the Asterisk Community at the 13th AstriCon, September 27-29, 2016 > http://www.asterisk.org/community/astricon-user-conference > > New to Asterisk? Start here: > https://wiki.asterisk.org/wiki/display/AST/Getting+Started > > asterisk-users mailing list > To UNSUBSCRIBE or update options visit: > http://lists.digium.com/mailman/listinfo/asterisk-users