Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Mod function(%)

Author
Message
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 2nd Sep 2009 15:29
Simple very useful.

for those that don't know using mod on two numbers returns the remander after dividing the two numbers.

so 13 % 2 would return 1 because 2 * 6 == 12 and so the remander is 1.

i got 10 millaseconds on trying 1000000 % 20 so it's pretty good. here's the function



course you can't create operators so it's just a function but it works!

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 2nd Sep 2009 16:10
I think it can be simplified and sped up a bit:



Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 2nd Sep 2009 19:18
Nice! i didn't think that would work because of the float value that "num/modulus" would return! you have clearly proven me wrong good job!

New Site! Check it out \/
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Sep 2009 01:41 Edited at: 12th Sep 2009 01:43
dbc is quirky it will only return a float if the modulus (new word for me ) is a float, otherwise it will return an integer.

TGC Forum - converting error messages into sarcasm since 2002.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 14th Sep 2009 14:38
@Latch

If I used the numbers 13 and 2 with your function, 13-(13/2*2)=-1, since 13-(13/2=6.5~7*2=14)=-1. You'd have to add another 0-" at the start.



TheComet


Make the paths of your enemies easier with WaypointPro!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Sep 2009 16:00
@ThComet
No, I'm pretty sure the function would return 1 as it stands. num/modulus returns the interger portion of the division. 13/2 = 6.5 but the return value is 6. If it were mod(2,13), then 2/13 = 0.154 and the return value from 2/13 would be 0 . Integers divided by integers return a truncated integer in DBC. In some BASICs there is actually a TRUNC() (short for truncate) function that returns the integer portion of a floating point number. In DBC, INT() is the equivalent function to TRUNC. But in the case of one integer divided by another, DBC maintains the data type as integer.

mod(13,2)

value=13 - ((13/2) * 2 )
value=13 - ((6) *2 )
value= 13 - 12
value= 1

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 14th Sep 2009 19:27
Ah, I thought 6.5 is rounded up to 7... Whoops, sorry.

TheComet


Make the paths of your enemies easier with WaypointPro!

Login to post a reply

Server time is: 2024-05-01 23:17:01
Your offset time is: 2024-05-01 23:17:01