iOS Question Bit.FMod vs Mod

ilan

Expert
Licensed User
Longtime User
hi

i am sure i am doing something wrong but i need to get the remainder of a Float value in b4i.
Mod function in b4i expect only integers so i use Bit.FMod function but i am not getting the result as in b4j/b4a

B4X:
For i = 0 To 5
  'b4a/b4j
   Log((i*-360)+0.5 Mod 360)
  'b4i
  'Log(Bit.FMod((i*-360)+0.5,360))
Next

Result:

b4a/b4j
Waiting for debugger to connect...
Program started.
0.5
-359.5
-719.5
-1079.5
-1439.5
-1799.5
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.

b4i

what am i doing wrong?
 

ilan

Expert
Licensed User
Longtime User
ok found my mistake

the problem was in b4a/b4j.
Mod function comes before everything else so it need to look like this:

B4X:
    For i = 0 To 5
        'b4a/b4j
           Log(((i*-360)+0.5) Mod 360)
        'b4i
        'Log(Bit.FMod((i*-360)+0.5,360))
    Next

and now i have the same result in both platforms, sorry rookie mistake
 
Upvote 0

emexes

Expert
Licensed User
I have a vague recollection that some BASIC dialects' modulus functions are actually remainders. Above zero they're the same; below zero they can differ.

When negative numbers are involved, I have found it simpler to roll my own. INT and FLOOR always round down.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…