User Tools

Site Tools


non-integer_20modulo_20operation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

non-integer_20modulo_20operation [2018/03/31 13:19]
127.0.0.1 external edit
non-integer_20modulo_20operation [2018/04/17 17:48] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, November 2011//\\ \\  BBC BASIC, in common with several other languages (e.g. C), provides only an integer [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​mod|MOD]] operator as standard. That is, you can pass non-integer values to MOD but they will be truncated to integers before the operation is carried out. For example:​\\ ​ //by Richard Russell, November 2011//\\ \\  BBC BASIC, in common with several other languages (e.g. C), provides only an integer [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin6.html#​mod|MOD]] operator as standard. That is, you can pass non-integer values to MOD but they will be truncated to integers before the operation is carried out. For example:​\\ ​
 +<code bb4w>
         PRINT 678.9 MOD 123.45         PRINT 678.9 MOD 123.45
 +</​code>​
 prints the value **63**.\\ \\  However the [[http://​en.wikipedia.org/​wiki/​Modulo_operation|modulo]] operation is not fundamentally limited to integer values and occasionally it can be useful to have a version which will work with non-integers. In C this is provided by the **fmod** library function. An equivalent function in BBC BASIC can be implemented as follows:​\\ ​ prints the value **63**.\\ \\  However the [[http://​en.wikipedia.org/​wiki/​Modulo_operation|modulo]] operation is not fundamentally limited to integer values and occasionally it can be useful to have a version which will work with non-integers. In C this is provided by the **fmod** library function. An equivalent function in BBC BASIC can be implemented as follows:​\\ ​
 +<code bb4w>
         DEF FNfmod(x,y) = x - x DIV y * y         DEF FNfmod(x,y) = x - x DIV y * y
 +</​code>​
 Now we can perform the following operation:​\\ ​ Now we can perform the following operation:​\\ ​
 +<code bb4w>
         PRINT FNfmod(678.9,​ 123.45)         PRINT FNfmod(678.9,​ 123.45)
 +</​code>​
 and the value printed is **61.65**.\\ \\  The **FNfmod** function listed above is restricted to values in the range -2147483648 to +2147483647 because of the use of the **DIV** operator. If you need an extended range (assuming *FLOAT64 mode is in use) the following function may be used:\\ \\  and the value printed is **61.65**.\\ \\  The **FNfmod** function listed above is restricted to values in the range -2147483648 to +2147483647 because of the use of the **DIV** operator. If you need an extended range (assuming *FLOAT64 mode is in use) the following function may be used:\\ \\ 
 +<code bb4w>
         DEF FNfmod(x,y) = x - SGNx * INT ABS(x/y) * ABSy         DEF FNfmod(x,y) = x - SGNx * INT ABS(x/y) * ABSy
 +</​code>​
non-integer_20modulo_20operation.txt ยท Last modified: 2018/04/17 17:48 by tbest3112