public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "JoshuaHopp at web dot de" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug math/15010] New: std::sin produces wrong results when rounding mode set
Date: Thu, 10 Jan 2013 19:08:00 -0000	[thread overview]
Message-ID: <bug-15010-131@http.sourceware.org/bugzilla/> (raw)

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.


             reply	other threads:[~2013-01-10 19:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-10 19:08 JoshuaHopp at web dot de [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-15010-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).