How to handle MOD returning a negative number?
Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
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
add a comment |
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
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
formula-field field-update
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 5 at 17:11
sfdcfoxsfdcfox
257k12201444
257k12201444
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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