Positive testing

vision

Member
Licensed User
Longtime User
Hi, I'm trying to convert a calculated angle into a bearing, and have used the expression below in visual basic. How do I do the same with no "<>"'s?

If dx<>0 Then
bear1=180-ATan(dy/dx)*rad2degree-90*(dx>0)+90*(dx<0)
Else If dy > 0 Then
bear1=180
Else
bear1 =0
End If

I'm a beginner at this and would appreciate any help.

regards, Peter
 

mjcoon

Well-Known Member
Licensed User
That subject heading caught me out; I read it like "beta testing"!

I know that it seems as if maths should be able to cope with all input values, but I don't think that's reasonable. In particular if both dx and dy are zero the answer is mathematically indeterminate, so yielding result of zero is arbitrary.

It may be possible to get round the divide-by-zero for only one of dx and dy being zero by calculating the hypotenuse and using ASin or ACos, but I don't think that it is really worthwhile.

Mike (who has cheated and used the algorithm embedded in Basic4PPC open source program GPS4PPC which cites Aviation Formulary V1.45. That has plenty of If's too!)
 

vision

Member
Licensed User
Longtime User
Thanks for the reply. I'm not too fussed with the division by zero issue (maybe max(dx,0)), more by the "not equal to" issue.
I thought this would be a common problem, and there would be an elegant solution.
regards, Peter
 

mjcoon

Well-Known Member
Licensed User
I'm not too fussed with the division by zero issue (maybe max(dx,0)), more by the "not equal to" issue.

Now I'm confused (as so often...). Isn't the reason for testing for zero (either using "If dx = 0" or "If dx <> 0") just about avoiding division by zero?

And how does "max(dx,0)" help? It would if you introduce an arbitrary minimum value such as "max(dx,0.00001)". But you would also need to know that dx cannot validly be negative and/or include abs(dx) and then re-instate the sign. All a bit heavy!

Mike.
 

vision

Member
Licensed User
Longtime User
Mike, partly it is about dividing by zero, but also to test whether dx is +ve or -ve (see back end of the line below). I thought of testing
If dx=0 is false then
if max(dx,0) =0 is then...

but I thought this would be a common problem, and maybe I was ignorant of a more elegant and easy solution.

bear1=180-ATan(dy/dx)*rad2degree-90*(dx>0)+90*(dx<0)


regards, Peter
 

klaus

Expert
Licensed User
Longtime User
Hi Mike,
About your question of calculating the mean value of bearing with values of 359 and 1 which gives 180.

I would do it that way:
Calculate the mean, max and min values and
If max - min >180 Then
mean = (mean + 180) Mod 360
End If

Best regards.
 

vision

Member
Licensed User
Longtime User
Thanks, you guys! This forum thingy is great! I think I can sort it- my problem is that true & false don't evaluate to 0 or 1, but the comparitors(?) "<" & ">" are still available- which didn't seem to be the case from help or the manual.

regards, Peter
 

mjcoon

Well-Known Member
Licensed User
... true & false don't evaluate to 0 or 1, but the comparitors(?) "<" & ">" are still available- which didn't seem to be the case from help or the manual.

Hi again Peter, your original code had "If dy > 0 Then" so I thought you didn't have a problem with the operators. Similarly since I think all your "If"s have "Else"s you don't need "If dx=0 is false then" because you can just swap over the If and Else parts.

I understand that as well as avoiding divide by zero you also need to establish which quadrant your hypotenuse is in to get answers that are not -90 > x < +90.

I hadn't noticed your use of boolean expressions as arithmetic terms, which would work in some languages (and be illegal in others!). Other languages have general expressions in the form (If boolean : value1 ; value2) to provide a neater alternative.

Mike.
 

klaus

Expert
Licensed User
Longtime User
This line: bear1=180-ATan(dy/dx)*rad2degree-90*(dx>0)+90*(dx<0)
will not work in B4PPC !

Logical operators are not accepted in mathematic equations.

I do it that way:
Sub Bearing(dx AsNumber, dy AsNumber)
If dx = 0 Then
If dy > 0 Then
Return 0
Else If dy < 0 Then
Return 180
Else
Return 999
End If
Else
If dx > 0 Then
Return 90-ATan(dy/dx)*Rad2Degree
Else
Return 270-ATan(dy/dx)*Rad2Degree
End If
End If
End Sub

Best regards.
 

Attachments

  • CalcBearing.sbp
    1.3 KB · Views: 199
Last edited:

vision

Member
Licensed User
Longtime User
Once again, thanks for your energy & efforts, guys! That's great!
regards, Peter
 

mjcoon

Well-Known Member
Licensed User
About your question of calculating the mean value of bearing with values of 359 and 1 which gives 180.

I would do it that way:
Calculate the mean, max and min values and
If max - min >180 Then
mean = (mean + 180) Mod 360
End If

Hi Klaus, thanks for your reply and sorry to take so long to respond.

Thinking more about it, and after experimenting and finding that solution does not really work...

(The trouble is that the mean value goes all over the place if small values are mixed up with large ones, so doing Mod 360 cannot cure that.)

I think that the only real answer will be to keep the northing and easting components (with signs) of the successive directions, average them separately, then convert back to a heading. That should remove any idea of one direction (North) being a boundary between large and small bearings...

Wish me luck! Mike.
 

klaus

Expert
Licensed User
Longtime User
Hi Mike,

Yes, this
If max - min >180 Then
mean = (mean + 180) Mod 360
End If
works only for two values.

To get the right answer you must change the all bearing values higher than 180 to negative values. Then it works as expected.

B4X:
[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Sum=[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] i=[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] BearingNb-[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Bearing(i)>[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]180 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]  Bear=Bearing(i)-[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]360[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] Else[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]  Bear=Bearing(i)[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff] End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] Sum=Sum+Bear[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Mean=Sum/BearingNb[/SIZE][/FONT]
[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT]

Attached a small test program.

Best regards.
 

Attachments

  • CalcBearing.sbp
    1.4 KB · Views: 193
Last edited:
Top