• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Checking Answer vs Response Problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I'm currently given the task of checking for an answer and accepting it if it doesn't differ by no more than 0.01 .. below you will find my current code:





So I have set my answer as '123'. However, when I run and give the response of 123.01 which should accept it since it's the minimum, it outputs 'false'.
I feel like I'm missing something so simple yet I can't figure it out.. Is it because of rounding at one point when parsing the string to a double?...

I appreciate any help I can receive.
Thanks!
 
Sheriff
Posts: 13510
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point numbers are imprecise so there's really no guarantee that you'll get exactly what you're expecting.

This is what I get on my MacBook Pro:


Edit: Maybe "imprecise" is the wrong word -- look up floating point error to see what I really meant to say.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Floating point numbers are imprecise so there's really no guarantee that you'll get exactly what you're expecting.

This is what I get on my MacBook Pro:


Edit: Maybe "imprecise" is the wrong word -- look up floating point error to see what I really meant to say.



Thanks for your reply.
So is my current code as good as its possibly going to get? or is there anything else I can do other than changing the value of the 'maxDifference'.
 
Bartender
Posts: 5894
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to be "precise", check out API for BigDecimal. It looks like you're getting your input from the console as a String which would work great with BigDecimal because its constructor takes a String.
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply guys, much appreciated!
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:If you have to be "precise", check out API for BigDecimal. It looks like you're getting your input from the console as a String which would work great with BigDecimal because its constructor takes a String.



Would I have to change my 'maxDifference to a 'BigDecimal' as well? This is what I currently have:


However, I'm still not able to compare by 'absoluteResult' to 'maxDifference'.
Thanks!
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:If you have to be "precise", check out API for BigDecimal. It looks like you're getting your input from the console as a String which would work great with BigDecimal because its constructor takes a String.


I think I got it... please correct me if I'm wrong:
 
Carey Brown
Bartender
Posts: 5894
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Bartender
Posts: 5894
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carmine Gendry wrote:


You should probably change
to

And then change
to

The way you had it you were converting (double)0.01 to a BigDecimal in a way that would introduce floating point errors.
 
Saloon Keeper
Posts: 1217
85
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are returning true if result <= 0 and false when result >0, you can use Conditional Operator ? :
 
Carey Brown
Bartender
Posts: 5894
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Since you are returning true if result <= 0 and false when result >0, you can use Conditional Operator ? :

Or even just:
 
Ganesh Patekar
Saloon Keeper
Posts: 1217
85
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Or even just:

Oh yes that much better!
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!