posted 6 years ago

hi ranchers,

i am mathematical based student now i am studing java. i had a doubt about how it is possible

x= x + 1;

x is equal to x then how x is equal to x+1

i am mathematical based student now i am studing java. i had a doubt about how it is possible

x= x + 1;

x is equal to x then how x is equal to x+1

posted 6 years ago

In Java, = is not used for equality but for assignment. That statement actually says "take the current value of x, add 1, and set the new value of x to that result". For equality you must use ==. So x == x + 1 will always be false.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 6 years ago

since you have the ; i think Rob is spot on, but....

and of course if you are talking about floating point numbers then it is very possible that x might be large enough so that adding 1 to x will not change the value ----- a good thing to do if you are math oriented, would be to search on floating point representation and precision.

and of course if you are talking about floating point numbers then it is very possible that x might be large enough so that adding 1 to x will not change the value ----- a good thing to do if you are math oriented, would be to search on floating point representation and precision.

posted 6 years ago

The ; doesn't matter in Java. = is assignment, == is comparison. There are languages (the horrible Visual Basic comes to mind...) where = is used for both. In those cases the context matters.

Purely theoretical, there are just two "numbers" for which x == x + 1. Those are +infinity and -infinity. That's basically the definition of +infinity: +infinity = +infinity + 1. This makes it recursive: +infinity = +infinity + 1 = +infinity + 2 = ...

For -infinity the definition is -infinity = -infinity - 1 but a little math quickly shows that it's actually the same definition as +infinity except on the other side of 0.

Steve is still correct though. double and float only have limited precision. That means that if the number becomes large enough (e.g. 10^20) the +1 is so small that it is ignored:

This is not a mathematical incorrection though; it's a mathematical rounding error. The + operation simply rounds 10^20 + 1 down to 10^20 which then is equal to 10^20.

Purely theoretical, there are just two "numbers" for which x == x + 1. Those are +infinity and -infinity. That's basically the definition of +infinity: +infinity = +infinity + 1. This makes it recursive: +infinity = +infinity + 1 = +infinity + 2 = ...

For -infinity the definition is -infinity = -infinity - 1 but a little math quickly shows that it's actually the same definition as +infinity except on the other side of 0.

Steve is still correct though. double and float only have limited precision. That means that if the number becomes large enough (e.g. 10^20) the +1 is so small that it is ignored:

This is not a mathematical incorrection though; it's a mathematical rounding error. The + operation simply rounds 10^20 + 1 down to 10^20 which then is equal to 10^20.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 6 years ago

Rob,

the ; means that the statement is java code....

the line: x = x + 1

Could be from a requirements doc for implementing in java ----- in fact i give the same requirment statement to my students --- ie: finding the smallest floating point numer such that: x = x + 1 is true.

the ; means that the statement is java code....

the line: x = x + 1

Could be from a requirements doc for implementing in java ----- in fact i give the same requirment statement to my students --- ie: finding the smallest floating point numer such that: x = x + 1 is true.

posted 6 years ago

Ah ok. I simply read that as code, not as anything that is not part of code such as specification.

Still, being taught using GCL (a purely abstract language) I find it odd to use a language that is syntactically different from the specs. GCL uses Pascal's := for assignment so there is a clear difference between assignment (always :=) and equality (always =). If you teach Java then = has two different meanings, and that is confusing to students. In this case I would either switch language or use == inside the specs.

Still, being taught using GCL (a purely abstract language) I find it odd to use a language that is syntactically different from the specs. GCL uses Pascal's := for assignment so there is a clear difference between assignment (always :=) and equality (always =). If you teach Java then = has two different meanings, and that is confusing to students. In this case I would either switch language or use == inside the specs.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 6 years ago

Thanks Rob, but that is exactally what this assigment is designed to do....Make the students think about the requirements.

We find that we have way too many students that just jump ahead and write anything for any assignment (ie: code before thinking). We have found that this assignment is a nice way to get them into thinking (ie: understand the problem before finding a solution).

We find that we have way too many students that just jump ahead and write anything for any assignment (ie: code before thinking). We have found that this assignment is a nice way to get them into thinking (ie: understand the problem before finding a solution).

It is sorta covered in the JavaRanch Style Guide. |