• Post Reply Bookmark Topic Watch Topic
  • New Topic

maths in java  RSS feed

 
satheesh subramani
Greenhorn
Posts: 29
Fedora Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
satheesh subramani
Greenhorn
Posts: 29
Fedora Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
both steve and rob very thanks
i had a little doubt but you people elaborate it nicely
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!