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