Ignore:
Timestamp:
2018-07-05T21:41:24Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a4b8b28
Parents:
25709c3
git-author:
Dzejrou <dzejrou@…> (2018-05-15 15:44:18)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
Message:

cpp: fixed bug found by complex tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/complex.hpp

    r25709c3 r937de98  
    3131
    3232#include <iosfwd>
     33#include <sstream>
    3334
    3435namespace std
     
    5152            template<class U>
    5253            constexpr complex(const complex<U>& other)
    53                 : real_(other.real_), imag_(other.imag_)
     54                : real_(other.real()), imag_(other.imag())
    5455            { /* DUMMY BODY */ }
    5556
     
    150151            complex& operator*=(const complex<U>& rhs)
    151152            {
     153                auto old_real = real_;
    152154                real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
    153                 imag_ = real_ * rhs.imag_ - imag_ * rhs.real_;
     155                imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
    154156
    155157                return *this;
     
    159161            complex& operator/=(const complex<U>& rhs)
    160162            {
     163                auto old_real = real_;
    161164                real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
    162165                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    163                 imag_ = (imag_ * rhs.real_ - real_ * rhs.imag_)
     166                imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
    164167                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    165168
     
    189192            template<class U>
    190193            constexpr complex(const complex<U>& other)
    191                 : real_(other.real_), imag_(other.imag_)
     194                : real_(other.real()), imag_(other.imag())
    192195            { /* DUMMY BODY */ }
    193196
     
    288291            complex& operator*=(const complex<U>& rhs)
    289292            {
     293                auto old_real = real_;
    290294                real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
    291                 imag_ = real_ * rhs.imag_ - imag_ * rhs.real_;
     295                imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
    292296
    293297                return *this;
     
    297301            complex& operator/=(const complex<U>& rhs)
    298302            {
     303                auto old_real = real_;
    299304                real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
    300305                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    301                 imag_ = (imag_ * rhs.real_ - real_ * rhs.imag_)
     306                imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
    302307                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    303308
     
    327332            template<class U>
    328333            constexpr complex(const complex<U>& other)
    329                 : real_(other.real_), imag_(other.imag_)
     334                : real_(other.real()), imag_(other.imag())
    330335            { /* DUMMY BODY */ }
    331336
     
    426431            complex& operator*=(const complex<U>& rhs)
    427432            {
     433                auto old_real = real_;
    428434                real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
    429                 imag_ = real_ * rhs.imag_ - imag_ * rhs.real_;
     435                imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
    430436
    431437                return *this;
     
    435441            complex& operator/=(const complex<U>& rhs)
    436442            {
     443                auto old_real = real_;
    437444                real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
    438445                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    439                 imag_ = (imag_ * rhs.real_ - real_ * rhs.imag_)
     446                imag_ = (imag_ * rhs.real_ - old_real * rhs.imag_)
    440447                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    441448
     
    465472            template<class U>
    466473            constexpr complex(const complex<U>& other)
    467                 : real_(other.real_), imag_(other.imag_)
     474                : real_(other.real()), imag_(other.imag())
    468475            { /* DUMMY BODY */ }
    469476
     
    564571            complex& operator*=(const complex<U>& rhs)
    565572            {
     573                auto old_real = real_;
    566574                real_ = real_ * rhs.real_ - imag_ * rhs.imag_;
    567                 imag_ = real_ * rhs.imag_ - imag_ * rhs.real_;
     575                imag_ = old_real * rhs.imag_ + imag_ * rhs.real_;
    568576
    569577                return *this;
     
    573581            complex& operator/=(const complex<U>& rhs)
    574582            {
     583                auto old_real = real_;
    575584                real_ = (real_ * rhs.real_ + imag_ * rhs.imag_)
    576585                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    577                 imag_ = (imag_ * rhs.real_ - real_ * rhs.imag_)
     586                imag_ = (imag_ * rhs.real_ - old_real* rhs.imag_)
    578587                      / (rhs.real_ * rhs.real_ + rhs.imag_ * rhs.imag_);
    579588
Note: See TracChangeset for help on using the changeset viewer.