Re: Decimal Display

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Rick Rothstein (MVP - VB) wrote:
The VB Mod function only works with integral values, and it Rounds (using Banker's Rounding) and number containing fractions BEFORE it performs the Mod operations.

Where were you in the Fractional Remainder thread when I asked someone to confirm that Mod was integer-only? ;-)

If you type Mod in the IDE and hit F1, you'll see this at the bottom of that page.

<quote>
The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (result) equals 5.

A = 19 Mod 6.7

Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number. Any fractional portion is truncated. However, if any expression is Null, result is Null. Any expression that is Empty is treated as 0.
</quote>

I like how casually they say "rounding floating-point numbers"... the Rounding being used is Banker's Rounding, so numbers ending in .5 will not always "behave" as expected (since the rounding will be toward the even whole number). I also highly dislike their use of "truncated" in the last paragraph... nothing is truncated... both numbers are Banker's Rounded to whole numbers where the Mod function then performs normally.

Any fractional part of the _RESULT_ is truncated. That is as opposed to rounding the result of the division after the dividend and divisor are previously rounded. Other than the omission of the rounding mode (not a trivial omission, granted), the description is pretty much spot on I think.

???

The Mod operator returns the "whole number" remainder after division... there is never a fractional result (the Banker's Rounding before performing the Mod operation insures that). The Mod operation is like how you first learned division (before you learned about decimal places)... 19 divided by 5 is 3 with a remainder of 4... the remainder of 4 is what the Mod function returns.

My bad...wasn't thinking straight. Was thinking of the other way 'round, the why of which I can't explain... :(

--
.



Relevant Pages

  • Re: Decimal Display
    ... Rick Rothstein wrote: and number containing fractions BEFORE it performs the Mod operations. ... The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. ... Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number. ...
    (microsoft.public.vb.general.discussion)
  • Re: VHDL real numbers
    ... The rule rounding has to do with the bottom bit of the integer portion. ... If the remainder is greater than 0.5, round up ... This is the functionality of "round nearest", which should be the default rounding style for all floating point numbers. ...
    (comp.lang.vhdl)
  • Re: Calculating Moon Phases
    ... The modulus, or remainder, operator divides number1 by number2 (rounding ... floating-point numbers to integers) and returns only the remainder as result. ... Note the comment about the rounding so essentially, ...
    (microsoft.public.vb.general.discussion)
  • Re: Decimal Display
    ... and number containing fractions BEFORE it performs the Mod operations. ... The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. ... Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number. ...
    (microsoft.public.vb.general.discussion)
  • Re: Decimal Display
    ... Rick Rothstein wrote: and number containing fractions BEFORE it performs the Mod operations. ... The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. ... Usually, the data type of result is a Byte, Byte variant, Integer, Integer variant, Long, or Variant containing a Long, regardless of whether or not result is a whole number. ...
    (microsoft.public.vb.general.discussion)