Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0.1 vs 0,1 in c++, why does neither result an error? [duplicate]

Tags:

c++

c++11

c++14

I come from a country where the way we write decimal numbers differs from the other place by whether I use ',' or '.' for decimal separator. Now this is why I by accident wrote somewhere in my c++ program:

while(true){
    long double x=0;
    //some code that increases the value of x 
    //by very small amount but it doesn't leave it at 0
    if(x>0,000001)break;
}

And this made the program run indefinitely. After wasting quite some time thinking my code was wrong I tried this:

while(true){
    long double x=0;
    //some code that increases the value of x 
    //by very small amount but it doesn't leave it at 0
    if(x>0.000001)break;
}

Which worked just fine. Later I understood my mistake (I used ',' instead of '.'), but now I am confused as to why doesn't the next code result with compilation error, and how come it's outcome is "greater"

long double g=0.001;
if(g>0,01)cout<<"greater";
else cout<<"smaller";

Edit: When I posted this question I didn't know that the comma is an operator. So the question was marked as duplicate and lead here How does the Comma Operator work . But there I couldn't actually find a comparison between '.' and ',' .

like image 283
Martin Dinev Avatar asked Dec 20 '25 09:12

Martin Dinev


1 Answers

, in C++ is an operator which returns the value of its second operand. So a, b evaluates to b.

, also has very low precedence, so your comparisons are actually evaluated as:

if((x>0), 01) -> if(01) -> if(true)

like image 69
Gordon Bailey Avatar answered Dec 21 '25 23:12

Gordon Bailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!