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.

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39072

23

posted

1

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

Joined: Dec 23, 2012
Posts: 47

posted

0

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

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.

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

Joined: Oct 13, 2005
Posts: 39072

23

posted

0

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Jay Mize
Ranch Hand

Joined: Dec 23, 2012
Posts: 47

posted

0

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.

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

Joined: Oct 13, 2005
Posts: 39072

23

posted

0

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.