About mod and then

New to FreeBASIC? Post your questions here.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

About mod and then

Post by nimdays »

Hello,I'm curious about this

Code: Select all

dim as integer x = -10
?x mod 3  ' -1
if x<0then?x
sleep
About the mod operator, the result should be negative or positive?
And about "then", No separator is fine, It's not an operator like "<,>,etc"

Thanks
fxm
Moderator
Posts: 12158
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: About mod and then

Post by fxm »

The result is the remainder of the integer division of x (-10) by 3:
result = x - (x \ 3) * 3

The integer division divides x by 3, and the fractional share of the resulting quotient is truncated (Fix operator):
(x \ 3) = Fix(x / 3)
(x \ 3) = Fix(-3.333...)
(x \ 3) = -3

Finally:
result = -10 - (-3) * 3
result = -10 + 9
result = -1
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: About mod and then

Post by dodicat »

Similar, but for doubles. (Same as the crt fmod)
The Then keyword should be separated by a space.
You got away with it by enclosing it with a literal and a question mark.
Probably a bug of sorts, but not worth fixing. (same as end if endif)

Code: Select all

 


 #define dmod(x,y) (y)*Frac((x)/(y))
 
print -10 mod 3
print dmod(-10,3)
print dmod(-10.5,3)


'integer test
 dim as long g
 for z as long=1 to 1000000
     var m=int(rnd*2000-rnd*2000)
     var n=int(rnd*2000-rnd*2000)
     if n<>0 then
     g=dmod(m,n)
     if m mod n <> g then
     print m mod n,g
 end if
 end if
next
print "done"
 sleep 
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: About mod and then

Post by counting_pine »

All you need to remember with Mod is that the result has the same sign as the input. (Except when the result is zero.)

(This means the result doesn't depend on the sign of the second operand at all.)
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: About mod and then

Post by nimdays »

Thank you All, I read somewhere that it's not mathematically correct and the remainder should be positive
dodicat wrote: The Then keyword should be separated by a space.
You got away with it by enclosing it with a literal and a question mark.
Probably a bug of sorts, but not worth fixing. (same as end if endif)
You're right, it's not serious
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: About mod and then

Post by counting_pine »

You could argue that 'mod' should always be non-negative, and '\' should always round downwards. I'd probably prefer it that way.
But it is the way it is because that's what people expect now, and it's what QBASIC did, and it's how x86 processors do it.

Either way, as long as the identity 'a = b * (a \ b) + (a mod b)' holds, I'm fairly happy.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: About mod and then

Post by nimdays »

Thanks again Admin.
Post Reply