[asterisk-bugs] [JIRA] (ASTERISK-26493) Is REMAINDER behaving in intended way?

Jonathan Harris (JIRA) noreply at issues.asterisk.org
Fri Oct 21 11:18:01 CDT 2016


Jonathan Harris created ASTERISK-26493:
------------------------------------------

             Summary: Is REMAINDER behaving in intended way?
                 Key: ASTERISK-26493
                 URL: https://issues.asterisk.org/jira/browse/ASTERISK-26493
             Project: Asterisk
          Issue Type: Bug
      Security Level: None
          Components: Core/General
    Affects Versions: 14.0.1
         Environment: Linux 4.4.0-38-generic #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
            Reporter: Jonathan Harris
            Severity: Minor


I was having issues with getting minutes and seconds from seconds, using some code provided by another user, using remainder I was getting negative seconds.

James Thomas helped me find a workaround, but from what he wrote, I wonder if REMAINDER is behaving as expected?

 the following test dialplan shows what I mean:

{code}
 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()
{code}

This is the output:

{code}
 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
{code}

James Thomas wrote:

{quote}
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)}
{quote}

The reason I'm filing this as a potential bug is that all other tools I've tried round it correctly, but REMAINDER acts differently. So just checking...




--
This message was sent by Atlassian JIRA
(v6.2#6252)



More information about the asterisk-bugs mailing list