Boriel Basic Forum
MOD doesn't work with type FIXED (*solved*) - Printable Version

+- Boriel Basic Forum (https://forum.boriel.com)
+-- Forum: Compilers and Computer Languages (https://forum.boriel.com/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://forum.boriel.com/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://forum.boriel.com/forumdisplay.php?fid=15)
+---- Thread: MOD doesn't work with type FIXED (*solved*) (/showthread.php?tid=167)



MOD doesn't work with type FIXED (*solved*) - britlion - 2010-02-25

I did wonder if this was a bug or not, since it might be odd to modulus a floating point number.

But mod works perfectly with type FLOAT, so logically it should also work well with type FIXED. Fixed division works, and the compiler seems to know where the code for mod should be - at label _MODF16.

Error: Undefined label '__MODF16'


Re: MOD doesn't work with type FIXED - boriel - 2010-02-25

britlion Wrote:I did wonder if this was a bug or not, since it might be odd to modulus a floating point number.

But mod works perfectly with type FLOAT, so logically it should also work well with type FIXED. Fixed division works, and the compiler seems to know where the code for mod should be - at label _MODF16.

Error: Undefined label '__MODF16'
Yes, this is a bug. As you might already guessed, Fixed type MOD is not implemented (I didn't exhaustively tested it, because I use fixed type only on +, - , *, / operations). Ok, will try to fix this week.


Re: MOD doesn't work with type FIXED - boriel - 2010-03-02

Ok, the bug has been fixed. Keep in mind that "MOD" is supposed to be used only with positive integer (Ubyte, UInteger, ULong) types. Anyway, the MODF16 is implemented, and it should work as expected, but due to the lack of precision, the following issues might happen:
  1. Lost of precision: 5 mod 3 = 2, but for fixed values it renders to 1.99999 aprox
  2. For negative numbers, the sign is propagated to the modulo.

The A mod B function is implemented as: fract(A / B) * B