[Home]

Summary:ASTERISK-26493: Is REMAINDER behaving in intended way?
Reporter:Jonathan Harris (lardconcepts)Labels:
Date Opened:2016-10-21 11:16:57Date Closed:2016-10-24 14:14:11
Priority:MinorRegression?
Status:Closed/CompleteComponents:Core/General
Versions:14.0.1 Frequency of
Occurrence
Constant
Related
Issues:
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/LinuxAttachments:
Description: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...
Comments:By: Asterisk Team (asteriskteam) 2016-10-21 11:16:57.958-0500

Thanks for creating a report! The issue has entered the triage process. That means the issue will wait in this status until a Bug Marshal has an opportunity to review the issue. Once the issue has been reviewed you will receive comments regarding the next steps towards resolution.

A good first step is for you to review the [Asterisk Issue Guidelines|https://wiki.asterisk.org/wiki/display/AST/Asterisk+Issue+Guidelines] if you haven't already. The guidelines detail what is expected from an Asterisk issue report.

Then, if you are submitting a patch, please review the [Patch Contribution Process|https://wiki.asterisk.org/wiki/display/AST/Patch+Contribution+Process].

By: cloos (cloos) 2016-10-21 16:19:58.315-0500

This got me curious.

REMAINDER() just calls remainderl(3) from libm.

That says that the quotient is rounded to the nearest int, so negative values are not unexpected.

For an always non-negative result you want to use modulus (infix %).


By: Rusty Newton (rnewton) 2016-10-24 14:14:11.282-0500

Appears to be working as documented. I don't see an issue here. Closing this one out.

https://linux.die.net/man/3/remainderl

https://wiki.asterisk.org/wiki/display/AST/Expr2+Built-in+Functions