javascript 数学运算符 *、/、+、-、^ 能否用于将非零数转换为 1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52979555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can mathematical operators *, /, +, -, ^ be used to convert a non-zero number to 1?
提问by dave
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.
我使用的软件 (Oracle Siebel) 仅支持带有运算符乘法、除法、减法、加法和异或 ( *, /, -, +, ^) 的JavaScript 表达式。我没有其他运营商,例如!或? :可用。
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.
使用上述运算符,是否可以将数字转换为非零的数字为 1,如果数字已经为零则将其保留为 0?该数字可以是正数、零或负数。
Example:
例子:
var c = 55;
var d; // d needs to set as 1
I tried c / c, but it evaluates to NaNwhen cis 0. dneeds to be 0 when cis 0.
我试过c / c,但它评估为何NaN时c为 0。当为 0 时 d需要c为 0。
c is a currency value, and it will have a maximum of two trailing digits and 12 leading digits.
c 是货币值,最多有两个尾随数字和 12 个前导数字。
I am trying to emulate an ifcondition by converting a number to a Boolean 0 or 1, and then multiplying other parts of the expression.
我试图if通过将数字转换为布尔值 0 或 1,然后乘以表达式的其他部分来模拟条件。
回答by CRice
Use the expression n/n^0.
使用表达式n/n^0。
If nis not zero:
如果n不为零:
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 nis zero:
如果n为零:
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^0is 0.
如您所见,所有非零值都转换为 1,而 0 保持为 0。这利用了 JavaScript 中NaN^0为 0的事实。
Demo:
演示:
[0, 1, 19575, -1].forEach(n => console.log(`${n} becomes ${n/n^0}.`))
回答by Joseph Sible-Reinstate Monica
c / (c + 5e-324)should work. (The constant 5e-324is 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.
c / (c + 5e-324)应该管用。(常量5e-324是Number.MIN_VALUE,可表示的最小正数。)如果 x 为 0,则恰好为 0,并且如果 x 非零(从技术上讲,如果 x 至少为 4.45014771701440252e-308,这是问题,0.01,是),JavaScript 的浮点数学太不精确,答案与 1 不同,所以它会准确地显示为 1。
回答by Kyle Wardle
(((c/c)^c) - c) * (((c/c)^c) - c)will always return 1 for negatives and positives and 0 for 0.
(((c/c)^c) - c) * (((c/c)^c) - c)将始终为负数和正数返回 1,为 0 返回 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:
编辑:正如@JosephSible 所提到的,我的一个更紧凑的版本和不使用常量的@CRice 版本是:
c/c^c-c
回答by Acccumulation
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**nif x is zero, but it will be equal to x-2**nif 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.
一个非常复杂的答案,但不依赖于有限的精度:如果你采用x^(2**n),这将始终等于x+2**n如果 x 为零,但x-2**n如果 x 在第 n 位有一个 1,它将等于。因此,对于 x=0,(x^(2**n)-x+2**n)/(2**(n+1)将始终为 1,但对于 x !=0,它有时会为零。因此,如果您取(x^(2**n)-x+2**n)/(2**(n+1)所有 n的乘积,然后将其与 1 异或,您将获得所需的函数。但是,您必须手动编码每个因素。如果您使用浮点数,则必须修改它。
If you have the ==operator, then (x==0)^1works.
如果你有==运营商,那么(x==0)^1工作。

