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

  • Home
  • SEARCH
  • 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 7513759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:17:05+00:00 2026-05-30T00:17:05+00:00

I have two double values that are truncated to two decimal spots. Each one

  • 0

I have two double values that are truncated to two decimal spots. Each one represents a spot on a radian circle, and they are either supposed to increment or decrement by 0.01 until they find a target value. Here’s the code:

    BigDecimal negPath = new BigDecimal(roundAndConvert(pendA.getTheta()));
    BigDecimal posPath = new BigDecimal(roundAndConvert(pendA.getTheta()));
    BigDecimal target = new BigDecimal(roundAndConvert(pendB.getTheta()));
    for (negPath = negPath; ((negPath.doubleValue() > -0.01) && (!negPath.equals(target))); negPath = negPath.subtract(new BigDecimal(0.01))) {}
    for (posPath = posPath; ((posPath.doubleValue() < 6.29) && (!posPath.equals(target))); posPath = posPath.add(new BigDecimal(0.01))) {}

//here's the method that rounds the values

public double roundAndConvert(double value)
{
        double holder = (int)(value*100) / 100.0;
        if (holder < 0)
        {
            while (holder < -6.28) {holder += 6.28;}
            holder += 6.28;
        }
        else
        {
            while (holder > 6.28) {holder -= 6.28;}
        }
        return holder;
}

The problem that I’m having is that the two Path values are returning doubles with long decimals, like so:

pos = 6.299999999999944
neg = -0.01999999999996656

The values that they return aren’t like this before the for loop, so I think it has to do with the increment/decrement.

EDIT: tried replacing the doubles with BigDecimals, but now the program is freezing at the for loops. I see the stringCache for the Path values, and they still have the long decimals.

EDIT: Another change to the coding, this time using all BigDecimal objects and equals in the for loops, but I’m still getting long decimals.

EDIT: finally worked with the coding shown. The accepted answerer explains what I was doing wrong. Here’s the code that works:

    for (negPath = negPath; ((negPath.doubleValue() > -0.01) && (!negPath.equals(new BigDecimal((int)(target.doubleValue()*100)/100.0)))); negPath = negPath.subtract(new BigDecimal(0.01))) 
    {
        negPath = new BigDecimal((int)(negPath.doubleValue()*100)/100.0);
    }
    for (posPath = posPath; ((posPath.doubleValue() < 6.29) && (!posPath.equals(new BigDecimal((int)(target.doubleValue()*100)/100.0)))); posPath = posPath.add(new BigDecimal(0.01))) 
    {
        negPath = new BigDecimal((int)(negPath.doubleValue()*100)/100.0);
    }
  • 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-05-30T00:17:07+00:00Added an answer on May 30, 2026 at 12:17 am

    Your code:

    for (negPath = negPath; 
          ((negPath.doubleValue() > -0.01) && (negPath.doubleValue() != target));
          negPath.subtract(new BigDecimal(0.01))) 
    {}
    

    I see two bad errors here. First:

    negPath.subtract(new BigDecimal(0.01))
    

    should be

    negPath = negPath.subtract(new BigDecimal(0.01))
    

    because a BigDecimal is immutable – each operation on them just creates a new one. This might be the case why your loop never terminates now – because the state does not change in each loop.

    Second:

    (negPath.doubleValue() != target)
    

    Comparing doubles that way is a no-no, because intermediate rounding-errors are quite sure to happen and then this part of the condition is never true.

    Rationale: Please read some basic documentation about floating point calculations – floats are not just numbers as you know them from mathematics, they must be treated with great care.

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

Sidebar

Related Questions

I have a fairly complicated function that takes several double values that represent two
I have two applications written in Java that communicate with each other using XML
I have two double data elements in an object. Sometimes they are set with
Let's assume we have two collections: List<double> values List<SomePoint> points where SomePoint is a
I have a mathematical formula in my program that takes in two values, both
I have two double a, b; I know that the following is true -1
I have a long piece of code that calculates two values ( doubles )
I have a site that dynamically creates two arrays of Lat/Long values based on
I have a class, Number , which represents a double with two digits after
I have two complex (i.e. objects with string, int, double, List and other home

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.