News:

  • June 26, 2026, 06:31:54 PM

Login with username, password and session length

Author Topic: Do-More MATH returns integer result for real operation  (Read 18756 times)

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3611
  • Darth Ladder
Do-More MATH returns integer result for real operation
« on: October 01, 2014, 12:07:48 AM »
I have the following expression in a MATH box, but it never seems to return anything but an integer.

Code: [Select]
(R1 + MIN((T20.Acc / AVGR(V1000, 12)) * R10, MHR100 / 1000.0)) % DrumSize
For what I'm using it for, that's probably OK, but I'd have expected the expression to yield a floating point number.  The results suggest that it's doing the computation in floating point and then converting to an integer answer at the last moment (which then gets converted back to a real with no decimal portion since the answer is assigned to R0).  Is it that MODULO is incapable of returning a floating point result?

I tried changing DrumSize from 2000 to 2000.0 to see if it would force the MODULO into real mode, but no change.
« Last Edit: October 01, 2014, 12:15:27 AM by Controls Guy »
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: Do-More MATH returns integer result for real operation
« Reply #1 on: October 01, 2014, 12:55:31 AM »
Modulo is an integer operator.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3611
  • Darth Ladder
Re: Do-More MATH returns integer result for real operation
« Reply #2 on: October 01, 2014, 01:18:54 AM »
Is there some reasonably fast, clean way to get the floating point equivalent?

The only way I can think to do it is to do the FP computation, assign the result to a scratch float register, then take that register minus its own INT.  (Or, do the computation, assign to both an FP and an INT, and subtract).
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3827
    • Host Engineering
Re: Do-More MATH returns integer result for real operation
« Reply #3 on: October 01, 2014, 08:51:36 AM »
I see two ways to do this.
Store the REAL division of the numerator (R0) and denominator (R1) in R100, then calculate the "modulus" from that value in R100:
MATH R100 "ABS(R0) / ABS(R1)"
MATH R101 "(R100 - TRUNC(R100)) * ABS(R1)"

Another way is to use integer math, if you know your floating point numbers are within a relatively small range.  2's complement integers have a range of basically 0..2Billion.  Say your numerator and denominator floating point numbers are in the range 0..10,000 then you could multiply both by 200,000.0 and still be "in range" of a signed DWORD.  Do the integer modulo with those two integer values, then divide the integer modulo result by that "factor" 200,000.0 as a REAL.  If the range of your numerator and/or denominator is larger than 10,000, then you would need to reduce the factor.  The smaller the factor, the less precise your result will be.

MATH R101 "(ABS(TOINT(ROUND(R0 * 200000.0))) % ABS(TOINT(ROUND(R1 * 200000.0)))) / 200000.0"

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3611
  • Darth Ladder
Re: Do-More MATH returns integer result for real operation
« Reply #4 on: October 01, 2014, 01:39:53 PM »
Good ideas, thanks!

Another idea that would work well in some applications is to check if numerator is greater than denominator, and if so, subtract denominator from numerator.  This isn't the equivalent of modulo except when 1 <= quotient < 2, but in some cases that's the only case expected to occur, or else you can tolerate the N scans required when N <= quotient < N + 1.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.