Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# implement pre/post-increment/decrement operators for floating point types?

What's so special about adding/subtracting 1 to/from a floating point value that it deserves a dedicated operator?

double a = -0.001234129;
a++; // ? 

I've never felt the need to use such a construction; it looks really weird to me. But if I ever had to, I'd feel much more comfortable with just:

a += 1;

Maybe it's because of my strong C++ background, but to me it makes a variable look like an array indexer.

Is there any reason for this?

like image 782
Trap Avatar asked Dec 27 '22 07:12

Trap


1 Answers

The ++ and -- operators operate on all other number types, why make an exception for floating point numbers? To me, that would be the more surprising choice.


Note that C++ also implements these for floating point:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    double a = 0.5;
    cout << a << '\n';
    ++a;
    cout << a << '\n';
    return 0;
}

Output:

0.5
1.5
like image 159
T.J. Crowder Avatar answered Dec 29 '22 21:12

T.J. Crowder