rem and mod

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

rem and mod

Paul Soderlind
Hi all,

rem(0.94,0.01) gives the answer 0.01 (in 3.8.1-4 from mxeoctave.osuv.de, but also in other windows binaries), but I had expected to get 0.  mod is similar.

The reason seems to be related to that fix(0.94/0.01) = 93, since 0.94/0.01 - 94 is evaluated to be -1.4211e-14. Clearly, floating point precision playing tricks. rem(94,1) does the right thing

Is this to be expected? (...matlab gives 0 and Octave''s ) Would be good to know. Thanks

Paul Söderlind
Reply | Threaded
Open this post in threaded view
|

Re: rem and mod

José Luis García Pallero
2014-06-26 12:45 GMT+02:00 Paul Soderlind <[hidden email]>:

> Hi all,
>
> rem(0.94,0.01) gives the answer 0.01 (in 3.8.1-4 from mxeoctave.osuv.de, but
> also in other windows binaries), but I had expected to get 0.  mod is
> similar.
>
> The reason seems to be related to that fix(0.94/0.01) = 93, since 0.94/0.01
> - 94 is evaluated to be -1.4211e-14. Clearly, floating point precision
> playing tricks. rem(94,1) does the right thing
>
> Is this to be expected? (...matlab gives 0 and Octave''s ) Would be good to
> know. Thanks

Octave 3.8.1 compiled by myself in Debian also gives the incorrect 0.01 answer

>
> Paul Söderlind
>
>
>
> --
> View this message in context: http://octave.1599824.n4.nabble.com/rem-and-mod-tp4665069.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-octave



