122

I am working with 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, and 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.

6
  • 3
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Oct 26, 2018 at 3:09
  • 10
    @SamuelLiew Although there are a lot of comments, and some of them should be removed (discussion, answer in comment), most of them are really request for clarification (for example). Comments asking whether it's an XY problem is borderline.
    – user202729
    Oct 26, 2018 at 12:50
  • Oracle Siebel Numeric Operators, seems to be Exponent: docs.oracle.com/cd/E95904_01/books/VBLANG/… Oct 26, 2018 at 19:49
  • 2
    @DerrickMoeller It seems there is a distinction between Siebel VB and Siebel eScript. In the latter, it is a bitwise xor. I'm not sure which of the two applies here.
    – CRice
    Oct 26, 2018 at 20:05
  • 3
    @user202729 once any entire comment thread gets too long, we are not going to individually treat comments differently, as the mod interface created by SE admins/devs prefers our default actions to be either "move to chat" or "delete all". If any comment is useful, it should be edited into the question itself or posted as an answer. If any comment is off-topic, they should have been self-pruned/deleted. If you would like to discuss this mod "feature" and/or the "too many comments" auto flags moderators get, please do bring it up for discussion on Meta or Meta.SE. Thank you.
    – Samuel Liew
    Oct 28, 2018 at 3:54

4 Answers 4

219

Use the expression n/n^0.

If n is not zero:

 Step    Explanation
------- -------------------------------------------------------------------------------
 n/n^0   Original expression.
 1^0     Any number divided by itself equals 1. Therefore n/n becomes 1.
 1       1 xor 0 equals 1.

If n is zero:

 Step    Explanation
------- -------------------------------------------------------------------------------
 n/n^0   Original expression.
 0/0^0   Since n is 0, n/n is 0/0.
 NaN^0   Zero divided by zero is mathematically undefined. Therefore 0/0 becomes NaN.
 0^0     In JavaScript, before any bitwise operation occurs, both operands are normalized.
         This means NaN becomes 0.
 0       0 xor 0 equals 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.

Demo:

[0, 1, 19575, -1].forEach(n => console.log(`${n} becomes ${n/n^0}.`))

9
  • 45
    TIL: Number.NaN ^ n === n o_O (where n is a finite number)
    – zerkms
    Oct 25, 2018 at 0:51
  • 17
    It seems that when used as a bitwise operand, NaN is conveniently converted to zero. See: Bitwise operations on non numbers
    – CRice
    Oct 25, 2018 at 0:57
  • 3
    @zerkms Maybe ~~NaN === 0 seems more natural
    – Bergi
    Oct 25, 2018 at 18:24
  • 9
    @EnricoBorba It is not exponentiation (which uses **), it is a bitwise XOR. Bitwise operations have a very low precedence in javascript so the division will happen first.
    – CRice
    Oct 26, 2018 at 1:37
  • 3
    ^ is bitwise xor operator. I doubt if ^ in the OP is bitwise xor when all other operators are arithmetic operators. It is probably the exponentiation operator (** in JavaScript).
    – Salman A
    Oct 26, 2018 at 19:57
150

c / (c + 5e-324) should work. (The constant 5e-324 is Number.MIN_VALUE, the smallest representable positive number.) 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.

12
  • 1
    you need to convert the expression to bool first, since the OP is trying to multiply 0 and 1 with other parts of the expression, and c / (c + 5e-324) * some_expression won't work
    – phuclv
    Oct 25, 2018 at 4:14
  • 29
    Taken a bug and turned it into a feature there Oct 25, 2018 at 19:33
  • 1
    @Medinoc yes, but the question also says that there will never be more than 2 digits after the decimal point, and 324 is a lot more than 2. Oct 26, 2018 at 12:56
  • 1
    It's interesting and it might work for OP, but I'd really hate to come across this kind of code when working in a team. At the very least, it should be well documented and tested. Oct 26, 2018 at 15:33
  • 2
    @JosephSible I know, but if you really take it that way, the most obvious answer should be Number(Boolean(n)), which uses no operator. I didn't say this is not what the answer the OP was looking for, but this is not the answer to the question the OP described. Probably should improve the question though.
    – zypA13510
    Oct 27, 2018 at 5:18
17

(((c/c)^c) - c) * (((c/c)^c) - c) will always return 1 for negatives and positives and 0 for 0.

It is definitely more confusing than the chosen answer and longer. However, I feel like it is less hacky and not relying on constants.

EDIT: As @JosephSible mentions, a more compact version of mine and @CRice's version which does not use constants is:

c/c^c-c
8
  • CRice's answer was not chosen, Joseph Sible was. In all honesty I didnt notice CRice's answer until after posting. Oct 26, 2018 at 12:13
  • 2
    It uses no constants though, only the operators and the given variable.
    – L3viathan
    Oct 26, 2018 at 12:15
  • 2
    @JosephSible c/c^(c-c) is just c Oct 26, 2018 at 16:42
  • 2
    @JosephSible I was reading ^ as exponentiation as implied by the OP "multiply, divide, subtract, add, and exponent (*, /, -, +, ^)". If ^ is XOR with its JavaScript precedence then I'm wrong. I asked for clarification on the OP. Oct 26, 2018 at 18:50
  • 2
    @TavianBarnes that's not what OP wrote; someone edited the question. I restored it to the way OP actually asked. Oct 26, 2018 at 19:50
2

A very complicated answer, but one that doesn't depend on limited precision: If you take x^(2**n), this will always be equal to x+2**n if x is zero, but it will be equal to x-2**n if x has a one in the nth place. Thus, for x=0, (x^(2**n)-x+2**n)/(2**(n+1) will always be 1, but it will sometimes be zero for x !=0. So if you take the product of (x^(2**n)-x+2**n)/(2**(n+1) over all n, then XOR that with 1, you will get your desired function. You'll have to manually code each factor, though. And you'll have to modify this if you're using floating points.

If you have the == operator, then (x==0)^1 works.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.