Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Quadratic formula result problem

 
Greenhorn
Posts: 10
Eclipse IDE Windows XP Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im having problems in getting the exact value of the roots, for some reason it doesnt read -4+sqrt(13) as -0.3944487245 but rather as -4 + (-0.3944487245) which it turn shows the answer as -4.3944487245 which is wrong, what did i do wrong here???

Oh btw the reason that i did multiplication by 10^7 and got the floors then divided it back to 10^7 is because our professor wanted every calculation result at cut at exactly 7 decimal places and uses that value in any subsequent calculation


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

To calculate d in line 27, just do: b * b - 4 * a * c; it's not necessary to use Math.pow to square something.

Why are you calculating dFloor and dFinal? That's not part of the quadratic formula. What is all the multiplying and dividing by 10 to the power 7 for? It looks like you're making it way more complicated than necessary.

With the quadratic formula you need to take care when d is negative; there are no (real-valued) solutions in that case (because you can't take the square root of a negative number). And if d is equal to 0, there is only one solution instead of two.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phil Maqui wrote:Oh btw the reason that i did multiplication by 10^7 and got the floors then divided it back to 10^7 is because our professor wanted every calculation result at cut at exactly 7 decimal places and uses that value in any subsequent calculation.


In which case I'd put it in a separate method (round(int)?), because it has nothing to do with calculating a quadratic and just confuses your readers.
Alternatively, you could just trim the excess when you print it out, viz:
System.out.printf("%.7f%n", r1);

Oh, and just to add to what Jesper said: (-1 * b) == -b.

I also tend to add brackets for clarity even if they're not strictly required, eg:
(-b + Math.sqrt(dFinal)) / (2 * a)
rather than
-b + Math.sqrt(dFinal) / (2 * a)
although I think most of yours are OK.

Winston
 
Marshal
Posts: 70354
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand from what Jesper said: have a look at the Math class, and see what the sqrt() method returns from a negative input. You would have to test for that result specially.
If you have 0 for d, you would be adding ±√0, which is 0, so you would still get two results from a programmatic solution. Obviously both results should be the same!
 
Phil Maqui
Greenhorn
Posts: 10
Eclipse IDE Windows XP Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the main reason why i just didn't get the roundoff value for each calculation is because in some cases it might change the values even by a little bit, and our prof WANTED the values to be cut and not to be rounded off at the 7th decimal place and use that value for subsequent calculations

But nonetheless i learned something and gained insight with all your comments, i figured that my mistake was with the grouping by brackets, since division takes priority over addition.

Thanks anyways ^__^
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic