Can mathematical operators *, /, +, -, ^ be used to convert a non-zero number to 1

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
1












I am working with a software (Oracle Siebel) that only supports Javascript expressions with operators multiply, divide, subtract, add, and XOR (*, /, -, +, ^). I don't have other operators such as ! or ?: available.



Using the above operators, is it possible to convert a number to 1 if it is non-zero and leave it 0 if it's already zero? The number may be positive, zero, or negative.



Example:



var c = 55;

var d; // d needs to set as 1


I tried c / c , but it evaluates to NaN when c is 0. d needs to be 0 when c is 0.



c is a currency value, it will have a maximum of two trailing digits and 12 leading digits.



I am trying to emulate an if condition by converting a number to a boolean 0 or 1, and then multiplying other parts of the expression.










share|improve this question























  • Use a tertiary operator: var d = c > 0 ? 1 : 0;
    – Ibu
    56 mins ago










  • Can the number be negative?
    – Jared Goguen
    56 mins ago






  • 1




    @Ibu their runtime supports only a very limited subset of js operators
    – zerkms
    55 mins ago







  • 1




    @Phix it does not support the not (!) operator
    – dave
    46 mins ago






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    45 mins ago














up vote
8
down vote

favorite
1












I am working with a software (Oracle Siebel) that only supports Javascript expressions with operators multiply, divide, subtract, add, and XOR (*, /, -, +, ^). I don't have other operators such as ! or ?: available.



Using the above operators, is it possible to convert a number to 1 if it is non-zero and leave it 0 if it's already zero? The number may be positive, zero, or negative.



Example:



var c = 55;

var d; // d needs to set as 1


I tried c / c , but it evaluates to NaN when c is 0. d needs to be 0 when c is 0.



c is a currency value, it will have a maximum of two trailing digits and 12 leading digits.



I am trying to emulate an if condition by converting a number to a boolean 0 or 1, and then multiplying other parts of the expression.










share|improve this question























  • Use a tertiary operator: var d = c > 0 ? 1 : 0;
    – Ibu
    56 mins ago










  • Can the number be negative?
    – Jared Goguen
    56 mins ago






  • 1




    @Ibu their runtime supports only a very limited subset of js operators
    – zerkms
    55 mins ago







  • 1




    @Phix it does not support the not (!) operator
    – dave
    46 mins ago






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    45 mins ago












up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





I am working with a software (Oracle Siebel) that only supports Javascript expressions with operators multiply, divide, subtract, add, and XOR (*, /, -, +, ^). I don't have other operators such as ! or ?: available.



Using the above operators, is it possible to convert a number to 1 if it is non-zero and leave it 0 if it's already zero? The number may be positive, zero, or negative.



Example:



var c = 55;

var d; // d needs to set as 1


I tried c / c , but it evaluates to NaN when c is 0. d needs to be 0 when c is 0.



c is a currency value, it will have a maximum of two trailing digits and 12 leading digits.



I am trying to emulate an if condition by converting a number to a boolean 0 or 1, and then multiplying other parts of the expression.










share|improve this question















I am working with a software (Oracle Siebel) that only supports Javascript expressions with operators multiply, divide, subtract, add, and XOR (*, /, -, +, ^). I don't have other operators such as ! or ?: available.



Using the above operators, is it possible to convert a number to 1 if it is non-zero and leave it 0 if it's already zero? The number may be positive, zero, or negative.



Example:



var c = 55;

var d; // d needs to set as 1


I tried c / c , but it evaluates to NaN when c is 0. d needs to be 0 when c is 0.



c is a currency value, it will have a maximum of two trailing digits and 12 leading digits.



I am trying to emulate an if condition by converting a number to a boolean 0 or 1, and then multiplying other parts of the expression.







javascript siebel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 13 mins ago









Joseph Sible

3,756627




3,756627










asked 1 hour ago









dave

1,7211125




1,7211125











  • Use a tertiary operator: var d = c > 0 ? 1 : 0;
    – Ibu
    56 mins ago










  • Can the number be negative?
    – Jared Goguen
    56 mins ago






  • 1




    @Ibu their runtime supports only a very limited subset of js operators
    – zerkms
    55 mins ago







  • 1




    @Phix it does not support the not (!) operator
    – dave
    46 mins ago






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    45 mins ago
















  • Use a tertiary operator: var d = c > 0 ? 1 : 0;
    – Ibu
    56 mins ago










  • Can the number be negative?
    – Jared Goguen
    56 mins ago






  • 1




    @Ibu their runtime supports only a very limited subset of js operators
    – zerkms
    55 mins ago







  • 1




    @Phix it does not support the not (!) operator
    – dave
    46 mins ago






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    45 mins ago















Use a tertiary operator: var d = c > 0 ? 1 : 0;
– Ibu
56 mins ago




Use a tertiary operator: var d = c > 0 ? 1 : 0;
– Ibu
56 mins ago












Can the number be negative?
– Jared Goguen
56 mins ago




Can the number be negative?
– Jared Goguen
56 mins ago




1




1




