public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug math/15010] New: std::sin produces wrong results when rounding mode set
@ 2013-01-10 19:08 JoshuaHopp at web dot de
  2013-01-10 19:08 ` [Bug math/15010] " JoshuaHopp at web dot de
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: JoshuaHopp at web dot de @ 2013-01-10 19:08 UTC (permalink / raw)
  To: glibc-bugs

http://sourceware.org/bugzilla/show_bug.cgi?id=15010

             Bug #: 15010
           Summary: std::sin produces wrong results when rounding mode set
           Product: glibc
           Version: 2.15
            Status: NEW
          Severity: normal
          Priority: P2
         Component: math
        AssignedTo: unassigned@sourceware.org
        ReportedBy: JoshuaHopp@web.de
    Classification: Unclassified


If the rounding mode is set (other than FE_TONEAREST), the sine function gives
wrong results for some input values. In fact, the results are not even in the
interval of [-1, 1] but in the dimension of ±1e50. Try this:

//--- snip ---
#include <random>
#include <limits>
#include <cmath>
#include <iostream>
#include <cfenv>

const unsigned ROUNDS = 1000000;

int main () {
    std::fesetround(FE_DOWNWARD); // or FE_UPWARD

    // initialize random number generator
    std::random_device rd;
    std::mt19937 rand(rd());

    // set bounds for input value "x"
    double limit_min = -10;
    double limit_max = 10;
    std::uniform_real_distribution<double> dis(limit_min, limit_max);

    for (int i=0; i<ROUNDS; i++) {

        // create a random number "x"
        double input_value = dis(rand);

        // calculate "sin(x)"
        double output_value = std::sin(input_value);

        // if "sin(x)" is out of bounds, print 
        if (output_value < -1 || output_value > 1) {
            std::cout << "sin(" << input_value << ") != " << output_value <<
std::endl;
        }
    }
    return 0;
}
//--- snap ---

Output:
sin(6.28305) != -1.0016
sin(-6.98816) != 1.91558e+53
sin(-5.40876) != -1.38604e+51
...

Compile with "g++ -std=c++11". Other functions in cmath (such as exp()) might
be affected as well, but harder to test.

----
architecturex86_64
Kernel version: 3.2.0-29-generic
gcc version: gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-11precise2) 
ld version: GNU ld (GNU Binutils for Ubuntu) 2.22

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


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

* [Bug math/15010] std::sin produces wrong results when rounding mode set
  2013-01-10 19:08 [Bug math/15010] New: std::sin produces wrong results when rounding mode set JoshuaHopp at web dot de
@ 2013-01-10 19:08 ` JoshuaHopp at web dot de
  2013-01-10 19:23 ` aj at suse dot de
  2014-06-13 19:04 ` fweimer at redhat dot com
  2 siblings, 0 replies; 4+ messages in thread
From: JoshuaHopp at web dot de @ 2013-01-10 19:08 UTC (permalink / raw)
  To: glibc-bugs

http://sourceware.org/bugzilla/show_bug.cgi?id=15010

Joshua Hopp <JoshuaHopp at web dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64
               Host|                            |x86_64

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


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

* [Bug math/15010] std::sin produces wrong results when rounding mode set
  2013-01-10 19:08 [Bug math/15010] New: std::sin produces wrong results when rounding mode set JoshuaHopp at web dot de
  2013-01-10 19:08 ` [Bug math/15010] " JoshuaHopp at web dot de
@ 2013-01-10 19:23 ` aj at suse dot de
  2014-06-13 19:04 ` fweimer at redhat dot com
  2 siblings, 0 replies; 4+ messages in thread
From: aj at suse dot de @ 2013-01-10 19:23 UTC (permalink / raw)
  To: glibc-bugs

http://sourceware.org/bugzilla/show_bug.cgi?id=15010

Andreas Jaeger <aj at suse dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |aj at suse dot de
         Resolution|                            |DUPLICATE

--- Comment #1 from Andreas Jaeger <aj at suse dot de> 2013-01-10 19:22:58 UTC ---
I run your program with glibc 2.17 and this seems to be fixed:
aj@byrd:/tmp> g++ t.cc -lm -O2 -std=gnu++11
aj@byrd:/tmp> ./a.out 
aj@byrd:/tmp> 

This should have been fixed already for glibc 2.16 - it's a duplicate of bug
3976.

*** This bug has been marked as a duplicate of bug 3976 ***

-- 
Configure bugmail: http://sourceware.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.


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

* [Bug math/15010] std::sin produces wrong results when rounding mode set
  2013-01-10 19:08 [Bug math/15010] New: std::sin produces wrong results when rounding mode set JoshuaHopp at web dot de
  2013-01-10 19:08 ` [Bug math/15010] " JoshuaHopp at web dot de
  2013-01-10 19:23 ` aj at suse dot de
@ 2014-06-13 19:04 ` fweimer at redhat dot com
  2 siblings, 0 replies; 4+ messages in thread
From: fweimer at redhat dot com @ 2014-06-13 19:04 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=15010

Florian Weimer <fweimer at redhat dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
              Flags|                            |security-

-- 
You are receiving this mail because:
You are on the CC list for the bug.


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

end of thread, other threads:[~2014-06-13 19:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-10 19:08 [Bug math/15010] New: std::sin produces wrong results when rounding mode set JoshuaHopp at web dot de
2013-01-10 19:08 ` [Bug math/15010] " JoshuaHopp at web dot de
2013-01-10 19:23 ` aj at suse dot de
2014-06-13 19:04 ` fweimer at redhat dot com

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).