<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678</link>
    <title>MATLAB Central Newsreader - bug in rem</title>
    <description>Feed for thread: bug in rem</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 31 Jul 2007 19:55:35 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385771</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;46ae685a$1@news.eftel.com.au&amp;gt;, ross  &amp;lt;ross@nospam.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;rem and mod should not, by definition, return values that exceed the magnitude &lt;br&gt;
&amp;gt;of the modulus or radix, and the software should be written to reflect that.&lt;br&gt;
&amp;gt;This is an "absolute" consideration.  IMO the software is broken if it doesn't &lt;br&gt;
&amp;gt;conform to the expected mathematical behaviour, when that is possible.&lt;br&gt;
&lt;br&gt;
The documentation for rem and mod do not indicate that the functions&lt;br&gt;
are the abstract algebraic functions of mathematics. Instead,&lt;br&gt;
the documentation gives explicit formulae for how the result is&lt;br&gt;
produced, and those explicit formulae lead to the results you observed.&lt;br&gt;
&lt;br&gt;
This is no different than understanding that the '+' operator&lt;br&gt;
does not implement the abstract algebraic addition of mathematics.&lt;br&gt;
Operators in a computer language are defined by their documented&lt;br&gt;
behaviour, not by the behaviour of mathematical constructs&lt;br&gt;
with similar names. Sometimes the difference is startling,&lt;br&gt;
but I don't think it rises to the level of,&lt;br&gt;
'This is an "absolute" consideration.'.&lt;br&gt;
&lt;br&gt;
If you need a language with complete abstract mathematical fidelity&lt;br&gt;
or one in which all computations are arranged to maximize&lt;br&gt;
retention of precision, then Matlab is not the right language&lt;br&gt;
for your purposes.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;Is there any thing whereof it may be said, See, this is new? It hath&lt;br&gt;
&amp;nbsp;&amp;nbsp;been already of old time, which was before us.       -- Ecclesiastes&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 31 Jul 2007 06:38:18 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385605</link>
      <author>ross</author>
      <description>"us " &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt;ross:&lt;br&gt;
&amp;gt;&amp;lt;SNIP FP headache evergreen...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; x = 1.5511623754564467e105;&lt;br&gt;
&amp;gt;&amp;gt; r = rem(x,10)&lt;br&gt;
&amp;gt;&amp;gt; r = 2.54629497041811e+089&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;...should not come as a surprise, because your &amp;lt;x&amp;gt; is represented with the &lt;br&gt;
&amp;gt;uncertainty of&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;     eps(x)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;that happens to be ---&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;     2.546294970418108e+089&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;us&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Look, the round-off error isn't the issue. You will note that I said "whatever &lt;br&gt;
the cause".  That phrase was meant to indicate that I know that the value &lt;br&gt;
returned is due to roundoff error, but the real problem is different.  In fact &lt;br&gt;
the real problem is that a rem routine should never return that value.  This is &lt;br&gt;
not an evergreen newbie problem, it's a comment on the inadequacy of the &lt;br&gt;
programming.&lt;br&gt;
&lt;br&gt;
rem and mod should not, by definition, return values that exceed the magnitude &lt;br&gt;
of the modulus or radix, and the software should be written to reflect that.&lt;br&gt;
This is an "absolute" consideration.  IMO the software is broken if it doesn't &lt;br&gt;
conform to the expected mathematical behaviour, when that is possible. In other &lt;br&gt;
words, if I want to express pi to 100 sig figs in a Matlab double, it can't be &lt;br&gt;
done.   However it is entirely possible to ensure that a remainder doesn't &lt;br&gt;
exceed the divisor, and it absolutely should be done.&lt;br&gt;
&lt;br&gt;
Secondly, I believe that there is a more "relative" consideration, which is &lt;br&gt;
that the numerical routines should protect the user from the effects of &lt;br&gt;
round-off error where possible. None of us disagree that one should understand &lt;br&gt;
the effects of round off error in order to program in an informed way.  &lt;br&gt;
However, it is absurd to have to put all the error trapping code into every &lt;br&gt;
application routine.  It should be in the basic numerical functions.  There are &lt;br&gt;
many applications where certain inputs might generate high disparaties in the &lt;br&gt;
values at some point in the routine.  The routine should not crash at that &lt;br&gt;
point.  It's true that if one does a careful error analysis one might expect &lt;br&gt;
those situations.  But this is not the 1950s anymore guys - the user can expect &lt;br&gt;
to have routines that dealt with out-of-range cases gracefully, not by &lt;br&gt;
returning absurd values.  If you don't include sensible error trapping in the &lt;br&gt;
routines, you force the user to be watching the details all the time instead of &lt;br&gt;
concentrating on the main objectives.  It's the old mainframe mentality where &lt;br&gt;
the programming issues were allowed to take precedence over what was important &lt;br&gt;
to the user, which was why programming was such a black art then.  It's very &lt;br&gt;
important to get away from this oldfashioned point of view.  (And for God's &lt;br&gt;
sake, before you send a bunch of irrelevant flames -- I am not arguing that an &lt;br&gt;
understanding of round-off error is oldfashioned).&lt;br&gt;
&lt;br&gt;
Ross&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 31 Jul 2007 04:23:58 -0400</pubDate>
      <title>bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385588</link>
      <author>ross</author>
      <description>Can you confirm the following (R2007a):&lt;br&gt;
