admin管理员组文章数量:1297132
I want to ensure that a number is positive, and if not return its mirror
I have started with this
var pos = Math.sqrt(x*x);
Is there a better, more efficient way?
I want to ensure that a number is positive, and if not return its mirror
I have started with this
var pos = Math.sqrt(x*x);
Is there a better, more efficient way?
Share asked Feb 13, 2012 at 10:21 Mild FuzzMild Fuzz 30.8k34 gold badges105 silver badges151 bronze badges 1-
3
I think
Math.sqrt(x*x)
is the most inefficient way :) – Sergey Gavruk Commented Feb 13, 2012 at 10:27
3 Answers
Reset to default 8Use Math.abs
.
Example: document.write(Math.abs(-5))
.
You should use Math.abs(number)
to do that.
The abs()
method returns the absolute value of a number.
var pos = Math.abs(x)
本文标签: javascriptIs there a better way to ensure a number is positiveStack Overflow
版权声明:本文标题:javascript - Is there a better way to ensure a number is positive? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741646396a2390208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论