[英]Forcing a large number in javascript
I have a javascript function which compares two numbers.我有一个比较两个数字的 javascript 函数。 If one number is bigger than the other, then a function runs.
如果一个数字大于另一个数字,则运行一个函数。
if (x > y) {
// do something
} else {
// do something else
}
I have a situation where I need x
to always have a greater value than y
.我有一种情况,我需要
x
始终具有比y
更大的值。
At the moment I am doing this by using a very large number, like so目前我正在通过使用一个非常大的数字来做到这一点,就像这样
var x = 9999999999999999
Although this works perfectly well, it feels like a hack.虽然这工作得很好,但感觉就像一个黑客。
I have considered using a function to calculate a number which will always be bigger than y
, but it seems counterproductive to add extra calculations to a program when not strictly necessary.我曾考虑使用一个函数来计算一个总是大于
y
,但在不是绝对必要的情况下向程序添加额外的计算似乎适得其反。
Can anyone confirm what is the best practice in this situation?谁能确认在这种情况下的最佳做法是什么?
You can use Number.MAX_VALUE
您可以使用
Number.MAX_VALUE
x = Number.MAX_VALUE
witch equals to 1.7976931348623157e+308女巫等于 1.7976931348623157e+308
maxValue = x > y ?最大值 = x > y ? x++ : y++;
x++:y++;
This will be useful if you need it to always generate a value greater than those entering the condition.如果您需要它始终生成大于输入条件的值,这将非常有用。
if (x > y) {
// do something
} else {
// do something else
}
I have a situation where I need x to always have a greater value than y.
我有一种情况,我需要 x 始终具有比 y 更大的值。
Are you trying to force the true
condition in order to call // do something
directly or is there a need beyond this code for x
to have a larger value?您是否试图强制
true
条件以调用// do something
直接// do something
,或者是否需要超出此代码的x
具有更大的值?
Instead of forcing the true
condition, move the somethings into functions, then update your code而不是强制为
true
条件,将东西移到函数中,然后更新你的代码
(x > y) ? doSomething() : doSomethingElse();
Now you can call doSomething()
whenever you want.现在,您可以随时调用
doSomething()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.