--
*****************************************
José Luis García Pallero
[hidden email]
(o<
/ / \
V_/_
Use Debian GNU/Linux and enjoy!
*****************************************

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: rem and mod

Olaf Till-2
On Thu, Jun 26, 2014 at 03:21:28PM +0200, José Luis García Pallero wrote:

> 2014-06-26 12:45 GMT+02:00 Paul Soderlind <[hidden email]>:
> > Hi all,
> >
> > rem(0.94,0.01) gives the answer 0.01 (in 3.8.1-4 from mxeoctave.osuv.de, but
> > also in other windows binaries), but I had expected to get 0.  mod is
> > similar.
> >
> > The reason seems to be related to that fix(0.94/0.01) = 93, since 0.94/0.01
> > - 94 is evaluated to be -1.4211e-14. Clearly, floating point precision
> > playing tricks. rem(94,1) does the right thing
> >
> > Is this to be expected? (...matlab gives 0 and Octave''s ) Would be good to
> > know. Thanks
>
> Octave 3.8.1 compiled by myself in Debian also gives the incorrect 0.01 answer
According to function xrem() in liboctave/numeric/lo-mappers.h (tip of
default branch), for

xrem (x, y) and q defined as q := x / y,

there is a case distinction for abs(x) <= 1 or > 1. For abs(x) > 1, q
is adjusted to an integer if its calculated deviation from an integer
is less than roughly the accuracy (epsilon * q) with which q is
represented (by a double in this case), which seems ok. But for abs(x)
<= 1, the assumed accuracy of q seems to be just epsilon, not epsilon
* q; whats the reason for this, or what am I seeing wrong? If the case
abs(x) <= 1 were not treated special, the OPs result probably would be
as expected (not tested).

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: rem and mod

Olaf Till-2
On Thu, Jun 26, 2014 at 04:17:03PM +0200, Olaf Till wrote:

> On Thu, Jun 26, 2014 at 03:21:28PM +0200, José Luis García Pallero wrote:
> > 2014-06-26 12:45 GMT+02:00 Paul Soderlind <[hidden email]>:
> > > Hi all,
> > >
> > > rem(0.94,0.01) gives the answer 0.01 (in 3.8.1-4 from mxeoctave.osuv.de, but
> > > also in other windows binaries), but I had expected to get 0.  mod is
> > > similar.
> > >
> > > The reason seems to be related to that fix(0.94/0.01) = 93, since 0.94/0.01
> > > - 94 is evaluated to be -1.4211e-14. Clearly, floating point precision
> > > playing tricks. rem(94,1) does the right thing
> > >
> > > Is this to be expected? (...matlab gives 0 and Octave''s ) Would be good to
> > > know. Thanks
> >
> > Octave 3.8.1 compiled by myself in Debian also gives the incorrect 0.01 answer
>
> According to function xrem() in liboctave/numeric/lo-mappers.h (tip of
> default branch), for
>
> xrem (x, y) and q defined as q := x / y,
>
> there is a case distinction for abs(x) <= 1 or > 1. For abs(x) > 1, q
> is adjusted to an integer if its calculated deviation from an integer
> is less than roughly the accuracy (epsilon * q) with which q is
> represented (by a double in this case), which seems ok. But for abs(x)
> <= 1, the assumed accuracy of q seems to be just epsilon, not epsilon
> * q; whats the reason for this, or what am I seeing wrong? If the case
> abs(x) <= 1 were not treated special, the OPs result probably would be
> as expected (not tested).
I filed a bug report

https://savannah.gnu.org/bugs/index.php?42627

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Matlab test needed (was: Re: rem and mod)

Olaf Till-2
In reply to this post by Paul Soderlind
On Thu, Jun 26, 2014 at 03:45:33AM -0700, Paul Soderlind wrote:
> <snip>
> (...matlab gives 0

Could you, or someone, please check if Matlab gives exactly zero, i.e

  format long

  rem (.94, .01)

Thanks, Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Matlab test needed (was: Re: rem and mod)

siko1056
On Fri, Jun 27, 2014 at 10:41 AM, Olaf Till <[hidden email]> wrote:
On Thu, Jun 26, 2014 at 03:45:33AM -0700, Paul Soderlind wrote:
> <snip>
> (...matlab gives 0

Could you, or someone, please check if Matlab gives exactly zero, i.e

  format long

  rem (.94, .01)

Thanks, Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


Hi Olaf,

for MATLAB 2014a it does:

>> format long
>> a = rem (.94, .01)
a =
     0
>> num2hex (a)
ans =
0000000000000000

Best,
Kai

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab test needed

Paul Söderlind


On 27/06/2014 10:46, Kai Torben Ohlhus wrote:
On Fri, Jun 27, 2014 at 10:41 AM, Olaf Till <[hidden email]> wrote:
On Thu, Jun 26, 2014 at 03:45:33AM -0700, Paul Soderlind wrote:
> <snip>
> (...matlab gives 0

Could you, or someone, please check if Matlab gives exactly zero, i.e

  format long

  rem (.94, .01)

Thanks, Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


Hi Olaf,

for MATLAB 2014a it does:

>> format long
>> a = rem (.94, .01)
a =
     0
>> num2hex (a)
ans =
0000000000000000

Best,
Kai


Confirmed in 2012b and 2014a (Win64).    /Paul S
-- 



----------------------------------------------------------
Paul Söderlind
Swiss Institute for Banking and Finance
University of St. Gallen
Rosenbergstrasse 52
CH-9000 St. Gallen
Switzerland

[hidden email]
https://sites.google.com/site/paulsoderlindecon/home
http://ssrn.com/author=25146
----------------------------------------------------------

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab test needed

Richard Balogh
In reply to this post by Olaf Till-2
Dňa 27.6.2014 10:41 Olaf Till  wrote / napísal(a):

> On Thu, Jun 26, 2014 at 03:45:33AM -0700, Paul Soderlind wrote:
>> <snip>
>> (...matlab gives 0
>
> Could you, or someone, please check if Matlab gives exactly zero, i.e
>
>    format long
>
>    rem (.94, .01)
>
> Thanks, Olaf
>


Matlab7.5.0.342 (R2007b)

>> format long
>> rem (.94, .01)

ans =

      0


Matlab 8.3.0.532 (R2014a)

>> format long
>> rem (.94, .01)

ans =

      0


Richard Balogh

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab test needed (was: Re: rem and mod)

isak.davids
In reply to this post by siko1056

On 27 June 2014 10:46, Kai Torben Ohlhus <[hidden email]> wrote:
On Fri, Jun 27, 2014 at 10:41 AM, Olaf Till <[hidden email]> wrote:
On Thu, Jun 26, 2014 at 03:45:33AM -0700, Paul Soderlind wrote:
> <snip>
> (...matlab gives 0

Could you, or someone, please check if Matlab gives exactly zero, i.e

  format long

  rem (.94, .01)

Thanks, Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


Hi Olaf,

for MATLAB 2014a it does:

>> format long
>> a = rem (.94, .01)
a =
     0
>> num2hex (a)
ans =
0000000000000000

Best,
Kai

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave


Hi,

on MATLAB R2011a 64bit, I get an exact zero.

Cheers,
IDD

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Matlab test needed (was: Re: rem and mod)

Olaf Till-2
Thanks to all!

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (853 bytes) Download Attachment