## Powerbasic port to Freebasic MOD operator

rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

### Powerbasic port to Freebasic MOD operator

I'm just beginning my code port and addressing the differences. Here is my handling of the MOD differences.

Code: Select all

`Function cmAMod(ByVal x as Double, ByVal y as Double) as Double' Variation of x MOD y for Real Numbers adjusted so that the modulus' of a multiple of the divisor is the divisor itself rather than zero.' ' If x MOD y = 0 then result is adjusted to y    Function = x - y * (cmCeiling(x / y) - 1)End FunctionFunction cmMod(ByVal x as Double, ByVal y as Double) as Double' x MOD y for Real Numbers, y<>0    Function = x - y * cmFloor(x / y)End FunctionFunction cmCeiling(ByVal x as Double) as Long' Return smallest interger greater than or equal to x    Function = cmFloor(x * -1) * -1End FunctionFunction cmFloor(ByVal x as Double) as Long' Return largest integer less than or equal to x    Function = Int(x)End Function`
caseih
Posts: 1562
Joined: Feb 26, 2007 5:32

### Re: Powerbasic port to Freebasic MOD operator

It would help if you specified what differences you are addressing. Which things are different in FB and how do they behave in PB?

I certainly hope PB never had x mod y return y when x mod y is supposed to be 0! Because that is not a modulus by definition. If PB ever did that I would consider that a bug in the compiler.
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

### Re: Powerbasic port to Freebasic MOD operator

FB MOD seems to only support integer types and PB supports real types.
counting_pine
Posts: 6230
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

### Re: Powerbasic port to Freebasic MOD operator

I presume AMod's general purpose tends to be for finding padding amounts, e.g. tab sizes, where you want to round up to the next multiple, but always with a value of 1 or more.
caseih
Posts: 1562
Joined: Feb 26, 2007 5:32

### Re: Powerbasic port to Freebasic MOD operator

I see. Just FYI there's a function in the C standard library called fmod() that you can use from FreeBASIC. It's in the "crt/math.bi" file.

Code: Select all

`#include "crt/math.bi"print fmod(3.3,1.2)`
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

### Re: Powerbasic port to Freebasic MOD operator

counting_pine wrote:I presume AMod's general purpose tends to be for finding padding amounts, e.g. tab sizes, where you want to round up to the next multiple, but always with a value of 1 or more.

AMod is useful when a non zero result is wanted. For instance, assume you have an integer contains some number of months and you want to know the ending month. If nMonths = 12 then nMonths MOD 12 is zero but nMonths AMOD 12 is 12.
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

### Re: Powerbasic port to Freebasic MOD operator

For the function cmCeiling:
replace Function = cmFloor(x * -1) * -1 (need's two multi ply's)
with Function = -cmFloor( -x) (same result but faster).