Powerbasic port to Freebasic MOD operator

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Powerbasic port to Freebasic MOD operator

Postby rpkelly » Sep 03, 2016 22:41

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 Function
Function 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 Function
Function cmCeiling(ByVal x as Double) as Long

' Return smallest interger greater than or equal to x

    Function = cmFloor(x * -1) * -1

End Function
Function 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

Postby caseih » Sep 04, 2016 13:59

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

Postby rpkelly » Sep 04, 2016 22:24

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

Re: Powerbasic port to Freebasic MOD operator

Postby counting_pine » Sep 05, 2016 12:56

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

Postby caseih » Sep 05, 2016 14:27

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

Postby rpkelly » Sep 05, 2016 22:27

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

Postby frisian » Sep 07, 2016 21:13

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

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 6 guests