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
    1 hour ago










  • Can the number be negative?
    – Jared Goguen
    1 hour ago






  • 1




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







  • 1




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






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    57 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
    1 hour ago










  • Can the number be negative?
    – Jared Goguen
    1 hour ago






  • 1




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







  • 1




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






  • 1




    @dave storing money in floating point numbers, uh-oh
    – zerkms
    57 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 24 mins ago









Joseph Sible

3,755627




3,755627










asked 1 hour ago









dave

1,7211125




1,7211125











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










  • Can the number be negative?
    – Jared Goguen
    1 hour ago






  • 1




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







  • 1




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






  • 1




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
















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










  • Can the number be negative?
    – Jared Goguen
    1 hour ago






  • 1




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







  • 1




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






  • 1




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















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




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












Can the number be negative?
– Jared Goguen
1 hour ago




Can the number be negative?
– Jared Goguen
1 hour ago




1




1




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





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





1




1




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




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




1




1




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




@dave storing money in floating point numbers, uh-oh
– zerkms
57 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
    56 mins ago






  • 2




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










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    55 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
    27 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    21 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. The first XOR gets all the differences between x and y while the second XOR isolates the differences in y from x. We can then add those differences to x, giving us an OR operator.



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).



I've assumed 32-bit ints, but it is easy to modify to 64-bit if needed.



function IS_TRUE(c) 
c = OR( c, c / 65536 ); /* c


Not as efficient as the marked answer, but it doesn't rely upon rounding errors :P






share|improve this answer


















  • 1




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











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    40 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
    56 mins ago






  • 2




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










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    55 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
    56 mins ago






  • 2




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










  • Ok, I just saw the comment now, yes this will work fine then.
    – visibleman
    55 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 46 mins ago

























answered 56 mins ago









Joseph Sible

3,755627




3,755627











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






  • 2




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










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
















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






  • 2




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










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















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




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




2




2




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




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












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




Ok, I just saw the comment now, yes this will work fine then.
– visibleman
55 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
    27 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    21 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
    27 mins ago











  • It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    21 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 28 mins ago

























answered 52 mins ago









CRice

10.9k1430




10.9k1430







  • 2




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











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












  • 2




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











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







2




2




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





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













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




It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
– CRice
21 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. The first XOR gets all the differences between x and y while the second XOR isolates the differences in y from x. We can then add those differences to x, giving us an OR operator.



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).



I've assumed 32-bit ints, but it is easy to modify to 64-bit if needed.



function IS_TRUE(c) 
c = OR( c, c / 65536 ); /* c


Not as efficient as the marked answer, but it doesn't rely upon rounding errors :P






share|improve this answer


















  • 1




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











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    40 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. The first XOR gets all the differences between x and y while the second XOR isolates the differences in y from x. We can then add those differences to x, giving us an OR operator.



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).



I've assumed 32-bit ints, but it is easy to modify to 64-bit if needed.



function IS_TRUE(c) 
c = OR( c, c / 65536 ); /* c


Not as efficient as the marked answer, but it doesn't rely upon rounding errors :P






share|improve this answer


















  • 1




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











  • Ah, good point. Let me fix that...
    – Ryan Pierce Williams
    40 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. The first XOR gets all the differences between x and y while the second XOR isolates the differences in y from x. We can then add those differences to x, giving us an OR operator.



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).



I've assumed 32-bit ints, but it is easy to modify to 64-bit if needed.



function IS_TRUE(c) 
c = OR( c, c / 65536 ); /* c


Not as efficient as the marked answer, but it doesn't rely upon rounding errors :P






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. The first XOR gets all the differences between x and y while the second XOR isolates the differences in y from x. We can then add those differences to x, giving us an OR operator.



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).



I've assumed 32-bit ints, but it is easy to modify to 64-bit if needed.



function IS_TRUE(c) 
c = OR( c, c / 65536 ); /* c


Not as efficient as the marked answer, but it doesn't rely upon rounding errors :P







share|improve this answer














share|improve this answer



share|improve this answer








edited 5 mins ago

























answered 43 mins ago









Ryan Pierce Williams

1074




1074







  • 1




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











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












  • 1




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











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







1




1




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





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













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




Ah, good point. Let me fix that...
– Ryan Pierce Williams
40 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













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery