How to handle MOD returning a negative number?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I discovered today that Salesforce's MOD function (in formulae and workflows) will return a negative value if given a negative input.



MOD(-5, 12) = -5


This goes against the mathematical definition of Modulo, and makes it useless when dealing with dates.



The only way I can think of to handle this is:



MOD(MOD(Field__c, 12) + 12, 12)


But this doesn't seem like a sensible approach and will eat up compiled bytes rather quickly.



Is there a more sensible way to handle this?










share|improve this question























  • You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

    – Adrian Larson
    Feb 5 at 17:07











  • @AdrianLarson it's not a bug...

    – sfdcfox
    Feb 5 at 17:08


















1















I discovered today that Salesforce's MOD function (in formulae and workflows) will return a negative value if given a negative input.



MOD(-5, 12) = -5


This goes against the mathematical definition of Modulo, and makes it useless when dealing with dates.



The only way I can think of to handle this is:



MOD(MOD(Field__c, 12) + 12, 12)


But this doesn't seem like a sensible approach and will eat up compiled bytes rather quickly.



Is there a more sensible way to handle this?










share|improve this question























  • You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

    – Adrian Larson
    Feb 5 at 17:07











  • @AdrianLarson it's not a bug...

    – sfdcfox
    Feb 5 at 17:08














1












1








1








I discovered today that Salesforce's MOD function (in formulae and workflows) will return a negative value if given a negative input.



MOD(-5, 12) = -5


This goes against the mathematical definition of Modulo, and makes it useless when dealing with dates.



The only way I can think of to handle this is:



MOD(MOD(Field__c, 12) + 12, 12)


But this doesn't seem like a sensible approach and will eat up compiled bytes rather quickly.



Is there a more sensible way to handle this?










share|improve this question














I discovered today that Salesforce's MOD function (in formulae and workflows) will return a negative value if given a negative input.



MOD(-5, 12) = -5


This goes against the mathematical definition of Modulo, and makes it useless when dealing with dates.



The only way I can think of to handle this is:



MOD(MOD(Field__c, 12) + 12, 12)


But this doesn't seem like a sensible approach and will eat up compiled bytes rather quickly.



Is there a more sensible way to handle this?







formula-field field-update






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 5 at 16:55









JazzerJazzer

386




386













  • You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

    – Adrian Larson
    Feb 5 at 17:07











  • @AdrianLarson it's not a bug...

    – sfdcfox
    Feb 5 at 17:08



















  • You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

    – Adrian Larson
    Feb 5 at 17:07











  • @AdrianLarson it's not a bug...

    – sfdcfox
    Feb 5 at 17:08

















You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

– Adrian Larson
Feb 5 at 17:07





You should open a case to report this bug. Other than that, you might be able to move the logic to Apex.

– Adrian Larson
Feb 5 at 17:07













@AdrianLarson it's not a bug...

– sfdcfox
Feb 5 at 17:08





@AdrianLarson it's not a bug...

– sfdcfox
Feb 5 at 17:08










1 Answer
1






active

oldest

votes


















3














Different languages handle negative modulo differently. If you check Wikipedia's Modulo operation article, you'll see that negative results might be returned if the divisor is negative, the dividend is negative, or never, or undefined. Salesforce's implementation is no more or less correct than any other implementation. Unfortunately, if you always need a positive result as in some other languages, you're basically limited to either the method you've chosen or choosing an arbitrarily large multiple of your modulus to add to:



MOD(120000000000+Field__c, 12)


For example, -1 would result in 119999999999, which ultimately results in the remainder 11.



I've even found at least one online calculator that operates in two modes, one of which being Salesforce's MOD operator



Finally, remember that Salesforce runs on Oracle, and so MOD works here just like it does in Oracle.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "459"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f249196%2fhow-to-handle-mod-returning-a-negative-number%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Different languages handle negative modulo differently. If you check Wikipedia's Modulo operation article, you'll see that negative results might be returned if the divisor is negative, the dividend is negative, or never, or undefined. Salesforce's implementation is no more or less correct than any other implementation. Unfortunately, if you always need a positive result as in some other languages, you're basically limited to either the method you've chosen or choosing an arbitrarily large multiple of your modulus to add to:



    MOD(120000000000+Field__c, 12)


    For example, -1 would result in 119999999999, which ultimately results in the remainder 11.



    I've even found at least one online calculator that operates in two modes, one of which being Salesforce's MOD operator



    Finally, remember that Salesforce runs on Oracle, and so MOD works here just like it does in Oracle.






    share|improve this answer




























      3














      Different languages handle negative modulo differently. If you check Wikipedia's Modulo operation article, you'll see that negative results might be returned if the divisor is negative, the dividend is negative, or never, or undefined. Salesforce's implementation is no more or less correct than any other implementation. Unfortunately, if you always need a positive result as in some other languages, you're basically limited to either the method you've chosen or choosing an arbitrarily large multiple of your modulus to add to:



      MOD(120000000000+Field__c, 12)


      For example, -1 would result in 119999999999, which ultimately results in the remainder 11.



      I've even found at least one online calculator that operates in two modes, one of which being Salesforce's MOD operator



      Finally, remember that Salesforce runs on Oracle, and so MOD works here just like it does in Oracle.






      share|improve this answer


























        3












        3








        3







        Different languages handle negative modulo differently. If you check Wikipedia's Modulo operation article, you'll see that negative results might be returned if the divisor is negative, the dividend is negative, or never, or undefined. Salesforce's implementation is no more or less correct than any other implementation. Unfortunately, if you always need a positive result as in some other languages, you're basically limited to either the method you've chosen or choosing an arbitrarily large multiple of your modulus to add to:



        MOD(120000000000+Field__c, 12)


        For example, -1 would result in 119999999999, which ultimately results in the remainder 11.



        I've even found at least one online calculator that operates in two modes, one of which being Salesforce's MOD operator



        Finally, remember that Salesforce runs on Oracle, and so MOD works here just like it does in Oracle.






        share|improve this answer













        Different languages handle negative modulo differently. If you check Wikipedia's Modulo operation article, you'll see that negative results might be returned if the divisor is negative, the dividend is negative, or never, or undefined. Salesforce's implementation is no more or less correct than any other implementation. Unfortunately, if you always need a positive result as in some other languages, you're basically limited to either the method you've chosen or choosing an arbitrarily large multiple of your modulus to add to:



        MOD(120000000000+Field__c, 12)


        For example, -1 would result in 119999999999, which ultimately results in the remainder 11.



        I've even found at least one online calculator that operates in two modes, one of which being Salesforce's MOD operator



        Finally, remember that Salesforce runs on Oracle, and so MOD works here just like it does in Oracle.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 5 at 17:11









        sfdcfoxsfdcfox

        263k12209456




        263k12209456






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f249196%2fhow-to-handle-mod-returning-a-negative-number%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Список кардиналов, возведённых папой римским Каликстом III

            Deduzione

            Mysql.sock missing - “Can't connect to local MySQL server through socket”