• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

help please

 
Rancher
Posts: 660
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code:-


When I run this part of code, I always end up with the 'zero' message_string
I have tried checking the abs value of (myCurrPositionValue - 0.2) say being less the 0.001 but the same result occurs

What am I doing wrong?

Bob M
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not compare Strings with == operator.
Use equals() method instead.

AvoidTheEqualityOperator (this is a link).

Also, instead ofyou can (and should) write
Also, by convention, you should use camelCaseVariableNames instead of underscored_variable_names.
 
Saloon Keeper
Posts: 14711
331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're testing curr_trade_dir for identity, not equality. When you write curr_trade_dir == "Down", you're testing whether they are exactly the same two objects in memory, not whether they mean the same thing. The lesson is simple: Don't use == where you should use equals().

Another thing, doubles can not represent certain values exactly. Doing comparisons like myCurrPositionValue == 0.2 is asking for trouble. If you really must use floating point arithmetic, you need to introduce a tolerance when you want to compare two values.

Finally, don't use variable names such as curr_trade_dir. Use currentTradeDirection. Strings are also a poor substitute for expressive data types. Use an enum for your directions:
 
Stephan van Hulst
Saloon Keeper
Posts: 14711
331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paweł is much quicker than I am today.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please UseAMeaningfulSubjectLine (this is also a link).
 
Bob Matthews
Rancher
Posts: 660
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you

Bob M
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic