简体   繁体   English

有没有更好的方法来简化此IF语句

[英]Is there a better way to simplify this IF statement

Is there a better way to simplify this IF statement. 有没有更好的方法来简化此IF语句。 I've been told there is no need of using bc here because this adds to both computational and maintenance complexity to large scripts. 有人告诉我这里不需要使用bc,因为这会增加大型脚本的计算和维护复杂性。 Is there a way to avoid the use of bc here: 有没有一种方法可以避免在这里使用bc:

if [ `bc -l <<< "$AUTO_INCREMENT_RATIO>=0.9"` -eq 1 ]
  then
     <DO SOMETHING HERE>
  fi

Thank you! 谢谢!

I was trying something like 我在尝试类似

if [ $AUTO_INCREMENT_RATIO>=0.9 -eq 1 ]; then <DO SOMETHING HERE>; fi

But I get an error 'integer expression expected' 但我收到一个错误“预期为整数表达式”

The best way to deal with floating points is to not use them in the first place! 处理浮点数的最佳方法是首先不要使用浮点数!

Reading the name of your variable, it's a ratio. 读取变量名,这是一个比率。 If by any chance, this ratio is a ratio of positive integers , say a/b , then, instead of comparing a/b with 0.9 , compare 10*a with 9*b . 如果这个比率是正整数的比率,例如a/b ,则不要将a/b0.9进行比较,而是将10*a9*b进行比较。

This might not be directly applicable in your case, but that's a good thing to remember. 这可能不适用于您的情况,但是请记住这是一件好事。 A lot of problems that seem to involve floats, in fact only involve rational numbers and hence can be solved (usually more efficiently) using integers only. 许多似乎涉及浮点数的问题实际上仅涉及有理数,因此仅使用整数即可解决(通常更有效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM