Giter Site home page Giter Site logo

seebeen / largest-remainder-method Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kschu91/largest-remainder-method

0.0 0.0 0.0 42 KB

A PHP implementation of the largest remainder method algorithm.

License: MIT License

PHP 96.62% Makefile 0.81% Dockerfile 2.57%

largest-remainder-method's Introduction

Build Status Code Coverage Scrutinizer Code Quality

largest remainder method algorithm

A PHP implementation of the largest remainder method algorithm. This method is the most common way to get rid of rounding issues when working with rounded percentage values.

The problem

Assume the following example:

18.562874251497007%
20.958083832335326%
18.562874251497007%
19.161676646706585%
22.75449101796407%

When rounding the above percentages using PHP´s rounding functions, we get:

19%
21%
19%
19%
23%

Which in fact sums up to 101% instead of 100%. The largest remainder method solves this issue by doing the following steps:

  1. Rounding all values down to the nearest integer value
  2. Determining the difference between the sum of the rounded values and total value
  3. Distributing the difference between the rounded values in decreasing order of their decimal parts

Installation

composer require "kschu91/largest-remainder-method"

If you are not familiar with composer: composer basic usage

Requirements

  • PHP >= 7.3

Basic Usage

$numbers = [
    18.562874251497007,
    20.958083832335326,
    18.562874251497007,
    19.161676646706585,
    22.75449101796407
];

$lr = new LargestRemainder($numbers);

print_r($lr->round());

which results in:

Array
(
    [0] => 19
    [1] => 21
    [2] => 18
    [3] => 19
    [4] => 23
)

Working with decimals aka. precision

The default precision is set to 0. But you can change this behaviour by using the setPrecision method:

$numbers = [
    18.562874251497007,
    20.958083832335326,
    18.562874251497007,
    19.161676646706585,
    22.75449101796407
];

$lr = new LargestRemainder($numbers);
$lr->setPrecision(2);

print_r($lr->round());

which results in:

Array
(
    [0] => 18.56
    [1] => 20.96
    [2] => 18.56
    [3] => 19.16
    [4] => 22.76
)

Working with complex arrays/objects

Mostly, you don´t have the numbers you want to apply this algorithm on in a simple array as in the examples above. You rather have them in objects or associative arrays. That´s why this library also supports callbacks for applying this algorithm.

You just have to supply 2 callbacks to the usort method. The first one, to fetch the relevant number from the object. And the second one to write the rounded number back to the resulting object.

Make sure to pass the first argument of the setter callback as reference, eg. as in the example below: &$item. If not, the resulting data will maintain their original numbers and are not rounded.

$objects = [
    ['a' => 18.562874251497007],
    ['a' => 20.958083832335326],
    ['a' => 18.562874251497007],
    ['a' => 19.161676646706585],
    ['a' => 22.75449101796407]
];

$lr = new LargestRemainder($objects);
$lr->setPrecision(2);

print_r($lr->uround(
    function ($item) {
        return $item['a'];
    },
    function (&$item, $value) {
        $item['a'] = $value;
    }
));

which results in:

Array
(
    [0] => Array
        (
            [a] => 18.55
        )

    [1] => Array
        (
            [a] => 20.94
        )

    [2] => Array
        (
            [a] => 18.55
        )

    [3] => Array
        (
            [a] => 19.15
        )

    [4] => Array
        (
            [a] => 22.74
        )

)

largest-remainder-method's People

Contributors

kschu91 avatar seebeen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.