Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If/else

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code, I want it to print y2 as 0.00 instead of -0.00 when y1 = 0.00 but somehow it only print for the first row and now the last. Thanks



***" r" =1 sorry.
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the actual value of y2 in the last line? I bet it isn’t 0.
That code is difficult to read because of poor formatting. Use {} after every if, else, while, etc. Use spaces round your binary operators.
Never use r ². Use r × r instead.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:This is my code, I want it to print y2 as 0.00 instead of -0.00 when y1 = 0.00

First, I completely agree with Campbell: your code is very difficult to read; and not just because of formatting.

Your field names are totally meaningless to anyone except you (and quite possibly you too if you ever come back to it in a few month's time); and secondly, there's no documentation.

Question: What is this program supposed to do? From a cursory glance it would appear to be printing out √(4-x^2) for a bunch of values of x, but I've absolutely no idea if that was your intention, or why you'd want to do it. Is it something to do with quadratics?

Also: Don't mix numeric types if you don't have to. '0' and '2' are integer literals, and you're using them with a double. Instead of:
if(y1 != 0)
use:
if(y1 != 0.0)
because 0.0 is a double literal (in fact, the compiler will probably convert the first literal for you, but it's sloppy practice).

Winston
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . First, I completely agree with Campbell: . . .
It does happen occasionally

Agree about 0.0 instead of 0, but you are right; the compiler (or runtime) will convert it to 0.0. But I bet it wouldn’t be equal to 0.0 either. Print the exact value of y or whatever, and you can work out what has gone wrong happened. It probably hasn’t gone wrong, but you are having problems because of misunderstanding floating‑point arithmetic (see no 20).
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the formating tips. The value of "r" suppose to be 1 sorry. The value of x1[index] for the last value is -1.00. "double y1 = Math.sqrt(Math.pow(r,2)-Math.pow(x1[index],2)); where "r" = 1" √(r^2)-(x1[index]^2). So r^2 = 1 , and x1[index]^2 = 1.00. Then 1-1 = 0. Wouldn't y1 be 0.00? Java is printing y1 = 2.1073424255447017E-8. Can someone tell me what does that mean? Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Thank you for the formating tips. The value of "r" suppose to be 1 sorry. The value of x1[index] for the last value is -1.00. "double y1 = Math.sqrt(Math.pow(r,2)-Math.pow(x1[index],2)); where "r" = 1" √(r^2)-(x1[index]^2). So r^2 = 1 , and x1[index]^2 = 1.00. Then 1-1 = 0. Wouldn't y1 be 0.00? Java is printing y1 = 2.1073424255447017E-8. Can someone tell me what does that mean? Thanks



"2.1073424255447017E-8" means 2.1073424255447017 times ten to the negatve eight. This is scientific notation (which you should get used to) and works out to ... 0.000000021073424255447017 -- which is very close to zero.


Also, take a look at point #20 in the JavaBeginnersFaq.

Henry
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it due to arithmetic inaccuracy that I am getting 2.1073424255447017E-8 and not 0? I did it by hand and it should be 0.
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing to notice is, it isn’t 0. It is close, but no cigar. That shows the hazards of using floating‑point arithmetic and hoping for a precise result. You are very unlikely ever to get back exactly to 0.0 with that sort of arithmetic. That is why, when you round to two decimal places, you are seeing -0.00. You will find out lots more in the No 20 which Henry and I have referred you to.
Try if (x < 0.000001 && x > -0.000001)...
That sort of thing might help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is a limitation of binary computers and how floating point numbers are handled.. It has nothing to do with Java specifically. Just about any modern programming language will have the same issue.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the information Campbell and Fred. May I verify that if you do it with paper and pencil, √(1-1) is equal to 0? Just checking I'm not getting my math wrong.
 
Henry Wong
author
Marshal
Pie
Posts: 21112
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Thank you for the information Campbell and Fred. May I verify that if you do it with paper and pencil, √(1-1) is equal to 0? Just checking I'm not getting my math wrong.



Not sure of the point that you are trying to make. The articles we point to says that there are minor inaccuracies -- which is what you got in you program. Are you trying to confirm that you are correct too? Isn't this second part obvious?

Henry
 
Campbell Ritchie
Sheriff
Posts: 48904
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that you are not subtracting 1 from 1. If you are getting 2.1073424255447017E-8 from your √ operation, it suggests you were actually passing 4.44089209850062662806e-16 (give or take the odd dozen) as a parameter to the sqrt() method. So obviously one (or both) of the two values you thought were 1.0 isn’t actually 1.0.
As Henry and Fred have pointed out, this sort of thing happens all the time when you use floating‑point arithmetic. It is unavoidable.

By the way, log(4.44089209850062662806e-16) ÷ log(2) = -51, as near as makes no difference. That suggests there is a difference 51 bits to the right. You might have one value which comes to
1.0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0010… and one of 1.0 in binary (that is not at all how IEEE754 numbers are represented) or 1.0 and
0.1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1110…
or something like that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic