Is this a good start?

Pages: 12
Okay thanks I will try other methods to improve the performance an easy fix that I can think of would be to just add to the end then reverse it for the final answer would that be better or should I look for a better performance method?

*edit also I don't like how my for loop looks I am going to fix that so it isn't so rubbish.
Last edited on
a+b *= 2
is nonsense any way you look at it.

(a+b).duplicate()
is a valid rvalue, and you know it.
Well I have come up with this now but I don't think its much better I gotta think of a better way where I don't need to reverse the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Integer& Integer::operator+=( Integer i )
{
    std::string temp( "" );
    unsigned short remainder( 0 ) , carry( 0 );

    while( !value.empty() && !i.value.empty() )
    {
        remainder = value.back() - '0' + i.value.back() - '0' + carry;
        value.pop_back();
        i.value.pop_back();
        if( remainder > 9 )
        {
            carry = 1;
            remainder -= 10;
        }
        else
        {
            carry = 0;
        }
        temp += remainder + '0';
    }

    temp += value += i.value;

    std::reverse( temp.begin() , temp.end() );

    value = temp;
    return( *this );
}


Edit*
Here is another method are either of these better than the orig?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Integer& Integer::operator+=( Integer i )
{
    std::string temp( "" ) , extra( "" );
    unsigned short remainder( 0 ) , carry( 0 );
    const bool lhs_greater = value.size() > i.value.size();

    while( value.size() != i.value.size() )
    {
        if( lhs_greater )
        {
            extra += value.at( 0 );
            value.erase( 0 , 1 );
        }
        else
        {
            extra += i.value.at( 0 );
            value.erase( 0 , 1 );
        }
    }
    
    auto lhs_it = value.rbegin() , rhs_it = i.value.rbegin();

    while( lhs_it != value.rend() )
    {
        remainder = *lhs_it - '0' + *rhs_it - '0' + carry;
        if( remainder > 9 )
        {
            carry = 1;
            remainder -= 10;
        }
        else
        {
            carry = 0;
        }
        temp += remainder + '0';
        ++lhs_it;
        ++rhs_it;
    }

    if( remainder == 1 ) extra.back() + remainder - '0';

    std::reverse( temp.begin() , temp.end() );

    value = extra + temp;
    return( *this );
}
Last edited on
Duoas wrote:
a+b *= 2 is nonsense any way you look at it.

(a+b).duplicate() is a valid rvalue, and you know it.
What if I said (a+b).operator*=(2)? It "is a valid rvalue, and you know it", yet it is also "nonsense any way you look at it."
Last edited on
Why is this so confusing?

I know it is possible to do it, but it is nonsensical to ASSIGN VALUE to (a+b), and any compiler worth it's salt ought to tell you you are probably doing something wrong. Why? Because (a+b) is not a proper lvalue.

(Now, I know you can shuffle the shells enough to make (a+b) into some grotesque version of an lvalue, but whatever you do to it will be non-obvious, because it breaks design pattern and KISS.)


In contrast, (a+b) is a perfectly normal rvalue, and there is nothing wrong with applying operations to it.


By definition, whenever you have an EQUALS SIGN, you are playing with an lvalue on the left, and an rvalue on the right.

(And again, I don't care if you can twist C++ syntax into something that invalidates this statement. I can do it too, but I won't, because it would be WRONG.)


So, you want to take two separate statements of mine, mix them, and claim that the false conclusion is my words? That's a straw man.

Go eat your own poo and stop throwing it at me.
Duoas I know what you mean, I am just saying ne555 has a point that you are not clear why one is nonsense and the other isn't. Obviously now you have been clear so there is no problem ;)
Last edited on
¿vlad? that's insulting.

I'm simply saying that you wouldn't apply operations that modify the state of a temporary object.
So you would only use functions that are declared as const.
Sorry, had vlad's name stuck in my head from earlier - didn't mean to insult you.
Last edited on
*Back to the topic*

I decided that I will just work on finishing the class then worrying about optimization since I'm kinda just wasting time and I think Duoas mentioned this earlier :p
Topic archived. No new replies allowed.
Pages: 12