本文介绍了比较两个花车,看他们是否都是消极的,或者都是积极的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要比较这两个浮点数,如果一个是负数,一个是正数抛出一个错误。如果他们都是积极的,或两者都是消极的,那很好。



有什么想法?



如果答案是肯定的,那么它们就是正确的答案。如果答案是肯定的,那么它们就是正确的答案。

如果答案是否定的,那么它们是相反的符号。

如果答案是零(在一些值来处理舍入误差),那么一个或两个都是零,你将不得不单独检查他们。然后,您必须决定在您的情况下是否将0视为正面或负面。


Hay guys, i have 2 floats, which both comes from input boxes.

I need to compare these 2 floats, if one is negative and one is positive thrown an error. If they're both positive, or both negative, thats fine.

Any ideas?

Thanks

解决方案

Multiply them together.

If the answer is positive then they are both the same sign.

If the answer is negative then they are of opposite sign.

If the answer is zero (within some value to take care of rounding error) then one or both are zero and you'll have to check them individually. You'll then have to decide whether 0 to be treated as positive or negative in your scenario.

这篇关于比较两个花车,看他们是否都是消极的,或者都是积极的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 09:20