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 8559941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:10:03+00:00 2026-06-11T16:10:03+00:00

Problem: Double minus another double is returning unaccurate results. Usual solution: Use big decimal

  • 0

Problem: Double minus another double is returning unaccurate results.

Usual solution: Use big decimal to perform calculations.

I can’t use this because it’s in the middle of a game loop, can only set big decimal with a value when it’s created with new, within a game loop I’m running out of memory.

What solution?

Here’s the code:

public void UpdateStatsBar(double WorldPopulation, double WorldDead)
{
    Number = mOriginalPopulation - WorldPopulation;
}

The kind of numbers I’m using go into the billions and go tens of digits deep (averages for stats) example: 1432,323,234.234215232425322342

  • 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-11T16:10:04+00:00Added an answer on June 11, 2026 at 4:10 pm

    Assuming you can’t have a decimal number of people live or dead, I would use long.

    Can you provide an example of the numbers you are using which are not producing accurate results?

    BTW: If you use whole numbers less than nine thousand trillion, even double will produce accurate results.

    BTW2: If you create new BigDecimal like mad but don’t retain them you will hurt performance but you won’t run out of memory. If you are running out of memory its not due to use BigDecimal. I suggest you use a memory profiler to find the true cause of the problem.

    An example

    BigDecimal bd = BigDecimal.ZERO;
    long l = 0;
    double d = 0;
    while (true) {
        for (int i = 0; i < 100000000; i++) {
            bd = bd.add(BigDecimal.ONE);
            l++;
            d++;
        }
        System.out.println("counting bd: " + bd + " l: " + l + " d:" + d);
    }
    

    prints number counting with BigDecimal, long and double. The format for double is different but the value is the same.

    counting bd: 100000000 l: 100000000 d:1.0E8
    counting bd: 200000000 l: 200000000 d:2.0E8
    counting bd: 300000000 l: 300000000 d:3.0E8
    ... deleted ...
    counting bd: 1900000000 l: 1900000000 d:1.9E9
    counting bd: 2000000000 l: 2000000000 d:2.0E9
    counting bd: 2100000000 l: 2100000000 d:2.1E9
    counting bd: 2200000000 l: 2200000000 d:2.2E9
    ... deleted ...
    

    Even with -mx2m limiting the heap to 2 MB it doesn’t run out of memory.

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

Sidebar

Related Questions

I'm having problem with double linked lists: I can't get data from nodes throgh
I know the problem with double/float, and it's recommended to use BigDecimal instead of
I have a bizarre problem with a 'double' property in Core Data. I have
I found a strange ToolStripButton double click problem. These steps will reproduce the problem:
I have a problem with the mapping of Oracle Float double precision datatype to
I have a weird problem in converting a string to double in .NET 3.5.
My problem is the following. I try to convert a string into double. In
Im having a small problem with storage of special characters like quotes, double quotes
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
After some painful experiences, I understand the problem of dangling pointers and double free.

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.