@Ibu their runtime supports only a very limited subset of js operators
– zerkms
55 mins ago





@Ibu their runtime supports only a very limited subset of js operators
– zerkms
55 mins ago





1




1




@Phix it does not support the not (!) operator
– dave
46 mins ago




@Phix it does not support the not (!) operator
– dave
46 mins ago




1




1




@dave storing money in floating point numbers, uh-oh
– zerkms
45 mins ago




@dave storing money in floating point numbers, uh-oh
– zerkms
45 mins ago












3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










c / (c + 5e-324) should work. If x is 0, that is exactly 0, and if x is nonzero (technically, if x is at least 4.45014771701440252e-308, which the smallest non-zero number allowed in the question, 0.01, is), JavaScript's floating-point math is too imprecise for the answer to be different than 1, so it will come out as exactly 1.






share|improve this answer






















  • s/JavaScript's floating-point math/IEEE754
    – zerkms
    44 mins ago






  • 2




    @visibleman Those won't come up per the comments
    – Jared Goguen
    44 mins ago










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    43 mins ago

















up vote
9
down vote













How about the expression n/n^0?






console.log([-1, 0, 1, 19575].map(n => n/n^0))





As you can see, all non-zero values get converted to 1, and 0 stays at 0. This leverages the fact that in Javascript, NaN^0 is 0.






share|improve this answer


















  • 2




    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    16 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    10 mins ago

















up vote
-2
down vote













Interesting problem. Here's a revised approach. First, let us create an OR function, which we can do with two XORs and an add.



function OR(x, y) 
return (x + (x ^ y) ^ x);



Next let us use a modified bit-parity type algorithm, but with OR instead of XOR. The result of this process will be a number that has the least-significant bit set if and only if the original value is non-zero. We can then XOR with a value that is all 1's except the first bit (ie, -2).



