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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:11:06+00:00 2026-06-18T03:11:06+00:00

Thanks to stack, I learned about floating point imprecision, so I went over to

  • 0

Thanks to stack, I learned about floating point imprecision, so I went over to the bc functions.

That works great on “normal” floats, but with extremely small floats, say 10^-10 types, bcadd always gives 0.

Can someone show me what I’m doing wrong to make these small floats add with precision?

Many thanks in advance!


PHP

$numerator = 1;
$denominator = 1000000000;
$quotientOne = $numerator / $denominator;

$numerator = 1;
$denominator = 1000000000000000;
$quotientTwo = $numerator / $denominator;

$smallSum = bcadd($quotientOne, $quotientTwo, 100);

echo $quotientOne . "<br>";
echo $quotientTwo . "<br>";
echo $smallSum . "<br>";

gives

1.0E-9
1.0E-15
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  • 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-18T03:11:07+00:00Added an answer on June 18, 2026 at 3:11 am

    The / operator returns either a float or an integer. It’s interfering with your attempts to use bcadd() correctly. The limitations of floating-point arithmetic will take hold before bcadd() gets a chance to show its stuff.

    echo gettype($numerator / $denominator);
    double
    

    Use bcdiv() instead. Note that the bc*() functions take strings as their arguments, and they also return a string.

    $ cat code/php/test.php
    <?php
    $num_1 = 1;
    $denom_1 = 1000000000;
    
    # Cast values to string type to be crystal clear about what you're doing.
    $q_1 = bcdiv((string)$num_1, (string)$denom_1, strlen($denom_1));
    printf("q_1: %s\n", $q_1);
    
    $num_2 = 1;
    $denom_2 = 1000000000000000;
    
    # php will do an implicit conversion to string anyway, though.
    $q_2 = bcdiv($num_2, $denom_2, strlen($denom_2));
    printf("q_2: %s\n", $q_2);
    
    printf("sum: %s\n", bcadd($q_1, $q_2, strlen($denom_2)));
    ?>
    
    $ php code/php/test.php
    q_1: 0.0000000010
    q_2: 0.0000000000000010
    sum: 0.0000000010000010
    

    Arbitrary-precision arithmetic is inherently slower than floating-point arithmetic. That’s the price you pay for dozens, hundreds, or thousands of digits of accuracy.

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

Sidebar

Related Questions

Thanks to stack overflow I'm almost done with a programming problem that's been driving
Thanks to these stack overflow posts, I know how to make an nmap that
So thanks to stack overflow, I've determined that document.write is clobbering my webpage/program every
Our task is to read information about table schema from a file, implement that
I recently learned JavaScript an all of the sudden I hear about Python... Should
Thanks for looking! Background I have virtually no experience with the LAMP stack, but
Does the stack get unwound (destructors run) when a SIGABRT occurs in C++? Thanks.
Thanks to Stack Overflow question Copy files to clipboard in C# , I was
I just learned you can stack SQL queries instead of running 4 different ones
I found a great plug-in for doing Auto-Complete with Javascript called AutoSuggest thanks to

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.