public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* incorrectly rounded square root
@ 2021-05-04  8:08 Paul Zimmermann
  2021-05-31 20:52 ` Jeff Johnston
  0 siblings, 1 reply; 19+ messages in thread
From: Paul Zimmermann @ 2021-05-04  8:08 UTC (permalink / raw)
  To: newlib

       Hi,

according to IEEE 754, the square root function should be correctly rounded
for all rounding modes. I noticed this is not the case in Newlib:

$ cat test_sqrt.c
#include <stdio.h>
#include <math.h>
#include <fenv.h>

#ifdef NEWLIB
int errno;
int* __errno () { return &errno; }
#endif

int main()
{
  int rnd[4] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD };
  char Rnd[4] = "NZUD";
  float x = 0x1.ff07fep+127f;
  float y;
  for (int i = 0; i < 4; i++)
  {
    fesetround (rnd[i]);
    y = sqrtf (x);
    printf ("RND%c: %a\n", Rnd[i], y);
  }
}

$ gcc -DNEWLIB -fno-builtin test_sqrt.c /localdisk/zimmerma/newlib-4.1.0/libm.a -lm
$ ./a.out
RNDN: 0x1.ff83fp+63
RNDZ: 0x1.ff83fp+63
RNDU: 0x1.ff83fp+63
RNDD: 0x1.ff83fp+63

The RNDZ and RNDD results are wrong. With glibc I get:

$ gcc -fno-builtin test_sqrt.c -lm
$ ./a.out
RNDN: 0x1.ff83fp+63
RNDZ: 0x1.ff83eep+63
RNDU: 0x1.ff83fp+63
RNDD: 0x1.ff83eep+63

Best regards,
Paul Zimmermann


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2021-06-12 22:31 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-04  8:08 incorrectly rounded square root Paul Zimmermann
2021-05-31 20:52 ` Jeff Johnston
2021-06-01  7:11   ` Paul Zimmermann
2021-06-01 16:28     ` Jeff Johnston
2021-06-02  7:51       ` Paul Zimmermann
2021-06-02 13:07         ` Joel Sherrill
2021-06-02 18:43           ` Jeff Johnston
2021-06-02 19:07             ` Marco Atzeri
2021-06-02 19:12               ` Joel Sherrill
2021-06-03  3:01                 ` Jeff Johnston
2021-06-03 10:21                   ` Paul Zimmermann
2021-06-03 15:50                     ` Jeff Johnston
2021-06-04  7:14                       ` Paul Zimmermann
2021-06-04 18:44                         ` Jeff Johnston
2021-06-04 18:59                           ` Joel Sherrill
2021-06-05 13:25                           ` Brian Inglis
2021-06-07  9:51                             ` Paul Zimmermann
2021-06-12 22:31                           ` Maciej W. Rozycki
2021-06-03 11:25               ` Paul Zimmermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).