Android Question [B4X] check Number1 is further away from 0 than number2 whether minus or plus

Alexander Stolte

Expert
Licensed User
Longtime User
I want to check if number1 is more away from 0 than number2.

Example:

Number1=10
Number2=-12
Number2 is further away from 0 than Number1 Return False

How can i check this?

Solution:
B4X:
Public Sub isNumberOneCloserToZero (numberOne As Int, numberTwo As Int) As Boolean
    Return (Abs(numberOne) >= Abs(numberTwo))
End Sub
 
Last edited:

Sandman

Expert
Licensed User
Longtime User
Something like this (not tested)
B4X:
Public Sub isNumberOneCloserToZero (numberOne as Int, numberTwo as Int) as Boolean
    return (abs(numberOne) < abs(numberTwo))
End Sub

I imagine it's the negative number that's throwing you off, the math isn't very difficult if they are both positive. So the solution is to use abs() to get the absolute value of both, which in effect makes them both positive and simple to compare.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Just to be clear, nobody's is wrong. As stated in #7, the OP's solution (see #1) is the answer to "isNumberTwoCloserToZero".
Miss-naming a Sub could become a problem for future debugging.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I saw that too, but then I thought...:)

‘such quibbling over semantics may seem petty stuff’ - Oxford Dictionary - example

At least for some. A more recent example:

"They are officially designated as the Russian Olympic Committee rather than Russia, but that’s semantics".— Rachel Axon, USA TODAY, 30 July 2021
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I saw that too, but then I thought...:)

‘such quibbling over semantics may seem petty stuff’ - Oxford Dictionary - example
In programming though, such things can introduce subtle bugs that a) seem to show up randomly and b) seem to be hard to trace.
 
Upvote 0
Top