seltsames resultat



  • hi

    #include <cmath>
    #include <iostream>
    #include <ostream>
    using namespace std;
    class vec2
    {
      float components[2];
    public:
      vec2(float x, float y) : x(components[0]), y(components[1]){components[0] = x;components[1] = y;}
      float& x,& y;
      float get_lenght() const
      {
        return sqrt(pow(components[0], 2) + pow(components[1], 2));
      }
      void set_angle(float angle)
      {
        float lenght_backup = get_lenght();
        components[0] = sin(angle) * lenght_backup;
        components[1] = cos(angle) * lenght_backup;
      }
    };
    ostream& operator<< (ostream& stm, const vec2& data)
    {  
      return stm << data.x << ' ' << data.y;
    }
    int main()
    {
      vec2 foo(3, 4);
      cout << foo << endl;
      foo.set_angle(M_PI);
      cout << foo;
    }
    

    Ausgabe

    3 4
    -4.37114e-07 -5
    

    wieso ist die zweite zeile der ausgabe nicht "0 -5" sondern irgend so ein quark?

    mfg

    edit: IST ES DENN SO SCHWER, DEN CODE HIER ZU POSTEN?



  • Da -4.37114 * 10[h]-7[/h] schon beinahe null ist handelt es sich wahrscheinlich um einen Rundungsfehler.


Anmelden zum Antworten