This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Checking Answer vs Response Problem

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 16601
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'.
 
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply guys, much appreciated!
 
Carmine Gendry
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent !  Good work.

[Edit:] oops. see next post.
 
Carey Brown
Saloon Keeper
Posts: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1249
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8609
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1249
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Or even just:

Oh yes that much better!
 
reply
    Bookmark Topic Watch Topic
  • New Topic