• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

If/else

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic