help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Switch rounding mode


From: J.Benoist Leger
Subject: Re: Switch rounding mode
Date: Fri, 17 Apr 2009 22:56:03 +0200
User-agent: Thunderbird 2.0.0.19 (X11/20090220)

Przemek Klosowski a écrit :
> In my opinion, the motivation for interval computing results from
> physical indeterminacy of inputs (e.g. temperature measured to within
> .01 degree). Such physical uncertainity intervals are significantly
> larger than numerical effects, so the effect of the rounding mode used
> in calculation should be negligible.

I'm agree, but in some case, there are problems with bounds. And
interval computing theory include these rounding mode. But in physics
I'm agree it should be negligible.
> 
> Having said that, C99 and later floating point library standards 
> provide fesetround() functionality, which changes the rounding mode
> (if supported by the FP hardware, of course). In Octave, you could
> write an .oct wrapper that calls fesetround() before your expression:
> 
> 
> fesetround(FE_DOWNWARD) ;  c.min = a.min + b.min
> fesetround(FE_UPWARD)   ;  c.max = a.max + b.max 
> fesetround(FE_DFL_ENV);
> 

Thanks, this is that I wanted.

Now I can that I want with this code :

setround(-1);
c.min=a+b;
setround(1);
c.max=a+b;

And I provide in enclosed file the source code of setround if others
wants use it. (It's my first octave C++ source code).

Thanks,

-- 
J.Benoist Leger
/* Copyright (c) 2009, J.Benoist Leger <address@hidden>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#include <fenv.h>
#include <octave/oct.h> 

DEFUN_DLD (setround, args, , "-*- texinfo -*-\n\
@deftypefn{Loadable Function} {OLD_MODE =} setround(NEW_MODE)\n\
@cindex setround round rounding\n\
For NEW_MODE = -1 switch rounding towards -inf.\n\n\
For NEW_MODE =  1 switch rounding towards +inf.\n\n\
For NEW_MODE =  0 switch rounding to nearest.\n\
@end deftypefn")
{
    int last = fegetround() ;
    if(args(0).int_value()==1)
        fesetround(FE_UPWARD);
    else if(args(0).int_value()==-1)
        fesetround(FE_DOWNWARD);
    else if(args(0).int_value()==0)
        fesetround(FE_TONEAREST);

    if(last==1024)
        return octave_value(-1);
    else if(last==2048)
        return octave_value(1);
    else
        return octave_value(0);
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]