This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug math/15010] New: std::sin produces wrong results when rounding mode set


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]