function IS_TRUE(c) = c >> 2 */
c = OR( c, c / 2 ); /* c





share|improve this answer


















  • 1




    != operator is not available. If it was you could just c != 0
    – zerkms
    29 mins ago











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    28 mins ago










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52979555%2fcan-mathematical-operators-be-used-to-convert-a-non-zero-number-to%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote



accepted










c / (c + 5e-324) should work. If x is 0, that is exactly 0, and if x is nonzero (technically, if x is at least 4.45014771701440252e-308, which the smallest non-zero number allowed in the question, 0.01, is), JavaScript's floating-point math is too imprecise for the answer to be different than 1, so it will come out as exactly 1.






share|improve this answer






















  • s/JavaScript's floating-point math/IEEE754
    – zerkms
    44 mins ago






  • 2




    @visibleman Those won't come up per the comments
    – Jared Goguen
    44 mins ago










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    43 mins ago














up vote
7
down vote



accepted










c / (c + 5e-324) should work. If x is 0, that is exactly 0, and if x is nonzero (technically, if x is at least 4.45014771701440252e-308, which the smallest non-zero number allowed in the question, 0.01, is), JavaScript's floating-point math is too imprecise for the answer to be different than 1, so it will come out as exactly 1.






share|improve this answer






















  • s/JavaScript's floating-point math/IEEE754
    – zerkms
    44 mins ago






  • 2




    @visibleman Those won't come up per the comments
    – Jared Goguen
    44 mins ago










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    43 mins ago












up vote
7
down vote



accepted







up vote
7
down vote



accepted






c / (c + 5e-324) should work. If x is 0, that is exactly 0, and if x is nonzero (technically, if x is at least 4.45014771701440252e-308, which the smallest non-zero number allowed in the question, 0.01, is), JavaScript's floating-point math is too imprecise for the answer to be different than 1, so it will come out as exactly 1.






share|improve this answer














c / (c + 5e-324) should work. If x is 0, that is exactly 0, and if x is nonzero (technically, if x is at least 4.45014771701440252e-308, which the smallest non-zero number allowed in the question, 0.01, is), JavaScript's floating-point math is too imprecise for the answer to be different than 1, so it will come out as exactly 1.







share|improve this answer














share|improve this answer



share|improve this answer








edited 34 mins ago

























answered 45 mins ago









Joseph Sible

3,756627




3,756627











  • s/JavaScript's floating-point math/IEEE754
    – zerkms
    44 mins ago






  • 2




    @visibleman Those won't come up per the comments
    – Jared Goguen
    44 mins ago










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    43 mins ago
















  • s/JavaScript's floating-point math/IEEE754
    – zerkms
    44 mins ago






  • 2




    @visibleman Those won't come up per the comments
    – Jared Goguen
    44 mins ago










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    43 mins ago















s/JavaScript's floating-point math/IEEE754
– zerkms
44 mins ago




s/JavaScript's floating-point math/IEEE754
– zerkms
44 mins ago




2




2




@visibleman Those won't come up per the comments
– Jared Goguen
44 mins ago




@visibleman Those won't come up per the comments
– Jared Goguen
44 mins ago












Ok, I just saw the comment now, yes this will work fine then.
– visibleman
43 mins ago




Ok, I just saw the comment now, yes this will work fine then.
– visibleman
43 mins ago












up vote
9
down vote













How about the expression n/n^0?






console.log([-1, 0, 1, 19575].map(n => n/n^0))





As you can see, all non-zero values get converted to 1, and 0 stays at 0. This leverages the fact that in Javascript, NaN^0 is 0.






share|improve this answer


















  • 2




    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    16 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    10 mins ago














up vote
9
down vote













How about the expression n/n^0?






console.log([-1, 0, 1, 19575].map(n => n/n^0))





As you can see, all non-zero values get converted to 1, and 0 stays at 0. This leverages the fact that in Javascript, NaN^0 is 0.






share|improve this answer


















  • 2




    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    16 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    10 mins ago












up vote
9
down vote










up vote
9
down vote









How about the expression n/n^0?






console.log([-1, 0, 1, 19575].map(n => n/n^0))





As you can see, all non-zero values get converted to 1, and 0 stays at 0. This leverages the fact that in Javascript, NaN^0 is 0.






share|improve this answer














How about the expression n/n^0?






console.log([-1, 0, 1, 19575].map(n => n/n^0))





As you can see, all non-zero values get converted to 1, and 0 stays at 0. This leverages the fact that in Javascript, NaN^0 is 0.






console.log([-1, 0, 1, 19575].map(n => n/n^0))





console.log([-1, 0, 1, 19575].map(n => n/n^0))






share|improve this answer














share|improve this answer



share|improve this answer








edited 16 mins ago

























answered 41 mins ago









CRice

10.9k1430




10.9k1430







  • 2




    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    16 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    10 mins ago












  • 2




    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    16 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    10 mins ago







2




2




TIL: Number.NaN ^ n === n o_O (where n is a finite number)
– zerkms
16 mins ago





TIL: Number.NaN ^ n === n o_O (where n is a finite number)
– zerkms
16 mins ago













It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
– CRice
10 mins ago




It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
– CRice
10 mins ago










up vote
-2
down vote













Interesting problem. Here's a revised approach. First, let us create an OR function, which we can do with two XORs and an add.



function OR(x, y) 
return (x + (x ^ y) ^ x);



Next let us use a modified bit-parity type algorithm, but with OR instead of XOR. The result of this process will be a number that has the least-significant bit set if and only if the original value is non-zero. We can then XOR with a value that is all 1's except the first bit (ie, -2).



function IS_TRUE(c) = c >> 2 */
c = OR( c, c / 2 ); /* c





share|improve this answer


















  • 1




    != operator is not available. If it was you could just c != 0
    – zerkms
    29 mins ago











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    28 mins ago














up vote
-2
down vote













Interesting problem. Here's a revised approach. First, let us create an OR function, which we can do with two XORs and an add.



function OR(x, y) 
return (x + (x ^ y) ^ x);



Next let us use a modified bit-parity type algorithm, but with OR instead of XOR. The result of this process will be a number that has the least-significant bit set if and only if the original value is non-zero. We can then XOR with a value that is all 1's except the first bit (ie, -2).



function IS_TRUE(c) = c >> 2 */
c = OR( c, c / 2 ); /* c





share|improve this answer


















  • 1




    != operator is not available. If it was you could just c != 0
    – zerkms
    29 mins ago











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    28 mins ago












up vote
-2
down vote










up vote
-2
down vote









Interesting problem. Here's a revised approach. First, let us create an OR function, which we can do with two XORs and an add.



function OR(x, y) 
return (x + (x ^ y) ^ x);



Next let us use a modified bit-parity type algorithm, but with OR instead of XOR. The result of this process will be a number that has the least-significant bit set if and only if the original value is non-zero. We can then XOR with a value that is all 1's except the first bit (ie, -2).



function IS_TRUE(c) = c >> 2 */
c = OR( c, c / 2 ); /* c





share|improve this answer














Interesting problem. Here's a revised approach. First, let us create an OR function, which we can do with two XORs and an add.



function OR(x, y) 
return (x + (x ^ y) ^ x);



Next let us use a modified bit-parity type algorithm, but with OR instead of XOR. The result of this process will be a number that has the least-significant bit set if and only if the original value is non-zero. We can then XOR with a value that is all 1's except the first bit (ie, -2).



function IS_TRUE(c) = c >> 2 */
c = OR( c, c / 2 ); /* c






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 min ago

























answered 31 mins ago









Ryan Pierce Williams

1114




1114







  • 1




    != operator is not available. If it was you could just c != 0
    – zerkms
    29 mins ago











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    28 mins ago












  • 1




    != operator is not available. If it was you could just c != 0
    – zerkms
    29 mins ago











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    28 mins ago







1




1




!= operator is not available. If it was you could just c != 0
– zerkms
29 mins ago





!= operator is not available. If it was you could just c != 0
– zerkms
29 mins ago













Ah, good point. Let me fix that...
– Ryan Pierce Williams
28 mins ago




Ah, good point. Let me fix that...
– Ryan Pierce Williams
28 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52979555%2fcan-mathematical-operators-be-used-to-convert-a-non-zero-number-to%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay