• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking Answer vs Response Problem  RSS feed

 
Carmine Gendry
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!
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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'.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent !  Good work.

[Edit:] oops. see next post.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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.
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • 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
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor 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
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Or even just:
Oh yes that much better!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!