To C programmers

alfcen

Well-Known Member
Licensed User
Longtime User
Hi,
Trying to convert a routine in C to B4A and get false results, believing that the code marked in red is wrongly converted. Anyone who could fix that for me?
Thanks a lot!
Robert


B4X:
int get_new_uranometria_page( const double ra, const double de)
{
   int zone = (int)( (90. - de) * 16. / 180. + .5);
   const char zone_splits[17] = { 1, 6, 10, 12, 15, 18, 18, 20, 20,
               20, 18, 18, 15, 12, 10, 6, 1 };
   int rval = (int)( (24. - ra) * (double)zone_splits[zone] / 24. + .5);
   rval %= zone_splits[zone];
   while( zone--)
      rval += zone_splits[zone];
   return( rval + 1);
}

B4X:
Sub PageNewUranometria(ra As Double, de As Double) As Int
   Dim zone, rval As Int
   Dim zone_splits() As Int
   zone = Floor(90 - de) * 16 / 180 + 0.5
   zone_splits = Array As Int(1, 6, 10, 12, 15, 18, 18, 20, 20, 20, 18, 18, 15, 12, 10, 6, 1)
   rval = ((24 - ra) * zone_splits(zone) / 24 + .5)
   rval = rval Mod zone_splits(zone)
[COLOR="Red"]   Do While rval = rval - 1
      zone = zone - 1
      rval = rval + zone_splits(zone)
   Loop[/COLOR]
   Return rval + 1
End Sub
 
Last edited:

derez

Expert
Licensed User
Longtime User
I'm not sure what should happen in the line
B4X:
while( zone--)
I guess it check the value of zone after reducing it , I don't know when it is true and when false (positive ?)
In your translation you test
B4X:
Do While rval = rval - 1
and it will never be true so the loop will not run.
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
I'm not sure what should happen in the line
while( zone--)

That's exactly what I do not understand either.
Anyway, thanks for writing, Derez
 

thedesolatesoul

Expert
Licensed User
Longtime User
That's exactly what I do not understand either.
Anyway, thanks for writing, Derez

That is a post-decrement.
The zone-1 statement will be executed AFTER the while check.
If you want it to happen before then make it: --zone

EDIT:
I think this should be:
B4X:
Do While (zone > 0)
      zone = zone - 1
      rval = rval + zone_splits(zone)
   Loop
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Hi thedesolatesoul,

Do While (zone > 0)

Bingo! That was the key. The results are poin-point.
Thanks so much for your expert help.

Cheers
Robert
 
Top