## digitalmars.D - modulus redux

- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 12 2009
- TomD <t_demmer nospam.web.de> Jul 12 2009
- Don <nospam nospam.com> Jul 13 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 13 2009
- Walter Bright <newshound1 digitalmars.com> Jul 13 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 13 2009
- Walter Bright <newshound1 digitalmars.com> Jul 13 2009
- Walter Bright <newshound1 digitalmars.com> Jul 13 2009
- Don <nospam nospam.com> Jul 14 2009
- Walter Bright <newshound1 digitalmars.com> Jul 14 2009
- Michiel Helvensteijn <m.helvensteijn.remove gmail.com> Jul 13 2009
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jul 13 2009
- "Steven Schveighoffer" <schveiguy yahoo.com> Jul 13 2009

Ok, here's how I rewrote the section on multiplicative operations. The text is hardly intelligible due to all formatting, sorry about that (but it looks much better in a specialized editor). Comments and suggestions welcome. \subsection{Multiplicative Expressions} The multiplicative expressions are multiplication (\ccbox{a * b}), division (\ccbox{a / b}), and remainder (\ccbox{a \% b}). They operate on numeric types only. The result type of either of these operations is same as the type of \ccbox{true ? a : b} (see~\S~\ref{sec:conditional-operator}). If~ b is zero in the integral operation \ccbox{a / b} or \ccbox{a \% b}, a hardware exception is thrown. If the division would yield a fractional number, it is always truncated towards zero (for example, \ccbox{7 / 3} yields~ 2 and \ccbox{-7 / 3} yields~ -2 ). The expression \ccbox{a \% b} is defined such that \cc{a == (a / b) * b + a \% b}, so \ccbox{7 \% 3} yields~ 1 and \ccbox{-7 / 3} yields~ -1 . \dee also defines modulus for floating-point numbers. The definition is more involved. When at least one of a and b is a floating-point value in \cc{a \% b}, the result is the largest (in absolute value) floating-point number r satisfying the following conditions: \begin{itemize*} \item a and r do not have opposite signs; \item r is smaller than b in absolute value, \ccbox{abs(r) < abs(b)}; \item there exists a number q of type long such that \ccbox{r == a - q * b}. \end{itemize*} If such a number cannot be found, \ccbox{a \% b} yields the Not A Number (NaN) special value. Andrei

Jul 12 2009

Andrei Alexandrescu Wrote: [...]The multiplicative expressions are multiplication (\ccbox{a * b}), division (\ccbox{a / b}), and remainder (\ccbox{a \% b}). They operate on numeric types only. The result type of either of these operations is same as the type of \ccbox{true ? a : b} (see~\S~\ref{sec:conditional-operator}).

Isn't (true?a:b) always (a)? TomD

Jul 12 2009

TomD wrote:Andrei Alexandrescu Wrote: [...]The multiplicative expressions are multiplication (\ccbox{a * b}), division (\ccbox{a / b}), and remainder (\ccbox{a \% b}). They operate on numeric types only. The result type of either of these operations is same as the type of \ccbox{true ? a : b} (see~\S~\ref{sec:conditional-operator}).

Isn't (true?a:b) always (a)?

Yes, but the type of the expression may be different from the type of a. Andrei

Jul 12 2009

Andrei Alexandrescu wrote:Ok, here's how I rewrote the section on multiplicative operations. The text is hardly intelligible due to all formatting, sorry about that (but it looks much better in a specialized editor). Comments and suggestions welcome. \subsection{Multiplicative Expressions} The multiplicative expressions are multiplication (\ccbox{a * b}), division (\ccbox{a / b}), and remainder (\ccbox{a \% b}). They operate on numeric types only. The result type of either of these operations is same as the type of \ccbox{true ? a : b} (see~\S~\ref{sec:conditional-operator}). If~ b is zero in the integral operation \ccbox{a / b} or \ccbox{a \% b}, a hardware exception is thrown. If the division would yield a fractional number, it is always truncated towards zero (for example, \ccbox{7 / 3} yields~ 2 and \ccbox{-7 / 3} yields~ -2 ). The expression \ccbox{a \% b} is defined such that \cc{a == (a / b) * b + a \% b}, so \ccbox{7 \% 3} yields~ 1 and \ccbox{-7 / 3} yields~ -1 . \dee also defines modulus for floating-point numbers. The definition is more involved. When at least one of a and b is a floating-point value in \cc{a \% b}, the result is the largest (in absolute value) floating-point number r satisfying the following conditions: \begin{itemize*} \item a and r do not have opposite signs; \item r is smaller than b in absolute value, \ccbox{abs(r) < abs(b)}; \item there exists a number q of type long such that \ccbox{r == a - q * b}. \end{itemize*} If such a number cannot be found, \ccbox{a \% b} yields the Not A Number (NaN) special value. Andrei

Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long'). In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

Jul 13 2009

Don wrote:Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long').

But if real is 79-bit long (as on Intel), the largest integer that could fit without loss in 1 << 63, and that would fit in a long. Are you saying r could spill into large integers that cannot be represented without loss?In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

