Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:09:18+00:00 2026-06-03T05:09:18+00:00

I have the following code which sometimes return as true and sometimes doesn’t. Any

  • 0

I have the following code which sometimes return as true and sometimes doesn’t.

Any idea what might be causing the varying result?

The 0.00 string comes from a JSON object.

(code simplified)

if(new Float("0.00")>0){
    // do something
}

EDIT:

What I have is some floats which I want to determine if its zero, less than zero or more than zero. The values could be stuff like 0.00, 0.001 or -0.001. How do I determine whether whether they are positive, negative or zero?

EDIT:

Perhaps I should clarify how I get the values. I might be totally something else that is causing the problem because I read about BigDecimal and tried to use it to no avail.

The values are extracted from a JSON feed (which has this format LT:0.000) using this code:

price = new BigDecimal(stocksJSONArray.getJSONObject(i).getString("LT"));

Then to test if price is greater or less than zero, I used the following conditional statements:

if(price.compareTo(BigDecimal.ZERO)==1){
    // greater than zero
}
else if(price.compareTo(BigDecimal.ZERO)==-1){
    // less than zero
}

This code is looped for many values read out from the JSON feed. And from the results, some price which is zero gets processed as greater than zero and some gets processed as less than zero. I’m suspecting that something else is causing the problem here?

I also did a test to see if the problem was with the accuracy of the data. So I did this:

DecimalFormat frmt = new DecimalFormat("0.000000000000000000000000");
String formatted = frmt.format(stock.change);

And for the zeros that got recognized as positives and negatives, the trace value for it was still 0.000000000000000000000000, not 0.000000000000000000000001 or something like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:09:19+00:00Added an answer on June 3, 2026 at 5:09 am

    Your expression will reliably produce a result of false in Java.

    However, suppose that the zero is, for example, the result of -1 divided by plus infinity. In this case it will be internally represented as, roughly said, -0.00. In some circumstances it will still be printed as a zero (without the minus sign), in others it will behave differently from 0.00.

    Floats can generally be compared for less-than in the same way as integers – there is a risk of a rounding error, but that error is not helped by adding or subtracting a random small value. It is different with comparison for equality.

    I suggest you double check your facts and do more reading about floating point behavior.

    Edit: I am greatly simplifying above to answer the original question. To answer the edit to the question, we need to go deeper.

    For any operation on floating point numbers one should know and consider the precision and accuracy of inputs, and the desired accuracy of the output. Sometimes the task is solvable and sometimes it is not – the input accuracy may not be sufficient to produce the answer.

    In your case, the precision is 32 bit, out of which 24 bits are the mantissa and 8 bits exponent. That means that this data type safely distinguishes 0.001 from 0.00100001 but not from 0.001000001 as you can easily see:

     System.out.println((float)0.001 < (float)0.001000001);
    

    (Note that you would get a different result if you did not force single precision comparison by the casts. In that case the computation would be done in double precision and the numbers would be safely distinguished – until you bring them even closer together.)

    So, precision is determined by the data type. Not so accuracy. Input accuracy is often much more challenging to determine than precision because it has nothing to do with the data type, except that accuracy can never be better than the precision.

    A mathematical real number can find itself in four possible circumstances with regard to representability in a particular floating point type, and those correspond to different treatments that it receives when it occurs as a literal, in the human readable decimal notation.

    • It might be representable accurately in binary. For example, 0 or 0.25. Then it is as accurate as an integer would be in an integer variable.
    • Or it is approximately representable with accuracy corresponding to the type’s precision. For example, 1/3 or 0.1 or 0.001. This happens when the exponent needed fits in the number of exponent bits available, but when the binary expansion of the number is either longer than the mantissa, or outright infinite.
    • Or it is approximately representable with drastically distorted accuracy. These are denormal (subnormal) numbers. It’s not only inaccurate, arithmetics on it may slow down to a crawl, which is typically documented correct behavior, and even a respectable Java compiler may sweat a little upon seeing literals of this kind which is a however a bug.
    • Or it does not fit at all and compilers will refuse the literal as too large.

    So, in your case, we have only three valid inputs: 0 (accurate), 0.001 (approximate) and -0.001 (approximate), and that makes your problem solvable. Simply compare your numbers with a 0 literal (which is accurate, by the way) and you will always get the expected boolean values (fully accurate output).

    This however depends on your inputs being directly derived from literals. If your inputs were one of 0.001, -0.001 and (float)1000 * (float)0.001 - 1, this would be a different question and you would have to get the answers, for example like this:

    if (-0.00001 < x && x < 0.00001) // then x is zero 
    

    And if you allow any inputs whatsoever, not just those three magic values, and have no clue about the input’s accuracy, then that’s just mission impossible. Even a literal starting as 0.000000000... with some garbage digits far down at the end will be converted by a Java compiler to a perfectly neutral zero, and after this happens, no amount of Java code will ever tell it from the accurate and beautiful 0.00, or from what happens if you add a minus sign to the underflowing value. It’s all the same, inaccurate zero, the same bit pattern in your variable, rather than 3 distinct values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I stripped out of any non-essential lines to
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps
I have the following code which lets the user plot two points on a
I have the following code which redirect pages depends on $path. ... $path =
I have the following code which is in a transaction. I'm not sure where/when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.