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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:59:05+00:00 2026-06-16T00:59:05+00:00

Problem description During my fluid simulation, the physical time is marching as 0, 0.001,

  • 0

Problem description

During my fluid simulation, the physical time is marching as 0, 0.001, 0.002, ..., 4.598, 4.599, 4.6, 4.601, 4.602, .... Now I want to choose time = 0.1, 0.2, ..., 4.5, 4.6, ... from this time series and then do the further analysis. So I wrote the following code to judge if the fractpart hits zero.

But I am so surprised that I found the following two division methods are getting two different results, what should I do?

double param, fractpart, intpart;
double org = 4.6;
double ddd = 0.1;

// This is the correct one I need. I got intpart=46 and fractpart=0
// param = org*(1/ddd);

// This is not what I want. I got intpart=45 and fractpart=1
param = org/ddd;

fractpart = modf(param , &intpart);
Info<< "\n\nfractpart\t=\t"
    << fractpart
    << "\nAnd intpart\t=\t"
    << intpart
    << endl;

Why does it happen in this way?
And if you guys tolerate me a little bit, can I shout loudly: “Could C++ committee do something about this? Because this is confusing.” 🙂

What is the best way to get a correct remainder to avoid the cut-off error effect? Is fmod a better solution? Thanks

Respond to the answer of

David Schwartz

double aTmp = 1;
double bTmp = 2;
double cTmp = 3;
double AAA = bTmp/cTmp;
double BBB = bTmp*(aTmp/cTmp);
Info<< "\n2/3\t=\t"
    << AAA
    << "\n2*(1/3)\t=\t"
    << BBB
    << endl;

And I got both ,

2/3     =       0.666667
2*(1/3) =       0.666667
  • 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-16T00:59:06+00:00Added an answer on June 16, 2026 at 12:59 am

    Floating point values cannot exactly represent every possible number, so your numbers are being approximated. This results in different results when used in calculations.

    If you need to compare floating point numbers, you should always use a small epsilon value rather than testing for equality. In your case I would round to the nearest integer (not round down), subtract that from the original value, and compare the abs() of the result against an epsilon.

    If the question is, why does the sum differ, the simple answer is that they are different sums. For a longer explanation, here are the actual representations of the numbers involved:

                 org:  4.5999999999999996 = 0x12666666666666 * 2^-50
                 ddd: 0.10000000000000001 = 0x1999999999999a * 2^-56
               1/ddd:                  10 = 0x14000000000000 * 2^-49
       org * (1/ddd):                  46 = 0x17000000000000 * 2^-47
           org / ddd:  45.999999999999993 = 0x16ffffffffffff * 2^-47
    

    You will see that neither input value is exactly represented in a double, each having been rounded up or down to the nearest value. org has been rounded down, because the next bit in the sequence would be 0. ddd has been rounded up, because the next bit in that sequence would be a 1.

    Because of this, when mathematical operations are performed the rounding can either cancel, or accumulate, depending on the operation and how the original numbers have been rounded.

    In this case, 1/0.1 happens to round neatly back to exactly 10.

    Multiplying org by 10 happens to round up.

    Dividing org by ddd happens to round down (I say ‘happens to’, but you’re dividing a rounded-down number by a rounded-up number, so it’s natural that the result is less).

    Different inputs will round differently.

    It’s only a single bit of error, which can be easily ignored with even a tiny epsilon.

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

Sidebar

Related Questions

Problem description: Read an xml file, traverse to a particular node (element), if it
Problem description : - Step 1: Take input FILE_NAME from user at main thread.
Short problem description: XCode 4.2 install right Target on device, but debug (run) always
The following is the problem description: let c[n] be the catalan number for n
This is my homework, but please read my problem description first. I have to
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
Description: I have a problem regarding DataGridView . I need to show a Client_Name
I'm having a problem with this: -(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u { I'm
Ok here is my problem : Before i start the description, let me to
I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action

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.