I take it D does not define a % b the IEEE 754 way (that's why I eliminated that mention). Is that correct? Andrei

Jul 13 2009

Andrei Alexandrescu wrote:Don wrote:Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long').

But if real is 79-bit long (as on Intel), the largest integer that could fit without loss in 1 << 63, and that would fit in a long. Are you saying r could spill into large integers that cannot be represented without loss?

The definition is without regard to the size of integral types. It only means "integer".In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

I take it D does not define a % b the IEEE 754 way (that's why I eliminated that mention). Is that correct?

No, it is defined as fmod, which is IEEE754 %. In fact, it is quite literally implemented by the FPREM instruction which is the same used for fmod(). Hmm, I just noticed that the code generator should use FPREM1 instead to get IEEE conformance. Darn. http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc108.htm http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc109.htm

Jul 13 2009

Walter Bright wrote:Andrei Alexandrescu wrote:Don wrote:Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long').

But if real is 79-bit long (as on Intel), the largest integer that could fit without loss in 1 << 63, and that would fit in a long. Are you saying r could spill into large integers that cannot be represented without loss?

The definition is without regard to the size of integral types. It only means "integer".In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

I take it D does not define a % b the IEEE 754 way (that's why I eliminated that mention). Is that correct?

No, it is defined as fmod, which is IEEE754 %. In fact, it is quite literally implemented by the FPREM instruction which is the same used for fmod(). Hmm, I just noticed that the code generator should use FPREM1 instead to get IEEE conformance. Darn. http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/ins ruct32_hh/vc108.htm http://www.sesp.cse.clrc.ac.uk/html/SoftwareTools/vtune/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/ins ruct32_hh/vc109.htm

http://d.puremagic.com/issues/show_bug.cgi?id=3171 What are friends for? Andrei

Jul 13 2009

Andrei Alexandrescu wrote:http://d.puremagic.com/issues/show_bug.cgi?id=3171 What are friends for?

Ridiculing the cars my friends drive, of course!

Jul 13 2009

Don wrote:Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long'). In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

I hope you don't mind if I cut & paste this into the spec pages!

Jul 13 2009

Walter Bright wrote:Don wrote:Close, but that's technically not true in the case where abs(a/b) > long.max. (The integer doesn't have to fit into a 'long'). In IEEE754, r= a % b is defined by the mathematical relation r = a – b * n , where n is the integer nearest the exact number a/b ; whenever abs( n – a/b) = 0.5 , then n is even. If r == 0 , its sign is the same as a.

I hope you don't mind if I cut & paste this into the spec pages!

I've just put a comment on bug 3171, I'm not sure that we really want IEEE behaviour. It obeys a == b * nearbyint(a/b) + a % b, but...

Jul 14 2009

Don wrote:I've just put a comment on bug 3171, I'm not sure that we really want IEEE behaviour. It obeys a == b * nearbyint(a/b) + a % b, but...

I saw that. Wild. But I think we should conform to IEEE behavior, even if it seems strange.

Jul 14 2009

Andrei Alexandrescu wrote:The multiplicative expressions are multiplication (\ccbox{a * b}), division (\ccbox{a / b}), and remainder (\ccbox{a \% b}). They operate on numeric types only. The result type of either of these operations is same as the type of \ccbox{true ? a : b} (see~\S~\ref{sec:conditional-operator}).

"either of" means "one of two" or "both of two". You're talking about three operations, so I'd leave out "either of". Also, while the (true ? a : b) thing may be true, is this really the clearest way to explain? Perhaps you should define a term to mean "common type of two operands" and use that to explain both typeof(a mulop b) and typeof(true ? a : b).... If such a number cannot be found, \ccbox{a \% b} yields the Not A Number (NaN) special value.

When is this? If b == 0? If b == NaN? Perhaps it would be better to be precise. -- Michiel Helvensteijn

Jul 13 2009

Michiel Helvensteijn wrote:Andrei Alexandrescu wrote:

"either of" means "one of two" or "both of two". You're talking about three operations, so I'd leave out "either of".

Ok, thanks.Also, while the (true ? a : b) thing may be true, is this really the clearest way to explain? Perhaps you should define a term to mean "common type of two operands" and use that to explain both typeof(a mulop b) and typeof(true ? a : b).

Turns out that defining the common type is rather involved. So I take time in the section dedicated to ?: and then I refer to it.... If such a number cannot be found, \ccbox{a \% b} yields the Not A Number (NaN) special value.

When is this? If b == 0? If b == NaN? Perhaps it would be better to be precise.

Oh there are quite a few situations, see e.g. http://msdn.microsoft.com/en-us/library/aa244368%28VS.60%29.aspx. I will add a few examples. Andrei

Jul 13 2009

On Mon, 13 Jul 2009 08:51:06 -0400, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> wrote:Michiel Helvensteijn wrote:Andrei Alexandrescu wrote:

three operations, so I'd leave out "either of".

Ok, thanks.

any of? -Steve

Jul 13 2009