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

  • A+
Category:Languages

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.

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: