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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:47:37+00:00 2026-05-12T23:47:37+00:00

We have a cost C which must be assigned to departments 1..n. Another computation

  • 0

We have a cost C which must be assigned to departments 1..n. Another computation produces the share for each department, it is a number from 0 to 1, with more than 5 decimal places. The sum of all department shares is exactly 1, but they are not necessarily equal.

The goal is to compute the exact dollars and cents to bill to each department. The sum of the bill must EXACTLY match the cost C, it cannot be a few pennies under or over. Furthermore, each department’s share must be not require fractional pennies. Additionally, although it is not fair to just dump the remainder onto the last department, it is not necessary to look back to previous timeframes. Note that simply rounding each department’s share to the penny almost always results in an over/under of a few pennies.

Grossly oversimplified example: C = 33.34 , 4 departments each with exactly 0.2500 share. Because 33.34 * 0.25 = 8.335 so you can see that two departments must pay 8.33 and two must pay 8.34. One correct assignment would be: d1=8.33, d2=8.34, d3=8.33, d4=8.34. If you round, every department pays 8.34 which results in an overage of $0.02. If you multiply this by many more departments and many more costs, you end up with hundred dollar discrepancies.

I want to do this in 1 pass, that is, I don’t want to loop through, find out I am off by 0.02, then loop again and tweak values until it is correct. I want to do this in 1 pass. I would also like to know if this algorithm has a name.

  • 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-12T23:47:38+00:00Added an answer on May 12, 2026 at 11:47 pm

    In Perl:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Data::Dumper;
    
    use List::AllUtils qw( sum );
    
    print Dumper allocate(10_000, .23, .37, .4);
    print Dumper allocate(33.34, .25, .25, .25, .25);
    print Dumper allocate(100, 1/3, 1/3, 1/3);
    
    sub allocate {
        my ($C, @shares) = @_;
    
        my @alloc;
    
        while ( my $share = shift @shares ) {
            push @alloc, sprintf '%.2f', $C * $share;
            $C -= $alloc[-1];
            my $denom = sum @shares;
            $_ /= $denom for @shares;
        }
    
        return \@alloc;
    }
    

    Output:

    $VAR1 = [
              '2300.00',
              '3700.00',
              '4000.00'
            ];
    $VAR1 = [
              '8.34',
              '8.33',
              '8.34',
              '8.33'
            ];
    $VAR1 = [
              '33.33',
              '33.34',
              '33.33'
            ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL Server 2008. I have this very large query which has a high cost
I have some code which must be thread safe whose behavior resembles this: protected
Using Ruby and Haml, I have a property which is cost. I believe (im
I have two arrays, one is a node-node-cost array [a_node,b_node,cost] which has 8000 entries
I have a cost optimization request that I don't know how if there is
I have a table with a float called 'cost' and timestamp called'created_at'. I would
I have a really silly problem that has cost me a load of time
I have a table that has a column that has the cost of a
In my db table I have a field float where I store cost of
have written this little class, which generates a UUID every time an object of

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.