----Original Message----
>From: Brian Dessent
>Sent: 30 June 2005 16:58

> Dave Korn wrote:
> 
>>   Absolutely, there's a rounding error of some sort.  Compare the
>> difference when compiling the testcase with -mno-cygwin (i.e. using
>> mingw maths lib): 
> 
> Isn't this just a case of the Cygwin math library choosing "round to
> even" and the MSVCRT/mingw library choosing "0.5 always rounds up"?  

  No, I really don't think that's the case, in fact I reckon they both
default to the same roundingmode, and the discrepancy remains the same in
any rounding mode.  Have a go with the attached version of the testcase,
which allows you to choose any rounding mode you like at runtime: just pass
a hex constant from the set (0, 0x400, 0x800, 0xc00) on the command line.

    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....
#include <stdio.h>
#include <math.h>

#ifdef __MINGW32__
#include <fenv.h>
#endif

#ifndef __MINGW32__

#define FE_TONEAREST    0x0000
#define FE_DOWNWARD     0x0400
#define FE_UPWARD       0x0800
#define FE_TOWARDZERO   0x0c00

 /* 7.6.3.2
    The fesetround function establishes the rounding direction
    represented by its argument round. If the argument is not equal
    to the value of a rounding direction macro, the rounding direction
    is not changed.  */

int fesetround (int mode)
{
  unsigned short _cw;
  if ((mode & ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO))
      != 0)
    return -1;
  __asm__ volatile ("fnstcw %0;": "=m" (_cw));
  _cw &= ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO);
  _cw |= mode;
  __asm__ volatile ("fldcw %0;" : : "m" (_cw));
  return 0;
}

/* 7.6.3.1
   The fegetround function returns the value of the rounding direction
   macro representing the current rounding direction.  */

int
fegetround (void)
{
  unsigned short _cw;
  __asm__ ("fnstcw %0;" : "=m" (_cw));
  return _cw
          & (FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO);
}
#endif

int main(int argc, const char **argv)
{
    if (argc >= 2)
    {
    int nrm, n;
        n = sscanf (argv[1], "%i", &nrm);
        if (n == 1) switch (nrm)
        {
            case 0:
            case 0x400:
            case 0x800:
            case 0xc00:
                fesetround (nrm);
        }
    }

    printf ("Rmode $%08x\n", fegetround ());

    printf("%0.2f\n", 0.105);
    printf("%0.2f\n", 0.115);
    printf("%0.2f\n", 0.125);
    printf("%0.2f\n", 0.135);

    return 0;
}


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Reply via email to