Can mathematical operators *, /, +, -, ^ be used to convert a non-zero number to 1
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
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
 |Â
show 12 more comments
up vote
8
down vote
favorite
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
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
 |Â
show 12 more comments
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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
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
javascript siebel
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
 |Â
show 12 more comments
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
 |Â
show 12 more comments
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.
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
add a comment |Â
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
.
2
TIL:Number.NaN ^ n === n
o_O (wheren
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
add a comment |Â
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
1
!=
operator is not available. If it was you could justc != 0
â zerkms
29 mins ago
Ah, good point. Let me fix that...
â Ryan Pierce Williams
28 mins ago
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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
.
2
TIL:Number.NaN ^ n === n
o_O (wheren
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
add a comment |Â
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
.
2
TIL:Number.NaN ^ n === n
o_O (wheren
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
add a comment |Â
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
.
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))
edited 16 mins ago
answered 41 mins ago
CRice
10.9k1430
10.9k1430
2
TIL:Number.NaN ^ n === n
o_O (wheren
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
add a comment |Â
2
TIL:Number.NaN ^ n === n
o_O (wheren
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
add a comment |Â
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
1
!=
operator is not available. If it was you could justc != 0
â zerkms
29 mins ago
Ah, good point. Let me fix that...
â Ryan Pierce Williams
28 mins ago
add a comment |Â
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
1
!=
operator is not available. If it was you could justc != 0
â zerkms
29 mins ago
Ah, good point. Let me fix that...
â Ryan Pierce Williams
28 mins ago
add a comment |Â
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
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
edited 1 min ago
answered 31 mins ago
Ryan Pierce Williams
1114
1114
1
!=
operator is not available. If it was you could justc != 0
â zerkms
29 mins ago
Ah, good point. Let me fix that...
â Ryan Pierce Williams
28 mins ago
add a comment |Â
1
!=
operator is not available. If it was you could justc != 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
add a comment |Â
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
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
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
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
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
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