&lt;br&gt;
x = 1.5511623754564467e105;&lt;br&gt;
r = rem(x,10)&lt;br&gt;
&lt;br&gt;
r  =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.54629497041811e+089&lt;br&gt;
&lt;br&gt;
If you can, there is a bug in rem( ), since the result should never be &amp;gt; 10, &lt;br&gt;
whatever the cause.&lt;br&gt;
&lt;br&gt;
ross&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 31 Jul 2007 01:08:27 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385615</link>
      <author>David Wilson</author>
      <description>ross &amp;lt;ross@nospam.com&amp;gt; wrote in message &amp;lt;46ae685a$1@news.eftel.com.au&amp;gt;...&lt;br&gt;
&amp;gt; "us " &amp;lt;us@neurol.unizh.ch&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;ross:&lt;br&gt;
&amp;gt; &amp;gt;&amp;lt;SNIP FP headache evergreen...&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; x = 1.5511623754564467e105;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; r = rem(x,10)&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; r = 2.54629497041811e+089&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;...should not come as a surprise, because your &amp;lt;x&amp;gt; is represented with the &lt;br&gt;
&amp;gt; &amp;gt;uncertainty of&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;     eps(x)&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;that happens to be ---&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;     2.546294970418108e+089&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;us&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Look, the round-off error isn't the issue. You will note that I said "whatever &lt;br&gt;
&amp;gt; the cause".  That phrase was meant to indicate that I know that the value &lt;br&gt;
&amp;gt; returned is due to roundoff error, but the real problem is different.  In fact &lt;br&gt;
&amp;gt; the real problem is that a rem routine should never return that value.  This is &lt;br&gt;
&amp;gt; not an evergreen newbie problem, it's a comment on the inadequacy of the &lt;br&gt;
&amp;gt; programming.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rem and mod should not, by definition, return values that exceed the magnitude &lt;br&gt;
&amp;gt; of the modulus or radix, and the software should be written to reflect that.&lt;br&gt;
&amp;gt; This is an "absolute" consideration.  IMO the software is broken if it doesn't &lt;br&gt;
&amp;gt; conform to the expected mathematical behaviour, when that is possible. In other &lt;br&gt;
&amp;gt; words, if I want to express pi to 100 sig figs in a Matlab double, it can't be &lt;br&gt;
&amp;gt; done.   However it is entirely possible to ensure that a remainder doesn't &lt;br&gt;
&amp;gt; exceed the divisor, and it absolutely should be done.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Secondly, I believe that there is a more "relative" consideration, which is &lt;br&gt;
&amp;gt; that the numerical routines should protect the user from the effects of &lt;br&gt;
&amp;gt; round-off error where possible. None of us disagree that one should understand &lt;br&gt;
&amp;gt; the effects of round off error in order to program in an informed way.  &lt;br&gt;
&amp;gt; However, it is absurd to have to put all the error trapping code into every &lt;br&gt;
&amp;gt; application routine.  It should be in the basic numerical functions.  There are &lt;br&gt;
&amp;gt; many applications where certain inputs might generate high disparaties in the &lt;br&gt;
&amp;gt; values at some point in the routine.  The routine should not crash at that &lt;br&gt;
&amp;gt; point.  It's true that if one does a careful error analysis one might expect &lt;br&gt;
&amp;gt; those situations.  But this is not the 1950s anymore guys - the user can expect &lt;br&gt;
&amp;gt; to have routines that dealt with out-of-range cases gracefully, not by &lt;br&gt;
&amp;gt; returning absurd values.  If you don't include sensible error trapping in the &lt;br&gt;
&amp;gt; routines, you force the user to be watching the details all the time instead of &lt;br&gt;
&amp;gt; concentrating on the main objectives.  It's the old mainframe mentality where &lt;br&gt;
&amp;gt; the programming issues were allowed to take precedence over what was important &lt;br&gt;
&amp;gt; to the user, which was why programming was such a black art then.  It's very &lt;br&gt;
&amp;gt; important to get away from this oldfashioned point of view.  (And for God's &lt;br&gt;
&amp;gt; sake, before you send a bunch of irrelevant flames -- I am not arguing that an &lt;br&gt;
&amp;gt; understanding of round-off error is oldfashioned).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ross&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
While your argument is well constructed regarding the fact that rem(a,b) should never return a value outside [0,b-1], &lt;br&gt;
the problem remains that what, in your opinion, should rem then return in your somewhat extreme example? &lt;br&gt;
&lt;br&gt;
We should not really return NaN or Inf simply because we can't compute it (&amp; it is already used), and we cannot really return anything in the range [0,b-1] since that seems to indicate that that is infact the answer. We could throw up a warning I suppose, but that's not done in other similar overflow cases. &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 31 Jul 2007 00:04:38 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385610</link>
      <author>ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)</author>
      <description>In article &amp;lt;46ae685a$1@news.eftel.com.au&amp;gt;, ross &amp;lt;ross@nospam.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Look, the round-off error isn't the issue. You will note that I said&lt;br&gt;
"whatever &lt;br&gt;
&amp;gt; the cause".  That phrase was meant to indicate that I know that the value &lt;br&gt;
&amp;gt; returned is due to roundoff error, but the real problem is different. &lt;br&gt;
In fact &lt;br&gt;
&amp;gt; the real problem is that a rem routine should never return that value. &lt;br&gt;
This is &lt;br&gt;
&amp;gt; not an evergreen newbie problem, it's a comment on the inadequacy of the &lt;br&gt;
&amp;gt; programming.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rem and mod should not, by definition, return values that exceed the&lt;br&gt;
magnitude &lt;br&gt;
&amp;gt; of the modulus or radix, and the software should be written to reflect that.&lt;br&gt;
&amp;gt; This is an "absolute" consideration.  IMO the software is broken if it&lt;br&gt;
doesn't &lt;br&gt;
&amp;gt; conform to the expected mathematical behaviour, when that is possible.&lt;br&gt;
In other &lt;br&gt;
&amp;gt; words, if I want to express pi to 100 sig figs in a Matlab double, it&lt;br&gt;
can't be &lt;br&gt;
&amp;gt; done.   However it is entirely possible to ensure that a remainder doesn't &lt;br&gt;
&amp;gt; exceed the divisor, and it absolutely should be done.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Secondly, I believe that there is a more "relative" consideration, which is &lt;br&gt;
&amp;gt; that the numerical routines should protect the user from the effects of &lt;br&gt;
&amp;gt; round-off error where possible. None of us disagree that one should&lt;br&gt;
understand &lt;br&gt;
&amp;gt; the effects of round off error in order to program in an informed way.  &lt;br&gt;
&amp;gt; However, it is absurd to have to put all the error trapping code into every &lt;br&gt;
&amp;gt; application routine.  It should be in the basic numerical functions. &lt;br&gt;
There are &lt;br&gt;
&amp;gt; many applications where certain inputs might generate high disparaties in the &lt;br&gt;
&amp;gt; values at some point in the routine.  The routine should not crash at that &lt;br&gt;
&amp;gt; point.  It's true that if one does a careful error analysis one might expect &lt;br&gt;
&amp;gt; those situations.  But this is not the 1950s anymore guys - the user can&lt;br&gt;
expect &lt;br&gt;
&amp;gt; to have routines that dealt with out-of-range cases gracefully, not by &lt;br&gt;
&amp;gt; returning absurd values.  If you don't include sensible error trapping in the &lt;br&gt;
&amp;gt; routines, you force the user to be watching the details all the time&lt;br&gt;
instead of &lt;br&gt;
&amp;gt; concentrating on the main objectives.  It's the old mainframe mentality where &lt;br&gt;
&amp;gt; the programming issues were allowed to take precedence over what was&lt;br&gt;
important &lt;br&gt;
&amp;gt; to the user, which was why programming was such a black art then.  It's very &lt;br&gt;
&amp;gt; important to get away from this oldfashioned point of view.  (And for God's &lt;br&gt;
&amp;gt; sake, before you send a bunch of irrelevant flames -- I am not arguing&lt;br&gt;
that an &lt;br&gt;
&amp;gt; understanding of round-off error is oldfashioned).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Ross&lt;br&gt;
--------------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;There is an old computer adage that is pertinent here, Ross.  "Garbage&lt;br&gt;
In, Garbage Out".  And, given the 53-bit accuracy limitation of IEEE&lt;br&gt;
binary double precision floating points numbers, "garbage" is precisely&lt;br&gt;
what you have put into the 'rem' function input when you write the absurd&lt;br&gt;
expression rem(1.5511623754564467e105,10)!  Even with the very first step&lt;br&gt;
of finding the nearest representable number to that decimal string, an&lt;br&gt;
integer in this case, the correct answer for the remainder with ten as&lt;br&gt;
divisor will have been irretrievably lost.  IEEE 754 binary numbers almost&lt;br&gt;
certainly cannot represent 1.5511623754564467e105 exactly even if it is an&lt;br&gt;
integer with 89 trailing zeros.  Matlab has to select the nearest integer&lt;br&gt;
it can realize and the difference in this case will be astronomically&lt;br&gt;
large, which will of course change the remainder completely.  Would you&lt;br&gt;
rather the 'rem' function issue the message, "You've to be kidding,&lt;br&gt;
fella!"?  In my opinion it would almost be justified.  The lesson to be&lt;br&gt;
learned here is that there is no substitute for using the little gray&lt;br&gt;
cells when it comes to writing good programs.  Matlab, and indeed all such&lt;br&gt;
numerical computation programs, simply won't do all your thinking for you&lt;br&gt;
- perhaps by the next millennium, but certainly not yet.  You as a&lt;br&gt;
programmer have to be constantly aware of such limitations when dealing&lt;br&gt;
with a finite set of numbers and attempting to approximate the&lt;br&gt;
mathematical ideal of the infinite continuum of real numbers.  If you do&lt;br&gt;
otherwise, you put yourself in the same class as people who ask&lt;br&gt;
indignantly why 100*.07==7 (or numerous similar equalities) come out false&lt;br&gt;
in matlab.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Mon, 30 Jul 2007 20:44:26 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385590</link>
      <author>us</author>
      <description>ross:&lt;br&gt;
&amp;lt;SNIP FP headache evergreen...&lt;br&gt;
&lt;br&gt;
&amp;gt; x = 1.5511623754564467e105;&lt;br&gt;
&amp;gt; r = rem(x,10)&lt;br&gt;
&amp;gt; r = 2.54629497041811e+089&lt;br&gt;
&lt;br&gt;
...should not come as a surprise, because your &amp;lt;x&amp;gt; is represented with the uncertainty of&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;eps(x)&lt;br&gt;
&lt;br&gt;
that happens to be ---&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2.546294970418108e+089&lt;br&gt;
&lt;br&gt;
us&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Mon, 30 Jul 2007 20:35:18 -0400</pubDate>
      <title>Re: bug in rem</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/153678#385589</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;46ae48de$1@news.eftel.com.au&amp;gt;, ross  &amp;lt;ross@nospam.com&amp;gt; wrote:&lt;br&gt;
&amp;gt;Can you confirm the following (R2007a):&lt;br&gt;
&lt;br&gt;
&amp;gt;x = 1.5511623754564467e105;&lt;br&gt;
&amp;gt;r = rem(x,10)&lt;br&gt;
&lt;br&gt;
&amp;gt;r  =&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;     2.54629497041811e+089&lt;br&gt;
&lt;br&gt;
&amp;gt;If you can, there is a bug in rem( ), since the result should never be &amp;gt; 10, &lt;br&gt;
&amp;gt;whatever the cause.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; help rem&lt;br&gt;
&amp;nbsp;REM    Remainder after division.&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;REM(x,y) is x - n.*y where n = fix(x./y) if y ~= 0.  If y is not an&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;integer and the quotient x./y is within roundoff error of an integer,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;then n is that integer. The inputs x and y must be real arrays of the&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;same size, or real scalars.&lt;br&gt;
&lt;br&gt;
If the inputs are floating point, then you are dealing with the&lt;br&gt;
limits of floating point representation. There simply aren't enough&lt;br&gt;
bits available in a Matlab float to calculate the answer properly.&lt;br&gt;
&lt;br&gt;
1.5511623754564467e105 does not have enough bits to represent&lt;br&gt;
right down to the units position. 1.5511623754564467e105 is&lt;br&gt;
really 1.5511623754564467e105 +/- one part in 2^(-53)&lt;br&gt;
which is 1.5511623754564467e105 +/- 1.722136184164e+89 or so.&lt;br&gt;
The answer you got back was within margin of error for the&lt;br&gt;
question.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;"law -- it's a commodity"&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Andrew Ryan (The Globe and Mail, 2005/11/26)&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
